Explorar el Código

Enabled ViewHighlight.Hover

Tig hace 10 meses
padre
commit
21e55cc6c0

+ 1 - 0
Terminal.Gui/Configuration/SourceGenerationContext.cs

@@ -15,6 +15,7 @@ namespace Terminal.Gui;
 [JsonSerializable (typeof (AlignmentModes))]
 [JsonSerializable (typeof (LineStyle))]
 [JsonSerializable (typeof (ShadowStyle))]
+[JsonSerializable (typeof (HighlightStyle))]
 [JsonSerializable (typeof (bool?))]
 [JsonSerializable (typeof (Dictionary<ColorName, string>))]
 [JsonSerializable (typeof (Dictionary<string, ThemeScope>))]

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

@@ -73,14 +73,6 @@ public class Border : Adornment
     /// <inheritdoc/>
     public override void BeginInit ()
     {
-#if HOVER
-        // TOOD: Hack - make Arrangement overridable
-        if ((Parent?.Arrangement & ViewArrangement.Movable) != 0)
-        {
-            HighlightStyle |= HighlightStyle.Hover;
-        }
-#endif
-
         base.BeginInit ();
 
 #if SUBVIEW_BASED_BORDER
@@ -245,16 +237,7 @@ public class Border : Adornment
             };
             ColorScheme = cs;
         }
-#if HOVER
-        else if (e.HighlightStyle.HasFlag (HighlightStyle.Hover))
-        {
-            if (!_savedHighlightLineStyle.HasValue)
-            {
-                _savedHighlightLineStyle = Parent?.BorderStyle ?? LineStyle;
-            }
-            LineStyle = LineStyle.Double;
-        }
-#endif
+
 
         if (e.NewValue == HighlightStyle.None && _savedForeColor.HasValue)
         {

+ 4 - 3
Terminal.Gui/View/HighlightStyle.cs

@@ -1,8 +1,11 @@
-namespace Terminal.Gui;
+using System.Text.Json.Serialization;
+
+namespace Terminal.Gui;
 
 /// <summary>
 /// Describes the highlight style of a view.
 /// </summary>
+[JsonConverter (typeof (JsonStringEnumConverter<HighlightStyle>))]
 [Flags]
 public enum HighlightStyle
 {
@@ -11,12 +14,10 @@ public enum HighlightStyle
     /// </summary>
     None = 0,
 
-#if HOVER
     /// <summary>
     /// The mouse is hovering over the view.
     /// </summary>
     Hover = 1,
-#endif
 
     /// <summary>
     /// The mouse is pressed within the <see cref="View.Viewport"/>.

+ 2 - 3
Terminal.Gui/View/View.Diagnostics.cs

@@ -20,10 +20,9 @@ public enum ViewDiagnosticFlags : uint
     Padding = 0b_0000_0010,
 
     /// <summary>
-    ///     When enabled, <see cref="Adornment.OnMouseEnter"/> and <see cref="Adornment.OnMouseLeave"/>
-    ///     will invert the foreground and background colors.
+    ///     When enabled the View's colors will be darker when the mouse is hovering over the View (See <see cref="View.MouseEnter"/> and <see cref="View.MouseLeave"/>.
     /// </summary>
-    MouseEnter = 0b_0000_00100
+    MouseOver = 0b_0000_00100
 }
 
 public partial class View

+ 42 - 17
Terminal.Gui/View/View.Drawing.cs

@@ -310,19 +310,18 @@ public partial class View // Drawing APIs
     ///     If set to <see langword="true"/> this uses the focused colors from the color scheme, otherwise
     ///     the regular ones.
     /// </param>
-    /// <param name="scheme">The color scheme to use.</param>
-    public void DrawHotString (string text, bool focused, ColorScheme scheme)
+    public void DrawHotString (string text, bool focused)
     {
         if (focused)
         {
-            DrawHotString (text, scheme.HotFocus, scheme.Focus);
+            DrawHotString (text, GetHotFocusColor (), GetFocusColor ());
         }
         else
         {
             DrawHotString (
                            text,
-                           Enabled ? scheme.HotNormal : scheme.Disabled,
-                           Enabled ? scheme.Normal : scheme.Disabled
+                           Enabled ? GetHotNormalColor () : ColorScheme.Disabled,
+                           Enabled ? GetNormalColor () : ColorScheme.Disabled
                           );
         }
     }
@@ -336,12 +335,31 @@ public partial class View // Drawing APIs
     public virtual Attribute GetFocusColor ()
     {
         ColorScheme cs = ColorScheme;
+
+        if (cs is null)
+        {
+            cs = new ();
+        }
+
+        return Enabled ? GetColor (cs.Focus) : cs.Disabled;
+    }
+
+    /// <summary>Determines the current <see cref="ColorScheme"/> based on the <see cref="Enabled"/> value.</summary>
+    /// <returns>
+    ///     <see cref="ColorScheme.Focus"/> if <see cref="Enabled"/> is <see langword="true"/> or
+    ///     <see cref="ColorScheme.Disabled"/> if <see cref="Enabled"/> is <see langword="false"/>. If it's
+    ///     overridden can return other values.
+    /// </returns>
+    public virtual Attribute GetHotFocusColor ()
+    {
+        ColorScheme cs = ColorScheme;
+
         if (cs is null)
         {
             cs = new ();
         }
 
-        return Enabled ? cs.Focus : cs.Disabled;
+        return Enabled ? GetColor (cs.HotFocus) : cs.Disabled;
     }
 
     /// <summary>Determines the current <see cref="ColorScheme"/> based on the <see cref="Enabled"/> value.</summary>
@@ -359,7 +377,7 @@ public partial class View // Drawing APIs
             cs = new ();
         }
 
