Controls, Startup&Driver improvements

This commit is contained in:
2023-08-09 11:54:32 +02:00
parent 2528487ff6
commit d549733b71
49 changed files with 875 additions and 120 deletions

View File

@@ -10,8 +10,30 @@ public abstract partial class View<T> : IView<T>
{
private readonly List<IDisposable> _disposables = new();
[Notify] private T? _dataContext;
public Action<Position> RenderMethod { get; set; }
public IApplicationContext? ApplicationContext { get; init; }
[Notify] private int? _minWidth;
[Notify] private int? _maxWidth;
[Notify] private int? _width;
[Notify] private int? _minHeight;
[Notify] private int? _maxHeight;
[Notify] private int? _height;
private bool _attached;
public bool Attached
{
get => _attached;
set
{
if (_attached == value) return;
_attached = value;
if (value)
{
AttachChildren();
}
}
}
public List<object> Extensions { get; } = new();
public Action<Position, Size> RenderMethod { get; set; }
public IApplicationContext? ApplicationContext { get; set; }
public event Action<IView>? Disposed;
protected List<string> RerenderProperties { get; } = new();
@@ -20,22 +42,27 @@ public abstract partial class View<T> : IView<T>
RenderMethod = DefaultRenderer;
((INotifyPropertyChanged) this).PropertyChanged += Handle_PropertyChanged;
}
public abstract Size GetRequestedSize();
protected virtual void AttachChildren()
{
}
private void Handle_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (e.PropertyName is not null
&& (e.PropertyName == nameof(IView.DataContext)
if (e.PropertyName is not null
&& (e.PropertyName == nameof(IView.DataContext)
|| RerenderProperties.Contains(e.PropertyName)
)
)
)
{
ApplicationContext?.EventLoop.RequestRerender();
}
}
protected abstract void DefaultRenderer(Position position);
protected abstract void DefaultRenderer(Position position, Size size);
public void Render(Position position)
public void Render(Position position, Size size)
{
if (RenderMethod is null)
{
@@ -47,16 +74,27 @@ public abstract partial class View<T> : IView<T>
+ DataContext?.GetType().Name);
}
RenderMethod(position);
RenderMethod(position, size);
}
public TChild CreateChild<TChild>() where TChild : IView<T>, new()
{
var child = new TChild
{
DataContext = DataContext,
ApplicationContext = ApplicationContext
};
var child = new TChild();
return AddChild(child);
}
public TChild CreateChild<TChild, TDataContext>(Func<T?, TDataContext?> dataContextMapper)
where TChild : IView<TDataContext>, new()
{
var child = new TChild();
return AddChild(child, dataContextMapper);
}
public virtual TChild AddChild<TChild>(TChild child) where TChild : IView<T>
{
child.DataContext = DataContext;
child.ApplicationContext = ApplicationContext;
var mapper = new DataContextMapper<T>(this, d => child.DataContext = d);
AddDisposable(mapper);
child.AddDisposable(mapper);
@@ -64,14 +102,12 @@ public abstract partial class View<T> : IView<T>
return child;
}
public TChild CreateChild<TChild, TDataContext>(Func<T?, TDataContext?> dataContextMapper)
where TChild : IView<TDataContext>, new()
public virtual TChild AddChild<TChild, TDataContext>(TChild child, Func<T?, TDataContext?> dataContextMapper)
where TChild : IView<TDataContext>
{
var child = new TChild
{
DataContext = dataContextMapper(DataContext),
ApplicationContext = ApplicationContext
};
child.DataContext = dataContextMapper(DataContext);
child.ApplicationContext = ApplicationContext;
var mapper = new DataContextMapper<T>(this, d => child.DataContext = dataContextMapper(d));
AddDisposable(mapper);
child.AddDisposable(mapper);