Skip to content

Commit 1180b2f

Browse files
committed
Code cleanup
1 parent 2b8884a commit 1180b2f

File tree

3 files changed

+46
-109
lines changed

3 files changed

+46
-109
lines changed

Terminal.Gui/Input/InputBindings.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
namespace Terminal.Gui;
33

44
/// <summary>
5-
/// Abstract base class for <see cref="KeyBindings"/> and <see cref="MouseBindings"/>.
5+
/// Abstract class for <see cref="KeyBindings"/> and <see cref="MouseBindings"/>.
66
/// </summary>
77
/// <typeparam name="TEvent">The type of the event (e.g. <see cref="Key"/> or <see cref="MouseFlags"/>).</typeparam>
88
/// <typeparam name="TBinding">The binding type (e.g. <see cref="KeyBinding"/>).</typeparam>
@@ -136,7 +136,7 @@ public void Clear (params Command [] command)
136136
public bool TryGet (TEvent eventArgs, out TBinding? binding) { return _bindings.TryGetValue (eventArgs, out binding); }
137137

138138
/// <summary>Gets the array of <see cref="Command"/>s bound to <paramref name="eventArgs"/> if it exists.</summary>
139-
/// <param name="eventArgs">The key to check.</param>
139+
/// <param name="eventArgs">The <typeparamref name="TEvent"/> to check.</param>
140140
/// <returns>
141141
/// The array of <see cref="Command"/>s if <paramref name="eventArgs"/> is bound. An empty <see cref="Command"/> array
142142
/// if not.

Terminal.Gui/View/View.Keyboard.cs

Lines changed: 16 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
#nullable enable
2-
using System.Diagnostics;
3-
using System.Reflection.Metadata;
4-
52
namespace Terminal.Gui;
63

74
public partial class View // Keyboard APIs
@@ -188,7 +185,7 @@ public virtual bool AddKeyBindingsForHotKey (Key prevHotKey, Key hotKey, object?
188185
{
189186
Commands = [Command.HotKey],
190187
Key = newKey,
191-
Data = context,
188+
Data = context
192189
};
193190

194191
// Add the base and Alt key
@@ -263,7 +260,8 @@ private void SetHotKeyFromTitle ()
263260
/// <para>
264261
/// If a more focused subview does not handle the key press, this method raises <see cref="OnKeyDown"/>/
265262
/// <see cref="KeyDown"/> to allow the
266-
/// 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.
263+
/// view to pre-process the key press. If <see cref="OnKeyDown"/>/<see cref="KeyDown"/> is not handled any commands
264+
/// bound to the key will be invoked.
267265
/// Then, only if no key bindings are
268266
/// handled, <see cref="OnKeyDownNotHandled"/>/<see cref="KeyDownNotHandled"/> will be raised allowing the view to
269267
/// process the key press.
@@ -305,6 +303,7 @@ public bool NewKeyDownEvent (Key key)
305303
}
306304

