From c67e4057384ba3a515ab42cd195be7a6b61297ee Mon Sep 17 00:00:00 2001 From: Abdulbaqi Alshareef Date: Wed, 12 Jun 2019 14:33:57 +0200 Subject: [PATCH 1/4] Add SelectionBrush and SelectionForegroundBrush Feature --- samples/ControlCatalog/Pages/TextBoxPage.xaml | 3 ++ .../Presenters/TextPresenter.cs | 30 ++++++++++++++----- src/Avalonia.Controls/TextBox.cs | 22 ++++++++++++-- src/Avalonia.Themes.Default/TextBox.xaml | 5 +++- 4 files changed, 50 insertions(+), 10 deletions(-) diff --git a/samples/ControlCatalog/Pages/TextBoxPage.xaml b/samples/ControlCatalog/Pages/TextBoxPage.xaml index 0c0a4d705b..49f2eea636 100644 --- a/samples/ControlCatalog/Pages/TextBoxPage.xaml +++ b/samples/ControlCatalog/Pages/TextBoxPage.xaml @@ -26,6 +26,9 @@ + diff --git a/src/Avalonia.Controls/Presenters/TextPresenter.cs b/src/Avalonia.Controls/Presenters/TextPresenter.cs index fbdf885709..94d9c2d7c5 100644 --- a/src/Avalonia.Controls/Presenters/TextPresenter.cs +++ b/src/Avalonia.Controls/Presenters/TextPresenter.cs @@ -19,6 +19,12 @@ namespace Avalonia.Controls.Presenters public static readonly StyledProperty PasswordCharProperty = AvaloniaProperty.Register(nameof(PasswordChar)); + public static readonly StyledProperty SelectionBrushProperty = + AvaloniaProperty.Register(nameof(SelectionBrushProperty)); + + public static readonly StyledProperty SelectionForegroundBrushProperty = + AvaloniaProperty.Register(nameof(SelectionForegroundBrushProperty)); + public static readonly DirectProperty SelectionStartProperty = TextBox.SelectionStartProperty.AddOwner( o => o.SelectionStart, @@ -34,7 +40,7 @@ namespace Avalonia.Controls.Presenters private int _selectionStart; private int _selectionEnd; private bool _caretBlink; - private IBrush _highlightBrush; + private IBrush _selectionBrush; static TextPresenter() { @@ -79,6 +85,18 @@ namespace Avalonia.Controls.Presenters set => SetValue(PasswordCharProperty, value); } + public IBrush SelectionBrush + { + get => GetValue(SelectionBrushProperty); + set => SetValue(SelectionBrushProperty, value); + } + + public IBrush SelectionForegroundBrush + { + get => GetValue(SelectionForegroundBrushProperty); + set => SetValue(SelectionForegroundBrushProperty, value); + } + public int SelectionStart { get @@ -129,14 +147,11 @@ namespace Avalonia.Controls.Presenters var rects = FormattedText.HitTestTextRange(start, length); - if (_highlightBrush == null) - { - _highlightBrush = (IBrush)this.FindResource("HighlightBrush"); - } + _selectionBrush = SelectionBrush; foreach (var rect in rects) { - context.FillRectangle(_highlightBrush, rect); + context.FillRectangle(_selectionBrush, rect); } } @@ -247,12 +262,13 @@ namespace Avalonia.Controls.Presenters var selectionEnd = SelectionEnd; var start = Math.Min(selectionStart, selectionEnd); var length = Math.Max(selectionStart, selectionEnd) - start; + var selectionForegroundBrush = SelectionForegroundBrush ?? Brushes.White; if (length > 0) { result.Spans = new[] { - new FormattedTextStyleSpan(start, length, foregroundBrush: Brushes.White), + new FormattedTextStyleSpan(start, length, foregroundBrush: selectionForegroundBrush), }; } diff --git a/src/Avalonia.Controls/TextBox.cs b/src/Avalonia.Controls/TextBox.cs index d43957313e..0d39458a5d 100644 --- a/src/Avalonia.Controls/TextBox.cs +++ b/src/Avalonia.Controls/TextBox.cs @@ -38,6 +38,12 @@ namespace Avalonia.Controls public static readonly StyledProperty PasswordCharProperty = AvaloniaProperty.Register(nameof(PasswordChar)); + public static readonly StyledProperty SelectionBrushProperty = + AvaloniaProperty.Register(nameof(SelectionBrushProperty)); + + public static readonly StyledProperty SelectionForegroundBrushProperty = + AvaloniaProperty.Register(nameof(SelectionForegroundBrushProperty)); + public static readonly DirectProperty SelectionStartProperty = AvaloniaProperty.RegisterDirect( nameof(SelectionStart), @@ -169,6 +175,18 @@ namespace Avalonia.Controls set => SetValue(PasswordCharProperty, value); } + public IBrush SelectionBrush + { + get => GetValue(SelectionBrushProperty); + set => SetValue(SelectionBrushProperty, value); + } + + public IBrush SelectionForegroundBrush + { + get => GetValue(SelectionForegroundBrushProperty); + set => SetValue(SelectionForegroundBrushProperty, value); + } + public int SelectionStart { get @@ -456,7 +474,7 @@ namespace Avalonia.Controls movement = true; selection = false; handled = true; - + } else if (Match(keymap.MoveCursorToTheEndOfLine)) { @@ -485,7 +503,7 @@ namespace Avalonia.Controls movement = true; selection = true; handled = true; - + } else if (Match(keymap.MoveCursorToTheEndOfLineWithSelection)) { diff --git a/src/Avalonia.Themes.Default/TextBox.xaml b/src/Avalonia.Themes.Default/TextBox.xaml index 6741bdc7d9..e286e513e0 100644 --- a/src/Avalonia.Themes.Default/TextBox.xaml +++ b/src/Avalonia.Themes.Default/TextBox.xaml @@ -3,6 +3,7 @@ + @@ -44,7 +45,9 @@ SelectionEnd="{TemplateBinding SelectionEnd}" TextAlignment="{TemplateBinding TextAlignment}" TextWrapping="{TemplateBinding TextWrapping}" - PasswordChar="{TemplateBinding PasswordChar}"/> + PasswordChar="{TemplateBinding PasswordChar}" + SelectionBrush="{TemplateBinding SelectionBrush}" + SelectionForegroundBrush="{TemplateBinding SelectionForegroundBrush}"/> From 185511d7e0b80e9c9bfb29a34b62b8d88b1b94d8 Mon Sep 17 00:00:00 2001 From: Abdulbaqi Alshareef Date: Thu, 13 Jun 2019 09:53:55 +0200 Subject: [PATCH 2/4] Set default SelectionForegroundBrush in default style resource instead of code --- src/Avalonia.Controls/Presenters/TextPresenter.cs | 3 +-- src/Avalonia.Themes.Default/Accents/BaseDark.xaml | 2 ++ src/Avalonia.Themes.Default/Accents/BaseLight.xaml | 2 ++ src/Avalonia.Themes.Default/TextBox.xaml | 1 + 4 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Avalonia.Controls/Presenters/TextPresenter.cs b/src/Avalonia.Controls/Presenters/TextPresenter.cs index 94d9c2d7c5..dd36f3da4c 100644 --- a/src/Avalonia.Controls/Presenters/TextPresenter.cs +++ b/src/Avalonia.Controls/Presenters/TextPresenter.cs @@ -262,13 +262,12 @@ namespace Avalonia.Controls.Presenters var selectionEnd = SelectionEnd; var start = Math.Min(selectionStart, selectionEnd); var length = Math.Max(selectionStart, selectionEnd) - start; - var selectionForegroundBrush = SelectionForegroundBrush ?? Brushes.White; if (length > 0) { result.Spans = new[] { - new FormattedTextStyleSpan(start, length, foregroundBrush: selectionForegroundBrush), + new FormattedTextStyleSpan(start, length, SelectionForegroundBrush), }; } diff --git a/src/Avalonia.Themes.Default/Accents/BaseDark.xaml b/src/Avalonia.Themes.Default/Accents/BaseDark.xaml index 8f7d56dbc6..0ed17fae76 100644 --- a/src/Avalonia.Themes.Default/Accents/BaseDark.xaml +++ b/src/Avalonia.Themes.Default/Accents/BaseDark.xaml @@ -22,6 +22,7 @@ #FF808080 #FF119EDA + #FFFFFFFF #FFFF0000 #10FF0000 @@ -39,6 +40,7 @@ + diff --git a/src/Avalonia.Themes.Default/Accents/BaseLight.xaml b/src/Avalonia.Themes.Default/Accents/BaseLight.xaml index 666596d710..3a8a8ec446 100644 --- a/src/Avalonia.Themes.Default/Accents/BaseLight.xaml +++ b/src/Avalonia.Themes.Default/Accents/BaseLight.xaml @@ -22,6 +22,7 @@ #FF808080 #FF086F9E + #FFFFFFFF #FFFF0000 #10FF0000 @@ -39,6 +40,7 @@ + diff --git a/src/Avalonia.Themes.Default/TextBox.xaml b/src/Avalonia.Themes.Default/TextBox.xaml index e286e513e0..9aba2275bf 100644 --- a/src/Avalonia.Themes.Default/TextBox.xaml +++ b/src/Avalonia.Themes.Default/TextBox.xaml @@ -4,6 +4,7 @@ + From 0760d805b9ed49485b5d4c0b5dc882aab809e278 Mon Sep 17 00:00:00 2001 From: Abdulbaqi Alshareef Date: Thu, 13 Jun 2019 10:06:36 +0200 Subject: [PATCH 3/4] Remove unneeded variable --- src/Avalonia.Controls/Presenters/TextPresenter.cs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Avalonia.Controls/Presenters/TextPresenter.cs b/src/Avalonia.Controls/Presenters/TextPresenter.cs index dd36f3da4c..c499ea0e0b 100644 --- a/src/Avalonia.Controls/Presenters/TextPresenter.cs +++ b/src/Avalonia.Controls/Presenters/TextPresenter.cs @@ -40,7 +40,6 @@ namespace Avalonia.Controls.Presenters private int _selectionStart; private int _selectionEnd; private bool _caretBlink; - private IBrush _selectionBrush; static TextPresenter() { @@ -147,11 +146,9 @@ namespace Avalonia.Controls.Presenters var rects = FormattedText.HitTestTextRange(start, length); - _selectionBrush = SelectionBrush; - foreach (var rect in rects) { - context.FillRectangle(_selectionBrush, rect); + context.FillRectangle(SelectionBrush, rect); } } From 85fe3f23f479738dfaedcb034c649507003e77a0 Mon Sep 17 00:00:00 2001 From: Abdulbaqi Alshareef Date: Thu, 13 Jun 2019 19:03:59 +0200 Subject: [PATCH 4/4] Fix the indentation --- src/Avalonia.Controls/Presenters/TextPresenter.cs | 4 ++-- src/Avalonia.Controls/TextBox.cs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Avalonia.Controls/Presenters/TextPresenter.cs b/src/Avalonia.Controls/Presenters/TextPresenter.cs index c499ea0e0b..2c8da58b44 100644 --- a/src/Avalonia.Controls/Presenters/TextPresenter.cs +++ b/src/Avalonia.Controls/Presenters/TextPresenter.cs @@ -20,10 +20,10 @@ namespace Avalonia.Controls.Presenters AvaloniaProperty.Register(nameof(PasswordChar)); public static readonly StyledProperty SelectionBrushProperty = - AvaloniaProperty.Register(nameof(SelectionBrushProperty)); + AvaloniaProperty.Register(nameof(SelectionBrushProperty)); public static readonly StyledProperty SelectionForegroundBrushProperty = - AvaloniaProperty.Register(nameof(SelectionForegroundBrushProperty)); + AvaloniaProperty.Register(nameof(SelectionForegroundBrushProperty)); public static readonly DirectProperty SelectionStartProperty = TextBox.SelectionStartProperty.AddOwner( diff --git a/src/Avalonia.Controls/TextBox.cs b/src/Avalonia.Controls/TextBox.cs index 0d39458a5d..0a64c99a66 100644 --- a/src/Avalonia.Controls/TextBox.cs +++ b/src/Avalonia.Controls/TextBox.cs @@ -39,10 +39,10 @@ namespace Avalonia.Controls AvaloniaProperty.Register(nameof(PasswordChar)); public static readonly StyledProperty SelectionBrushProperty = - AvaloniaProperty.Register(nameof(SelectionBrushProperty)); + AvaloniaProperty.Register(nameof(SelectionBrushProperty)); public static readonly StyledProperty SelectionForegroundBrushProperty = - AvaloniaProperty.Register(nameof(SelectionForegroundBrushProperty)); + AvaloniaProperty.Register(nameof(SelectionForegroundBrushProperty)); public static readonly DirectProperty SelectionStartProperty = AvaloniaProperty.RegisterDirect(