Bläddra i källkod

Renamed to HighlightOnPress and added unit tests

Tig 1 år sedan
förälder
incheckning
c1896ee048

+ 26 - 21
Terminal.Gui/View/ViewMouse.cs

@@ -3,9 +3,9 @@
 public partial class View
 {
     /// <summary>
-    /// Gets or sets whether the <see cref="View"/> will invert the colors when the mouse button is pressed/released.
+    /// Gets or sets whether the <see cref="View"/> will highlight the view visually when the mouse button is pressed/released.
     /// </summary>
-    public bool InvertColorsOnPress { get; set; }
+    public bool HighlightOnPress { get; set; }
 
     /// <summary>Gets or sets a value indicating whether this <see cref="View"/> want continuous button pressed event.</summary>
     public virtual bool WantContinuousButtonPressed { get; set; }
@@ -125,9 +125,9 @@ public partial class View
 
 
         // Default behavior is to invoke Accept (via HotKey) on clicked.
-        if (!WantContinuousButtonPressed &&
-            Application.MouseGrabView != this &&
-            mouseEvent.Flags.HasFlag (MouseFlags.Button1Clicked)
+        if ((!WantContinuousButtonPressed &&
+             Application.MouseGrabView != this &&
+             mouseEvent.Flags.HasFlag (MouseFlags.Button1Clicked))
             || mouseEvent.Flags.HasFlag (MouseFlags.Button2Clicked)
             || mouseEvent.Flags.HasFlag (MouseFlags.Button3Clicked)
             || mouseEvent.Flags.HasFlag (MouseFlags.Button4Clicked))
@@ -135,7 +135,7 @@ public partial class View
             return OnMouseClick (args);
         }
 
-        if (InvertColorsOnPress && mouseEvent.Flags.HasFlag (MouseFlags.Button1Pressed))
+        if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Pressed))
         {
             // If WantContinuousButtonPressed is true, and this is not the first pressed event,
             // invoke Accept (via HotKey)
@@ -150,30 +150,35 @@ public partial class View
                 Application.GrabMouse (this);
                 _savedColorScheme = ColorScheme;
 
-                if (CanFocus)
+                if (HighlightOnPress && ColorScheme is { })
                 {
-                    // TODO: Make the inverted color configurable
-                    var cs = new ColorScheme (ColorScheme)
+                    if (CanFocus)
                     {
-                        Focus = new Attribute (ColorScheme.Normal.Foreground, ColorScheme.Focus.Background)
-                    };
-                    ColorScheme = cs;
-                }
-                else
-                {
-                    var cs = new ColorScheme (ColorScheme)
+                        // TODO: Make the inverted color configurable
+                        var cs = new ColorScheme (ColorScheme)
+                        {
+                            Focus = new Attribute (ColorScheme.Normal.Foreground, ColorScheme.Focus.Background)
+                        };
+                        ColorScheme = cs;
+                    }
+                    else
                     {
-                        Normal = new Attribute (ColorScheme.Focus.Background, ColorScheme.Normal.Foreground)
-                    };
-                    ColorScheme = cs;
+                        var cs = new ColorScheme (ColorScheme)
+                        {
+                            Normal = new Attribute (ColorScheme.Focus.Background, ColorScheme.Normal.Foreground)
+                        };
+                        ColorScheme = cs;
+                    }
                 }
 
+                if (CanFocus){
                 // Set the focus, but don't invoke Accept
                 SetFocus ();
+                }
             }
         }
 
