TextBox improvements, CommandPalette
This commit is contained in:
@@ -83,6 +83,7 @@ public class CommandPaletteViewModel : FuzzyPanelViewModel<ICommandPaletteEntryV
|
||||
|
||||
keyEventArgs.Handled = true;
|
||||
Close();
|
||||
SearchText = string.Empty;
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
@@ -19,15 +19,38 @@ public class CommandPalette
|
||||
_theme = theme;
|
||||
_commandPaletteService = commandPaletteService;
|
||||
}
|
||||
|
||||
public Border<IRootViewModel> View()
|
||||
{
|
||||
var inputTextBox = new TextBox<IRootViewModel>()
|
||||
.WithKeyHandler(k =>
|
||||
.WithKeyHandler((sender, k) =>
|
||||
{
|
||||
if (k.Key == Keys.Escape)
|
||||
{
|
||||
_commandPaletteService.CloseCommandPalette();
|
||||
}
|
||||
|
||||
if (!k.Handled)
|
||||
{
|
||||
sender.DataContext?.CommandPalette.HandleKeyDown(k);
|
||||
}
|
||||
|
||||
if (!k.Handled)
|
||||
{
|
||||
sender.DataContext?.CommandPalette.HandleKeyUp(k);
|
||||
}
|
||||
|
||||
if (k.Key == Keys.Enter)
|
||||
{
|
||||
sender.Text = String.Empty;
|
||||
}
|
||||
})
|
||||
.WithTextHandler((sender, text) =>
|
||||
{
|
||||
if (sender.DataContext is not null)
|
||||
{
|
||||
sender.DataContext.CommandPalette.SearchText = text;
|
||||
}
|
||||
});
|
||||
|
||||
var root = new Border<IRootViewModel>
|
||||
@@ -79,14 +102,16 @@ public class CommandPalette
|
||||
|
||||
item.Bind(
|
||||
item.Parent,
|
||||
d => d.CommandPalette.SelectedItem == item ? _theme.ListViewItemTheme.SelectedBackgroundColor : null,
|
||||
t => t.Background
|
||||
d => d.CommandPalette.SelectedItem == item.DataContext ? _theme.ListViewItemTheme.SelectedBackgroundColor : null,
|
||||
t => t.Background,
|
||||
v => v
|
||||
);
|
||||
|
||||
item.Bind(
|
||||
item.Parent,
|
||||
d => d.CommandPalette.SelectedItem == item ? _theme.ListViewItemTheme.SelectedForegroundColor : null,
|
||||
t => t.Foreground
|
||||
d => d.CommandPalette.SelectedItem == item.DataContext ? _theme.ListViewItemTheme.SelectedForegroundColor : null,
|
||||
t => t.Foreground,
|
||||
v => v
|
||||
);
|
||||
|
||||
return root;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Diagnostics;
|
||||
using System.ComponentModel;
|
||||
using System.Diagnostics;
|
||||
using GeneralInputKey;
|
||||
using PropertyChanged.SourceGenerator;
|
||||
using TerminalUI.Color;
|
||||
@@ -19,7 +20,8 @@ public partial class TextBox<T> : View<T>, IFocusable, IDisplayView
|
||||
IColor? BackgroundColor
|
||||
);
|
||||
|
||||
private readonly List<Action<GeneralKeyEventArgs>> _keyHandlers = new();
|
||||
private readonly List<Action<TextBox<T>, GeneralKeyEventArgs>> _keyHandlers = new();
|
||||
private readonly List<Action<TextBox<T>, string>> _textHandlers = new();
|
||||
|
||||
private RenderState? _lastRenderState;
|
||||
private string _text = string.Empty;
|
||||
@@ -53,6 +55,19 @@ public partial class TextBox<T> : View<T>, IFocusable, IDisplayView
|
||||
_textLines = _text.Split(Environment.NewLine).ToList();
|
||||
RerenderProperties.Add(nameof(Text));
|
||||
RerenderProperties.Add(nameof(MultiLine));
|
||||
|
||||
((INotifyPropertyChanged) this).PropertyChanged += OnPropertyChangedEventHandler;
|
||||
}
|
||||
|
||||
private void OnPropertyChangedEventHandler(object sender, PropertyChangedEventArgs args)
|
||||
{
|
||||
if (args.PropertyName == nameof(Text))
|
||||
{
|
||||
foreach (var textHandler in _textHandlers)
|
||||
{
|
||||
textHandler(this, Text);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateTextField()
|
||||
@@ -283,16 +298,28 @@ public partial class TextBox<T> : View<T>, IFocusable, IDisplayView
|
||||
{
|
||||
foreach (var keyHandler in _keyHandlers)
|
||||
{
|
||||
keyHandler(keyEventArgs);
|
||||
keyHandler(this, keyEventArgs);
|
||||
if (keyEventArgs.Handled) return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public TextBox<T> WithKeyHandler(Action<GeneralKeyEventArgs> keyHandler)
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
base.Dispose(disposing);
|
||||
_keyHandlers.Clear();
|
||||
}
|
||||
|
||||
public TextBox<T> WithKeyHandler(Action<TextBox<T>, GeneralKeyEventArgs> keyHandler)
|
||||
{
|
||||
_keyHandlers.Add(keyHandler);
|
||||
return this;
|
||||
}
|
||||
|
||||
public TextBox<T> WithTextHandler(Action<TextBox<T>, string> textChanged)
|
||||
{
|
||||
_textHandlers.Add(textChanged);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -151,7 +151,22 @@ public abstract partial class View<T> : IView<T>
|
||||
);
|
||||
}
|
||||
|
||||
var renderResult = RenderMethod(renderContext, position, size);
|
||||
bool renderResult;
|
||||
if (Background != null || Foreground != null)
|
||||
{
|
||||
var newRenderContext = new RenderContext(
|
||||
renderContext.ConsoleDriver,
|
||||
renderContext.ForceRerender,
|
||||
Foreground ?? renderContext.Foreground,
|
||||
Background ?? renderContext.Background,
|
||||
renderContext.Statistics);
|
||||
renderResult = RenderMethod(newRenderContext, position, size);
|
||||
}
|
||||
else
|
||||
{
|
||||
renderResult = RenderMethod(renderContext, position, size);
|
||||
}
|
||||
|
||||
if (renderResult)
|
||||
{
|
||||
renderContext.Statistics.RenderedViews++;
|
||||
|
||||
Reference in New Issue
Block a user