Browse Source

Moved view highlight on press code into View. Updated all Views that would benefit.

Note this breaks Charmap because ScrollView blindly grabs mouse on Enter. This will be fixed in #3323.
Tig 1 year ago
parent
commit
06cc7d8f71

+ 2 - 0
Terminal.Gui/ConsoleDrivers/WindowsDriver.cs

@@ -1911,6 +1911,8 @@ internal class WindowsDriver : ConsoleDriver
             {
             {
                 _point = null;
                 _point = null;
             }
             }
+            _processButtonClick = true;
+
         }
         }
         else if (mouseEvent.EventFlags == WindowsConsole.EventFlags.MouseMoved
         else if (mouseEvent.EventFlags == WindowsConsole.EventFlags.MouseMoved
                  && !_isOneFingerDoubleClicked
                  && !_isOneFingerDoubleClicked

+ 69 - 8
Terminal.Gui/View/ViewMouse.cs

@@ -2,6 +2,11 @@
 
 
 public partial class View
 public partial class View
 {
 {
+    /// <summary>
+    /// Gets or sets whether the <see cref="View"/> will invert the colors when the mouse button is pressed/released.
+    /// </summary>
+    public bool InvertColorsOnPress { get; set; }
+
     /// <summary>Gets or sets a value indicating whether this <see cref="View"/> want continuous button pressed event.</summary>
     /// <summary>Gets or sets a value indicating whether this <see cref="View"/> want continuous button pressed event.</summary>
     public virtual bool WantContinuousButtonPressed { get; set; }
     public virtual bool WantContinuousButtonPressed { get; set; }
 
 
@@ -91,6 +96,9 @@ public partial class View
         return args.Handled;
         return args.Handled;
     }
     }
 
 
+    [CanBeNull]
+    private ColorScheme _savedColorScheme;
+
     // TODO: OnMouseEvent should not be public virtual, but protected.
     // TODO: OnMouseEvent should not be public virtual, but protected.
     /// <summary>Called when a mouse event occurs within the view's <see cref="Bounds"/>.</summary>
     /// <summary>Called when a mouse event occurs within the view's <see cref="Bounds"/>.</summary>
     /// <remarks>
     /// <remarks>
@@ -115,23 +123,76 @@ public partial class View
 
 
         var args = new MouseEventEventArgs (mouseEvent);
         var args = new MouseEventEventArgs (mouseEvent);
 
 
-        // Clicked support for all buttons and single and double click
-        if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Clicked)
+
+        // Default behavior is to invoke Accept (via HotKey) on clicked.
+        if (!WantContinuousButtonPressed &&
+            Application.MouseGrabView != this &&
+            mouseEvent.Flags.HasFlag (MouseFlags.Button1Clicked)
             || mouseEvent.Flags.HasFlag (MouseFlags.Button2Clicked)
             || mouseEvent.Flags.HasFlag (MouseFlags.Button2Clicked)
             || mouseEvent.Flags.HasFlag (MouseFlags.Button3Clicked)
             || mouseEvent.Flags.HasFlag (MouseFlags.Button3Clicked)
             || mouseEvent.Flags.HasFlag (MouseFlags.Button4Clicked))
             || mouseEvent.Flags.HasFlag (MouseFlags.Button4Clicked))
         {
         {
-            OnMouseClick (args);
+            return OnMouseClick (args);
         }
         }
 
 
-        if (mouseEvent.Flags.HasFlag (MouseFlags.Button1DoubleClicked)
-            || mouseEvent.Flags.HasFlag (MouseFlags.Button2DoubleClicked)
-            || mouseEvent.Flags.HasFlag (MouseFlags.Button3DoubleClicked)
-            || mouseEvent.Flags.HasFlag (MouseFlags.Button4DoubleClicked))
+        if (InvertColorsOnPress && mouseEvent.Flags.HasFlag (MouseFlags.Button1Pressed))
         {
         {
-            OnMouseClick (args);
+            // If WantContinuousButtonPressed is true, and this is not the first pressed event,
+            // invoke Accept (via HotKey)
+            if (WantContinuousButtonPressed && Application.MouseGrabView == this)
+            {
+                return OnMouseClick (args);
+            }
+
+            // The first time we get pressed event, grab the mouse and invert the colors
+            if (Application.MouseGrabView != this)
+            {
+                Application.GrabMouse (this);
+                _savedColorScheme = ColorScheme;
+                var cs = new ColorScheme (new Attribute (ColorScheme.Normal.Background, ColorScheme.Normal.Foreground));
+                ColorScheme = cs;
+
+                // Set the focus, but don't invoke Accept
+                SetFocus ();
+            }
         }
         }
 
 
