Terminal UI V2, advanced binding
This commit is contained in:
@@ -12,6 +12,7 @@ public abstract class DeclarativePropertyBase<T> : IDeclarativeProperty<T>
|
||||
private readonly List<Action<IDeclarativeProperty<T>, T>> _unsubscribeTriggers = new();
|
||||
private readonly List<IDisposable> _triggerDisposables = new();
|
||||
private readonly object _triggerLock = new();
|
||||
private readonly object _subscriberLock = new();
|
||||
|
||||
private T? _value;
|
||||
|
||||
@@ -35,7 +36,12 @@ public abstract class DeclarativePropertyBase<T> : IDeclarativeProperty<T>
|
||||
|
||||
protected async Task NotifySubscribersAsync(T? newValue, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var subscribers = _subscribers.ToList();
|
||||
List<Func<T?, CancellationToken, Task>> subscribers;
|
||||
lock (_subscriberLock)
|
||||
{
|
||||
subscribers = _subscribers.ToList();
|
||||
}
|
||||
|
||||
foreach (var handler in subscribers)
|
||||
{
|
||||
await handler(newValue, cancellationToken);
|
||||
@@ -50,13 +56,23 @@ public abstract class DeclarativePropertyBase<T> : IDeclarativeProperty<T>
|
||||
|
||||
public IDisposable Subscribe(Func<T?, CancellationToken, Task> onChange)
|
||||
{
|
||||
_subscribers.Add(onChange);
|
||||
lock (_subscriberLock)
|
||||
{
|
||||
_subscribers.Add(onChange);
|
||||
}
|
||||
|
||||
onChange(_value, default);
|
||||
|
||||
return new Unsubscriber<T>(this, onChange);
|
||||
}
|
||||
|
||||
public void Unsubscribe(Func<T, CancellationToken, Task> onChange) => _subscribers.Remove(onChange);
|
||||
public void Unsubscribe(Func<T, CancellationToken, Task> onChange)
|
||||
{
|
||||
lock (_subscriberLock)
|
||||
{
|
||||
_subscribers.Remove(onChange);
|
||||
}
|
||||
}
|
||||
|
||||
public IDeclarativeProperty<T> RegisterTrigger(
|
||||
Func<IDeclarativeProperty<T>, T, IDisposable?> triggerSubscribe,
|
||||
@@ -139,7 +155,10 @@ public abstract class DeclarativePropertyBase<T> : IDeclarativeProperty<T>
|
||||
disposable.Dispose();
|
||||
}
|
||||
|
||||
_subscribers.Clear();
|
||||
lock (_subscriberLock)
|
||||
{
|
||||
_subscribers.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
protected void AddDisposable(IDisposable disposable) => _disposables.Add(disposable);
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
namespace TerminalUI;
|
||||
using TerminalUI.ConsoleDrivers;
|
||||
|
||||
namespace TerminalUI;
|
||||
|
||||
public class ApplicationContext : IApplicationContext
|
||||
{
|
||||
public required IConsoleDriver ConsoleDriver { get; init; }
|
||||
public IEventLoop EventLoop { get; init; }
|
||||
public bool IsRunning { get; set; }
|
||||
|
||||
|
||||
@@ -12,16 +12,15 @@ public class Binding<TDataContext, TResult> : IDisposable
|
||||
private IView<TDataContext> _dataSourceView;
|
||||
private object? _propertySource;
|
||||
private PropertyInfo _targetProperty;
|
||||
private readonly List<string> _rerenderProperties;
|
||||
private readonly IDisposableCollection? _propertySourceDisposableCollection;
|
||||
private INotifyPropertyChanged? _dataSourceLastDataContext;
|
||||
private IDisposableCollection? _propertySourceDisposableCollection;
|
||||
private PropertyTrackTreeItem? _propertyTrackTreeItem;
|
||||
private IPropertyChangeTracker? _propertyChangeTracker;
|
||||
|
||||
public Binding(
|
||||
IView<TDataContext> dataSourceView,
|
||||
Expression<Func<TDataContext?, TResult>> dataContextExpression,
|
||||
object? propertySource,
|
||||
PropertyInfo targetProperty,
|
||||
IEnumerable<string>? rerenderProperties = null
|
||||
PropertyInfo targetProperty
|
||||
)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(dataSourceView);
|
||||
@@ -31,70 +30,148 @@ public class Binding<TDataContext, TResult> : IDisposable
|
||||
_dataContextMapper = dataContextExpression.Compile();
|
||||
_propertySource = propertySource;
|
||||
_targetProperty = targetProperty;
|
||||
_rerenderProperties = rerenderProperties?.ToList() ?? new List<string>();
|
||||
|
||||
FindReactiveProperties(dataContextExpression);
|
||||
InitTrackingTree(dataContextExpression);
|
||||
|
||||
UpdateTrackers();
|
||||
|
||||
dataSourceView.PropertyChanged += View_PropertyChanged;
|
||||
var initialValue = _dataContextMapper(_dataSourceView.DataContext);
|
||||
_targetProperty.SetValue(_propertySource, initialValue);
|
||||
UpdateTargetProperty();
|
||||
|
||||
AddToSourceDisposables(propertySource);
|
||||
|
||||
dataSourceView.AddDisposable(this);
|
||||
}
|
||||
|
||||
private void AddToSourceDisposables(object? propertySource)
|
||||
{
|
||||
if (propertySource is IDisposableCollection propertySourceDisposableCollection)
|
||||
{
|
||||
propertySourceDisposableCollection.AddDisposable(this);
|
||||
_propertySourceDisposableCollection = propertySourceDisposableCollection;
|
||||
}
|
||||
|
||||
if (_dataSourceView.DataContext is INotifyPropertyChanged dataSourcePropertyChanged)
|
||||
{
|
||||
_dataSourceLastDataContext = dataSourcePropertyChanged;
|
||||
dataSourcePropertyChanged.PropertyChanged += DataContext_PropertyChanged;
|
||||
}
|
||||
|
||||
dataSourceView.AddDisposable(this);
|
||||
}
|
||||
|
||||
private void FindReactiveProperties(Expression expression)
|
||||
private void InitTrackingTree(Expression<Func<TDataContext?, TResult>> dataContextExpression)
|
||||
{
|
||||
var properties = new List<string>();
|
||||
FindReactiveProperties(dataContextExpression, properties);
|
||||
|
||||
if (properties.Count > 0)
|
||||
{
|
||||
var rootItem = new PropertyTrackTreeItem();
|
||||
foreach (var property in properties)
|
||||
{
|
||||
var pathParts = property.Split('.');
|
||||
var currentItem = rootItem;
|
||||
for (var i = 0; i < pathParts.Length; i++)
|
||||
{
|
||||
if (!currentItem.Children.TryGetValue(pathParts[i], out var child))
|
||||
{
|
||||
child = new PropertyTrackTreeItem();
|
||||
currentItem.Children.Add(pathParts[i], child);
|
||||
}
|
||||
|
||||
currentItem = child;
|
||||
}
|
||||
}
|
||||
|
||||
_propertyTrackTreeItem = rootItem;
|
||||
}
|
||||
}
|
||||
|
||||
private string? FindReactiveProperties(Expression expression, List<string> properties)
|
||||
{
|
||||
if (expression is LambdaExpression lambdaExpression)
|
||||
{
|
||||
FindReactiveProperties(lambdaExpression.Body);
|
||||
SavePropertyPath(FindReactiveProperties(lambdaExpression.Body, properties));
|
||||
}
|
||||
else if (expression is ConditionalExpression conditionalExpression)
|
||||
{
|
||||
FindReactiveProperties(conditionalExpression.IfFalse);
|
||||
FindReactiveProperties(conditionalExpression.IfTrue);
|
||||
SavePropertyPath(FindReactiveProperties(conditionalExpression.IfFalse, properties));
|
||||
SavePropertyPath(FindReactiveProperties(conditionalExpression.IfTrue, properties));
|
||||
}
|
||||
else if (expression is MemberExpression {Member: PropertyInfo dataContextPropertyInfo})
|
||||
else if (expression is MemberExpression memberExpression)
|
||||
{
|
||||
_rerenderProperties.Add(dataContextPropertyInfo.Name);
|
||||
if (memberExpression.Expression is not null)
|
||||
{
|
||||
FindReactiveProperties(memberExpression.Expression, properties);
|
||||
|
||||
if (FindReactiveProperties(memberExpression.Expression, properties) is { } path
|
||||
&& memberExpression.Member is PropertyInfo dataContextPropertyInfo)
|
||||
{
|
||||
path += "." + memberExpression.Member.Name;
|
||||
return path;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (expression is MethodCallExpression methodCallExpression)
|
||||
{
|
||||
if (methodCallExpression.Object is
|
||||
{
|
||||
NodeType:
|
||||
not ExpressionType.Parameter
|
||||
and not ExpressionType.Constant
|
||||
} methodObject)
|
||||
{
|
||||
SavePropertyPath(FindReactiveProperties(methodObject, properties));
|
||||
}
|
||||
|
||||
foreach (var argument in methodCallExpression.Arguments)
|
||||
{
|
||||
SavePropertyPath(FindReactiveProperties(argument, properties));
|
||||
}
|
||||
}
|
||||
else if (expression is BinaryExpression binaryExpression)
|
||||
{
|
||||
SavePropertyPath(FindReactiveProperties(binaryExpression.Left, properties));
|
||||
SavePropertyPath(FindReactiveProperties(binaryExpression.Right, properties));
|
||||
}
|
||||
else if (expression is UnaryExpression unaryExpression)
|
||||
{
|
||||
SavePropertyPath(FindReactiveProperties(unaryExpression.Operand, properties));
|
||||
}
|
||||
else if (expression is ParameterExpression parameterExpression)
|
||||
{
|
||||
if (parameterExpression.Type == typeof(TDataContext))
|
||||
{
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
void SavePropertyPath(string? path)
|
||||
{
|
||||
if (path is null) return;
|
||||
path = path.TrimStart('.');
|
||||
properties.Add(path);
|
||||
}
|
||||
//TODO: Handle other expression types
|
||||
}
|
||||
|
||||
private void View_PropertyChanged(object? sender, PropertyChangedEventArgs e)
|
||||
{
|
||||
if (e.PropertyName != nameof(IView<TDataContext>.DataContext)) return;
|
||||
|
||||
if (_dataSourceLastDataContext is not null)
|
||||
{
|
||||
_dataSourceLastDataContext.PropertyChanged -= DataContext_PropertyChanged;
|
||||
}
|
||||
|
||||
if (_dataSourceView.DataContext is INotifyPropertyChanged dataSourcePropertyChanged)
|
||||
{
|
||||
_dataSourceLastDataContext = dataSourcePropertyChanged;
|
||||
dataSourcePropertyChanged.PropertyChanged += DataContext_PropertyChanged;
|
||||
}
|
||||
|
||||
UpdateTrackers();
|
||||
UpdateTargetProperty();
|
||||
}
|
||||
|
||||
private void DataContext_PropertyChanged(object? sender, PropertyChangedEventArgs e)
|
||||
private void UpdateTrackers()
|
||||
{
|
||||
if (e.PropertyName == null
|
||||
|| !_rerenderProperties.Contains(e.PropertyName)) return;
|
||||
UpdateTargetProperty();
|
||||
if (_propertyChangeTracker is not null)
|
||||
{
|
||||
_propertyChangeTracker.Dispose();
|
||||
}
|
||||
|
||||
if (_propertyTrackTreeItem is not null)
|
||||
{
|
||||
_propertyChangeTracker = PropertyChangeHelper.TraverseDataContext(
|
||||
_propertyTrackTreeItem,
|
||||
_dataSourceView.DataContext,
|
||||
UpdateTargetProperty
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateTargetProperty()
|
||||
|
||||
41
src/Library/TerminalUI/ConsoleDrivers/DotnetDriver.cs
Normal file
41
src/Library/TerminalUI/ConsoleDrivers/DotnetDriver.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
using TerminalUI.Models;
|
||||
using ConsoleColor = TerminalUI.Models.ConsoleColor;
|
||||
|
||||
namespace TerminalUI.ConsoleDrivers;
|
||||
|
||||
public class DotnetDriver : IConsoleDriver
|
||||
{
|
||||
public virtual void Init() => Console.Clear();
|
||||
|
||||
public void SetCursorPosition(Position position) => Console.SetCursorPosition(position.PosX, position.PosY);
|
||||
|
||||
public void ResetColor() => Console.ResetColor();
|
||||
|
||||
public Position GetCursorPosition()
|
||||
{
|
||||
var (x, y) = Console.GetCursorPosition();
|
||||
return new(x, y);
|
||||
}
|
||||
|
||||
public void Write(string text) => Console.Write(text);
|
||||
|
||||
public void Write(char text) => Console.Write(text);
|
||||
|
||||
public virtual void Dispose() {}
|
||||
|
||||
public bool CanRead() => Console.KeyAvailable;
|
||||
public ConsoleKeyInfo ReadKey() => Console.ReadKey(true);
|
||||
|
||||
public void SetCursorVisible(bool cursorVisible) => Console.CursorVisible = cursorVisible;
|
||||
public virtual void SetForegroundColor(IColor foreground)
|
||||
{
|
||||
if (foreground is not ConsoleColor consoleColor) throw new NotSupportedException();
|
||||
Console.ForegroundColor = consoleColor.Color;
|
||||
}
|
||||
|
||||
public virtual void SetBackgroundColor(IColor background)
|
||||
{
|
||||
if (background is not ConsoleColor consoleColor) throw new NotSupportedException();
|
||||
Console.ForegroundColor = consoleColor.Color;
|
||||
}
|
||||
}
|
||||
19
src/Library/TerminalUI/ConsoleDrivers/IConsoleDriver.cs
Normal file
19
src/Library/TerminalUI/ConsoleDrivers/IConsoleDriver.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using TerminalUI.Models;
|
||||
|
||||
namespace TerminalUI.ConsoleDrivers;
|
||||
|
||||
public interface IConsoleDriver
|
||||
{
|
||||
void Init();
|
||||
void Dispose();
|
||||
void SetCursorPosition(Position position);
|
||||
void ResetColor();
|
||||
Position GetCursorPosition();
|
||||
void Write(string text);
|
||||
void Write(char text);
|
||||
bool CanRead();
|
||||
ConsoleKeyInfo ReadKey();
|
||||
void SetCursorVisible(bool cursorVisible);
|
||||
void SetForegroundColor(IColor foreground);
|
||||
void SetBackgroundColor(IColor background);
|
||||
}
|
||||
16
src/Library/TerminalUI/ConsoleDrivers/WindowsDriver.cs
Normal file
16
src/Library/TerminalUI/ConsoleDrivers/WindowsDriver.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using TerminalUI.Models;
|
||||
|
||||
namespace TerminalUI.ConsoleDrivers;
|
||||
|
||||
public sealed class WindowsDriver : DotnetDriver
|
||||
{
|
||||
public override void Init() => Console.Out.Write("\x1b[?1049h");
|
||||
|
||||
public override void Dispose() => Console.Out.Write("\x1b[?1049l");
|
||||
|
||||
public override void SetBackgroundColor(IColor background)
|
||||
=> Write(background.ToConsoleColor());
|
||||
|
||||
public override void SetForegroundColor(IColor foreground)
|
||||
=> Write(foreground.ToConsoleColor());
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using TerminalUI.Traits;
|
||||
using TerminalUI.Models;
|
||||
using TerminalUI.Traits;
|
||||
|
||||
namespace TerminalUI.Controls;
|
||||
|
||||
@@ -9,7 +10,7 @@ public abstract class ContentView<T>: View<T>, IContentRenderer
|
||||
ContentRendererMethod = DefaultContentRender;
|
||||
}
|
||||
public IView? Content { get; set; }
|
||||
public Action ContentRendererMethod { get; set; }
|
||||
public Action<Position> ContentRendererMethod { get; set; }
|
||||
|
||||
private void DefaultContentRender() => Content?.Render();
|
||||
private void DefaultContentRender(Position position) => Content?.Render(position);
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.ComponentModel;
|
||||
using TerminalUI.Models;
|
||||
using TerminalUI.Traits;
|
||||
|
||||
namespace TerminalUI.Controls;
|
||||
@@ -6,12 +7,10 @@ namespace TerminalUI.Controls;
|
||||
public interface IView : INotifyPropertyChanged, IDisposableCollection
|
||||
{
|
||||
object? DataContext { get; set; }
|
||||
Action RenderMethod { get; set; }
|
||||
IApplicationContext ApplicationContext { get; init;}
|
||||
Action<Position> RenderMethod { get; set; }
|
||||
IApplicationContext? ApplicationContext { get; init;}
|
||||
event Action<IView> Disposed;
|
||||
event Action<IView> RenderRequested;
|
||||
void Render();
|
||||
void RequestRerender();
|
||||
void Render(Position position);
|
||||
}
|
||||
|
||||
public interface IView<T> : IView
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using System.Buffers;
|
||||
using System.Collections.ObjectModel;
|
||||
using DeclarativeProperty;
|
||||
using TerminalUI.Models;
|
||||
|
||||
namespace TerminalUI.Controls;
|
||||
|
||||
@@ -9,7 +10,7 @@ public class ListView<TDataContext, TItem> : View<TDataContext>
|
||||
private static readonly ArrayPool<ListViewItem<TItem>> ListViewItemPool = ArrayPool<ListViewItem<TItem>>.Shared;
|
||||
|
||||
private readonly List<IDisposable> _itemsDisposables = new();
|
||||
private Func<IEnumerable<TItem>>? _getItems;
|
||||
private Func<IEnumerable<TItem>?>? _getItems;
|
||||
private object? _itemsSource;
|
||||
private ListViewItem<TItem>[]? _listViewItems;
|
||||
private int _listViewItemLength;
|
||||
@@ -30,11 +31,20 @@ public class ListView<TDataContext, TItem> : View<TDataContext>
|
||||
_itemsDisposables.Clear();
|
||||
|
||||
if (_itemsSource is IDeclarativeProperty<ObservableCollection<TItem>> observableDeclarativeProperty)
|
||||
{
|
||||
observableDeclarativeProperty.PropertyChanged += (_, _) => ApplicationContext?.EventLoop.RequestRerender();
|
||||
_getItems = () => observableDeclarativeProperty.Value;
|
||||
}
|
||||
else if (_itemsSource is IDeclarativeProperty<ReadOnlyObservableCollection<TItem>> readOnlyObservableDeclarativeProperty)
|
||||
{
|
||||
readOnlyObservableDeclarativeProperty.PropertyChanged += (_, _) => ApplicationContext?.EventLoop.RequestRerender();
|
||||
_getItems = () => readOnlyObservableDeclarativeProperty.Value;
|
||||
}
|
||||
else if (_itemsSource is IDeclarativeProperty<IEnumerable<TItem>> enumerableDeclarativeProperty)
|
||||
{
|
||||
enumerableDeclarativeProperty.PropertyChanged += (_, _) => ApplicationContext?.EventLoop.RequestRerender();
|
||||
_getItems = () => enumerableDeclarativeProperty.Value;
|
||||
}
|
||||
else if (_itemsSource is ICollection<TItem> collection)
|
||||
_getItems = () => collection;
|
||||
else if (_itemsSource is TItem[] array)
|
||||
@@ -54,18 +64,20 @@ public class ListView<TDataContext, TItem> : View<TDataContext>
|
||||
|
||||
public Func<ListViewItem<TItem>, IView?> ItemTemplate { get; set; } = DefaultItemTemplate;
|
||||
|
||||
protected override void DefaultRenderer()
|
||||
protected override void DefaultRenderer(Position position)
|
||||
{
|
||||
var listViewItems = InstantiateItemViews();
|
||||
var deltaY = 0;
|
||||
foreach (var item in listViewItems)
|
||||
{
|
||||
item.Render();
|
||||
item.Render(position with {PosY = position.PosY + deltaY++});
|
||||
}
|
||||
}
|
||||
|
||||
private Span<ListViewItem<TItem>> InstantiateItemViews()
|
||||
{
|
||||
if (_getItems is null)
|
||||
var items = _getItems?.Invoke()?.ToList();
|
||||
if (items is null)
|
||||
{
|
||||
if (_listViewItemLength != 0)
|
||||
{
|
||||
@@ -74,11 +86,10 @@ public class ListView<TDataContext, TItem> : View<TDataContext>
|
||||
|
||||
return _listViewItems;
|
||||
}
|
||||
var items = _getItems().ToList();
|
||||
|
||||
Span<ListViewItem<TItem>> listViewItems;
|
||||
|
||||
if (_listViewItems is null || _listViewItems.Length != items.Count)
|
||||
if (_listViewItems is null || _listViewItemLength != items.Count)
|
||||
{
|
||||
var newListViewItems = ListViewItemPool.Rent(items.Count);
|
||||
for (var i = 0; i < items.Count; i++)
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
using TerminalUI.Traits;
|
||||
using TerminalUI.Models;
|
||||
|
||||
namespace TerminalUI.Controls;
|
||||
|
||||
public class ListViewItem<T> : ContentView<T>
|
||||
{
|
||||
protected override void DefaultRenderer()
|
||||
protected override void DefaultRenderer(Position position)
|
||||
{
|
||||
if (ContentRendererMethod is null)
|
||||
{
|
||||
@@ -16,6 +16,6 @@ public class ListViewItem<T> : ContentView<T>
|
||||
+ DataContext?.GetType().Name);
|
||||
}
|
||||
|
||||
ContentRendererMethod();
|
||||
ContentRendererMethod(position);
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,18 @@
|
||||
using PropertyChanged.SourceGenerator;
|
||||
using TerminalUI.Extensions;
|
||||
using TerminalUI.Models;
|
||||
|
||||
namespace TerminalUI.Controls;
|
||||
|
||||
public partial class TextBlock<T> : View<T>
|
||||
{
|
||||
private record RenderContext(Position Position, string? Text, IColor? Foreground, IColor? Background);
|
||||
|
||||
private RenderContext? _renderContext;
|
||||
|
||||
[Notify] private string? _text = string.Empty;
|
||||
[Notify] private IColor? _foreground;
|
||||
[Notify] private IColor? _background;
|
||||
|
||||
public TextBlock()
|
||||
{
|
||||
@@ -16,8 +23,35 @@ public partial class TextBlock<T> : View<T>
|
||||
);
|
||||
|
||||
RerenderProperties.Add(nameof(Text));
|
||||
RerenderProperties.Add(nameof(Foreground));
|
||||
RerenderProperties.Add(nameof(Background));
|
||||
}
|
||||
|
||||
protected override void DefaultRenderer()
|
||||
=> Console.Write(Text);
|
||||
protected override void DefaultRenderer(Position position)
|
||||
{
|
||||
var driver = ApplicationContext!.ConsoleDriver;
|
||||
var renderContext = new RenderContext(position, Text, _foreground, _background);
|
||||
if (!NeedsRerender(renderContext)) return;
|
||||
|
||||
_renderContext = renderContext;
|
||||
|
||||
if (Text is null) return;
|
||||
|
||||
driver.SetCursorPosition(position);
|
||||
driver.ResetColor();
|
||||
if (Foreground is { } foreground)
|
||||
{
|
||||
driver.SetForegroundColor(foreground);
|
||||
}
|
||||
|
||||
if (Background is { } background)
|
||||
{
|
||||
driver.SetBackgroundColor(background);
|
||||
}
|
||||
|
||||
driver.Write(Text);
|
||||
}
|
||||
|
||||
private bool NeedsRerender(RenderContext renderContext)
|
||||
=> _renderContext is null || _renderContext != renderContext;
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
using System.ComponentModel;
|
||||
using System.Runtime.CompilerServices;
|
||||
using PropertyChanged.SourceGenerator;
|
||||
using TerminalUI.Models;
|
||||
|
||||
namespace TerminalUI.Controls;
|
||||
|
||||
@@ -9,10 +10,9 @@ public abstract partial class View<T> : IView<T>
|
||||
{
|
||||
private readonly List<IDisposable> _disposables = new();
|
||||
[Notify] private T? _dataContext;
|
||||
public Action RenderMethod { get; set; }
|
||||
public IApplicationContext ApplicationContext { get; init; }
|
||||
public Action<Position> RenderMethod { get; set; }
|
||||
public IApplicationContext? ApplicationContext { get; init; }
|
||||
public event Action<IView>? Disposed;
|
||||
public event Action<IView>? RenderRequested;
|
||||
protected List<string> RerenderProperties { get; } = new();
|
||||
|
||||
protected View()
|
||||
@@ -29,13 +29,13 @@ public abstract partial class View<T> : IView<T>
|
||||
)
|
||||
)
|
||||
{
|
||||
RenderRequested?.Invoke(this);
|
||||
ApplicationContext?.EventLoop.RequestRerender();
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract void DefaultRenderer();
|
||||
protected abstract void DefaultRenderer(Position position);
|
||||
|
||||
public void Render()
|
||||
public void Render(Position position)
|
||||
{
|
||||
if (RenderMethod is null)
|
||||
{
|
||||
@@ -47,11 +47,9 @@ public abstract partial class View<T> : IView<T>
|
||||
+ DataContext?.GetType().Name);
|
||||
}
|
||||
|
||||
RenderMethod();
|
||||
RenderMethod(position);
|
||||
}
|
||||
|
||||
public void RequestRerender() => RenderRequested?.Invoke(this);
|
||||
|
||||
public TChild CreateChild<TChild>() where TChild : IView<T>, new()
|
||||
{
|
||||
var child = new TChild
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
using System.Buffers;
|
||||
using System.Collections.Concurrent;
|
||||
using TerminalUI.Controls;
|
||||
using TerminalUI.Controls;
|
||||
using TerminalUI.Models;
|
||||
|
||||
namespace TerminalUI;
|
||||
|
||||
@@ -8,10 +7,8 @@ public class EventLoop : IEventLoop
|
||||
{
|
||||
private readonly IApplicationContext _applicationContext;
|
||||
private readonly object _lock = new();
|
||||
private readonly ArrayPool<IView> _viewPool = ArrayPool<IView>.Shared;
|
||||
|
||||
private readonly ConcurrentBag<IView> _viewsToRenderInstantly = new();
|
||||
private readonly LinkedList<IView> _viewsToRender = new();
|
||||
private readonly List<IView> _viewsToRender = new();
|
||||
private bool _rerenderRequested;
|
||||
|
||||
public EventLoop(IApplicationContext applicationContext)
|
||||
{
|
||||
@@ -21,6 +18,7 @@ public class EventLoop : IEventLoop
|
||||
public void Run()
|
||||
{
|
||||
_applicationContext.IsRunning = true;
|
||||
_rerenderRequested = true;
|
||||
while (_applicationContext.IsRunning)
|
||||
{
|
||||
Render();
|
||||
@@ -28,103 +26,36 @@ public class EventLoop : IEventLoop
|
||||
}
|
||||
}
|
||||
|
||||
public void RequestRerender()
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
_rerenderRequested = true;
|
||||
}
|
||||
}
|
||||
|
||||
public void Render()
|
||||
{
|
||||
IView[]? viewsToRenderCopy = null;
|
||||
IView[]? viewsAlreadyRendered = null;
|
||||
try
|
||||
List<IView> viewsToRender;
|
||||
lock (_lock)
|
||||
{
|
||||
int viewsToRenderCopyCount;
|
||||
IView[]? viewsToRenderInstantly;
|
||||
if (!_rerenderRequested) return;
|
||||
_rerenderRequested = false;
|
||||
|
||||
lock (_lock)
|
||||
{
|
||||
CleanViewsToRender();
|
||||
|
||||
viewsToRenderCopyCount = _viewsToRender.Count;
|
||||
viewsToRenderCopy = _viewPool.Rent(_viewsToRender.Count);
|
||||
_viewsToRender.CopyTo(viewsToRenderCopy, 0);
|
||||
|
||||
viewsToRenderInstantly = _viewsToRenderInstantly.ToArray();
|
||||
_viewsToRenderInstantly.Clear();
|
||||
}
|
||||
|
||||
viewsAlreadyRendered = _viewPool.Rent(viewsToRenderCopy.Length + viewsToRenderInstantly.Length);
|
||||
var viewsAlreadyRenderedIndex = 0;
|
||||
|
||||
foreach (var view in viewsToRenderInstantly)
|
||||
{
|
||||
if (Contains(viewsAlreadyRendered, view, viewsAlreadyRenderedIndex)) continue;
|
||||
|
||||
view.Render();
|
||||
viewsAlreadyRendered[viewsAlreadyRenderedIndex++] = view;
|
||||
}
|
||||
|
||||
for (var i = 0; i < viewsToRenderCopyCount; i++)
|
||||
{
|
||||
var view = viewsToRenderCopy[i];
|
||||
if (Contains(viewsAlreadyRendered, view, viewsAlreadyRenderedIndex)) continue;
|
||||
|
||||
view.Render();
|
||||
viewsAlreadyRendered[viewsAlreadyRenderedIndex++] = view;
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (viewsToRenderCopy is not null)
|
||||
_viewPool.Return(viewsToRenderCopy);
|
||||
|
||||
if (viewsAlreadyRendered is not null)
|
||||
_viewPool.Return(viewsAlreadyRendered);
|
||||
}
|
||||
}
|
||||
|
||||
private void CleanViewsToRender()
|
||||
{
|
||||
IView[]? viewsAlreadyProcessed = null;
|
||||
try
|
||||
{
|
||||
viewsAlreadyProcessed = _viewPool.Rent(_viewsToRender.Count);
|
||||
var viewsAlreadyProcessedIndex = 0;
|
||||
|
||||
var currentItem = _viewsToRender.First;
|
||||
for (var i = 0; i < _viewsToRender.Count && currentItem is not null; i++)
|
||||
{
|
||||
if (Contains(viewsAlreadyProcessed, currentItem.Value, viewsAlreadyProcessedIndex))
|
||||
{
|
||||
var itemToRemove = currentItem;
|
||||
currentItem = currentItem.Next;
|
||||
_viewsToRender.Remove(itemToRemove);
|
||||
continue;
|
||||
}
|
||||
|
||||
viewsAlreadyProcessed[viewsAlreadyProcessedIndex++] = currentItem.Value;
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (viewsAlreadyProcessed is not null)
|
||||
{
|
||||
_viewPool.Return(viewsAlreadyProcessed);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static bool Contains(IView[] views, IView view, int max)
|
||||
{
|
||||
for (var i = 0; i < max; i++)
|
||||
{
|
||||
if (views[i] == view) return true;
|
||||
viewsToRender = _viewsToRender.ToList();
|
||||
}
|
||||
|
||||
return false;
|
||||
foreach (var view in viewsToRender)
|
||||
{
|
||||
view.Render(new Position(0, 0));
|
||||
}
|
||||
}
|
||||
|
||||
public void AddViewToRender(IView view)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
_viewsToRender.AddLast(view);
|
||||
_viewsToRender.Add(view);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -10,8 +10,7 @@ public static class Binding
|
||||
this TView targetView,
|
||||
IView<TDataContext> dataSourceView,
|
||||
Expression<Func<TDataContext?, TResult>> dataContextExpression,
|
||||
Expression<Func<TView, TResult>> propertyExpression,
|
||||
IEnumerable<string>? rerenderProperties = null)
|
||||
Expression<Func<TView, TResult>> propertyExpression)
|
||||
{
|
||||
if (propertyExpression.Body is not MemberExpression {Member: PropertyInfo propertyInfo})
|
||||
throw new AggregateException(nameof(propertyExpression) + " must be a property expression");
|
||||
@@ -20,8 +19,7 @@ public static class Binding
|
||||
dataSourceView,
|
||||
dataContextExpression,
|
||||
targetView,
|
||||
propertyInfo,
|
||||
rerenderProperties
|
||||
propertyInfo
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,10 @@
|
||||
namespace TerminalUI;
|
||||
using TerminalUI.ConsoleDrivers;
|
||||
|
||||
namespace TerminalUI;
|
||||
|
||||
public interface IApplicationContext
|
||||
{
|
||||
IEventLoop EventLoop { get; init; }
|
||||
bool IsRunning { get; set; }
|
||||
IConsoleDriver ConsoleDriver { get; init; }
|
||||
}
|
||||
@@ -7,4 +7,5 @@ public interface IEventLoop
|
||||
void Render();
|
||||
void AddViewToRender(IView view);
|
||||
void Run();
|
||||
void RequestRerender();
|
||||
}
|
||||
14
src/Library/TerminalUI/Models/Color256.cs
Normal file
14
src/Library/TerminalUI/Models/Color256.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using System.ComponentModel;
|
||||
|
||||
namespace TerminalUI.Models;
|
||||
|
||||
public record struct Color256(byte Color, ColorType Type) : IColor
|
||||
{
|
||||
public string ToConsoleColor()
|
||||
=> Type switch
|
||||
{
|
||||
ColorType.Foreground => $"\x1b[38;5;{Color}m",
|
||||
ColorType.Background => $"\x1b[48;5;{Color}m",
|
||||
_ => throw new InvalidEnumArgumentException(nameof(Type), (int) Type, typeof(ColorType))
|
||||
};
|
||||
}
|
||||
14
src/Library/TerminalUI/Models/ColorRGB.cs
Normal file
14
src/Library/TerminalUI/Models/ColorRGB.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using System.ComponentModel;
|
||||
|
||||
namespace TerminalUI.Models;
|
||||
|
||||
public record struct ColorRgb(byte R, byte G, byte B, ColorType Type) : IColor
|
||||
{
|
||||
public string ToConsoleColor()
|
||||
=> Type switch
|
||||
{
|
||||
ColorType.Foreground => $"\x1b[38;2;{R};{G};{B};m",
|
||||
ColorType.Background => $"\x1b[48;2;{R};{G};{B};m",
|
||||
_ => throw new InvalidEnumArgumentException(nameof(Type), (int) Type, typeof(ColorType))
|
||||
};
|
||||
}
|
||||
8
src/Library/TerminalUI/Models/ColorType.cs
Normal file
8
src/Library/TerminalUI/Models/ColorType.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
namespace TerminalUI.Models;
|
||||
|
||||
public enum ColorType
|
||||
{
|
||||
Unknown,
|
||||
Foreground,
|
||||
Background
|
||||
}
|
||||
83
src/Library/TerminalUI/Models/Colors.cs
Normal file
83
src/Library/TerminalUI/Models/Colors.cs
Normal file
@@ -0,0 +1,83 @@
|
||||
namespace TerminalUI.Models;
|
||||
|
||||
public static class Color256Colors
|
||||
{
|
||||
public static class Backgrounds
|
||||
{
|
||||
public static readonly Color256 Black = new(0, ColorType.Background);
|
||||
public static readonly Color256 Blue = new(9, ColorType.Background);
|
||||
public static readonly Color256 Cyan = new(11, ColorType.Background);
|
||||
public static readonly Color256 DarkBlue = new(1, ColorType.Background);
|
||||
public static readonly Color256 DarkCyan = new(3, ColorType.Background);
|
||||
public static readonly Color256 DarkGray = new(8, ColorType.Background);
|
||||
public static readonly Color256 DarkGreen = new(2, ColorType.Background);
|
||||
public static readonly Color256 DarkMagenta = new(5, ColorType.Background);
|
||||
public static readonly Color256 DarkRed = new(4, ColorType.Background);
|
||||
public static readonly Color256 DarkYellow = new(6, ColorType.Background);
|
||||
public static readonly Color256 Gray = new(7, ColorType.Background);
|
||||
public static readonly Color256 Green = new(10, ColorType.Background);
|
||||
public static readonly Color256 Magenta = new(13, ColorType.Background);
|
||||
public static readonly Color256 Red = new(12, ColorType.Background);
|
||||
public static readonly Color256 White = new(15, ColorType.Background);
|
||||
}
|
||||
public static class Foregrounds
|
||||
{
|
||||
public static readonly Color256 Black = new(0, ColorType.Foreground);
|
||||
public static readonly Color256 Blue = new(9, ColorType.Foreground);
|
||||
public static readonly Color256 Cyan = new(11, ColorType.Foreground);
|
||||
public static readonly Color256 DarkBlue = new(1, ColorType.Foreground);
|
||||
public static readonly Color256 DarkCyan = new(3, ColorType.Foreground);
|
||||
public static readonly Color256 DarkGray = new(8, ColorType.Foreground);
|
||||
public static readonly Color256 DarkGreen = new(2, ColorType.Foreground);
|
||||
public static readonly Color256 DarkMagenta = new(5, ColorType.Foreground);
|
||||
public static readonly Color256 DarkRed = new(4, ColorType.Foreground);
|
||||
public static readonly Color256 DarkYellow = new(6, ColorType.Foreground);
|
||||
public static readonly Color256 Gray = new(7, ColorType.Foreground);
|
||||
public static readonly Color256 Green = new(10, ColorType.Foreground);
|
||||
public static readonly Color256 Magenta = new(13, ColorType.Foreground);
|
||||
public static readonly Color256 Red = new(12, ColorType.Foreground);
|
||||
public static readonly Color256 White = new(15, ColorType.Foreground);
|
||||
}
|
||||
}
|
||||
|
||||
public static class ConsoleColors
|
||||
{
|
||||
public static class Backgrounds
|
||||
{
|
||||
public static readonly ConsoleColor Black = new(System.ConsoleColor.Black, ColorType.Background);
|
||||
public static readonly ConsoleColor Blue = new(System.ConsoleColor.Blue, ColorType.Background);
|
||||
public static readonly ConsoleColor Cyan = new(System.ConsoleColor.Cyan, ColorType.Background);
|
||||
public static readonly ConsoleColor DarkBlue = new(System.ConsoleColor.DarkBlue, ColorType.Background);
|
||||
public static readonly ConsoleColor DarkCyan = new(System.ConsoleColor.DarkCyan, ColorType.Background);
|
||||
public static readonly ConsoleColor DarkGray = new(System.ConsoleColor.DarkGray, ColorType.Background);
|
||||
public static readonly ConsoleColor DarkGreen = new(System.ConsoleColor.DarkGreen, ColorType.Background);
|
||||
public static readonly ConsoleColor DarkMagenta = new(System.ConsoleColor.DarkMagenta, ColorType.Background);
|
||||
public static readonly ConsoleColor DarkRed = new(System.ConsoleColor.DarkRed, ColorType.Background);
|
||||
public static readonly ConsoleColor DarkYellow = new(System.ConsoleColor.DarkYellow, ColorType.Background);
|
||||
public static readonly ConsoleColor Gray = new(System.ConsoleColor.Gray, ColorType.Background);
|
||||
public static readonly ConsoleColor Green = new(System.ConsoleColor.Green, ColorType.Background);
|
||||
public static readonly ConsoleColor Magenta = new(System.ConsoleColor.Magenta, ColorType.Background);
|
||||
public static readonly ConsoleColor Red = new(System.ConsoleColor.Red, ColorType.Background);
|
||||
public static readonly ConsoleColor White = new(System.ConsoleColor.White, ColorType.Background);
|
||||
public static readonly ConsoleColor Yellow = new(System.ConsoleColor.Yellow, ColorType.Background);
|
||||
}
|
||||
public static class Foregrounds
|
||||
{
|
||||
public static readonly ConsoleColor Black = new(System.ConsoleColor.Black, ColorType.Foreground);
|
||||
public static readonly ConsoleColor Blue = new(System.ConsoleColor.Blue, ColorType.Foreground);
|
||||
public static readonly ConsoleColor Cyan = new(System.ConsoleColor.Cyan, ColorType.Foreground);
|
||||
public static readonly ConsoleColor DarkBlue = new(System.ConsoleColor.DarkBlue, ColorType.Foreground);
|
||||
public static readonly ConsoleColor DarkCyan = new(System.ConsoleColor.DarkCyan, ColorType.Foreground);
|
||||
public static readonly ConsoleColor DarkGray = new(System.ConsoleColor.DarkGray, ColorType.Foreground);
|
||||
public static readonly ConsoleColor DarkGreen = new(System.ConsoleColor.DarkGreen, ColorType.Foreground);
|
||||
public static readonly ConsoleColor DarkMagenta = new(System.ConsoleColor.DarkMagenta, ColorType.Foreground);
|
||||
public static readonly ConsoleColor DarkRed = new(System.ConsoleColor.DarkRed, ColorType.Foreground);
|
||||
public static readonly ConsoleColor DarkYellow = new(System.ConsoleColor.DarkYellow, ColorType.Foreground);
|
||||
public static readonly ConsoleColor Gray = new(System.ConsoleColor.Gray, ColorType.Foreground);
|
||||
public static readonly ConsoleColor Green = new(System.ConsoleColor.Green, ColorType.Foreground);
|
||||
public static readonly ConsoleColor Magenta = new(System.ConsoleColor.Magenta, ColorType.Foreground);
|
||||
public static readonly ConsoleColor Red = new(System.ConsoleColor.Red, ColorType.Foreground);
|
||||
public static readonly ConsoleColor White = new(System.ConsoleColor.White, ColorType.Foreground);
|
||||
public static readonly ConsoleColor Yellow = new(System.ConsoleColor.Yellow, ColorType.Foreground);
|
||||
}
|
||||
}
|
||||
6
src/Library/TerminalUI/Models/ConsoleColor.cs
Normal file
6
src/Library/TerminalUI/Models/ConsoleColor.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace TerminalUI.Models;
|
||||
|
||||
public record ConsoleColor(System.ConsoleColor Color, ColorType Type) : IColor
|
||||
{
|
||||
public string ToConsoleColor() => throw new NotImplementedException();
|
||||
}
|
||||
7
src/Library/TerminalUI/Models/IColor.cs
Normal file
7
src/Library/TerminalUI/Models/IColor.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace TerminalUI.Models;
|
||||
|
||||
public interface IColor
|
||||
{
|
||||
ColorType Type { get; }
|
||||
string ToConsoleColor();
|
||||
}
|
||||
3
src/Library/TerminalUI/Models/Position.cs
Normal file
3
src/Library/TerminalUI/Models/Position.cs
Normal file
@@ -0,0 +1,3 @@
|
||||
namespace TerminalUI.Models;
|
||||
|
||||
public record struct Position(int PosX, int PosY);
|
||||
113
src/Library/TerminalUI/PropertyChangeTracker.cs
Normal file
113
src/Library/TerminalUI/PropertyChangeTracker.cs
Normal file
@@ -0,0 +1,113 @@
|
||||
using System.ComponentModel;
|
||||
|
||||
namespace TerminalUI;
|
||||
|
||||
internal interface IPropertyChangeTracker : IDisposable
|
||||
{
|
||||
Dictionary<string, IPropertyChangeTracker> Children { get; }
|
||||
}
|
||||
|
||||
internal abstract class PropertyChangeTrackerBase : IPropertyChangeTracker
|
||||
{
|
||||
public Dictionary<string, IPropertyChangeTracker> Children { get; } = new();
|
||||
|
||||
public virtual void Dispose()
|
||||
{
|
||||
foreach (var propertyChangeTracker in Children.Values)
|
||||
{
|
||||
propertyChangeTracker.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal class PropertyChangeTracker : PropertyChangeTrackerBase
|
||||
{
|
||||
private readonly PropertyTrackTreeItem _propertyTrackTreeItem;
|
||||
private readonly INotifyPropertyChanged _target;
|
||||
private readonly IEnumerable<string> _propertiesToListen;
|
||||
private readonly Action _updateBinding;
|
||||
|
||||
public PropertyChangeTracker(
|
||||
PropertyTrackTreeItem propertyTrackTreeItem,
|
||||
INotifyPropertyChanged target,
|
||||
IEnumerable<string> propertiesToListen,
|
||||
Action updateBinding)
|
||||
{
|
||||
_propertyTrackTreeItem = propertyTrackTreeItem;
|
||||
_target = target;
|
||||
_propertiesToListen = propertiesToListen;
|
||||
_updateBinding = updateBinding;
|
||||
target.PropertyChanged += Target_PropertyChanged;
|
||||
}
|
||||
|
||||
private void Target_PropertyChanged(object? sender, PropertyChangedEventArgs e)
|
||||
{
|
||||
var propertyName = e.PropertyName;
|
||||
if (propertyName is null || !_propertiesToListen.Contains(propertyName))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_updateBinding();
|
||||
Children.Remove(propertyName);
|
||||
|
||||
var newChild = PropertyChangeHelper.TraverseDataContext(
|
||||
_propertyTrackTreeItem.Children[propertyName],
|
||||
_target.GetType().GetProperty(propertyName)?.GetValue(_target),
|
||||
_updateBinding
|
||||
);
|
||||
|
||||
if (newChild is not null)
|
||||
{
|
||||
Children.Add(propertyName, newChild);
|
||||
}
|
||||
}
|
||||
|
||||
public override void Dispose()
|
||||
{
|
||||
_target.PropertyChanged -= Target_PropertyChanged;
|
||||
|
||||
base.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
internal class NonSubscriberPropertyChangeTracker : PropertyChangeTrackerBase
|
||||
{
|
||||
}
|
||||
|
||||
internal class PropertyTrackTreeItem
|
||||
{
|
||||
public Dictionary<string, PropertyTrackTreeItem> Children { get; } = new();
|
||||
}
|
||||
|
||||
internal static class PropertyChangeHelper
|
||||
{
|
||||
internal static IPropertyChangeTracker? TraverseDataContext(
|
||||
PropertyTrackTreeItem propertyTrackTreeItem,
|
||||
object? obj,
|
||||
Action updateBinding
|
||||
)
|
||||
{
|
||||
if (obj is null) return null;
|
||||
|
||||
IPropertyChangeTracker tracker = obj is INotifyPropertyChanged notifyPropertyChanged
|
||||
? new PropertyChangeTracker(propertyTrackTreeItem, notifyPropertyChanged, propertyTrackTreeItem.Children.Keys, updateBinding)
|
||||
: new NonSubscriberPropertyChangeTracker();
|
||||
|
||||
foreach (var (propertyName, trackerTreeItem) in propertyTrackTreeItem.Children)
|
||||
{
|
||||
var childTracker = TraverseDataContext(
|
||||
trackerTreeItem,
|
||||
obj.GetType().GetProperty(propertyName)?.GetValue(obj),
|
||||
updateBinding
|
||||
);
|
||||
|
||||
if (childTracker is not null)
|
||||
{
|
||||
tracker.Children.Add(propertyName, childTracker);
|
||||
}
|
||||
}
|
||||
|
||||
return tracker;
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,10 @@
|
||||
using TerminalUI.Controls;
|
||||
using TerminalUI.Models;
|
||||
|
||||
namespace TerminalUI.Traits;
|
||||
|
||||
public interface IContentRenderer
|
||||
{
|
||||
IView? Content { get; set; }
|
||||
Action ContentRendererMethod { get; set; }
|
||||
Action<Position> ContentRendererMethod { get; set; }
|
||||
}
|
||||
Reference in New Issue
Block a user