From 8a4c89fe8aec969068467ba29f089f6559f16105 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Mutnia=C5=84ski?= <45796706+RobertMut@users.noreply.github.com> Date: Wed, 6 Dec 2023 16:44:09 +0100 Subject: [PATCH] Add CaretIndex (#13851) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Robert MutniaƄski --- .../AutoCompleteBox.Properties.cs | 18 ++++++++ .../Controls/AutoCompleteBox.xaml | 1 + .../Controls/AutoCompleteBox.xaml | 1 + .../AutoCompleteBoxTests.cs | 42 ++++++++++++++++++- 4 files changed, 61 insertions(+), 1 deletion(-) diff --git a/src/Avalonia.Controls/AutoCompleteBox/AutoCompleteBox.Properties.cs b/src/Avalonia.Controls/AutoCompleteBox/AutoCompleteBox.Properties.cs index 47a0c531ba..8a1d38f88a 100644 --- a/src/Avalonia.Controls/AutoCompleteBox/AutoCompleteBox.Properties.cs +++ b/src/Avalonia.Controls/AutoCompleteBox/AutoCompleteBox.Properties.cs @@ -15,6 +15,15 @@ namespace Avalonia.Controls { public partial class AutoCompleteBox { + /// + /// Defines see property. + /// + public static readonly StyledProperty CaretIndexProperty = + TextBox.CaretIndexProperty.AddOwner(new( + defaultValue: 0, + defaultBindingMode:BindingMode.TwoWay, + coerce: TextBox.CoerceCaretIndex)); + public static readonly StyledProperty WatermarkProperty = TextBox.WatermarkProperty.AddOwner(); @@ -158,6 +167,15 @@ namespace Avalonia.Controls AvaloniaProperty.Register>>?>( nameof(AsyncPopulator)); + /// + /// Gets or sets the caret index + /// + public int CaretIndex + { + get => GetValue(CaretIndexProperty); + set => SetValue(CaretIndexProperty, value); + } + /// /// Gets or sets the minimum number of characters required to be entered /// in the text box before the displays possible matches. diff --git a/src/Avalonia.Themes.Fluent/Controls/AutoCompleteBox.xaml b/src/Avalonia.Themes.Fluent/Controls/AutoCompleteBox.xaml index 228aeaad17..083d90b010 100644 --- a/src/Avalonia.Themes.Fluent/Controls/AutoCompleteBox.xaml +++ b/src/Avalonia.Themes.Fluent/Controls/AutoCompleteBox.xaml @@ -42,6 +42,7 @@ BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="{TemplateBinding CornerRadius}" + CaretIndex="{TemplateBinding CaretIndex, Mode=TwoWay}" FontSize="{TemplateBinding FontSize}" FontFamily="{TemplateBinding FontFamily}" FontWeight="{TemplateBinding FontWeight}" diff --git a/src/Avalonia.Themes.Simple/Controls/AutoCompleteBox.xaml b/src/Avalonia.Themes.Simple/Controls/AutoCompleteBox.xaml index b861c5f3ea..f013539b18 100644 --- a/src/Avalonia.Themes.Simple/Controls/AutoCompleteBox.xaml +++ b/src/Avalonia.Themes.Simple/Controls/AutoCompleteBox.xaml @@ -16,6 +16,7 @@ BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="{TemplateBinding CornerRadius}" + CaretIndex="{TemplateBinding CaretIndex, Mode=TwoWay}" DataValidationErrors.Errors="{TemplateBinding (DataValidationErrors.Errors)}" Watermark="{TemplateBinding Watermark}" /> diff --git a/tests/Avalonia.Controls.UnitTests/AutoCompleteBoxTests.cs b/tests/Avalonia.Controls.UnitTests/AutoCompleteBoxTests.cs index f31df71c0d..88303df08e 100644 --- a/tests/Avalonia.Controls.UnitTests/AutoCompleteBoxTests.cs +++ b/tests/Avalonia.Controls.UnitTests/AutoCompleteBoxTests.cs @@ -456,6 +456,45 @@ namespace Avalonia.Controls.UnitTests }); } + [Fact] + public void CaretIndex_Changes() + { + string text = "Sample text"; + string expectedText = "Saple text"; + RunTest((control, textbox) => + { + control.Text = text; + control.Measure(Size.Infinity); + Dispatcher.UIThread.RunJobs(); + + textbox.RaiseEvent(new KeyEventArgs + { + RoutedEvent = InputElement.KeyDownEvent, + Key = Key.Right + }); + Dispatcher.UIThread.RunJobs(); + + Assert.Equal(1, control.CaretIndex); + Assert.Equal(textbox.CaretIndex, control.CaretIndex); + + control.CaretIndex = 3; + + Assert.Equal(3, control.CaretIndex); + Assert.Equal(textbox.CaretIndex, control.CaretIndex); + + textbox.RaiseEvent(new KeyEventArgs + { + RoutedEvent = InputElement.KeyDownEvent, + Key = Key.Back + }); + Dispatcher.UIThread.RunJobs(); + + Assert.Equal(2, control.CaretIndex); + Assert.Equal(textbox.CaretIndex, control.CaretIndex); + Assert.True(control.Text == expectedText && textbox.Text == expectedText); + }); + } + /// /// Retrieves a defined predicate filter through a new AutoCompleteBox /// control instance. @@ -1111,7 +1150,8 @@ namespace Avalonia.Controls.UnitTests var textBox = new TextBox { - Name = "PART_TextBox" + Name = "PART_TextBox", + [!!TextBox.CaretIndexProperty] = control[!!AutoCompleteBox.CaretIndexProperty] }.RegisterInNameScope(scope); var listbox = new ListBox