From 7eb241e6f7bfeede7a29b6de8b3d4905c7d7da64 Mon Sep 17 00:00:00 2001 From: Tig Date: Sun, 22 Sep 2024 08:18:08 -0600 Subject: [PATCH] Refactored Higlight event --- Terminal.Gui/View/View.Mouse.cs | 77 +++++++++++++++++--------------- UICatalog/Scenarios/Shortcuts.cs | 14 +++--- 2 files changed, 49 insertions(+), 42 deletions(-) diff --git a/Terminal.Gui/View/View.Mouse.cs b/Terminal.Gui/View/View.Mouse.cs index f27ab07b5..9715bdbbe 100644 --- a/Terminal.Gui/View/View.Mouse.cs +++ b/Terminal.Gui/View/View.Mouse.cs @@ -8,17 +8,6 @@ public partial class View // Mouse APIs { private ColorScheme? _savedHighlightColorScheme; - /// - /// Fired when the view is highlighted. Set to - /// to implement a custom highlight scheme or prevent the view from being highlighted. - /// - public event EventHandler>? Highlight; - - /// - /// Gets or sets whether the will be highlighted visually while the mouse button is - /// pressed. - /// - public HighlightStyle HighlightStyle { get; set; } /// Event fired when a mouse click occurs. /// @@ -305,30 +294,6 @@ public partial class View // Mouse APIs return args.Handled; } - /// - /// Called when the view is to be highlighted. - /// - /// , if the event was handled, otherwise. - protected virtual bool? OnHighlight (CancelEventArgs 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; - } - /// Invokes the MouseClick event. /// /// @@ -425,6 +390,41 @@ public partial class View // Mouse APIs return false; } + #region Highlight + + /// + /// Gets or sets whether the will be highlighted visually while the mouse button is + /// pressed. + /// + public HighlightStyle HighlightStyle { get; set; } + private bool RaiseHighlight (CancelEventArgs args) + { + if (OnHighlight (args) == true) + { + return true; + } + + Highlight?.Invoke (this, args); + + return args.Cancel; + } + + /// + /// Called when the view is to be highlighted. + /// + /// , if the event was handled, otherwise. + protected virtual bool OnHighlight (CancelEventArgs args) + { + return false; + } + + /// + /// Fired when the view is highlighted. Set to + /// to implement a custom highlight scheme or prevent the view from being highlighted. + /// + public event EventHandler>? Highlight; + + /// /// Enables the highlight for the view when the mouse is pressed. Called from OnMouseEvent. /// @@ -452,11 +452,14 @@ public partial class View // Mouse APIs HighlightStyle copy = HighlightStyle; CancelEventArgs 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 + /// /// For cases where the view is grabbed and the mouse is clicked, this method handles the released event (typically /// when or are set). diff --git a/UICatalog/Scenarios/Shortcuts.cs b/UICatalog/Scenarios/Shortcuts.cs index 15cbe7864..81fdf0ca7 100644 --- a/UICatalog/Scenarios/Shortcuts.cs +++ b/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}"); } }