From f5f850bb93342a55fb26f718b7cb6f624c8e7d96 Mon Sep 17 00:00:00 2001 From: Tig Date: Sun, 6 Oct 2024 16:36:04 -0600 Subject: [PATCH] Fixed combobox --- Terminal.Gui/View/View.Command.cs | 4 +-- Terminal.Gui/Views/ComboBox.cs | 42 +++++++++++++++++-------------- UnitTests/Views/ComboBoxTests.cs | 5 ++-- 3 files changed, 28 insertions(+), 23 deletions(-) diff --git a/Terminal.Gui/View/View.Command.cs b/Terminal.Gui/View/View.Command.cs index 9e1e0f03c..52b42f47b 100644 --- a/Terminal.Gui/View/View.Command.cs +++ b/Terminal.Gui/View/View.Command.cs @@ -83,14 +83,14 @@ public partial class View // Command APIs if (isDefaultView != this && isDefaultView is Button { IsDefault: true } button) { - bool? handled = isDefaultView.InvokeCommand (Command.Accept); + bool? handled = isDefaultView.InvokeCommand (Command.Accept, ctx: new (Command.Accept, null, null, this)); if (handled == true) { return true; } } - return SuperView?.InvokeCommand (Command.Accept) == true; + return SuperView?.InvokeCommand (Command.Accept, ctx: new (Command.Accept, null, null, this)) == true; } return Accepted is null ? null : args.Handled; diff --git a/Terminal.Gui/Views/ComboBox.cs b/Terminal.Gui/Views/ComboBox.cs index db8b8946d..28816516e 100644 --- a/Terminal.Gui/Views/ComboBox.cs +++ b/Terminal.Gui/Views/ComboBox.cs @@ -33,20 +33,17 @@ public class ComboBox : View, IDesignable _listview = new ComboListView (this, HideDropdownListOnClick) { CanFocus = true, TabStop = TabBehavior.NoStop }; _search.TextChanged += Search_Changed; - _search.Accepted += Search_Accept; _listview.Y = Pos.Bottom (_search); _listview.OpenSelectedItem += (sender, a) => Selected (); + _listview.Accepted += (sender, args) => + { + // This prevents Accepted from bubbling up to the combobox + args.Handled = true; - Add (_search, _listview); - - // BUGBUG: This should not be needed; LayoutComplete will handle - Initialized += (s, e) => ProcessLayout (); - - // On resize - LayoutComplete += (sender, a) => ProcessLayout (); - ; - + // But OpenSelectedItem won't be fired because of that. So do it here. + Selected (); + }; _listview.SelectedItemChanged += (sender, e) => { if (!HideDropdownListOnClick && _searchSet.Count > 0) @@ -54,6 +51,13 @@ public class ComboBox : View, IDesignable SetValue (_searchSet [_listview.SelectedItem]); } }; + Add (_search, _listview); + + // BUGBUG: This should not be needed; LayoutComplete will handle + Initialized += (s, e) => ProcessLayout (); + + // On resize + LayoutComplete += (sender, a) => ProcessLayout (); Added += (s, e) => { @@ -74,7 +78,14 @@ public class ComboBox : View, IDesignable }; // Things this view knows how to do - AddCommand (Command.Accept, () => ActivateSelected ()); + AddCommand (Command.Accept, (ctx) => + { + if (ctx.Data == _search) + { + return null; + } + return ActivateSelected (); + }); AddCommand (Command.Toggle, () => ExpandCollapse ()); AddCommand (Command.Expand, () => Expand ()); AddCommand (Command.Collapse, () => Collapse ()); @@ -387,7 +398,7 @@ public class ComboBox : View, IDesignable { if (Selected ()) { - //return false; + return false; } return RaiseAccepted () == true; @@ -658,12 +669,6 @@ public class ComboBox : View, IDesignable SetSearchSet (); } - // Tell TextField to handle Accepted Command (Enter) - void Search_Accept (object sender, HandledEventArgs e) - { - e.Handled = true; - } - private void Search_Changed (object sender, EventArgs e) { if (_source is null) @@ -801,7 +806,6 @@ public class ComboBox : View, IDesignable _listview.Clear (); _listview.Height = CalculateHeight (); SuperView?.MoveSubviewToStart (this); - _listview.SetFocus (); } private bool UnixEmulation () diff --git a/UnitTests/Views/ComboBoxTests.cs b/UnitTests/Views/ComboBoxTests.cs index b03eb58f2..92cfb8c51 100644 --- a/UnitTests/Views/ComboBoxTests.cs +++ b/UnitTests/Views/ComboBoxTests.cs @@ -817,7 +817,6 @@ Three ", { ObservableCollection source = ["One", "Two", "Three"]; var cb = new ComboBox { Width = 10 }; - cb.SetSource (source); var top = new Toplevel (); top.Add (cb); @@ -826,6 +825,8 @@ Three ", top.Add (otherView); Application.Begin (top); + cb.SetSource (source); + Assert.True (cb.HasFocus); Assert.Equal (-1, cb.SelectedItem); Assert.Equal (string.Empty, cb.Text); @@ -833,7 +834,7 @@ Three ", cb.OpenSelectedItem += (s, _) => opened = true; - Assert.True (Application.OnKeyDown (Key.Enter)); + Assert.False (Application.OnKeyDown (Key.Enter)); Assert.False (opened); cb.Text = "Tw";