Jelajahi Sumber

Refactored Higlight event

Tig 10 bulan lalu
induk
melakukan
7eb241e6f7
2 mengubah file dengan 49 tambahan dan 42 penghapusan
  1. 41 36
      Terminal.Gui/View/View.Mouse.cs
  2. 8 6
      UICatalog/Scenarios/Shortcuts.cs

+ 41 - 36
Terminal.Gui/View/View.Mouse.cs

@@ -8,17 +8,6 @@ public partial class View // Mouse APIs
 {
     private ColorScheme? _savedHighlightColorScheme;
 
-    /// <summary>
-    ///     Fired when the view is highlighted. Set <see cref="CancelEventArgs.Cancel"/> to <see langword="true"/>
-    ///     to implement a custom highlight scheme or prevent the view from being highlighted.
-    /// </summary>
-    public event EventHandler<CancelEventArgs<HighlightStyle>>? Highlight;
-
-    /// <summary>
-    ///     Gets or sets whether the <see cref="View"/> will be highlighted visually while the mouse button is
-    ///     pressed.
-    /// </summary>
-    public HighlightStyle HighlightStyle { get; set; }
 
     /// <summary>Event fired when a mouse click occurs.</summary>
     /// <remarks>
@@ -305,30 +294,6 @@ public partial class View // Mouse APIs
         return args.Handled;
     }
 
-    /// <summary>
-    ///     Called when the view is to be highlighted.
-    /// </summary>
-    /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
-    protected virtual bool? OnHighlight (CancelEventArgs<HighlightStyle> args)
-    {
-        Highlight?.Invoke (this, args);
-
-        if (args.Cancel)
-        {
-            return true;
-        }
-
-        Margin?.Highlight?.Invoke (this, args);
-
-        //args = new (highlight);
-        //Border?.Highlight?.Invoke (this, args);
-
-        //args = new (highlight);
-        //Padding?.Highlight?.Invoke (this, args);
-
-        return args.Cancel;
-    }
-
     /// <summary>Invokes the MouseClick event.</summary>
     /// <remarks>
     ///     <para>
@@ -425,6 +390,41 @@ public partial class View // Mouse APIs
         return false;
     }
 
+    #region Highlight
+
+    /// <summary>
+    ///     Gets or sets whether the <see cref="View"/> will be highlighted visually while the mouse button is
+    ///     pressed.
+    /// </summary>
+    public HighlightStyle HighlightStyle { get; set; }
+    private bool RaiseHighlight (CancelEventArgs<HighlightStyle> args)
+    {
+        if (OnHighlight (args) == true)
+        {
+            return true;
+        }
+
+        Highlight?.Invoke (this, args);
+
+        return args.Cancel;
+    }
+
+    /// <summary>
+    ///     Called when the view is to be highlighted.
+    /// </summary>
+    /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
+    protected virtual bool OnHighlight (CancelEventArgs<HighlightStyle> args)
+    {
+        return false;
+    }
+
+    /// <summary>
+    ///     Fired when the view is highlighted. Set <see cref="CancelEventArgs.Cancel"/> to <see langword="true"/>
+    ///     to implement a custom highlight scheme or prevent the view from being highlighted.
+    /// </summary>
+    public event EventHandler<CancelEventArgs<HighlightStyle>>? Highlight;
+
+
     /// <summary>
     ///     Enables the highlight for the view when the mouse is pressed. Called from OnMouseEvent.
     /// </summary>
@@ -452,11 +452,14 @@ public partial class View // Mouse APIs
         HighlightStyle copy = HighlightStyle;
         CancelEventArgs<HighlightStyle> args = new (ref copy, ref newHighlightStyle);
 
-        if (OnHighlight (args) == true)
+        if (RaiseHighlight (args))
         {
             return true;
         }
 
+        // For Shadow
+        Margin?.RaiseHighlight (args);
+
         if (args.NewValue.HasFlag (HighlightStyle.Pressed) || args.NewValue.HasFlag (HighlightStyle.PressedOutside))
         {
             if (_savedHighlightColorScheme is null && ColorScheme is { })
@@ -500,6 +503,8 @@ public partial class View // Mouse APIs
         return false;
     }
 
+    #endregion
+
     /// <summary>
     ///     For cases where the view is grabbed and the mouse is clicked, this method handles the released event (typically
     ///     when <see cref="WantContinuousButtonPressed"/> or <see cref="HighlightStyle"/> are set).

+ 8 - 6
UICatalog/Scenarios/Shortcuts.cs

@@ -57,6 +57,8 @@ public class Shortcuts : Scenario
             Text = "Width is 35",
             KeyBindingScope = KeyBindingScope.Application,
         };
+        vShortcut1.HighlightStyle |= HighlightStyle.Hover;
+
         Application.Top.Add (vShortcut1);
 
         var vShortcut2 = new Shortcut
@@ -72,14 +74,14 @@ public class Shortcuts : Scenario
             {
                 Orientation = Orientation.Vertical,
                 RadioLabels = ["O_ne", "T_wo", "Th_ree", "Fo_ur"]
-            }
+            },
         };
 
         ((RadioGroup)vShortcut2.CommandView).SelectedItemChanged += (o, args) =>
-                                                                   {
-                                                                       eventSource.Add ($"SelectedItemChanged: {o.GetType ().Name} - {args.SelectedItem}");
-                                                                       eventLog.MoveDown ();
-                                                                   };
+                                                                    {
+                                                                        eventSource.Add ($"SelectedItemChanged: {o.GetType ().Name} - {args.SelectedItem}");
+                                                                        eventLog.MoveDown ();
+                                                                    };
 
         vShortcut2.Accept += (o, args) =>
                             {
@@ -378,6 +380,6 @@ public class Shortcuts : Scenario
     private void Button_Clicked (object sender, HandledEventArgs e)
     {
         //e.Cancel = true;
-        MessageBox.Query ("Hi", $"You clicked {sender}"); 
+        MessageBox.Query ("Hi", $"You clicked {sender}");
     }
 }