From 17295c2ddf69aee2d7b05c7bb445423412b11fec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E5=8A=BC?= Date: Tue, 23 Apr 2024 22:53:26 +0800 Subject: [PATCH 1/2] =?UTF-8?q?TagInput=20=E5=A2=9E=E5=8A=A0=20CleanLostFo?= =?UTF-8?q?cus=20=E7=94=A8=E4=BA=8E=E5=A4=B1=E5=8E=BB=E7=84=A6=E7=82=B9?= =?UTF-8?q?=E5=90=8E=E6=B8=85=E7=A9=BATextBox=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- demo/Ursa.Demo/Pages/TagInputDemo.axaml | 1 + src/Ursa/Controls/TagInput/TagInput.cs | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/demo/Ursa.Demo/Pages/TagInputDemo.axaml b/demo/Ursa.Demo/Pages/TagInputDemo.axaml index fc94f33..eb5dbfc 100644 --- a/demo/Ursa.Demo/Pages/TagInputDemo.axaml +++ b/demo/Ursa.Demo/Pages/TagInputDemo.axaml @@ -21,6 +21,7 @@ diff --git a/src/Ursa/Controls/TagInput/TagInput.cs b/src/Ursa/Controls/TagInput/TagInput.cs index 920d9ac..aa45ab4 100644 --- a/src/Ursa/Controls/TagInput/TagInput.cs +++ b/src/Ursa/Controls/TagInput/TagInput.cs @@ -50,6 +50,7 @@ public class TagInput : TemplatedControl { _textBox = new TextBox(); _textBox.AddHandler(KeyDownEvent, OnTextBoxKeyDown, RoutingStrategies.Tunnel); + _textBox.AddHandler(LostFocusEvent, OnTextBox_LostFocus, RoutingStrategies.Bubble); Items = new AvaloniaList { _textBox @@ -57,6 +58,14 @@ public class TagInput : TemplatedControl Tags = new ObservableCollection(); } + private void OnTextBox_LostFocus(object? sender, RoutedEventArgs e) + { + if(CleanLostFocus) + { + _textBox.Text = ""; + } + } + public static readonly StyledProperty InputThemeProperty = AvaloniaProperty.Register( nameof(InputTheme)); @@ -86,6 +95,16 @@ public class TagInput : TemplatedControl set => SetValue(SeparatorProperty, value); } + public static readonly StyledProperty CleanLostFocusProperty = AvaloniaProperty.Register( + nameof(CleanLostFocus), defaultValue: false); + + public bool CleanLostFocus + { + get => GetValue(CleanLostFocusProperty); + set => SetValue(CleanLostFocusProperty, value); + } + + public static readonly StyledProperty AllowDuplicatesProperty = AvaloniaProperty.Register( nameof(AllowDuplicates), defaultValue: true); From 46b233c35ebf33d450411ce0c44400374c70aad7 Mon Sep 17 00:00:00 2001 From: rabbitism Date: Tue, 23 Apr 2024 23:25:59 +0800 Subject: [PATCH 2/2] feat: improve lost focus behavior. --- demo/Ursa.Demo/Pages/TagInputDemo.axaml | 2 +- .../Controls/TagInput/LostFocusBehavior.cs | 8 +++ src/Ursa/Controls/TagInput/TagInput.cs | 72 ++++++++++--------- 3 files changed, 49 insertions(+), 33 deletions(-) create mode 100644 src/Ursa/Controls/TagInput/LostFocusBehavior.cs diff --git a/demo/Ursa.Demo/Pages/TagInputDemo.axaml b/demo/Ursa.Demo/Pages/TagInputDemo.axaml index eb5dbfc..dadccf2 100644 --- a/demo/Ursa.Demo/Pages/TagInputDemo.axaml +++ b/demo/Ursa.Demo/Pages/TagInputDemo.axaml @@ -21,7 +21,7 @@ diff --git a/src/Ursa/Controls/TagInput/LostFocusBehavior.cs b/src/Ursa/Controls/TagInput/LostFocusBehavior.cs new file mode 100644 index 0000000..a3d24a7 --- /dev/null +++ b/src/Ursa/Controls/TagInput/LostFocusBehavior.cs @@ -0,0 +1,8 @@ +namespace Ursa.Controls; + +public enum LostFocusBehavior +{ + None, + Add, + Clear, +} \ No newline at end of file diff --git a/src/Ursa/Controls/TagInput/TagInput.cs b/src/Ursa/Controls/TagInput/TagInput.cs index aa45ab4..920f01c 100644 --- a/src/Ursa/Controls/TagInput/TagInput.cs +++ b/src/Ursa/Controls/TagInput/TagInput.cs @@ -60,9 +60,14 @@ public class TagInput : TemplatedControl private void OnTextBox_LostFocus(object? sender, RoutedEventArgs e) { - if(CleanLostFocus) + switch (LostFocusBehavior) { - _textBox.Text = ""; + case LostFocusBehavior.Add: + AddTags(); + break; + case LostFocusBehavior.Clear: + _textBox.Text = ""; + break; } } @@ -95,13 +100,13 @@ public class TagInput : TemplatedControl set => SetValue(SeparatorProperty, value); } - public static readonly StyledProperty CleanLostFocusProperty = AvaloniaProperty.Register( - nameof(CleanLostFocus), defaultValue: false); + public static readonly StyledProperty LostFocusBehaviorProperty = AvaloniaProperty.Register( + nameof(LostFocusBehavior)); - public bool CleanLostFocus + public LostFocusBehavior LostFocusBehavior { - get => GetValue(CleanLostFocusProperty); - set => SetValue(CleanLostFocusProperty, value); + get => GetValue(LostFocusBehaviorProperty); + set => SetValue(LostFocusBehaviorProperty, value); } @@ -222,31 +227,7 @@ public class TagInput : TemplatedControl { if (args.Key == Key.Enter) { - if (_textBox.Text?.Length > 0) - { - string[] values; - if (!string.IsNullOrEmpty(Separator)) - { - values = _textBox.Text.Split(new string[] { Separator }, - StringSplitOptions.RemoveEmptyEntries); - } - else - { - values = new[] { _textBox.Text }; - } - - if (!AllowDuplicates && Tags != null) - values = values.Distinct().Except(Tags).ToArray(); - - for (int i = 0; i < values.Length; i++) - { - int index = Items.Count - 1; - // Items.Insert(index, values[i]); - Tags?.Insert(index, values[i]); - } - - _textBox.Text = ""; - } + AddTags(); } else if (args.Key == Key.Delete || args.Key == Key.Back) { @@ -262,6 +243,33 @@ public class TagInput : TemplatedControl } } } + + private void AddTags() + { + if (!(_textBox.Text?.Length > 0)) return; + string[] values; + if (!string.IsNullOrEmpty(Separator)) + { + values = _textBox.Text.Split(new string[] { Separator }, + StringSplitOptions.RemoveEmptyEntries); + } + else + { + values = new[] { _textBox.Text }; + } + + if (!AllowDuplicates && Tags != null) + values = values.Distinct().Except(Tags).ToArray(); + + for (int i = 0; i < values.Length; i++) + { + int index = Items.Count - 1; + // Items.Insert(index, values[i]); + Tags?.Insert(index, values[i]); + } + + _textBox.Text = ""; + } public void Close(object o) {