Console ItemPreview, select item after delete

This commit is contained in:
2023-08-18 10:41:30 +02:00
parent fd9a20e888
commit 1b60af389b
17 changed files with 280 additions and 61 deletions

View File

@@ -44,6 +44,12 @@ public abstract class ChildCollectionView<TConcrete, T>
};
}
public override void AddChild(IView child)
{
base.AddChild(child);
_children.Add(child);
}
public override TChild AddChild<TChild>(TChild child)
{
child = base.AddChild(child);

View File

@@ -1,8 +1,11 @@
using System.Collections;
using System.Linq.Expressions;
using TerminalUI.Extensions;
namespace TerminalUI.Controls;
public record ChildWithDataContextMapper<TSourceDataContext, TTargetDataContext>(IView<TTargetDataContext> Child, Func<TSourceDataContext?, TTargetDataContext?> DataContextMapper);
public record ChildWithDataContextBinding<TSourceDataContext, TTargetDataContext>(IView<TTargetDataContext> Child, Expression<Func<TSourceDataContext?, TTargetDataContext?>> DataContextExpression);
public class ChildInitializer<T> : IEnumerable<IView>
{
@@ -17,6 +20,16 @@ public class ChildInitializer<T> : IEnumerable<IView>
public void Add<TDataContext>(ChildWithDataContextMapper<T, TDataContext> item)
=> _childContainer.AddChild(item.Child, item.DataContextMapper);
public void Add<TDataContext>(ChildWithDataContextBinding<T, TDataContext> item)
{
item.Child.Bind(
_childContainer,
item.DataContextExpression,
c => c.DataContext
);
_childContainer.AddChild(item.Child);
}
public IEnumerator<IView> GetEnumerator() => _childContainer.Children.GetEnumerator();

View File

@@ -55,6 +55,7 @@ public interface IView<T> : IView
TChild CreateChild<TChild, TDataContext>(Func<T?, TDataContext?> dataContextMapper)
where TChild : IView<TDataContext>, new();
void AddChild(IView child);
TChild AddChild<TChild>(TChild child) where TChild : IView<T>;
TChild AddChild<TChild, TDataContext>(TChild child, Func<T?, TDataContext?> dataContextMapper)

View File

@@ -26,12 +26,14 @@ public sealed partial class TextBlock<T> : View<TextBlock<T>, T>, IDisplayView
[Notify] private string? _text = string.Empty;
[Notify] private TextAlignment _textAlignment = TextAlignment.Left;
[Notify] private ITextFormat? _textFormat;
[Notify] private int _textStartIndex;
public TextBlock()
{
RerenderProperties.Add(nameof(Text));
RerenderProperties.Add(nameof(TextAlignment));
RerenderProperties.Add(nameof(TextFormat));
RerenderProperties.Add(nameof(TextStartIndex));
((INotifyPropertyChanged) this).PropertyChanged += (o, e) =>
{
@@ -78,7 +80,17 @@ public sealed partial class TextBlock<T> : View<TextBlock<T>, T>, IDisplayView
SetStyleColor(renderContext, foreground, background, _textFormat);
RenderText(_textLines, renderContext, position, size, skipRender, TransformText);
var textLines = _textLines;
if (_textStartIndex < _textLines.Length)
{
textLines = _textLines[_textStartIndex..];
}
else
{
_textStartIndex = _textLines.Length - size.Height;
}
RenderText(textLines, renderContext, position, size, skipRender, TransformText);
return !skipRender;
}

View File

@@ -376,6 +376,12 @@ public abstract partial class View<TConcrete, T> : IView<T> where TConcrete : Vi
return child;
}
public virtual void AddChild(IView child)
{
Debug.Assert(child != null);
SetupNewChild(child, null);
}
public virtual TChild AddChild<TChild, TDataContext>(TChild child, Func<T?, TDataContext?> dataContextMapper)
where TChild : IView<TDataContext>
{
@@ -387,15 +393,18 @@ public abstract partial class View<TConcrete, T> : IView<T> where TConcrete : Vi
return child;
}
private void SetupNewChild(IView child, IDisposable dataContextmapper)
private void SetupNewChild(IView child, IDisposable? dataContextMapper)
{
child.ApplicationContext = ApplicationContext;
child.Attached = Attached;
child.VisualParent = this;
VisualChildren.Add(child);
AddDisposable(dataContextmapper);
child.AddDisposable(dataContextmapper);
if (dataContextMapper is not null)
{
AddDisposable(dataContextMapper);
child.AddDisposable(dataContextMapper);
}
}
public virtual void RemoveChild<TDataContext>(IView<TDataContext> child)