+        if (InvertColorsOnPress && mouseEvent.Flags.HasFlag (MouseFlags.Button1Released))
+        {
+            // When the mouse is released, if WantContinuousButtonPressed is set, invoke Accept one last time.
+            if (WantContinuousButtonPressed)
+            {
+                OnMouseClick (args);
+            }
+
+            if (Application.MouseGrabView == this)
+            {
+                Application.UngrabMouse ();
+                if (_savedColorScheme is { })
+                {
+                    ColorScheme = _savedColorScheme;
+                    _savedColorScheme = null;
+                }
+            }
+        }
+
+        //// Clicked support for all buttons and single and double click
+        //if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Clicked)
+        //    || mouseEvent.Flags.HasFlag (MouseFlags.Button2Clicked)
+        //    || mouseEvent.Flags.HasFlag (MouseFlags.Button3Clicked)
+        //    || mouseEvent.Flags.HasFlag (MouseFlags.Button4Clicked))
+        //{
+        //    OnMouseClick (args);
+        //}
+
+        //if (mouseEvent.Flags.HasFlag (MouseFlags.Button1DoubleClicked)
+        //    || mouseEvent.Flags.HasFlag (MouseFlags.Button2DoubleClicked)
+        //    || mouseEvent.Flags.HasFlag (MouseFlags.Button3DoubleClicked)
+        //    || mouseEvent.Flags.HasFlag (MouseFlags.Button4DoubleClicked))
+        //{
+        //    OnMouseClick (args);
+        //}
+
         if (args.Handled != true)
         if (args.Handled != true)
         {
         {
             MouseEvent?.Invoke (this, args);
             MouseEvent?.Invoke (this, args);

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

@@ -58,8 +58,8 @@ public class Button : View
         KeyBindings.Add (Key.Enter, Command.HotKey);
         KeyBindings.Add (Key.Enter, Command.HotKey);
 
 
         TitleChanged += Button_TitleChanged;
         TitleChanged += Button_TitleChanged;
-        MouseEvent += Button_MouseEvent;
-        //MouseClick += Button_MouseClick;
+        MouseClick += Button_MouseClick;
+        InvertColorsOnPress = true;
     }
     }
 
 
     [CanBeNull]
     [CanBeNull]

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

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

+ 55 - 45
Terminal.Gui/Views/ColorPicker.cs

@@ -29,6 +29,49 @@ public class ColorPicker : View
     /// <summary>Initializes a new instance of <see cref="ColorPicker"/>.</summary>
     /// <summary>Initializes a new instance of <see cref="ColorPicker"/>.</summary>
     public ColorPicker () { SetInitialProperties (); }
     public ColorPicker () { SetInitialProperties (); }
 
 
+    private void SetInitialProperties ()
+    {
+        InvertColorsOnPress = true;
+
+        CanFocus = true;
+        AddCommands ();
+        AddKeyBindings ();
+
+        LayoutStarted += (o, a) =>
+                         {
+                             Thickness thickness = GetAdornmentsThickness ();
+                             Width = _cols * BoxWidth + thickness.Vertical;
+                             Height = _rows * BoxHeight + thickness.Horizontal;
+                         };
+//        MouseEvent += ColorPicker_MouseEvent;
+        MouseClick += ColorPicker_MouseClick;
+    }
+
+    // TODO: Decouple Cursor from SelectedColor so that mouse press-and-hold can show the color under the cursor.
+    //private void ColorPicker_MouseEvent (object sender, MouseEventEventArgs me)
+    //{
+    //    if (me.MouseEvent.X > Bounds.Width || me.MouseEvent.Y > Bounds.Height)
+    //    {
+    //        me.Handled = true;
+
+    //        return;
+    //    }
+
+    //    me.Handled = true;
+
+    //    return;
+    //}
+
+    private void ColorPicker_MouseClick (object sender, MouseEventEventArgs me)
+    {
+        if (CanFocus)
+        {
+            Cursor = new Point (me.MouseEvent.X / _boxWidth, me.MouseEvent.Y / _boxHeight);
+            SetFocus ();
+            me.Handled = true;
+        }
+    }
+
     /// <summary>Height of a color box</summary>
     /// <summary>Height of a color box</summary>
     public int BoxHeight
     public int BoxHeight
     {
     {
@@ -88,37 +131,6 @@ public class ColorPicker : View
     /// <summary>Fired when a color is picked.</summary>
     /// <summary>Fired when a color is picked.</summary>
     public event EventHandler<ColorEventArgs> ColorChanged;
     public event EventHandler<ColorEventArgs> ColorChanged;
 
 
-    ///<inheritdoc/>
-    protected internal override bool OnMouseEvent  (MouseEvent me)
-    {
-        if (!me.Flags.HasFlag (MouseFlags.Button1Clicked) || !CanFocus)
-        {
-            return false;
-        }
-
-        SetFocus ();
-
-        if (me.X > Bounds.Width || me.Y > Bounds.Height)
-        {
-            return true;
-        }
-
-        Cursor = new Point (me.X / _boxWidth, me.Y / _boxHeight);
-
-        return true;
-    }
-
-    /// <summary>Moves the selected item index to the next row.</summary>
-    /// <returns></returns>
-    public virtual bool MoveDown ()
-    {
-        if (Cursor.Y < _rows - 1)
-        {
-            SelectedColor += _cols;
-        }
-
-        return true;
-    }
 
 
     /// <summary>Moves the selected item index to the previous column.</summary>
     /// <summary>Moves the selected item index to the previous column.</summary>
     /// <returns></returns>
     /// <returns></returns>
@@ -156,6 +168,18 @@ public class ColorPicker : View
         return true;
         return true;
     }
     }
 
 
