Browse Source

Add SelectionBrush and SelectionForegroundBrush Feature

pull/2654/head
Abdulbaqi Alshareef 7 years ago
parent
commit
c67e405738
  1. 3
      samples/ControlCatalog/Pages/TextBoxPage.xaml
  2. 30
      src/Avalonia.Controls/Presenters/TextPresenter.cs
  3. 22
      src/Avalonia.Controls/TextBox.cs
  4. 5
      src/Avalonia.Themes.Default/TextBox.xaml

3
samples/ControlCatalog/Pages/TextBoxPage.xaml

@ -26,6 +26,9 @@
<TextBox Width="200" Text="Left aligned text" TextAlignment="Left" /> <TextBox Width="200" Text="Left aligned text" TextAlignment="Left" />
<TextBox Width="200" Text="Center aligned text" TextAlignment="Center" /> <TextBox Width="200" Text="Center aligned text" TextAlignment="Center" />
<TextBox Width="200" Text="Right aligned text" TextAlignment="Right" /> <TextBox Width="200" Text="Right aligned text" TextAlignment="Right" />
<TextBox Width="200" Text="Custom selection brush"
SelectionStart="5" SelectionEnd="22"
SelectionBrush="Green" SelectionForegroundBrush="Yellow"/>
</StackPanel> </StackPanel>
<StackPanel Orientation="Vertical" Spacing="8"> <StackPanel Orientation="Vertical" Spacing="8">

30
src/Avalonia.Controls/Presenters/TextPresenter.cs