-        return Enabled ? cs.HotNormal : cs.Disabled;
+        return Enabled ? GetColor (cs.HotNormal) : cs.Disabled;
     }
 
     /// <summary>Determines the current <see cref="ColorScheme"/> based on the <see cref="Enabled"/> value.</summary>
@@ -377,16 +395,23 @@ public partial class View // Drawing APIs
             cs = new ();
         }
 
-        if (Diagnostics.HasFlag (ViewDiagnosticFlags.MouseEnter) && _mouseOver)
+        Attribute disabled = new (cs.Disabled.Foreground, cs.Disabled.Background);
+        if (Diagnostics.HasFlag (ViewDiagnosticFlags.MouseOver) && _mouseOver)
         {
-            // Make Normal and Disabled darker when the mouse is over the view
-            cs = new ColorScheme (cs)
-            {
-                Normal = new (ColorScheme.Normal.Foreground.GetDarkerColor (), ColorScheme.Normal.Background.GetDarkerColor()),
-                Disabled = new (ColorScheme.Disabled.Foreground.GetDarkerColor (), ColorScheme.Disabled.Background.GetDarkerColor ())
-            };
+            disabled = new (disabled.Foreground.GetDarkerColor (), disabled.Background.GetDarkerColor ());
         }
-        return Enabled ? cs.Normal : cs.Disabled;
+        return Enabled ? GetColor (cs.Normal) : disabled;
+    }
+
+    private Attribute GetColor (Attribute inputAttribute)
+    {
+        Attribute attr = inputAttribute;
+        if (HighlightStyle.HasFlag (HighlightStyle.Hover) && _mouseOver)
+        {
+            attr = new (inputAttribute.Foreground.GetHighlightColor (), inputAttribute.Background);
+        }
+
+        return attr;
     }
 
     /// <summary>Moves the drawing cursor to the specified <see cref="Viewport"/>-relative location in the view.</summary>
@@ -501,7 +526,7 @@ public partial class View // Drawing APIs
             TextFormatter?.Draw (
                                  drawRect,
                                  HasFocus ? GetFocusColor () : GetNormalColor (),
-                                 HasFocus ? ColorScheme.HotFocus : GetHotNormalColor (),
+                                 HasFocus ? GetHotFocusColor () : GetHotNormalColor (),
                                  Rectangle.Empty
                                 );
             SetSubViewNeedsDisplay ();