+    /// <summary>Moves the selected item index to the next row.</summary>
+    /// <returns></returns>
+    public virtual bool MoveDown ()
+    {
+        if (Cursor.Y < _rows - 1)
+        {
+            SelectedColor += _cols;
+        }
+
+        return true;
+    }
+
     ///<inheritdoc/>
     ///<inheritdoc/>
     public override void OnDrawContent (Rectangle contentArea)
     public override void OnDrawContent (Rectangle contentArea)
     {
     {
@@ -265,18 +289,4 @@ public class ColorPicker : View
             AddRune (p.Key.X, p.Key.Y, p.Value);
             AddRune (p.Key.X, p.Key.Y, p.Value);
         }
         }
     }
     }
-
-    private void SetInitialProperties ()
-    {
-        CanFocus = true;
-        AddCommands ();
-        AddKeyBindings ();
-
-        LayoutStarted += (o, a) =>
-                         {
-                             Thickness thickness = GetAdornmentsThickness ();
-                             Width = _cols * BoxWidth + thickness.Vertical;
-                             Height = _rows * BoxHeight + thickness.Horizontal;
-                         };
-    }
 }
 }

+ 11 - 5
Terminal.Gui/Views/DatePicker.cs

@@ -215,11 +215,14 @@ public class DatePicker : View
         _previousMonthButton = new Button
         _previousMonthButton = new Button
         {
         {
             AutoSize = false,
             AutoSize = false,
-            X = Pos.Center () - 4,
+            X = Pos.Center () - 2,
             Y = Pos.Bottom (_calendar) - 1,
             Y = Pos.Bottom (_calendar) - 1,
             Height = 1,
             Height = 1,
-            Width = CalculateCalendarWidth () / 2,
-            Text = GetBackButtonText ()
+            Width = 2,
+            Text = GetBackButtonText (),
+            WantContinuousButtonPressed = true,
+            NoPadding = true,
+            NoDecorations = true
         };
         };
 
 
         _previousMonthButton.Accept += (sender, e) =>
         _previousMonthButton.Accept += (sender, e) =>
@@ -235,8 +238,11 @@ public class DatePicker : View
             X = Pos.Right (_previousMonthButton) + 2,
             X = Pos.Right (_previousMonthButton) + 2,
             Y = Pos.Bottom (_calendar) - 1,
             Y = Pos.Bottom (_calendar) - 1,
             Height = 1,
             Height = 1,
-            Width = CalculateCalendarWidth () / 2,
-            Text = GetBackButtonText ()
+            Width = 2,
+            Text = GetForwardButtonText(),
+            WantContinuousButtonPressed = true,
+            NoPadding = true,
+            NoDecorations = true
         };
         };
 
 
         _nextMonthButton.Accept += (sender, e) =>
         _nextMonthButton.Accept += (sender, e) =>

+ 21 - 1
UICatalog/Scenarios/Buttons.cs

@@ -389,7 +389,27 @@ public class Buttons : Scenario
         {
         {
             X = 0,
             X = 0,
             Y = Pos.Bottom (label) + 1,
             Y = Pos.Bottom (label) + 1,
-            Title = "_Repeat (CanFocus; press-and-hold):",
+            Title = "_No Repeat:",
+        };
+        int noRepeatAcceptCount = 0;
+        var noRepeatButton = new Button ()
+        {
+            X = Pos.Right (label) + 1,
+            Y = Pos.Top (label),
+            Title = $"Accept Count: {noRepeatAcceptCount}",
+            WantContinuousButtonPressed = false,
+        };
+        noRepeatButton.Accept += (s, e) =>
+                                 {
+                                     noRepeatButton.Title = $"Accept Count: {++noRepeatAcceptCount}";
+                                 };
+        main.Add(label, noRepeatButton);
+
+        label = new Label ()
+        {
+            X = 0,
+            Y = Pos.Bottom (label) + 1,
+            Title = "_Repeat (press-and-hold):",
         };
         };
         int acceptCount = 0;
         int acceptCount = 0;
         var repeatButton = new Button ()
         var repeatButton = new Button ()