Code cleanup

This commit is contained in:
Tig
2024-12-09 22:18:25 -08:00
parent 2b8884a881
commit 1180b2fe31
3 changed files with 46 additions and 109 deletions

View File

@@ -2,7 +2,7 @@
namespace Terminal.Gui;
/// <summary>
/// Abstract base class for <see cref="KeyBindings"/> and <see cref="MouseBindings"/>.
/// Abstract class for <see cref="KeyBindings"/> and <see cref="MouseBindings"/>.
/// </summary>
/// <typeparam name="TEvent">The type of the event (e.g. <see cref="Key"/> or <see cref="MouseFlags"/>).</typeparam>
/// <typeparam name="TBinding">The binding type (e.g. <see cref="KeyBinding"/>).</typeparam>
@@ -136,7 +136,7 @@ public abstract class InputBindings<TEvent, TBinding> where TBinding : IInputBin
public bool TryGet (TEvent eventArgs, out TBinding? binding) { return _bindings.TryGetValue (eventArgs, out binding); }
/// <summary>Gets the array of <see cref="Command"/>s bound to <paramref name="eventArgs"/> if it exists.</summary>
/// <param name="eventArgs">The key to check.</param>
/// <param name="eventArgs">The <typeparamref name="TEvent"/> to check.</param>
/// <returns>
/// The array of <see cref="Command"/>s if <paramref name="eventArgs"/> is bound. An empty <see cref="Command"/> array
/// if not.

View File

@@ -1,7 +1,4 @@
#nullable enable
using System.Diagnostics;
using System.Reflection.Metadata;
namespace Terminal.Gui;
public partial class View // Keyboard APIs
@@ -188,7 +185,7 @@ public partial class View // Keyboard APIs
{
Commands = [Command.HotKey],
Key = newKey,
Data = context,
Data = context
};
// Add the base and Alt key
@@ -263,7 +260,8 @@ public partial class View // Keyboard APIs
/// <para>
/// If a more focused subview does not handle the key press, this method raises <see cref="OnKeyDown"/>/
/// <see cref="KeyDown"/> to allow the
/// view to pre-process the key press. If <see cref="OnKeyDown"/>/<see cref="KeyDown"/> is not handled any commands bound to the key will be invoked.
/// view to pre-process the key press. If <see cref="OnKeyDown"/>/<see cref="KeyDown"/> is not handled any commands
/// bound to the key will be invoked.
/// Then, only if no key bindings are
/// handled, <see cref="OnKeyDownNotHandled"/>/<see cref="KeyDownNotHandled"/> will be raised allowing the view to
/// process the key press.
@@ -305,6 +303,7 @@ public partial class View // Keyboard APIs
}
bool? handled = false;
if (InvokeCommandsBoundToHotKey (key, ref handled))
{
return true;
@@ -588,17 +587,20 @@ public partial class View // Keyboard APIs
// BUGBUG: This will miss any hotkeys in subviews of Adornments.
/// <summary>
/// Invokes any commands bound to <paramref name="key"/> on this view and subviews.
/// Invokes any commands bound to <paramref name="hotKey"/> on this view and subviews.
/// </summary>
/// <param name="key"></param>
/// <param name="hotKey"></param>
/// <param name="handled"></param>
/// <returns></returns>
internal bool InvokeCommandsBoundToHotKey (Key key, ref bool? handled)
internal bool InvokeCommandsBoundToHotKey (Key hotKey, ref bool? handled)
{
bool? weHandled = InvokeCommandsBoundToHotKey (key);
if (weHandled is true)
// Process this View
if (HotKeyBindings.TryGet (hotKey, out KeyBinding binding))
{
return true;
if (InvokeCommands (binding.Commands, binding) is true)
{
return true;
}
}
// Now, process any HotKey bindings in the subviews
@@ -609,7 +611,7 @@ public partial class View // Keyboard APIs
continue;
}
bool recurse = subview.InvokeCommandsBoundToHotKey (key, ref handled);
bool recurse = subview.InvokeCommandsBoundToHotKey (hotKey, ref handled);
if (recurse || (handled is { } && (bool)handled))
{
@@ -620,38 +622,6 @@ public partial class View // Keyboard APIs
return false;
}
// TODO: This is a "prototype" debug check. It may be too annoying vs. useful.
// TODO: A better approach would be to have Application hold a list of bound Hotkeys, similar to
// TODO: how Application holds a list of Application Scoped key bindings and then check that list.
/// <summary>
/// Returns true if Key is bound in this view hierarchy. For debugging
/// </summary>
/// <param name="key">The key to test.</param>
/// <param name="boundView">Returns the view the key is bound to.</param>
/// <returns></returns>
public bool IsHotKeyBound (Key key, out View? boundView)
{
// recurse through the subviews to find the views that has the key bound
boundView = null;
foreach (View subview in Subviews)
{
if (subview.HotKeyBindings.TryGet (key, out _))
{
boundView = subview;
return true;
}
if (subview.IsHotKeyBound (key, out boundView))
{
return true;
}
}
return false;
}
/// <summary>
/// Invokes the Commands bound to <paramref name="key"/>.
/// <para>See <see href="../docs/keyboard.md">for an overview of Terminal.Gui keyboard APIs.</see></para>
@@ -671,27 +641,9 @@ public partial class View // Keyboard APIs
return null;
}
#if DEBUG
//if (Application.KeyBindings.TryGet (key, out KeyBinding b))
//{
// Debug.WriteLine (
// $"WARNING: InvokeKeyBindings ({key}) - An Application scope binding exists for this key. The registered view will not invoke Command.");
//}
// TODO: This is a "prototype" debug check. It may be too annoying vs. useful.
// Scour the bindings up our View hierarchy
// to ensure that the key is not already bound to a different set of commands.
if (SuperView?.IsHotKeyBound (key, out View? previouslyBoundView) ?? false)
{
Debug.WriteLine ($"WARNING: InvokeKeyBindings ({key}) - A subview or peer has bound this Key and will not see it: {previouslyBoundView}.");
}
#endif
return InvokeCommands<KeyBinding> (binding.Commands, binding);
return InvokeCommands (binding.Commands, binding);
}
/// <summary>
/// Invokes the Commands bound to <paramref name="hotKey"/>.
/// <para>See <see href="../docs/keyboard.md">for an overview of Terminal.Gui keyboard APIs.</see></para>
@@ -711,24 +663,7 @@ public partial class View // Keyboard APIs
return null;
}
//#if DEBUG
// //if (Application.KeyBindings.TryGet (key, out KeyBinding b))
// //{
// // Debug.WriteLine (
// // $"WARNING: InvokeKeyBindings ({key}) - An Application scope binding exists for this key. The registered view will not invoke Command.");
// //}
// // TODO: This is a "prototype" debug check. It may be too annoying vs. useful.
// // Scour the bindings up our View hierarchy
// // to ensure that the key is not already bound to a different set of commands.
// if (SuperView?.IsHotKeyBound (hotKey, out View? previouslyBoundView) ?? false)
// {
// Debug.WriteLine ($"WARNING: InvokeKeyBindings ({hotKey}) - A subview or peer has bound this Key and will not see it: {previouslyBoundView}.");
// }
//#endif
return InvokeCommands<KeyBinding> (binding.Commands, binding);
return InvokeCommands (binding.Commands, binding);
}
#endregion Key Bindings

