Procházet zdrojové kódy

Clicking on padding sets focus

Tig Kindel před 1 rokem
rodič
revize
6b374a9026

+ 3 - 0
Terminal.Gui/Application.cs

@@ -1,3 +1,4 @@
+using System.Diagnostics;
 using System.Globalization;
 using System.Reflection;
 using System.Text.Json.Serialization;
@@ -1509,6 +1510,8 @@ public static partial class Application
 
         WantContinuousButtonPressedView = view.WantContinuousButtonPressed ? view : null;
 
+        Debug.WriteLine ($"OnMouseEvent: ({a.MouseEvent.X},{a.MouseEvent.Y}) - {a.MouseEvent.Flags}");
+
         if (view.OnMouseEvent (me))
         {
             // Should we bubble up the event, if it is not handled?

+ 5 - 2
Terminal.Gui/ConsoleDrivers/NetDriver.cs

@@ -2,6 +2,7 @@
 // NetDriver.cs: The System.Console-based .NET driver, works on Windows and Unix, but is not particularly efficient.
 //
 
+using System.Diagnostics;
 using System.Runtime.InteropServices;
 using static Terminal.Gui.ConsoleDrivers.ConsoleKeyMapping;
 using static Terminal.Gui.NetEvents;
@@ -1135,7 +1136,9 @@ internal class NetDriver : ConsoleDriver
 
                 break;
             case EventType.Mouse:
-                OnMouseEvent (new MouseEventEventArgs (ToDriverMouse (inputEvent.MouseEvent)));
+                MouseEvent me = ToDriverMouse (inputEvent.MouseEvent);
+                Debug.WriteLine ($"NetDriver: ({me.X},{me.Y}) - {me.Flags}");
+                OnMouseEvent (new MouseEventEventArgs (me));
 
                 break;
             case EventType.WindowSize:
@@ -1376,7 +1379,7 @@ internal class NetDriver : ConsoleDriver
 
     private MouseEvent ToDriverMouse (NetEvents.MouseEvent me)
     {
-        //System.Diagnostics.Debug.WriteLine ($"X: {me.Position.X}; Y: {me.Position.Y}; ButtonState: {me.ButtonState}");
+        System.Diagnostics.Debug.WriteLine ($"X: {me.Position.X}; Y: {me.Position.Y}; ButtonState: {me.ButtonState}");
 
         MouseFlags mouseFlag = 0;
 

+ 9 - 1
Terminal.Gui/ConsoleDrivers/WindowsDriver.cs

@@ -1387,6 +1387,12 @@ internal class WindowsDriver : ConsoleDriver
 
             case WindowsConsole.EventType.Mouse:
                 MouseEvent me = ToDriverMouse (inputEvent.MouseEvent);
+
+                if (me is null)
+                {
+                    break;
+                }
+
                 OnMouseEvent (new MouseEventEventArgs (me));
 
                 if (_processButtonClick)
@@ -1763,6 +1769,7 @@ internal class WindowsDriver : ConsoleDriver
         return mouseFlag;
     }
 
+    [CanBeNull]
     private MouseEvent ToDriverMouse (WindowsConsole.MouseEventRecord mouseEvent)
     {
         var mouseFlag = MouseFlags.AllEvents;
@@ -1993,8 +2000,9 @@ internal class WindowsDriver : ConsoleDriver
                 _pointMove = new Point (mouseEvent.MousePosition.X, mouseEvent.MousePosition.Y);
             }
         }
-        else if (mouseEvent.ButtonState == 0 && mouseEvent.EventFlags == 0)
+        else if (mouseEvent is { ButtonState: 0, EventFlags: 0 })
         {
+            // This happens on a double or triple click event.
             mouseFlag = 0;
         }
 

+ 5 - 0
Terminal.Gui/Input/Mouse.cs

@@ -5,6 +5,11 @@ namespace Terminal.Gui;
 [Flags]
 public enum MouseFlags
 {
+    /// <summary>
+    ///    No mouse event. This is the default value for <see cref="MouseEvent.Flags"/> when no mouse event is being reported.
+    /// </summary>
+    None = 0,
+
     /// <summary>The first mouse button was pressed.</summary>
     Button1Pressed = 0x2,
 

+ 9 - 10
Terminal.Gui/View/Adornment/Padding.cs

@@ -52,16 +52,15 @@ public class Padding : Adornment
     /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
     protected internal override bool OnMouseEvent (MouseEvent mouseEvent)
     {
-        //if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Clicked))
-        //{
-        //    if (Parent.CanFocus && !Parent.HasFocus)
-        //    {
-        //        Parent.SetFocus ();
-        //        Parent.SetNeedsDisplay ();
-        //    }
-
-        //    return OnMouseEvent(mouseEvent);
-        //}
+        if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Clicked))
+        {
+            if (Parent.CanFocus && !Parent.HasFocus)
+            {
+                Parent.SetFocus ();
+                Parent.SetNeedsDisplay ();
+                return true;
+            }
+        }
 
         return false;
     }