307305
bool? handled = false;
306+
308307
if (InvokeCommandsBoundToHotKey (key, ref handled))
309308
{
310309
return true;
@@ -588,17 +587,20 @@ private static bool InvokeCommandsBoundToKeyOnAdornment (Adornment adornment, Ke
588587

589588
// BUGBUG: This will miss any hotkeys in subviews of Adornments.
590589
/// <summary>
591-
/// Invokes any commands bound to <paramref name="key"/> on this view and subviews.
590+
/// Invokes any commands bound to <paramref name="hotKey"/> on this view and subviews.
592591
/// </summary>
593-
/// <param name="key"></param>
592+
/// <param name="hotKey"></param>
594593
/// <param name="handled"></param>
595594
/// <returns></returns>
596-
internal bool InvokeCommandsBoundToHotKey (Key key, ref bool? handled)
595+
internal bool InvokeCommandsBoundToHotKey (Key hotKey, ref bool? handled)
597596
{
598-
bool? weHandled = InvokeCommandsBoundToHotKey (key);
599-
if (weHandled is true)
597+
// Process this View
598+
if (HotKeyBindings.TryGet (hotKey, out KeyBinding binding))
600599
{
601-
return true;
600+
if (InvokeCommands (binding.Commands, binding) is true)
601+
{
602+
return true;
603+
}
602604
}
603605

604606
// Now, process any HotKey bindings in the subviews
@@ -609,7 +611,7 @@ internal bool InvokeCommandsBoundToHotKey (Key key, ref bool? handled)
609611
continue;
610612
}
611613

612-
bool recurse = subview.InvokeCommandsBoundToHotKey (key, ref handled);
614+
bool recurse = subview.InvokeCommandsBoundToHotKey (hotKey, ref handled);
613615

614616
if (recurse || (handled is { } && (bool)handled))
615617
{
@@ -620,38 +622,6 @@ internal bool InvokeCommandsBoundToHotKey (Key key, ref bool? handled)
620622
return false;
621623
}
622624

623-
// TODO: This is a "prototype" debug check. It may be too annoying vs. useful.
624-
// TODO: A better approach would be to have Application hold a list of bound Hotkeys, similar to
625-
// TODO: how Application holds a list of Application Scoped key bindings and then check that list.
626-
/// <summary>
627-
/// Returns true if Key is bound in this view hierarchy. For debugging
628-
/// </summary>
629-
/// <param name="key">The key to test.</param>
630-
/// <param name="boundView">Returns the view the key is bound to.</param>
631-
/// <returns></returns>
632-
public bool IsHotKeyBound (Key key, out View? boundView)
633-
{
634-
// recurse through the subviews to find the views that has the key bound
635-
boundView = null;
636-
637-
foreach (View subview in Subviews)
638-
{
639-
if (subview.HotKeyBindings.TryGet (key, out _))
640-
{
641-
boundView = subview;
642-
643-
return true;
644-
}
645-
646-
if (subview.IsHotKeyBound (key, out boundView))
647-
{
648-
return true;
649-
}
650-
}
651-
652-
return false;
653-
}
654-
655625
/// <summary>
656626
/// Invokes the Commands bound to <paramref name="key"/>.
657627
/// <para>See <see href="../docs/keyboard.md">for an overview of Terminal.Gui keyboard APIs.</see></para>
@@ -671,27 +641,9 @@ public bool IsHotKeyBound (Key key, out View? boundView)
671641
return null;
672642
}
673643

674-
#if DEBUG
675-
676-
//if (Application.KeyBindings.TryGet (key, out KeyBinding b))
677-
//{
678-
// Debug.WriteLine (
679-
// $"WARNING: InvokeKeyBindings ({key}) - An Application scope binding exists for this key. The registered view will not invoke Command.");
680-
//}
681-
682-
// TODO: This is a "prototype" debug check. It may be too annoying vs. useful.
683-
// Scour the bindings up our View hierarchy
684-
// to ensure that the key is not already bound to a different set of commands.
685-
if (SuperView?.IsHotKeyBound (key, out View? previouslyBoundView) ?? false)
686-
{
687-
Debug.WriteLine ($"WARNING: InvokeKeyBindings ({key}) - A subview or peer has bound this Key and will not see it: {previouslyBoundView}.");
688-
}
689-
690-
#endif
691-
return InvokeCommands<KeyBinding> (binding.Commands, binding);
644+
return InvokeCommands (binding.Commands, binding);
692645
}
693646

694-
695647
/// <summary>
696648
/// Invokes the Commands bound to <paramref name="hotKey"/>.
697649
/// <para>See <see href="../docs/keyboard.md">for an overview of Terminal.Gui keyboard APIs.</see></para>
@@ -711,24 +663,7 @@ public bool IsHotKeyBound (Key key, out View? boundView)
711663
return null;
712664
}
713665

714-
//#if DEBUG
715-
716-
// //if (Application.KeyBindings.TryGet (key, out KeyBinding b))
717-
// //{
718-
// // Debug.WriteLine (
719-
// // $"WARNING: InvokeKeyBindings ({key}) - An Application scope binding exists for this key. The registered view will not invoke Command.");
720-
// //}
721-
722-
// // TODO: This is a "prototype" debug check. It may be too annoying vs. useful.
723-
// // Scour the bindings up our View hierarchy
724-
// // to ensure that the key is not already bound to a different set of commands.
725-
// if (SuperView?.IsHotKeyBound (hotKey, out View? previouslyBoundView) ?? false)
726-
// {
727-
// Debug.WriteLine ($"WARNING: InvokeKeyBindings ({hotKey}) - A subview or peer has bound this Key and will not see it: {previouslyBoundView}.");
728-
// }
729-
730-
//#endif
731-
return InvokeCommands<KeyBinding> (binding.Commands, binding);
666+
return InvokeCommands (binding.Commands, binding);
732667
}
733668

734669
#endregion Key Bindings

Terminal.Gui/View/View.Mouse.cs

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#nullable enable
22
using System.ComponentModel;
3-
using System.Diagnostics;
43

54
namespace Terminal.Gui;
65

@@ -21,7 +20,6 @@ private void SetupMouse ()
2120
MouseBindings.Add (MouseFlags.Button1Clicked | MouseFlags.ButtonCtrl, Command.Select);
2221
}
2322