View File

@@ -1,6 +1,5 @@
#nullable enable
using System.ComponentModel;
using System.Diagnostics;
namespace Terminal.Gui;
@@ -21,7 +20,6 @@ public partial class View // Mouse APIs
MouseBindings.Add (MouseFlags.Button1Clicked | MouseFlags.ButtonCtrl, Command.Select);
}
/// <summary>
/// Invokes the Commands bound to the MouseFlags specified by <paramref name="mouseEventArgs"/>.
/// <para>See <see href="../docs/mouse.md">for an overview of Terminal.Gui mouse APIs.</see></para>
@@ -43,7 +41,7 @@ public partial class View // Mouse APIs
binding.MouseEventArgs = mouseEventArgs;
return InvokeCommands<MouseBinding> (binding.Commands, binding);
return InvokeCommands (binding.Commands, binding);
}
#region MouseEnterLeave
@@ -52,7 +50,8 @@ public partial class View // Mouse APIs
private ColorScheme? _savedNonHoverColorScheme;
/// <summary>
/// INTERNAL Called by <see cref="Application.RaiseMouseEvent"/> when the mouse moves over the View's <see cref="Frame"/>.
/// INTERNAL Called by <see cref="Application.RaiseMouseEvent"/> when the mouse moves over the View's
/// <see cref="Frame"/>.
/// <see cref="MouseLeave"/> will
/// be raised when the mouse is no longer over the <see cref="Frame"/>. If another View occludes this View, the
/// that View will also receive MouseEnter/Leave events.
@@ -167,7 +166,8 @@ public partial class View // Mouse APIs
public event EventHandler<CancelEventArgs>? MouseEnter;
/// <summary>
/// INTERNAL Called by <see cref="Application.RaiseMouseEvent"/> when the mouse leaves <see cref="Frame"/>, or is occluded
/// INTERNAL Called by <see cref="Application.RaiseMouseEvent"/> when the mouse leaves <see cref="Frame"/>, or is
/// occluded
/// by another non-SubView.
/// </summary>
/// <remarks>
@@ -245,7 +245,8 @@ public partial class View // Mouse APIs
public bool WantMousePositionReports { get; set; }
/// <summary>
/// Processes a new <see cref="MouseEvent"/>. This method is called by <see cref="Application.RaiseMouseEvent"/> when a mouse
/// Processes a new <see cref="MouseEvent"/>. This method is called by <see cref="Application.RaiseMouseEvent"/> when a
/// mouse
/// event occurs.
/// </summary>
/// <remarks>
@@ -260,8 +261,10 @@ public partial class View // Mouse APIs
/// See <see cref="SetPressedHighlight"/> for more information.
/// </para>
/// <para>
/// If <see cref="WantContinuousButtonPressed"/> is <see langword="true"/>, the <see cref="RaiseMouseEvent"/>/<see cref="MouseEvent"/> event
/// will be raised on any new mouse event where <see cref="Terminal.Gui.MouseEventArgs.Flags"/> indicates a button is pressed.
/// If <see cref="WantContinuousButtonPressed"/> is <see langword="true"/>, the <see cref="RaiseMouseEvent"/>/
/// <see cref="MouseEvent"/> event
/// will be raised on any new mouse event where <see cref="Terminal.Gui.MouseEventArgs.Flags"/> indicates a button
/// is pressed.
/// </para>
/// </remarks>
/// <param name="mouseEvent"></param>
@@ -332,7 +335,7 @@ public partial class View // Mouse APIs
/// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
public bool RaiseMouseEvent (MouseEventArgs mouseEvent)
{
if (OnMouseEvent (mouseEvent) || mouseEvent.Handled == true)
if (OnMouseEvent (mouseEvent) || mouseEvent.Handled)
{
return true;
}
@@ -350,10 +353,7 @@ public partial class View // Mouse APIs
/// </remarks>
/// <param name="mouseEvent"></param>
/// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
protected virtual bool OnMouseEvent (MouseEventArgs mouseEvent)
{
return false;
}
protected virtual bool OnMouseEvent (MouseEventArgs mouseEvent) { return false; }
/// <summary>Raised when a mouse event occurs.</summary>
/// <remarks>
@@ -368,7 +368,8 @@ public partial class View // Mouse APIs
#region Mouse Pressed Events
/// <summary>
/// INTERNAL For cases where the view is grabbed and the mouse is clicked, this method handles the released event (typically
/// INTERNAL 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).
/// </summary>
/// <remarks>
@@ -394,7 +395,8 @@ public partial class View // Mouse APIs
}
/// <summary>
/// INTERNAL For cases where the view is grabbed and the mouse is clicked, this method handles the released event (typically
/// INTERNAL 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).
/// </summary>
/// <remarks>
@@ -463,7 +465,8 @@ public partial class View // Mouse APIs
/// Called when the mouse is either clicked or double-clicked.
/// </para>
/// <para>
/// If <see cref="WantContinuousButtonPressed"/> is <see langword="true"/>, will be invoked on every mouse event where
/// If <see cref="WantContinuousButtonPressed"/> is <see langword="true"/>, will be invoked on every mouse event
/// where
/// the mouse button is pressed.
/// </para>
/// </remarks>
@@ -507,7 +510,8 @@ public partial class View // Mouse APIs
/// Called when the mouse is either clicked or double-clicked.
/// </para>
/// <para>
/// If <see cref="WantContinuousButtonPressed"/> is <see langword="true"/>, will be called on every mouse event where
/// If <see cref="WantContinuousButtonPressed"/> is <see langword="true"/>, will be called on every mouse event
/// where
/// the mouse button is pressed.
/// </para>
/// </remarks>
@@ -521,14 +525,16 @@ public partial class View // Mouse APIs
/// Raised when the mouse is either clicked or double-clicked.
/// </para>
/// <para>
/// If <see cref="WantContinuousButtonPressed"/> is <see langword="true"/>, will be raised on every mouse event where
/// If <see cref="WantContinuousButtonPressed"/> is <see langword="true"/>, will be raised on every mouse event
/// where
/// the mouse button is pressed.
/// </para>
/// </remarks>
public event EventHandler<MouseEventArgs>? MouseClick;
/// <summary>
/// INTERNAL For cases where the view is grabbed and the mouse is clicked, this method handles the click event (typically
/// INTERNAL For cases where the view is grabbed and the mouse is clicked, this method handles the click event
/// (typically
/// when <see cref="WantContinuousButtonPressed"/> or <see cref="HighlightStyle"/> are set).
/// </summary>
/// <remarks>
@@ -562,10 +568,8 @@ public partial class View // Mouse APIs
return false;
}
#endregion Mouse Clicked Events
#region Mouse Wheel Events
/// <summary>Raises the <see cref="OnMouseWheel"/>/<see cref="MouseWheel"/> event.</summary>
@@ -601,7 +605,8 @@ public partial class View // Mouse APIs
}
/// <summary>
/// Called when a mouse wheel event occurs. Check <see cref="MouseEventArgs.Flags"/> to see which wheel was moved was clicked.
/// Called when a mouse wheel event occurs. Check <see cref="MouseEventArgs.Flags"/> to see which wheel was moved was
/// clicked.
/// </summary>
/// <remarks>
/// </remarks>
@@ -828,8 +833,5 @@ public partial class View // Mouse APIs
return viewsUnderMouse;
}
private void DisposeMouse ()
{
}
private void DisposeMouse () { }
}