浏览代码

Got Hover working for all Views (Button) and Border. Still a bit of a prototype.

Tig 1 年之前
父节点
当前提交
ac667dae2c

+ 1 - 1
Terminal.Gui/View/Adornment/Adornment.cs

@@ -212,7 +212,7 @@ public class Adornment : View
     }
     }
 
 
     /// <inheritdoc/>
     /// <inheritdoc/>
-    protected internal override bool OnMouseEnter (MouseEvent mouseEvent)
+    protected internal override bool? OnMouseEnter (MouseEvent mouseEvent)
     {
     {
         // Invert Normal
         // Invert Normal
         if (Diagnostics.HasFlag (ViewDiagnosticFlags.MouseEnter) && ColorScheme != null)
         if (Diagnostics.HasFlag (ViewDiagnosticFlags.MouseEnter) && ColorScheme != null)

+ 26 - 8
Terminal.Gui/View/Adornment/Border.cs

@@ -57,7 +57,7 @@ public class Border : Adornment
         Application.GrabbingMouse += Application_GrabbingMouse;
         Application.GrabbingMouse += Application_GrabbingMouse;
         Application.UnGrabbingMouse += Application_UnGrabbingMouse;
         Application.UnGrabbingMouse += Application_UnGrabbingMouse;
 
 
-        HighlightStyle = HighlightStyle.Pressed;
+        HighlightStyle |= HighlightStyle.Pressed;
         Highlight += Border_Highlight;
         Highlight += Border_Highlight;
     }
     }
 
 
@@ -73,6 +73,12 @@ public class Border : Adornment
     /// <inheritdoc/>
     /// <inheritdoc/>
     public override void BeginInit ()
     public override void BeginInit ()
     {
     {
+        // TOOD: Hack - make Arragnement overidable
+        if ((Parent?.Arrangement & ViewArrangement.Movable) != 0)
+        {
+            HighlightStyle |= HighlightStyle.Hover;
+        }
+
         base.BeginInit ();
         base.BeginInit ();
 
 
 #if SUBVIEW_BASED_BORDER
 #if SUBVIEW_BASED_BORDER
@@ -100,7 +106,7 @@ public class Border : Adornment
             LayoutStarted += OnLayoutStarted;
             LayoutStarted += OnLayoutStarted;
     }
     }
 #endif
 #endif
-}
+    }
 
 
 #if SUBVIEW_BASED_BORDER
 #if SUBVIEW_BASED_BORDER
     private void OnLayoutStarted (object sender, LayoutEventArgs e)
     private void OnLayoutStarted (object sender, LayoutEventArgs e)
@@ -190,18 +196,30 @@ public class Border : Adornment
 
 
     #region Mouse Support
     #region Mouse Support
 
 
-    private LineStyle _savedHighlightLineStyle;
+    private LineStyle? _savedHighlightLineStyle;
 
 
     private void Border_Highlight (object sender, HighlightEventArgs e)
     private void Border_Highlight (object sender, HighlightEventArgs e)
     {
     {
-        if (e.HighlightStyle == HighlightStyle)
+        if (e.HighlightStyle.HasFlag (HighlightStyle.Pressed))
         {
         {
-            _savedHighlightLineStyle = Parent?.BorderStyle ?? LineStyle;
+            if (!_savedHighlightLineStyle.HasValue)
+            {
+                _savedHighlightLineStyle = Parent?.BorderStyle ?? LineStyle;
+            }
             LineStyle = LineStyle.Heavy;
             LineStyle = LineStyle.Heavy;
         }
         }
-        else
+        else if (e.HighlightStyle.HasFlag (HighlightStyle.Hover))
+        {
+            if (!_savedHighlightLineStyle.HasValue)
+            {
+                _savedHighlightLineStyle = Parent?.BorderStyle ?? LineStyle;
+            }
+            LineStyle = LineStyle.Double;
+        }
+
+        if (e.HighlightStyle == HighlightStyle.None && _savedHighlightLineStyle.HasValue)
         {
         {
-            LineStyle = _savedHighlightLineStyle;
+            LineStyle = _savedHighlightLineStyle.Value;
         }
         }
         Parent?.SetNeedsDisplay ();
         Parent?.SetNeedsDisplay ();
         e.Cancel = true;
         e.Cancel = true;
@@ -321,7 +339,7 @@ public class Border : Adornment
         }
         }
     }
     }
 
 
-#endregion Mouse Support
+    #endregion Mouse Support
 
 
     /// <inheritdoc/>
     /// <inheritdoc/>
     public override void OnDrawContent (Rectangle contentArea)
     public override void OnDrawContent (Rectangle contentArea)