24-
2523
/// <summary>
2624
/// Invokes the Commands bound to the MouseFlags specified by <paramref name="mouseEventArgs"/>.
2725
/// <para>See <see href="../docs/mouse.md">for an overview of Terminal.Gui mouse APIs.</see></para>
@@ -43,7 +41,7 @@ private void SetupMouse ()
4341

4442
binding.MouseEventArgs = mouseEventArgs;
4543

46-
return InvokeCommands<MouseBinding> (binding.Commands, binding);
44+
return InvokeCommands (binding.Commands, binding);
4745
}
4846

4947
#region MouseEnterLeave
@@ -52,7 +50,8 @@ private void SetupMouse ()
5250
private ColorScheme? _savedNonHoverColorScheme;
5351

5452
/// <summary>
55-
/// INTERNAL Called by <see cref="Application.RaiseMouseEvent"/> when the mouse moves over the View's <see cref="Frame"/>.
53+
/// INTERNAL Called by <see cref="Application.RaiseMouseEvent"/> when the mouse moves over the View's
54+
/// <see cref="Frame"/>.
5655
/// <see cref="MouseLeave"/> will
5756
/// be raised when the mouse is no longer over the <see cref="Frame"/>. If another View occludes this View, the
5857
/// that View will also receive MouseEnter/Leave events.
@@ -167,7 +166,8 @@ private void SetupMouse ()
167166
public event EventHandler<CancelEventArgs>? MouseEnter;
168167

169168
/// <summary>
170-
/// INTERNAL Called by <see cref="Application.RaiseMouseEvent"/> when the mouse leaves <see cref="Frame"/>, or is occluded
169+
/// INTERNAL Called by <see cref="Application.RaiseMouseEvent"/> when the mouse leaves <see cref="Frame"/>, or is
170+
/// occluded
171171
/// by another non-SubView.
172172
/// </summary>
173173
/// <remarks>
@@ -245,7 +245,8 @@ protected virtual void OnMouseLeave () { }
245245
public bool WantMousePositionReports { get; set; }
246246

247247
/// <summary>
248-
/// Processes a new <see cref="MouseEvent"/>. This method is called by <see cref="Application.RaiseMouseEvent"/> when a mouse
248+
/// Processes a new <see cref="MouseEvent"/>. This method is called by <see cref="Application.RaiseMouseEvent"/> when a
249+
/// mouse
249250
/// event occurs.
250251
/// </summary>
251252
/// <remarks>
@@ -260,8 +261,10 @@ protected virtual void OnMouseLeave () { }
260261
/// See <see cref="SetPressedHighlight"/> for more information.
261262
/// </para>
262263
/// <para>
263-
/// If <see cref="WantContinuousButtonPressed"/> is <see langword="true"/>, the <see cref="RaiseMouseEvent"/>/<see cref="MouseEvent"/> event
264-
/// will be raised on any new mouse event where <see cref="Terminal.Gui.MouseEventArgs.Flags"/> indicates a button is pressed.
264+
/// If <see cref="WantContinuousButtonPressed"/> is <see langword="true"/>, the <see cref="RaiseMouseEvent"/>/
265+
/// <see cref="MouseEvent"/> event
266+
/// will be raised on any new mouse event where <see cref="Terminal.Gui.MouseEventArgs.Flags"/> indicates a button
267+
/// is pressed.
265268
/// </para>
266269
/// </remarks>
267270
/// <param name="mouseEvent"></param>
@@ -332,7 +335,7 @@ protected virtual void OnMouseLeave () { }
332335
/// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
333336
public bool RaiseMouseEvent (MouseEventArgs mouseEvent)
334337
{
335-
if (OnMouseEvent (mouseEvent) || mouseEvent.Handled == true)
338+
if (OnMouseEvent (mouseEvent) || mouseEvent.Handled)
336339
{
337340
return true;
338341
}
@@ -350,10 +353,7 @@ public bool RaiseMouseEvent (MouseEventArgs mouseEvent)
350353
/// </remarks>
351354
/// <param name="mouseEvent"></param>
352355
/// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
353-
protected virtual bool OnMouseEvent (MouseEventArgs mouseEvent)
354-
{
355-
return false;
356-
}
356+
protected virtual bool OnMouseEvent (MouseEventArgs mouseEvent) { return false; }
357357

358358
/// <summary>Raised when a mouse event occurs.</summary>
359359
/// <remarks>
@@ -368,7 +368,8 @@ protected virtual bool OnMouseEvent (MouseEventArgs mouseEvent)
368368
#region Mouse Pressed Events
369369

370370
/// <summary>
371-
/// INTERNAL For cases where the view is grabbed and the mouse is clicked, this method handles the released event (typically
371+
/// INTERNAL For cases where the view is grabbed and the mouse is clicked, this method handles the released event
372+
/// (typically
372373
/// when <see cref="WantContinuousButtonPressed"/> or <see cref="HighlightStyle"/> are set).
373374
/// </summary>
374375
/// <remarks>
@@ -394,7 +395,8 @@ internal bool WhenGrabbedHandleReleased (MouseEventArgs mouseEvent)
394395
}
395396