-        if (InvertColorsOnPress && mouseEvent.Flags.HasFlag (MouseFlags.Button1Released))
+        if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Released))
         {
             // When the mouse is released, if WantContinuousButtonPressed is set, invoke Accept one last time.
             if (WantContinuousButtonPressed)
@@ -184,7 +189,7 @@ public partial class View
             if (Application.MouseGrabView == this)
             {
                 Application.UngrabMouse ();
-                if (_savedColorScheme is { })
+                if (HighlightOnPress && _savedColorScheme is { })
                 {
                     ColorScheme = _savedColorScheme;
                     _savedColorScheme = null;

+ 1 - 1
Terminal.Gui/Views/Button.cs

@@ -50,7 +50,7 @@ public class Button : View
 
         CanFocus = true;
         AutoSize = true;
-        InvertColorsOnPress = true;
+        HighlightOnPress = true;
 
         // Override default behavior of View
         AddCommand (Command.HotKey, () =>

+ 1 - 1
Terminal.Gui/Views/CheckBox.cs

@@ -35,7 +35,7 @@ public class CheckBox : View
 
         TitleChanged += Checkbox_TitleChanged;
 
-        InvertColorsOnPress = true;
+        HighlightOnPress = true;
         MouseClick += CheckBox_MouseClick;
     }
 

+ 1 - 1
Terminal.Gui/Views/ColorPicker.cs

@@ -31,7 +31,7 @@ public class ColorPicker : View
 
     private void SetInitialProperties ()
     {
-        InvertColorsOnPress = true;
+        HighlightOnPress = true;
 
         CanFocus = true;
         AddCommands ();

+ 1 - 1
Terminal.Gui/Views/RadioGroup.cs

@@ -77,7 +77,7 @@ public class RadioGroup : View
 
         LayoutStarted += RadioGroup_LayoutStarted;
 
-        InvertColorsOnPress = true;
+        HighlightOnPress = true;
 
         MouseClick += RadioGroup_MouseClick;
     }

+ 116 - 4
UnitTests/View/MouseTests.cs

@@ -75,8 +75,6 @@ public class MouseTests (ITestOutputHelper output)
         Assert.Equal (new Point (4, 4), testView.Frame.Location);
         Application.OnMouseEvent (new (new () { X = xy, Y = xy, Flags = MouseFlags.Button1Pressed }));
 
-        Assert.False (Application.MouseGrabView is { } && (Application.MouseGrabView != testView.Margin && Application.MouseGrabView != testView.Border));
-
         Application.OnMouseEvent (new (new () { X = xy + 1, Y = xy + 1, Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition }));
 
         Assert.Equal (expectedMoved, new Point (5, 5) == testView.Frame.Location);
@@ -135,12 +133,12 @@ public class MouseTests (ITestOutputHelper output)
         {
             X = 0, Y = 0,
             Height = 1,
-            Width  = 1,
+            Width = 1,
             CanFocus = true,
         };
 
         view.AutoSize = false;
-        view.X = Pos.Right(otherView);
+        view.X = Pos.Right (otherView);
         view.Y = 0;
         view.Width = 10;
         view.Height = 1;
@@ -318,4 +316,118 @@ public class MouseTests (ITestOutputHelper output)
         Assert.False (me.Handled);
         view.Dispose ();
     }
+
+    [Fact]
+    public void WantContinuousButtonPressed_False_Button1Press_Release_DoesNotClick ()
+    {
+        var view = new View ()
+        {
+            WantContinuousButtonPressed = false
+        };
+
+        var clicked = 0;
+
+        view.MouseClick += (s, e) => clicked++;
+
+        var me = new MouseEvent ()
+        {
+            Flags = MouseFlags.Button1Pressed
+        };
+
+        view.OnMouseEvent (me);
+        Assert.Equal (0, clicked);
+        me.Handled = false;
+
+        view.OnMouseEvent (me);
+        Assert.Equal (0, clicked);
+        me.Handled = false;
+
+        me.Flags = MouseFlags.Button1Released;
+        view.OnMouseEvent (me);
+        Assert.Equal (0, clicked);
+
+        me.Flags = MouseFlags.Button1Clicked;
+        view.OnMouseEvent (me);
+        Assert.Equal (1, clicked);
+
+        view.Dispose ();
+    }
+    [Fact]
+    public void WantContinuousButtonPressed_True_Button1Press_Release_Repeats ()
+    {
+        var view = new View ()
+        {
+            WantContinuousButtonPressed = true
+        };
+
+        var clicked = 0;
+
+        view.MouseClick += (s, e) => clicked++;
+
+        var me = new MouseEvent ()
+        {
+            Flags = MouseFlags.Button1Pressed
+        };
+
+        view.OnMouseEvent (me);
+        Assert.Equal (0, clicked);
+        me.Handled = false;
+
+        view.OnMouseEvent (me);
+        Assert.Equal (1, clicked);
+        me.Handled = false;
+
+        me.Flags = MouseFlags.Button1Released;
+        view.OnMouseEvent (me);
+        Assert.Equal (2, clicked);
+
+        me.Flags = MouseFlags.Button1Clicked;
+        view.OnMouseEvent (me);
+        Assert.Equal (2, clicked);
+
+        view.Dispose ();
+    }
+
+    [Fact]
+    public void HighlightOnPress_False_No_Highlights ()
+    {
+        var view = new View ()
+        {
+            HighlightOnPress = false
+        };
+        view.ColorScheme = new ColorScheme (new Attribute (ColorName.Red, ColorName.Blue));
+        ColorScheme originalColorScheme = view.ColorScheme;
+
+        var me = new MouseEvent ()
+        {
+            Flags = MouseFlags.Button1Pressed
+        };
+
+        view.OnMouseEvent (me);
+        Assert.Equal (originalColorScheme, view.ColorScheme);
+
+        view.Dispose ();
+    }
+
+
+    [Fact]
+    public void HighlightOnPress_False_Highlights ()
+    {
+        var view = new View ()
+        {
+            HighlightOnPress = true
+        };
+        view.ColorScheme = new ColorScheme (new Attribute (ColorName.Red, ColorName.Blue));
+        ColorScheme originalColorScheme = view.ColorScheme;
+
+        var me = new MouseEvent ()
+        {
+            Flags = MouseFlags.Button1Pressed
+        };
+
+        view.OnMouseEvent (me);
+        Assert.NotEqual (originalColorScheme, view.ColorScheme);
+
+        view.Dispose ();
+    }
 }

+ 4 - 4
UnitTests/Views/ButtonTests.cs

@@ -104,7 +104,7 @@ public class ButtonTests (ITestOutputHelper output)
         tab.Add (ckbMatchWholeWord);
 
         var tabView = new TabView { Width = Dim.Fill (), Height = Dim.Fill () };
-        tabView.AddTab (new() { DisplayText = "Find", View = tab }, true);
+        tabView.AddTab (new () { DisplayText = "Find", View = tab }, true);
 
         var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
 
@@ -412,7 +412,7 @@ public class ButtonTests (ITestOutputHelper output)
         TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
         btn.Dispose ();
 
-        btn = new() { Text = "_Test", IsDefault = true };
+        btn = new () { Text = "_Test", IsDefault = true };
         btn.BeginInit ();
         btn.EndInit ();
         Assert.Equal ('_', btn.HotKeySpecifier.Value);
@@ -432,7 +432,7 @@ public class ButtonTests (ITestOutputHelper output)
 
         btn.Dispose ();
 
-        btn = new() { X = 1, Y = 2, Text = "_abc", IsDefault = true };
+        btn = new () { X = 1, Y = 2, Text = "_abc", IsDefault = true };
         btn.BeginInit ();
         btn.EndInit ();
         Assert.Equal ("_abc", btn.Text);
@@ -757,4 +757,4 @@ public class ButtonTests (ITestOutputHelper output)
         Assert.Equal (new (0, 0, 30, 5), pos);
         top.Dispose ();
     }
-}
+}