浏览代码

Fixed combobox

Tig 9 月之前
父节点
当前提交
f5f850bb93
共有 3 个文件被更改,包括 29 次插入24 次删除
  1. 2 2
      Terminal.Gui/View/View.Command.cs
  2. 24 20
      Terminal.Gui/Views/ComboBox.cs
  3. 3 2
      UnitTests/Views/ComboBoxTests.cs

+ 2 - 2
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)
             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)
                 if (handled == true)
                 {
                 {
                     return 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;
         return Accepted is null ? null : args.Handled;

+ 24 - 20
Terminal.Gui/Views/ComboBox.cs

@@ -33,20 +33,17 @@ public class ComboBox : View, IDesignable
         _listview = new ComboListView (this, HideDropdownListOnClick) { CanFocus = true, TabStop = TabBehavior.NoStop };
         _listview = new ComboListView (this, HideDropdownListOnClick) { CanFocus = true, TabStop = TabBehavior.NoStop };
 
 
         _search.TextChanged += Search_Changed;
         _search.TextChanged += Search_Changed;
-        _search.Accepted += Search_Accept;
 
 
         _listview.Y = Pos.Bottom (_search);
         _listview.Y = Pos.Bottom (_search);
         _listview.OpenSelectedItem += (sender, a) => Selected ();
         _listview.OpenSelectedItem += (sender, a) => Selected ();
-
-        Add (_search, _listview);
-
-        // BUGBUG: This should not be needed; LayoutComplete will handle
-        Initialized += (s, e) => ProcessLayout ();
-
-        // On resize
-        LayoutComplete += (sender, a) => ProcessLayout ();
-        ;
-
+        _listview.Accepted += (sender, args) =>
+                              {
+                                  // This prevents Accepted from bubbling up to the combobox
+                                  args.Handled = true;
+
+                                  // But OpenSelectedItem won't be fired because of that. So do it here.
+                                  Selected ();
+                              };
         _listview.SelectedItemChanged += (sender, e) =>
         _listview.SelectedItemChanged += (sender, e) =>
                                          {
                                          {
                                              if (!HideDropdownListOnClick && _searchSet.Count > 0)
                                              if (!HideDropdownListOnClick && _searchSet.Count > 0)
@@ -54,6 +51,13 @@ public class ComboBox : View, IDesignable
                                                  SetValue (_searchSet [_listview.SelectedItem]);
                                                  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) =>
         Added += (s, e) =>
                  {
                  {
@@ -74,7 +78,14 @@ public class ComboBox : View, IDesignable
                  };
                  };
 
 
         // Things this view knows how to do
         // 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.Toggle, () => ExpandCollapse ());
         AddCommand (Command.Expand, () => Expand ());
         AddCommand (Command.Expand, () => Expand ());
         AddCommand (Command.Collapse, () => Collapse ());
         AddCommand (Command.Collapse, () => Collapse ());
@@ -387,7 +398,7 @@ public class ComboBox : View, IDesignable
         {
         {
             if (Selected ())
             if (Selected ())
             {
             {
-                //return false;
+                return false;
             }
             }
 
 
             return RaiseAccepted () == true;
             return RaiseAccepted () == true;
@@ -658,12 +669,6 @@ public class ComboBox : View, IDesignable
         SetSearchSet ();
         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)
     private void Search_Changed (object sender, EventArgs e)
     {
     {
         if (_source is null)
         if (_source is null)
@@ -801,7 +806,6 @@ public class ComboBox : View, IDesignable
         _listview.Clear ();
         _listview.Clear ();
         _listview.Height = CalculateHeight ();
         _listview.Height = CalculateHeight ();
         SuperView?.MoveSubviewToStart (this);
         SuperView?.MoveSubviewToStart (this);
-        _listview.SetFocus ();
     }
     }
 
 
     private bool UnixEmulation ()
     private bool UnixEmulation ()

+ 3 - 2
UnitTests/Views/ComboBoxTests.cs

@@ -817,7 +817,6 @@ Three ",
     {
     {
         ObservableCollection<string> source = ["One", "Two", "Three"];
         ObservableCollection<string> source = ["One", "Two", "Three"];
         var cb = new ComboBox { Width = 10 };
         var cb = new ComboBox { Width = 10 };
-        cb.SetSource (source);
         var top = new Toplevel ();
         var top = new Toplevel ();
 
 
         top.Add (cb);
         top.Add (cb);
@@ -826,6 +825,8 @@ Three ",
         top.Add (otherView);
         top.Add (otherView);
         Application.Begin (top);
         Application.Begin (top);
 
 
+        cb.SetSource (source);
+
         Assert.True (cb.HasFocus);
         Assert.True (cb.HasFocus);
         Assert.Equal (-1, cb.SelectedItem);
         Assert.Equal (-1, cb.SelectedItem);
         Assert.Equal (string.Empty, cb.Text);
         Assert.Equal (string.Empty, cb.Text);
@@ -833,7 +834,7 @@ Three ",
 
 
         cb.OpenSelectedItem += (s, _) => opened = true;
         cb.OpenSelectedItem += (s, _) => opened = true;
 
 
-        Assert.True (Application.OnKeyDown (Key.Enter));
+        Assert.False (Application.OnKeyDown (Key.Enter));
         Assert.False (opened);
         Assert.False (opened);
 
 
         cb.Text = "Tw";
         cb.Text = "Tw";