Console MessageBox, admin mode

This commit is contained in:
2023-08-28 13:49:54 +02:00
parent bb44ca0308
commit bc865011d3
18 changed files with 313 additions and 88 deletions

View File

@@ -11,6 +11,7 @@ public class DotnetDriver : IConsoleDriver
public int ThreadId { get; set; }
public void EnterRestrictedMode() => CheckThreadId = true;
public void ExitRestrictedMode() => CheckThreadId = false;
public virtual bool Init()
{

View File

@@ -24,4 +24,5 @@ public interface IConsoleDriver
Size GetWindowSize();
void Clear();
void EnterRestrictedMode();
void ExitRestrictedMode();
}

View File

@@ -0,0 +1,94 @@
using GeneralInputKey;
using PropertyChanged.SourceGenerator;
using TerminalUI.Color;
using TerminalUI.ConsoleDrivers;
using TerminalUI.Models;
using TerminalUI.Traits;
namespace TerminalUI.Controls;
public partial class Button<T> : ContentView<Button<T>, T>, IFocusable
{
private record RenderState(IColor? Foreground, IColor? Background);
private Position? _cursorRenderPosition;
private RenderState? _lastRenderState;
[Notify] private Position? _cursorPosition;
private List<Action<Button<T>>> _clickHandlers = new();
public Button()
{
RerenderProperties.Add(nameof(CursorPosition));
}
public void Focus()
=> ApplicationContext?.FocusManager.SetFocus(this);
public void UnFocus()
=> ApplicationContext?.FocusManager.UnFocus(this);
public void SetCursorPosition(IConsoleDriver consoleDriver)
{
if (_cursorRenderPosition is null) return;
consoleDriver.SetCursorPosition(_cursorRenderPosition.Value);
}
protected override Size CalculateSize()
{
if (Content is null || !Content.IsVisible) return new Size(0, 0);
return Content.GetRequestedSize();
}
protected override bool DefaultRenderer(in RenderContext renderContext, Position position, Size size)
{
if (ContentRendererMethod is null)
{
throw new NullReferenceException(
nameof(ContentRendererMethod)
+ " is null, cannot render content of "
+ Content?.GetType().Name
+ " with DataContext of "
+ DataContext?.GetType().Name);
}
var backgroundColor = Background ?? renderContext.Background;
var foregroundColor = Foreground ?? renderContext.Foreground;
var renderState = new RenderState(foregroundColor, backgroundColor);
var forceRerender = !renderContext.ForceRerender && !NeedsRerender(renderState);
_lastRenderState = renderState;
var childRenderContext = renderContext with
{
Background = backgroundColor,
Foreground = foregroundColor,
ForceRerender = forceRerender
};
_cursorRenderPosition = position;
return ContentRendererMethod(childRenderContext, position, size);
}
private bool NeedsRerender(RenderState renderState) => renderState != _lastRenderState;
public override void HandleKeyInput(GeneralKeyEventArgs keyEventArgs)
{
if (keyEventArgs.Key != Keys.Enter) return;
keyEventArgs.Handled = true;
foreach (var clickHandler in _clickHandlers)
{
clickHandler(this);
}
}
public Button<T> WithClickHandler(Action<Button<T>> handler)
{
_clickHandlers.Add(handler);
return this;
}
}

View File

@@ -125,8 +125,7 @@ public sealed partial class ItemsControl<TDataContext, TItem>
{
if (!child.IsVisible) continue;
if (!_requestedSizes.TryGetValue(child, out var childSize)) throw new Exception("Child size not found");
if (!_requestedSizes.TryGetValue(child, out var childSize)) continue;
var childPosition = Orientation == Orientation.Vertical
? position with {Y = position.Y + delta}

View File

@@ -8,6 +8,7 @@ public class EventLoop : IEventLoop
private readonly ILogger<EventLoop> _logger;
private readonly List<Action> _initializers = new();
private readonly List<Action> _permanentQueue = new();
private readonly List<Action> _finalizers = new();
public int ThreadId { get; set; } = -1;
@@ -21,6 +22,7 @@ public class EventLoop : IEventLoop
public void AddToPermanentQueue(Action action) => _permanentQueue.Add(action);
public void AddInitializer(Action action) => _initializers.Add(action);
public void AddFinalizer(Action action) => _finalizers.Add(action);
public void Run()
{
@@ -35,6 +37,10 @@ public class EventLoop : IEventLoop
ProcessQueues();
Thread.Sleep(10);
}
foreach (var finalizer in _finalizers)
{
finalizer();
}
ThreadId = -1;
}

View File

@@ -6,4 +6,5 @@ public interface IEventLoop
void AddToPermanentQueue(Action action);
void AddInitializer(Action action);
int ThreadId { get; set; }
void AddFinalizer(Action action);
}

View File

@@ -33,6 +33,10 @@ public class RenderEngine : IRenderEngine
_applicationContext.ConsoleDriver.ThreadId = _eventLoop.ThreadId;
_applicationContext.ConsoleDriver.EnterRestrictedMode();
});
_eventLoop.AddFinalizer(() =>
{
_applicationContext.ConsoleDriver.ExitRestrictedMode();
});
}
public void RequestRerender(IView view) => RequestRerender();