@@ -513,7 +538,7 @@ public partial class View // Drawing APIs
         if (_subviews is { } && SubViewNeedsDisplay)
         {
             IEnumerable<View> subviewsNeedingDraw;
-            if (TabStop == TabBehavior.TabGroup && _subviews.Count(v => v.Arrangement.HasFlag (ViewArrangement.Overlapped)) > 0)
+            if (TabStop == TabBehavior.TabGroup && _subviews.Count (v => v.Arrangement.HasFlag (ViewArrangement.Overlapped)) > 0)
             {
                 // TODO: This is a temporary hack to make overlapped non-Toplevels have a zorder. See also View.SetFocus
                 subviewsNeedingDraw = _subviews.Where (

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

@@ -165,16 +165,6 @@ public partial class View // Mouse APIs
 
         MouseEnter?.Invoke (this, eventArgs);
 
-#if HOVER
-        if (HighlightStyle.HasFlag(HighlightStyle.Hover))
-        {
-            if (SetHighlight (HighlightStyle.Hover))
-            {
-                return true;
-            }
-        }
-#endif
-
         _mouseOver = !eventArgs.Cancel;
 
         if (eventArgs.Cancel)
@@ -182,8 +172,7 @@ public partial class View // Mouse APIs
             return true;
         }
 
-        // Invert Normal
-        if (Diagnostics.HasFlag (ViewDiagnosticFlags.MouseEnter) && ColorScheme != null)
+        if ((HighlightStyle.HasFlag(HighlightStyle.Hover) ||  Diagnostics.HasFlag (ViewDiagnosticFlags.MouseOver)))
         {
             SetNeedsDisplay ();
         }
@@ -263,14 +252,12 @@ public partial class View // Mouse APIs
 
         MouseLeave?.Invoke (this, EventArgs.Empty);
 
-#if HOVER
-        if (HighlightStyle.HasFlag (HighlightStyle.Hover))
+        _mouseOver = false;
+
+        if ((HighlightStyle.HasFlag (HighlightStyle.Hover) || Diagnostics.HasFlag (ViewDiagnosticFlags.MouseOver)))
         {
-            SetHighlight (HighlightStyle.None);
+            SetNeedsDisplay ();
         }
-#endif
-
-        _mouseOver = false;
     }
 
     /// <summary>
@@ -443,7 +430,7 @@ public partial class View // Mouse APIs
     /// </summary>
     /// <remarks>
     ///     <para>
-    ///         Set <see cref="HighlightStyle"/> to have the view highlighted based on the mouse.
+    ///         Set <see cref="HighlightStyle"/> to <see cref="HighlightStyle.Pressed"/> to enable.
     ///     </para>
     ///     <para>
     ///         Calls <see cref="OnHighlight"/> which fires the <see cref="Highlight"/> event.
@@ -469,24 +456,7 @@ public partial class View // Mouse APIs
         {
             return true;
         }
-#if HOVER
-        if (style.HasFlag (HighlightStyle.Hover))
-        {
-            if (_savedHighlightColorScheme is null && ColorScheme is { })
-            {
-                _savedHighlightColorScheme ??= ColorScheme;
 
-                var cs = new ColorScheme (ColorScheme)
-                {
-                    Normal = GetFocusColor (),
-                    HotNormal = ColorScheme.HotFocus
-                };
-                ColorScheme = cs;
-            }
-
-            return true;
-        }
-#endif
         if (args.NewValue.HasFlag (HighlightStyle.Pressed) || args.NewValue.HasFlag (HighlightStyle.PressedOutside))
         {
             if (_savedHighlightColorScheme is null && ColorScheme is { })

+ 7 - 4
Terminal.Gui/Views/Button.cs

@@ -39,6 +39,12 @@ public class Button : View, IDesignable
     [SerializableConfigurationProperty (Scope = typeof (ThemeScope))]
     public static ShadowStyle DefaultShadow { get; set; } = ShadowStyle.None;
 
+    /// <summary>
+    /// Gets or sets the default Highlight Style.
+    /// </summary>
+    [SerializableConfigurationProperty (Scope = typeof (ThemeScope))]
+    public static HighlightStyle DefaultHighlightStyle { get; set; } = HighlightStyle.Pressed | HighlightStyle.Hover;
+
     /// <summary>Initializes a new instance of <see cref="Button"/>.</summary>
     public Button ()
     {
@@ -54,10 +60,7 @@ public class Button : View, IDesignable
         Width = Dim.Auto (DimAutoStyle.Text);
 
         CanFocus = true;
-        HighlightStyle |= HighlightStyle.Pressed;
-#if HOVER
-        HighlightStyle |= HighlightStyle.Hover;
-#endif
+        HighlightStyle = DefaultHighlightStyle;
 
         // Override default behavior of View
         AddCommand (Command.HotKey, () =>

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

@@ -4,6 +4,12 @@ namespace Terminal.Gui;
 /// <summary>Shows a check box that can be cycled between three states.</summary>
 public class CheckBox : View
 {
+    /// <summary>
+    /// Gets or sets the default Highlight Style.
+    /// </summary>
+    [SerializableConfigurationProperty (Scope = typeof (ThemeScope))]
+    public static HighlightStyle DefaultHighlightStyle { get; set; } = HighlightStyle.PressedOutside | HighlightStyle.Pressed | HighlightStyle.Hover;
+
     /// <summary>
     ///     Initializes a new instance of <see cref="CheckBox"/>.
     /// </summary>
@@ -23,7 +29,7 @@ public class CheckBox : View
 
         TitleChanged += Checkbox_TitleChanged;
 
-        HighlightStyle = Gui.HighlightStyle.PressedOutside | Gui.HighlightStyle.Pressed;
+        HighlightStyle = DefaultHighlightStyle;
         MouseClick += CheckBox_MouseClick;
     }
 

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

@@ -56,7 +56,7 @@ public class NumericUpDown<T> : View where T : notnull
             Title = $"{Glyphs.DownArrow}",
             WantContinuousButtonPressed = true,
             CanFocus = false,
-            ShadowStyle = ShadowStyle.None
+            ShadowStyle = ShadowStyle.None,
         };
 
         _number = new ()
@@ -81,7 +81,7 @@ public class NumericUpDown<T> : View where T : notnull
             Title = $"{Glyphs.UpArrow}",
             WantContinuousButtonPressed = true,
             CanFocus = false,
-            ShadowStyle = ShadowStyle.None
+            ShadowStyle = ShadowStyle.None,
         };
 
         CanFocus = true;

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

@@ -284,7 +284,7 @@ public class RadioGroup : View, IDesignable, IOrientation
                     }
                     else if (HasFocus && i == _cursor)
                     {
-                        Application.Driver?.SetAttribute (ColorScheme.Focus);
+                        Application.Driver?.SetAttribute (GetFocusColor ());
                     }
 
                     if (rune == HotKeySpecifier && j + 1 < rlRunes.Length)
@@ -312,7 +312,7 @@ public class RadioGroup : View, IDesignable, IOrientation
             }
             else
             {
-                DrawHotString (rl, HasFocus && i == _cursor, ColorScheme);
+                DrawHotString (rl, HasFocus && i == _cursor);
             }
         }
     }

