Browse Source

Add CaretBrush property to Textbox

pull/2651/head
Abdulbaqi Alshareef 7 years ago
parent
commit
434af7d56d
  1. 1
      samples/ControlCatalog/Pages/TextBoxPage.xaml
  2. 32
      src/Avalonia.Controls/Presenters/TextPresenter.cs
  3. 9
      src/Avalonia.Controls/TextBox.cs
  4. 3
      src/Avalonia.Themes.Default/TextBox.xaml

1
samples/ControlCatalog/Pages/TextBoxPage.xaml

@ -26,6 +26,7 @@
<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 caret brush" CaretBrush="DarkOrange"/>
</StackPanel> </StackPanel>
<StackPanel Orientation="Vertical" Spacing="8"> <StackPanel Orientation="Vertical" Spacing="8">

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

@ -19,6 +19,9 @@ 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> CaretBrushProperty =
AvaloniaProperty.Register<TextPresenter, IBrush>(nameof(CaretBrushProperty));
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,
@ -35,7 +38,7 @@ namespace Avalonia.Controls.Presenters
private int _selectionEnd; private int _selectionEnd;
private bool _caretBlink; private bool _caretBlink;
private IBrush _highlightBrush; private IBrush _highlightBrush;
static TextPresenter() static TextPresenter()
{ {
AffectsRender<TextPresenter>(PasswordCharProperty); AffectsRender<TextPresenter>(PasswordCharProperty);
@ -79,6 +82,12 @@ namespace Avalonia.Controls.Presenters
set => SetValue(PasswordCharProperty, value); set => SetValue(PasswordCharProperty, value);
} }
public IBrush CaretBrush
{
get => GetValue(CaretBrushProperty);
set => SetValue(CaretBrushProperty, value);
}
public int SelectionStart public int SelectionStart
{ {
get get
@ -144,16 +153,21 @@ namespace Avalonia.Controls.Presenters
if (selectionStart == selectionEnd) if (selectionStart == selectionEnd)
{ {
var backgroundColor = (((Control)TemplatedParent).GetValue(BackgroundProperty) as SolidColorBrush)?.Color; var caretBrush = CaretBrush;
var caretBrush = Brushes.Black;
if (backgroundColor.HasValue) if (caretBrush is null)
{ {
byte red = (byte)~(backgroundColor.Value.R); var backgroundColor = (((Control)TemplatedParent).GetValue(BackgroundProperty) as SolidColorBrush)?.Color;
byte green = (byte)~(backgroundColor.Value.G); if (backgroundColor.HasValue)
byte blue = (byte)~(backgroundColor.Value.B); {
byte red = (byte)~(backgroundColor.Value.R);
caretBrush = new SolidColorBrush(Color.FromRgb(red, green, blue)); byte green = (byte)~(backgroundColor.Value.G);
byte blue = (byte)~(backgroundColor.Value.B);
caretBrush = new SolidColorBrush(Color.FromRgb(red, green, blue));
}
else
caretBrush = Brushes.Black;
} }
if (_caretBlink) if (_caretBlink)

9
src/Avalonia.Controls/TextBox.cs

@ -38,6 +38,9 @@ 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> CaretBrushProperty =
AvaloniaProperty.Register<TextPresenter, IBrush>(nameof(CaretBrushProperty));
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 +172,12 @@ namespace Avalonia.Controls
set => SetValue(PasswordCharProperty, value); set => SetValue(PasswordCharProperty, value);
} }
public IBrush CaretBrush
{
get => GetValue(CaretBrushProperty);
set => SetValue(CaretBrushProperty, value);
}
public int SelectionStart public int SelectionStart
{ {
get get

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

@ -44,7 +44,8 @@
SelectionEnd="{TemplateBinding SelectionEnd}" SelectionEnd="{TemplateBinding SelectionEnd}"
TextAlignment="{TemplateBinding TextAlignment}" TextAlignment="{TemplateBinding TextAlignment}"
TextWrapping="{TemplateBinding TextWrapping}" TextWrapping="{TemplateBinding TextWrapping}"
PasswordChar="{TemplateBinding PasswordChar}"/> PasswordChar="{TemplateBinding PasswordChar}"
CaretBrush="{TemplateBinding CaretBrush}"/>
</Panel> </Panel>
</ScrollViewer> </ScrollViewer>
</DataValidationErrors> </DataValidationErrors>

Loading…
Cancel
Save