396397
/// <summary>
397-
/// INTERNAL For cases where the view is grabbed and the mouse is clicked, this method handles the released event (typically
398+
/// INTERNAL For cases where the view is grabbed and the mouse is clicked, this method handles the released event
399+
/// (typically
398400
/// when <see cref="WantContinuousButtonPressed"/> or <see cref="HighlightStyle"/> are set).
399401
/// </summary>
400402
/// <remarks>
@@ -463,7 +465,8 @@ private bool WhenGrabbedHandlePressed (MouseEventArgs mouseEvent)
463465
/// Called when the mouse is either clicked or double-clicked.
464466
/// </para>
465467
/// <para>
466-
/// If <see cref="WantContinuousButtonPressed"/> is <see langword="true"/>, will be invoked on every mouse event where
468+
/// If <see cref="WantContinuousButtonPressed"/> is <see langword="true"/>, will be invoked on every mouse event
469+
/// where
467470
/// the mouse button is pressed.
468471
/// </para>
469472
/// </remarks>
@@ -507,7 +510,8 @@ protected bool RaiseMouseClickEvent (MouseEventArgs args)
507510
/// Called when the mouse is either clicked or double-clicked.
508511
/// </para>
509512
/// <para>
510-
/// If <see cref="WantContinuousButtonPressed"/> is <see langword="true"/>, will be called on every mouse event where
513+
/// If <see cref="WantContinuousButtonPressed"/> is <see langword="true"/>, will be called on every mouse event
514+
/// where
511515
/// the mouse button is pressed.
512516
/// </para>
513517
/// </remarks>
@@ -521,14 +525,16 @@ protected bool RaiseMouseClickEvent (MouseEventArgs args)
521525
/// Raised when the mouse is either clicked or double-clicked.
522526
/// </para>
523527
/// <para>
524-
/// If <see cref="WantContinuousButtonPressed"/> is <see langword="true"/>, will be raised on every mouse event where
528+
/// If <see cref="WantContinuousButtonPressed"/> is <see langword="true"/>, will be raised on every mouse event
529+
/// where
525530
/// the mouse button is pressed.
526531
/// </para>
527532
/// </remarks>
528533
public event EventHandler<MouseEventArgs>? MouseClick;
529534

530535
/// <summary>
531-
/// INTERNAL For cases where the view is grabbed and the mouse is clicked, this method handles the click event (typically
536+
/// INTERNAL For cases where the view is grabbed and the mouse is clicked, this method handles the click event
537+
/// (typically
532538
/// when <see cref="WantContinuousButtonPressed"/> or <see cref="HighlightStyle"/> are set).
533539
/// </summary>
534540
/// <remarks>
@@ -562,10 +568,8 @@ internal bool WhenGrabbedHandleClicked (MouseEventArgs mouseEvent)
562568
return false;
563569
}
564570

565-
566571
#endregion Mouse Clicked Events
567572

568-
569573
#region Mouse Wheel Events
570574

571575
/// <summary>Raises the <see cref="OnMouseWheel"/>/<see cref="MouseWheel"/> event.</summary>
@@ -601,7 +605,8 @@ protected bool RaiseMouseWheelEvent (MouseEventArgs args)
601605
}
602606

603607
/// <summary>
604-
/// Called when a mouse wheel event occurs. Check <see cref="MouseEventArgs.Flags"/> to see which wheel was moved was clicked.
608+
/// Called when a mouse wheel event occurs. Check <see cref="MouseEventArgs.Flags"/> to see which wheel was moved was
609+
/// clicked.
605610
/// </summary>
606611
/// <remarks>
607612
/// </remarks>
@@ -828,8 +833,5 @@ internal bool SetPressedHighlight (HighlightStyle newHighlightStyle)
828833
return viewsUnderMouse;
829834
}
830835

831-
private void DisposeMouse ()
832-
{
833-
834-
}
836+
private void DisposeMouse () { }
835837
}

0 commit comments

Comments
 (0)