+ 1 - 8
Terminal.Gui/Views/TextField.cs

@@ -730,14 +730,7 @@ public class TextField : View
     /// <inheritdoc/>
     public override Attribute GetNormalColor ()
     {
-        ColorScheme cs = ColorScheme;
-
-        if (ColorScheme is null)
-        {
-            cs = new ColorScheme ();
-        }
-
-        return Enabled ? cs.Focus : cs.Disabled;
+        return GetFocusColor ();
     }
 
     /// <summary>

+ 1 - 8
Terminal.Gui/Views/TextView.cs

@@ -3142,14 +3142,7 @@ public class TextView : View
     /// <inheritdoc/>
     public override Attribute GetNormalColor ()
     {
-        ColorScheme? cs = ColorScheme;
-
-        if (ColorScheme is null)
-        {
-            cs = new ();
-        }
-
-        return Enabled ? cs.Focus : cs.Disabled;
+        return GetFocusColor ();
     }
 
     /// <summary>

+ 10 - 22
UICatalog/UICatalog.cs

@@ -516,18 +516,6 @@ public class UICatalogApp
                                                                       Application.Refresh ();
                                                                   };
 
-                //ShDiagnostics = new Shortcut ()
-                //{
-                //    HelpText = "Diagnostic flags",
-                //    CommandView = new RadioGroup()
-                //    {
-                //        RadioLabels = ["Off", "Ruler", "Padding", "MouseEnter"],
-
-                //        CanFocus = false,
-                //        Orientation = Orientation.Vertical,
-                //    }
-                //};
-
                 StatusBar.Add (
                                new Shortcut
                                {
@@ -792,7 +780,7 @@ public class UICatalogApp
             const string OFF = "View Diagnostics: _Off";
             const string RULER = "View Diagnostics: _Ruler";
             const string PADDING = "View Diagnostics: _Padding";
-            const string MOUSEENTER = "View Diagnostics: _MouseEnter";
+            const string MOUSEOVER = "View Diagnostics: _MouseOver";
             var index = 0;
 
             List<MenuItem> menuItems = new ();
@@ -810,7 +798,7 @@ public class UICatalogApp
                 {
                     item.Checked = !_diagnosticFlags.HasFlag (ViewDiagnosticFlags.Padding)
                                    && !_diagnosticFlags.HasFlag (ViewDiagnosticFlags.Ruler)
-                                   && !_diagnosticFlags.HasFlag (ViewDiagnosticFlags.MouseEnter);
+                                   && !_diagnosticFlags.HasFlag (ViewDiagnosticFlags.MouseOver);
                 }
                 else
                 {
@@ -823,12 +811,12 @@ public class UICatalogApp
 
                                    if (item.Title == t && item.Checked == false)
                                    {
-                                       _diagnosticFlags &= ~(ViewDiagnosticFlags.Padding | ViewDiagnosticFlags.Ruler | ViewDiagnosticFlags.MouseEnter);
+                                       _diagnosticFlags &= ~(ViewDiagnosticFlags.Padding | ViewDiagnosticFlags.Ruler | ViewDiagnosticFlags.MouseOver);
                                        item.Checked = true;
                                    }
                                    else if (item.Title == t && item.Checked == true)
                                    {
-                                       _diagnosticFlags |= ViewDiagnosticFlags.Padding | ViewDiagnosticFlags.Ruler | ViewDiagnosticFlags.MouseEnter;
+                                       _diagnosticFlags |= ViewDiagnosticFlags.Padding | ViewDiagnosticFlags.Ruler | ViewDiagnosticFlags.MouseOver;
                                        item.Checked = false;
                                    }
                                    else
@@ -851,7 +839,7 @@ public class UICatalogApp
                                        {
                                            menuItem.Checked = !_diagnosticFlags.HasFlag (ViewDiagnosticFlags.Ruler)
                                                               && !_diagnosticFlags.HasFlag (ViewDiagnosticFlags.Padding)
-                                                              && !_diagnosticFlags.HasFlag (ViewDiagnosticFlags.MouseEnter);
+                                                              && !_diagnosticFlags.HasFlag (ViewDiagnosticFlags.MouseOver);
                                        }
                                        else if (menuItem.Title != t)
                                        {
@@ -874,7 +862,7 @@ public class UICatalogApp
                     "Off" => OFF,
                     "Ruler" => RULER,
                     "Padding" => PADDING,
-                    "MouseEnter" => MOUSEENTER,
+                    "MouseEnter" => MOUSEOVER,
                     _ => ""
                 };
             }
@@ -885,7 +873,7 @@ public class UICatalogApp
                 {
                     RULER => ViewDiagnosticFlags.Ruler,
                     PADDING => ViewDiagnosticFlags.Padding,
-                    MOUSEENTER => ViewDiagnosticFlags.MouseEnter,
+                    MOUSEOVER => ViewDiagnosticFlags.MouseOver,
                     _ => null!
                 };
             }
@@ -916,14 +904,14 @@ public class UICatalogApp
                         }
 
                         break;
-                    case ViewDiagnosticFlags.MouseEnter:
+                    case ViewDiagnosticFlags.MouseOver:
                         if (add)
                         {
-                            _diagnosticFlags |= ViewDiagnosticFlags.MouseEnter;
+                            _diagnosticFlags |= ViewDiagnosticFlags.MouseOver;
                         }
                         else
                         {
-                            _diagnosticFlags &= ~ViewDiagnosticFlags.MouseEnter;
+                            _diagnosticFlags &= ~ViewDiagnosticFlags.MouseOver;
                         }
 
                         break;

+ 4 - 0
UnitTests/Configuration/SerializableConfigurationPropertyTests.cs

@@ -8,6 +8,10 @@ namespace Terminal.Gui.ConfigurationTests;
 
 public class SerializableConfigurationPropertyTests
 {
+
+    /// <summary>
+    ///     If this test fails, you need to add a new property with the SerializableConfigurationProperty attribute.
+    /// </summary>
     [Fact]
     public void Test_SerializableConfigurationProperty_Types_Added_To_JsonSerializerContext ()
     {