فهرست منبع

Add event handlers for Accept to match Leave events

tznind 11 ماه پیش
والد
کامیت
6bcda35384
2فایلهای تغییر یافته به همراه68 افزوده شده و 23 حذف شده
  1. 10 23
      Terminal.Gui/Views/ColorPicker.cs
  2. 58 0
      UnitTests/Views/ColorPickerTests.cs

+ 10 - 23
Terminal.Gui/Views/ColorPicker.cs

@@ -63,7 +63,8 @@ public class ColorPicker : View
                     Y = y,
                     Width = textFieldWidth
                 };
-                tfValue.HasFocusChanged += UpdateSingleBarValueFromTextField;
+                tfValue.HasFocusChanged += (s,_)=> UpdateSingleBarValueFromTextField(s);
+                tfValue.Accept += (s, _)=>UpdateSingleBarValueFromTextField(s);
                 _textFields.Add (bar, tfValue);
             }
 
@@ -152,7 +153,8 @@ public class ColorPicker : View
         };
         _tfName.Autocomplete = auto;
 
-        _tfName.HasFocusChanged += UpdateValueFromName;
+        _tfName.HasFocusChanged += (s,_)=> UpdateValueFromName(s);
+        _tfName.Accept += (s, _) => UpdateValueFromName (s);
     }
 
     private void CreateTextField ()
@@ -181,7 +183,8 @@ public class ColorPicker : View
         Add (_lbHex);
         Add (_tfHex);
 
-        _tfHex.HasFocusChanged += UpdateValueFromTextField;
+        _tfHex.HasFocusChanged += (s, _) => UpdateValueFromTextField(s);
+        _tfHex.Accept += (s, _) => UpdateValueFromTextField (s);
     }
 
     private void DisposeOldViews ()
@@ -192,7 +195,6 @@ public class ColorPicker : View
 
             if (_textFields.TryGetValue (bar, out TextField? tf))
             {
-                tf.HasFocusChanged -= UpdateSingleBarValueFromTextField;
                 Remove (tf);
                 tf.Dispose ();
             }
@@ -214,7 +216,6 @@ public class ColorPicker : View
         if (_tfHex != null)
         {
             Remove (_tfHex);
-            _tfHex.HasFocusChanged -= UpdateValueFromTextField;
             _tfHex.Dispose ();
             _tfHex = null;
         }
@@ -229,7 +230,6 @@ public class ColorPicker : View
         if (_tfName != null)
         {
             Remove (_tfName);
-            _tfName.HasFocusChanged -= UpdateValueFromName;
             _tfName.Dispose ();
             _tfName = null;
         }
@@ -277,12 +277,8 @@ public class ColorPicker : View
         }
     }
 
-    private void UpdateSingleBarValueFromTextField (object? sender, HasFocusEventArgs e)
+    private void UpdateSingleBarValueFromTextField (object? sender)
     {
-        if (e.NewValue)
-        {
-            return;
-        }
 
         foreach (KeyValuePair<IColorBar, TextField> kvp in _textFields)
         {
@@ -296,13 +292,8 @@ public class ColorPicker : View
         }
     }
 
-    private void UpdateValueFromName (object? sender, HasFocusEventArgs e)
+    private void UpdateValueFromName (object? sender)
     {
-        if (e.NewValue)
-        {
-            return;
-        }
-
         if (_tfName == null)
         {
             return;
@@ -319,13 +310,9 @@ public class ColorPicker : View
         }
     }
 
-    private void UpdateValueFromTextField (object? sender, HasFocusEventArgs e)
+    private void UpdateValueFromTextField (object? sender)
     {
-            if (e.NewValue)
-        {
-            return;
-        }
-
+        
         if (_tfHex == null)
         {
             return;

+ 58 - 0
UnitTests/Views/ColorPickerTests.cs

@@ -790,6 +790,64 @@ public class ColorPickerTests
         Application.ResetState ();
     }
 
+    /// <summary>
+    /// In this version we use the Enter button to accept the typed text instead
+    /// of tabbing to the next view.
+    /// </summary>
+    [Fact]
+    [SetupFakeDriver]
+    public void ColorPicker_EnterHexFor_ColorName_AcceptVariation ()
+    {
+        var cp = GetColorPicker (ColorModel.RGB, true, true);
+        Application.Navigation = new ();
+        Application.Current = new ();
+        Application.Current.Add (cp);
+
+        cp.Draw ();
+
+        var name = GetTextField (cp, ColorPickerPart.ColorName);
+        var hex = GetTextField (cp, ColorPickerPart.Hex);
+
+        hex.SetFocus ();
+
+        Assert.True (hex.HasFocus);
+        Assert.Same (hex, cp.Focused);
+
+        hex.Text = "";
+        name.Text = "";
+
+        Assert.Empty (hex.Text);
+        Assert.Empty (name.Text);
+
+        Application.OnKeyDown ('#');
+        Assert.Empty (name.Text);
+        //7FFFD4
+
+        Assert.Equal ("#", hex.Text);
+        Application.OnKeyDown ('7');
+        Application.OnKeyDown ('F');
+        Application.OnKeyDown ('F');
+        Application.OnKeyDown ('F');
+        Application.OnKeyDown ('D');
+        Assert.Empty (name.Text);
+
+        Application.OnKeyDown ('4');
+
+        Assert.True (hex.HasFocus);
+
+        // Should stay in the hex field (because accept not tab)
+        Application.OnKeyDown (Key.Enter);
+        Assert.True (hex.HasFocus);
+        Assert.Same (hex, cp.Focused);
+
+        // But still, Color name should be recognised as a known string and populated
+        Assert.Equal ("#7FFFD4", hex.Text);
+        Assert.Equal ("Aquamarine", name.Text);
+
+        Application.Current?.Dispose ();
+        Application.ResetState ();
+    }
+
     [Fact]
     public void TestColorNames ()
     {