+ 41 - 9
Terminal.Gui/View/ViewMouse.cs

@@ -90,7 +90,17 @@ public partial class View
             return false;
             return false;
         }
         }
 
 
-        return OnMouseEnter (mouseEvent);
+        if (OnMouseEnter (mouseEvent) == true)
+        {
+            return true;
+        }
+
+        if (HighlightStyle.HasFlag(HighlightStyle.Hover))
+        {
+            SetHighlight (HighlightStyle.Hover);
+        }
+
+        return false;
     }
     }
 
 
     /// <summary>
     /// <summary>
@@ -108,7 +118,7 @@ public partial class View
     /// </remarks>
     /// </remarks>
     /// <param name="mouseEvent"></param>
     /// <param name="mouseEvent"></param>
     /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
     /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
-    protected internal virtual bool OnMouseEnter (MouseEvent mouseEvent)
+    protected internal virtual bool? OnMouseEnter (MouseEvent mouseEvent)
     {
     {
 
 
         var args = new MouseEventEventArgs (mouseEvent);
         var args = new MouseEventEventArgs (mouseEvent);
@@ -150,7 +160,17 @@ public partial class View
             return false;
             return false;
         }
         }
 
 
-        return OnMouseLeave (mouseEvent);
+        if (OnMouseEnter (mouseEvent) == true)
+        {
+            return true;
+        }
+
+        if (HighlightStyle.HasFlag (HighlightStyle.Hover))
+        {
+            SetHighlight (HighlightStyle.None);
+        }
+
+        return false;
     }
     }
     /// <summary>
     /// <summary>
     ///     Called by <see cref="NewMouseEvent"/> when a mouse leaves <see cref="Bounds"/>. The view will
     ///     Called by <see cref="NewMouseEvent"/> when a mouse leaves <see cref="Bounds"/>. The view will
@@ -400,12 +420,28 @@ public partial class View
     /// </remarks>
     /// </remarks>
     internal void SetHighlight (HighlightStyle style)
     internal void SetHighlight (HighlightStyle style)
     {
     {
+        // TODO: Make the highlight colors configurable
+
         // Enable override via virtual method and/or event
         // Enable override via virtual method and/or event
         if (OnHighlight (style) == true)
         if (OnHighlight (style) == true)
         {
         {
             return;
             return;
         }
         }
 
 
+        if (style.HasFlag (HighlightStyle.Hover))
+        {
+            if (_savedHighlightColorScheme is null && ColorScheme is { })
+            {
+                _savedHighlightColorScheme ??= ColorScheme;
+
+                var cs = new ColorScheme (ColorScheme)
+                {
+                    Normal = new (ColorName.BrightRed, ColorName.Black),
+                };
+                ColorScheme = cs;
+            }
+        }
+
         if (style.HasFlag (HighlightStyle.Pressed) || style.HasFlag (HighlightStyle.PressedOutside))
         if (style.HasFlag (HighlightStyle.Pressed) || style.HasFlag (HighlightStyle.PressedOutside))
         {
         {
 
 
@@ -415,14 +451,9 @@ public partial class View
 
 
                 if (CanFocus)
                 if (CanFocus)
                 {
                 {
-                    // TODO: Make the inverted color configurable
                     var cs = new ColorScheme (ColorScheme)
                     var cs = new ColorScheme (ColorScheme)
                     {
                     {
-                        // For Buttons etc...
                         Focus = new (ColorScheme.Normal.Foreground, ColorScheme.Focus.Background),
                         Focus = new (ColorScheme.Normal.Foreground, ColorScheme.Focus.Background),
-
-                        // For Adornments
-                        Normal = new (ColorScheme.Focus.Foreground, ColorScheme.Normal.Background)
                     };
                     };
                     ColorScheme = cs;
                     ColorScheme = cs;
                 }
                 }
@@ -437,7 +468,8 @@ public partial class View
                 }
                 }
             }
             }
         }
         }
-        else
+
+        if (style == HighlightStyle.None)
         {
         {
             // Unhighlight
             // Unhighlight
             if (_savedHighlightColorScheme is { })
             if (_savedHighlightColorScheme is { })

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

@@ -54,6 +54,7 @@ public class Button : View
         CanFocus = true;
         CanFocus = true;
         AutoSize = true;
         AutoSize = true;
         HighlightStyle |= HighlightStyle.Pressed;
         HighlightStyle |= HighlightStyle.Pressed;
+        HighlightStyle |= HighlightStyle.Hover;
 
 
         // Override default behavior of View
         // Override default behavior of View
         AddCommand (Command.HotKey, () =>
         AddCommand (Command.HotKey, () =>