+ 11 - 1
Terminal.Gui/View/ViewMouse.cs

@@ -9,6 +9,14 @@ public partial class View
     /// <value><see langword="true"/> if want mouse position reports; otherwise, <see langword="false"/>.</value>
     public virtual bool WantMousePositionReports { get; set; }
 
+    /// <summary>Event fired when a mouse event occurs.</summary>
+    /// <remarks>
+    /// <para>
+    /// The coordinates are relative to <see cref="View.Bounds"/>.
+    /// </para>
+    /// </remarks>
+    public event EventHandler<MouseEventEventArgs> MouseEvent;
+
     /// <summary>Event fired when a mouse click occurs.</summary>
     /// <remarks>
     /// <para>
@@ -123,7 +131,9 @@ public partial class View
             return OnMouseClick (args);
         }
 
-        return false;
+        MouseEvent?.Invoke (this, args);
+
+        return args.Handled == true;
     }
 
     /// <summary>Invokes the MouseClick event.</summary>

+ 51 - 12
UICatalog/Scenarios/Mouse.cs

@@ -14,31 +14,70 @@ public class Mouse : Scenario
         ml = new Label { X = 1, Y = 1, Text = "Mouse: " };
         List<string> rme = new ();
 
-        var test = new Label { X = 1, Y = 2, Text = "Se iniciará el análisis" };
-        Win.Add (test);
         Win.Add (ml);
 
-        var rmeList = new ListView
+        var logList = new ListView
         {
-            X = Pos.Right (test) + 25,
-            Y = Pos.Top (test) + 1,
-            Width = Dim.Fill () - 1,
+            X = Pos.AnchorEnd (41),
+            Y = 0,
+            Width = 41,
             Height = Dim.Fill (),
             ColorScheme = Colors.ColorSchemes ["TopLevel"],
             Source = new ListWrapper (rme)
         };
-        Win.Add (rmeList);
+        Win.Add (logList);
 
         Application.MouseEvent += (sender, a) =>
                                   {
                                       ml.Text = $"Mouse: ({a.MouseEvent.X},{a.MouseEvent.Y}) - {a.MouseEvent.Flags} {count}";
                                       rme.Add ($"({a.MouseEvent.X},{a.MouseEvent.Y}) - {a.MouseEvent.Flags} {count++}");
-                                      rmeList.MoveDown ();
+                                      logList.MoveDown ();
                                   };
 
-        // I have no idea what this was intended to show off in demo.c
-        var drag = new Label { X = 1, Y = 4, Text = "Drag: " };
-        var dragText = new TextField { X = Pos.Right (drag), Y = Pos.Top (drag), Width = 40 };
-        Win.Add (drag, dragText);
+        Win.Add (new MouseDemo ()
+        {
+            X = 0,
+            Y = 3,
+            Width = 15,
+            Height = 10,
+            Text = "Mouse Demo",
+            TextAlignment = TextAlignment.Centered,
+            VerticalTextAlignment = VerticalTextAlignment.Middle,
+            ColorScheme = Colors.ColorSchemes ["Dialog"],
+        });
+
+    }
+
+    public class MouseDemo : View
+    {
+        private bool _button1PressedOnEnter = false;
+        public MouseDemo ()
+        {
+            CanFocus = true;
+            MouseEvent += (s, e) =>
+                          {
+                              if (e.MouseEvent.Flags.HasFlag (MouseFlags.Button1Pressed))
+                              {
+                                  if (!_button1PressedOnEnter)
+                                  {
+                                      ColorScheme = Colors.ColorSchemes ["Toplevel"];
+                                  }
+                              }
+                              if (e.MouseEvent.Flags.HasFlag (MouseFlags.Button1Released))
+                              {
+                                  ColorScheme = Colors.ColorSchemes ["Dialog"];
+                                  _button1PressedOnEnter = false;
+                              }
+                          };
+            MouseLeave += (s, e) =>
+                          {
+                              ColorScheme = Colors.ColorSchemes ["Dialog"];
+                              _button1PressedOnEnter = false;
+                          };
+            MouseEnter += (s, e) =>
+                          {
+                              _button1PressedOnEnter = e.MouseEvent.Flags.HasFlag (MouseFlags.Button1Pressed);
+                          };
+        }
     }
 }