diff --git a/samples/ControlCatalog/Pages/TextBoxPage.xaml b/samples/ControlCatalog/Pages/TextBoxPage.xaml index 877875a2e1..64118a00b4 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 854a4dc07a..6b5391454f 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 StyledProperty CaretBrushProperty = AvaloniaProperty.Register(nameof(CaretBrushProperty)); @@ -37,7 +43,6 @@ namespace Avalonia.Controls.Presenters private int _selectionStart; private int _selectionEnd; private bool _caretBlink; - private IBrush _highlightBrush; static TextPresenter() { @@ -82,6 +87,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 IBrush CaretBrush { get => GetValue(CaretBrushProperty); @@ -138,14 +155,9 @@ namespace Avalonia.Controls.Presenters var rects = FormattedText.HitTestTextRange(start, length); - if (_highlightBrush == null) - { - _highlightBrush = (IBrush)this.FindResource("HighlightBrush"); - } - foreach (var rect in rects) { - context.FillRectangle(_highlightBrush, rect); + context.FillRectangle(SelectionBrush, rect); } } @@ -266,7 +278,7 @@ namespace Avalonia.Controls.Presenters { result.Spans = new[] { - new FormattedTextStyleSpan(start, length, foregroundBrush: Brushes.White), + new FormattedTextStyleSpan(start, length, SelectionForegroundBrush), }; } diff --git a/src/Avalonia.Controls/TextBox.cs b/src/Avalonia.Controls/TextBox.cs index ec2909288f..876e6a66a7 100644 --- a/src/Avalonia.Controls/TextBox.cs +++ b/src/Avalonia.Controls/TextBox.cs @@ -38,8 +38,14 @@ 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 StyledProperty CaretBrushProperty = - AvaloniaProperty.Register(nameof(CaretBrushProperty)); + AvaloniaProperty.Register(nameof(CaretBrushProperty)); public static readonly DirectProperty SelectionStartProperty = AvaloniaProperty.RegisterDirect( @@ -172,6 +178,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 IBrush CaretBrush { get => GetValue(CaretBrushProperty); @@ -465,7 +483,7 @@ namespace Avalonia.Controls movement = true; selection = false; handled = true; - + } else if (Match(keymap.MoveCursorToTheEndOfLine)) { @@ -494,7 +512,7 @@ namespace Avalonia.Controls movement = true; selection = true; handled = true; - + } else if (Match(keymap.MoveCursorToTheEndOfLineWithSelection)) { 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 a82ea1289b..ec1f6e9e7d 100644 --- a/src/Avalonia.Themes.Default/TextBox.xaml +++ b/src/Avalonia.Themes.Default/TextBox.xaml @@ -3,6 +3,8 @@ + + @@ -45,6 +47,8 @@ TextAlignment="{TemplateBinding TextAlignment}" TextWrapping="{TemplateBinding TextWrapping}" PasswordChar="{TemplateBinding PasswordChar}" + SelectionBrush="{TemplateBinding SelectionBrush}" + SelectionForegroundBrush="{TemplateBinding SelectionForegroundBrush}" CaretBrush="{TemplateBinding CaretBrush}"/>