@ -19,6 +19,12 @@ namespace Avalonia.Controls.Presenters
public static readonly StyledProperty<char> PasswordCharProperty = public static readonly StyledProperty<char> PasswordCharProperty =
AvaloniaProperty.Register<TextPresenter, char>(nameof(PasswordChar)); AvaloniaProperty.Register<TextPresenter, char>(nameof(PasswordChar));
public static readonly StyledProperty<IBrush> SelectionBrushProperty =
AvaloniaProperty.Register<TextPresenter, IBrush>(nameof(SelectionBrushProperty));
public static readonly StyledProperty<IBrush> SelectionForegroundBrushProperty =
AvaloniaProperty.Register<TextPresenter, IBrush>(nameof(SelectionForegroundBrushProperty));
public static readonly DirectProperty<TextPresenter, int> SelectionStartProperty = public static readonly DirectProperty<TextPresenter, int> SelectionStartProperty =
TextBox.SelectionStartProperty.AddOwner<TextPresenter>( TextBox.SelectionStartProperty.AddOwner<TextPresenter>(
o => o.SelectionStart, o => o.SelectionStart,
@ -34,7 +40,7 @@ namespace Avalonia.Controls.Presenters
private int _selectionStart; private int _selectionStart;
private int _selectionEnd; private int _selectionEnd;
private bool _caretBlink; private bool _caretBlink;
private IBrush _highlightBrush; private IBrush _selectionBrush;
static TextPresenter() static TextPresenter()
{ {
@ -79,6 +85,18 @@ namespace Avalonia.Controls.Presenters
set => SetValue(PasswordCharProperty, value); 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 public int SelectionStart
{ {
get get
@ -129,14 +147,11 @@ namespace Avalonia.Controls.Presenters
var rects = FormattedText.HitTestTextRange(start, length); var rects = FormattedText.HitTestTextRange(start, length);
if (_highlightBrush == null) _selectionBrush = SelectionBrush;
{
_highlightBrush = (IBrush)this.FindResource("HighlightBrush");
}
foreach (var rect in rects) 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 selectionEnd = SelectionEnd;
var start = Math.Min(selectionStart, selectionEnd); var start = Math.Min(selectionStart, selectionEnd);
var length = Math.Max(selectionStart, selectionEnd) - start; var length = Math.Max(selectionStart, selectionEnd) - start;
var selectionForegroundBrush = SelectionForegroundBrush ?? Brushes.White;
if (length > 0) if (length > 0)
{ {
result.Spans = new[] result.Spans = new[]
{ {
new FormattedTextStyleSpan(start, length, foregroundBrush: Brushes.White), new FormattedTextStyleSpan(start, length, foregroundBrush: selectionForegroundBrush),
}; };
} }

22
src/Avalonia.Controls/TextBox.cs

@ -38,6 +38,12 @@ namespace Avalonia.Controls
public static readonly StyledProperty<char> PasswordCharProperty = public static readonly StyledProperty<char> PasswordCharProperty =
AvaloniaProperty.Register<TextBox, char>(nameof(PasswordChar)); AvaloniaProperty.Register<TextBox, char>(nameof(PasswordChar));
public static readonly StyledProperty<IBrush> SelectionBrushProperty =
AvaloniaProperty.Register<TextBox, IBrush>(nameof(SelectionBrushProperty));
public static readonly StyledProperty<IBrush> SelectionForegroundBrushProperty =
AvaloniaProperty.Register<TextBox, IBrush>(nameof(SelectionForegroundBrushProperty));
public static readonly DirectProperty<TextBox, int> SelectionStartProperty = public static readonly DirectProperty<TextBox, int> SelectionStartProperty =
AvaloniaProperty.RegisterDirect<TextBox, int>( AvaloniaProperty.RegisterDirect<TextBox, int>(
nameof(SelectionStart), nameof(SelectionStart),
@ -169,6 +175,18 @@ namespace Avalonia.Controls
set => SetValue(PasswordCharProperty, value); 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 public int SelectionStart
{ {
get get
@ -456,7 +474,7 @@ namespace Avalonia.Controls
movement = true; movement = true;
selection = false; selection = false;
handled = true; handled = true;
} }
else if (Match(keymap.MoveCursorToTheEndOfLine)) else if (Match(keymap.MoveCursorToTheEndOfLine))
{ {
@ -485,7 +503,7 @@ namespace Avalonia.Controls
movement = true; movement = true;
selection = true; selection = true;
handled = true; handled = true;
} }
else if (Match(keymap.MoveCursorToTheEndOfLineWithSelection)) else if (Match(keymap.MoveCursorToTheEndOfLineWithSelection))
{ {

5
src/Avalonia.Themes.Default/TextBox.xaml

@ -3,6 +3,7 @@
<Setter Property="Background" Value="{DynamicResource ThemeBackgroundBrush}"/> <Setter Property="Background" Value="{DynamicResource ThemeBackgroundBrush}"/>
<Setter Property="BorderBrush" Value="{DynamicResource ThemeBorderMidBrush}"/> <Setter Property="BorderBrush" Value="{DynamicResource ThemeBorderMidBrush}"/>
<Setter Property="BorderThickness" Value="{DynamicResource ThemeBorderThickness}"/> <Setter Property="BorderThickness" Value="{DynamicResource ThemeBorderThickness}"/>
<Setter Property="SelectionBrush" Value="{DynamicResource HighlightBrush}"/>
<Setter Property="Padding" Value="4"/> <Setter Property="Padding" Value="4"/>
<Setter Property="Template"> <Setter Property="Template">
<ControlTemplate> <ControlTemplate>
@ -44,7 +45,9 @@
SelectionEnd="{TemplateBinding SelectionEnd}" SelectionEnd="{TemplateBinding SelectionEnd}"
TextAlignment="{TemplateBinding TextAlignment}" TextAlignment="{TemplateBinding TextAlignment}"
TextWrapping="{TemplateBinding TextWrapping}" TextWrapping="{TemplateBinding TextWrapping}"
PasswordChar="{TemplateBinding PasswordChar}"/> PasswordChar="{TemplateBinding PasswordChar}"
SelectionBrush="{TemplateBinding SelectionBrush}"
SelectionForegroundBrush="{TemplateBinding SelectionForegroundBrush}"/>
</Panel> </Panel>
</ScrollViewer> </ScrollViewer>
</DataValidationErrors> </DataValidationErrors>

Loading…
Cancel
Save