Skip to content

Commit

Permalink
Merged v2_develop.
Browse files Browse the repository at this point in the history
Code cleanup
  • Loading branch information
tig committed Dec 7, 2024
1 parent 3e6e758 commit 5a0b350
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 55 deletions.
2 changes: 1 addition & 1 deletion Terminal.Gui/ConsoleDrivers/AnsiEscapeSequenceRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class AnsiEscapeSequenceRequest : AnsiEscapeSequence


/// <summary>
/// Sends the <see cref="Request"/> to the raw output stream of the current <see cref="ConsoleDriver"/>.
/// Sends the <see cref="AnsiEscapeSequence.Request"/> to the raw output stream of the current <see cref="ConsoleDriver"/>.
/// Only call this method from the main UI thread. You should use <see cref="AnsiRequestScheduler"/> if
/// sending many requests.
/// </summary>
Expand Down
4 changes: 3 additions & 1 deletion Terminal.Gui/Input/ICommandContext.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#nullable enable
namespace Terminal.Gui;

#pragma warning disable CS1574 // XML comment has cref attribute that could not be resolved
/// <summary>
/// Describes the context in which a <see cref="Command"/> is being invoked. <see cref="CommandContext{TBindingType}"/> inherits from this interface.
/// When a <see cref="Command"/> is invoked,
/// a context object is passed to Command handlers as an <see cref="ICommandContext"/> reference.
/// </summary>
/// <seealso cref="View.AddCommand(Command, CommandImplementation)"/>.
/// <seealso cref="View.AddCommand(Command, View.CommandImplementation)"/>.
#pragma warning restore CS1574 // XML comment has cref attribute that could not be resolved
public interface ICommandContext
{
/// <summary>
Expand Down
1 change: 0 additions & 1 deletion Terminal.Gui/Input/Keyboard/KeyBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public KeyBinding (Command [] commands, object? context = null)

/// <summary>Initializes a new instance.</summary>
/// <param name="commands">The commands this key binding will invoke.</param>
/// <param name="scope">The scope of the <see cref="Commands"/>.</param>
/// <param name="boundView">The view the key binding is bound to.</param>
/// <param name="data">Arbitrary data that can be associated with this key binding.</param>
public KeyBinding (Command [] commands, View? boundView, object? data = null)
Expand Down
10 changes: 3 additions & 7 deletions Terminal.Gui/Input/Keyboard/KeyBindings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,13 @@ public void Add (Key key, KeyBinding binding)
Bindings.Add (new (key), binding);
}

#pragma warning disable CS1574 // XML comment has cref attribute that could not be resolved
/// <summary>
/// <para>
/// Adds a new key combination that will trigger the commands in <paramref name="commands"/> (if supported by the
/// View - see <see cref="View.GetSupportedCommands"/>).
/// </para>
/// <para>
/// This is a helper function for <see cref="Add(Key,KeyBinding,View?)"/>. If used for a View (
/// <see cref="BoundView"/> is set), the scope will be set to <see cref="KeyBindingScope.Focused"/>.
/// Otherwise, it will be set to <see cref="KeyBindingScope.Application"/>.
/// </para>
/// <para>
/// If the key is already bound to a different array of <see cref="Command"/>s it will be rebound
/// <paramref name="commands"/>.
/// </para>
Expand All @@ -74,6 +70,7 @@ public void Add (Key key, KeyBinding binding)
/// consumed if any took effect.
/// </param>
/// <exception cref="ArgumentException">If <paramref name="commands"/> is empty.</exception>
#pragma warning restore CS1574 // XML comment has cref attribute that could not be resolved
public void Add (Key key, params Command [] commands)
{
Add (key, new KeyBinding (commands));
Expand Down Expand Up @@ -168,8 +165,7 @@ public IEnumerable<Key> GetKeysFromCommands (params Command [] commands)

/// <summary>Removes a <see cref="KeyBinding"/> from the collection.</summary>
/// <param name="key"></param>
/// <param name="boundViewForAppScope">Optional View for <see cref="KeyBindingScope.Application"/> bindings.</param>
public void Remove (Key key, View? boundViewForAppScope = null)
public void Remove (Key key)
{
if (!TryGet (key, out KeyBinding _))
{
Expand Down
30 changes: 18 additions & 12 deletions Terminal.Gui/View/View.Keyboard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -299,13 +299,13 @@ public bool NewKeyDownEvent (Key key)
// During (this is what can be cancelled)

// TODO: NewKeyDownEvent returns bool. It should be bool? so state of InvokeCommands can be reflected up stack
if (InvokeCommandsBoundToKey (key) is true || key.Handled)
if (InvokeCommands (key) is true || key.Handled)
{
return true;
}

bool? handled = false;
if (InvokeCommandsBoundToHotKeyOnSubviews (key, ref handled))
if (InvokeCommandsBoundToHotKey (key, ref handled))
{
return true;
}
Expand Down Expand Up @@ -516,7 +516,7 @@ bool RaiseKeyUp (Key k)
/// <summary>Gets the bindings for this view that will be invoked only if this view has focus.</summary>
public KeyBindings KeyBindings { get; internal set; } = null!;

/// <summary>Gets the bindings for this view that will be invoked regardless of whehter this view has focus or not.</summary>
/// <summary>Gets the bindings for this view that will be invoked regardless of whether this view has focus or not.</summary>
public KeyBindings HotKeyBindings { get; internal set; } = null!;

/// <summary>
Expand All @@ -531,15 +531,15 @@ bool RaiseKeyUp (Key k)
/// <see langword="true"/> if at least one command was invoked and handled (or
/// cancelled); input processing should stop.
/// </returns>
internal bool? InvokeCommandsBoundToKey (Key key)
internal bool? InvokeCommands (Key key)
{
// * If no key binding was found, `InvokeKeyBindings` returns `null`.
// Continue passing the event (return `false` from `OnInvokeKeyBindings`).
// * If key bindings were found, but none handled the key (all `Command`s returned `false`),
// `InvokeKeyBindings` returns `false`. Continue passing the event (return `false` from `OnInvokeKeyBindings`)..
// * If key bindings were found, and any handled the key (at least one `Command` returned `true`),
// `InvokeKeyBindings` returns `true`. Continue passing the event (return `false` from `OnInvokeKeyBindings`).
bool? handled = InvokeCommandsBoundToFocusedKey (key);
bool? handled = DoInvokeCommands (key);

if (handled is true)
{
Expand Down Expand Up @@ -568,7 +568,7 @@ bool RaiseKeyUp (Key k)

private static bool InvokeCommandsBoundToKeyOnAdornment (Adornment adornment, Key key, ref bool? handled)
{
bool? adornmentHandled = adornment.InvokeCommandsBoundToKey (key);
bool? adornmentHandled = adornment.InvokeCommands (key);

if (adornmentHandled is true)
{
Expand All @@ -582,7 +582,7 @@ private static bool InvokeCommandsBoundToKeyOnAdornment (Adornment adornment, Ke

foreach (View subview in adornment.Subviews)
{
bool? subViewHandled = subview.InvokeCommandsBoundToKey (key);
bool? subViewHandled = subview.InvokeCommands (key);

if (subViewHandled is { })
{
Expand All @@ -598,23 +598,30 @@ private static bool InvokeCommandsBoundToKeyOnAdornment (Adornment adornment, Ke
return false;
}

internal bool InvokeCommandsBoundToHotKeyOnSubviews (Key key, ref bool? handled, bool invoke = true)
// BUGBUG: This will miss any hotkeys in subviews of Adornments.
/// <summary>
/// Invokes any commands bound to <paramref name="key"/> on this view and subviews.
/// </summary>
/// <param name="key"></param>
/// <param name="handled"></param>
/// <returns></returns>
internal bool InvokeCommandsBoundToHotKey (Key key, ref bool? handled)
{
bool? weHandled = InvokeCommandsBoundToHotKey (key);
if (weHandled is true)
{
return true;
}

// Now, process any key bindings in the subviews that are tagged to KeyBindingScope.HotKey.
// Now, process any HotKey bindings in the subviews
foreach (View subview in Subviews)
{
if (subview == Focused)
{
continue;
}

bool recurse = subview.InvokeCommandsBoundToHotKeyOnSubviews (key, ref handled, invoke);
bool recurse = subview.InvokeCommandsBoundToHotKey (key, ref handled);

if (recurse || (handled is { } && (bool)handled))
{
Expand Down Expand Up @@ -669,7 +676,7 @@ public bool IsHotKeyBound (Key key, out View? boundView)
/// <see langword="true"/> if at least one command was invoked and handled (or cancelled); input processing should
/// stop.
/// </returns>
protected bool? InvokeCommandsBoundToFocusedKey (Key key)
protected bool? DoInvokeCommands (Key key)
{
if (!KeyBindings.TryGet (key, out KeyBinding binding))
{
Expand Down Expand Up @@ -702,7 +709,6 @@ public bool IsHotKeyBound (Key key, out View? boundView)
/// <para>See <see href="../docs/keyboard.md">for an overview of Terminal.Gui keyboard APIs.</see></para>
/// </summary>
/// <param name="key">The key event passed.</param>
/// <param name="scope">The scope.</param>
/// <returns>
/// <see langword="null"/> if no command was invoked; input processing should continue.
/// <see langword="false"/> if at least one command was invoked and was not handled (or cancelled); input processing
Expand Down
3 changes: 3 additions & 0 deletions Terminal.Gui/Views/CharMap/CharMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ public class CharMap : View, IDesignable

private ContextMenu _contextMenu = new ();


/// <summary>
/// Initializes a new instance.
/// </summary>
[RequiresUnreferencedCode ("AOT")]
[RequiresDynamicCode ("AOT")]
public CharMap ()
{
base.ColorScheme = Colors.ColorSchemes ["Dialog"];
Expand Down
2 changes: 1 addition & 1 deletion Terminal.Gui/Views/Menu/Menu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ protected override bool OnKeyDownNotHandled (Key keyEvent)
{
// We didn't handle the key, pass it on to host
bool? handled = null;
return _host.InvokeCommandsBoundToHotKeyOnSubviews (keyEvent, ref handled, true) == true;
return _host.InvokeCommandsBoundToHotKey (keyEvent, ref handled) == true;
}

protected override bool OnMouseEvent (MouseEventArgs me)
Expand Down
2 changes: 1 addition & 1 deletion Terminal.Gui/Views/Shortcut.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace Terminal.Gui;
/// - Pressing the HotKey specified by <see cref="CommandView"/>.
/// </para>
/// <para>
/// If <see cref="BindKeyToApplication"/> is <see cref="KeyBindingScope.Application"/>, <see cref="Key"/> will invoke
/// If <see cref="BindKeyToApplication"/> is <see langword="true"/>, <see cref="Key"/> will invoke
/// <see cref="Command.Accept"/>
/// regardless of what View has focus, enabling an application-wide keyboard shortcut.
/// </para>
Expand Down
2 changes: 0 additions & 2 deletions UnitTests/ConsoleDrivers/AnsiResponseParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -438,8 +438,6 @@ public void ShouldSwallowUnknownResponses_WhenDelegateSaysSo ()
[Fact]
public void UnknownResponses_ParameterShouldMatch ()
{
int i = 0;

// Track unknown responses passed to the UnexpectedResponseHandler
var unknownResponses = new List<string> ();

Expand Down
4 changes: 2 additions & 2 deletions UnitTests/View/Keyboard/KeyboardEventTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -273,12 +273,12 @@ public void NewKeyUpEvent_KeyUp_Handled_True_Stops_Processing ()
[InlineData (null, null)]
[InlineData (true, true)]
[InlineData (false, false)]
public void InvokeCommandsBoundToKey_Returns_Nullable_Properly (bool? toReturn, bool? expected)
public void InvokeCommands_Returns_Nullable_Properly (bool? toReturn, bool? expected)
{
var view = new KeyBindingsTestView ();
view.CommandReturns = toReturn;

bool? result = view.InvokeCommandsBoundToKey (Key.A);
bool? result = view.InvokeCommands (Key.A);
Assert.Equal (expected, result);
}

Expand Down
48 changes: 21 additions & 27 deletions UnitTests/View/ViewCommandTests.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
using System.ComponentModel;
using System.Text;
using Xunit.Abstractions;

namespace Terminal.Gui.ViewTests;

public class ViewCommandTests (ITestOutputHelper output)
public class ViewCommandTests
{
#region OnAccept/Accept tests

[Fact]
public void Accept_Command_Raises_NoFocus ()
{
Expand Down Expand Up @@ -77,8 +74,8 @@ public void Accept_Command_Invokes_Accept_Event ()
[Fact]
public void Accept_Command_Bubbles_Up_To_SuperView ()
{
var view = new ViewEventTester () { Id = "view" };
var subview = new ViewEventTester () { Id = "subview" };
var view = new ViewEventTester { Id = "view" };
var subview = new ViewEventTester { Id = "subview" };
view.Add (subview);

subview.InvokeCommand (Command.Accept);
Expand All @@ -97,7 +94,7 @@ public void Accept_Command_Bubbles_Up_To_SuperView ()
Assert.Equal (1, view.OnAcceptedCount);

// Add a super view to test deeper hierarchy
var superView = new ViewEventTester () { Id = "superView" };
var superView = new ViewEventTester { Id = "superView" };
superView.Add (view);

subview.InvokeCommand (Command.Accept);
Expand Down Expand Up @@ -135,7 +132,7 @@ public void MouseClick_Does_Not_Invoke_Accept_Command ()
[CombinatorialData]
public void Select_Command_Raises_SetsFocus (bool canFocus)
{
var view = new ViewEventTester ()
var view = new ViewEventTester
{
CanFocus = canFocus
};
Expand Down Expand Up @@ -236,30 +233,29 @@ public ViewEventTester ()
CanFocus = true;

Accepting += (s, a) =>
{
a.Cancel = HandleAccepted;
AcceptedCount++;
};
{
a.Cancel = HandleAccepted;
AcceptedCount++;
};

HandlingHotKey += (s, a) =>
{
a.Cancel = HandleHandlingHotKey;
HandlingHotKeyCount++;
};

{
a.Cancel = HandleHandlingHotKey;
HandlingHotKeyCount++;
};

Selecting += (s, a) =>
{
a.Cancel = HandleSelecting;
SelectingCount++;
};
{
a.Cancel = HandleSelecting;
SelectingCount++;
};
}

public int OnAcceptedCount { get; set; }
public int AcceptedCount { get; set; }
public bool HandleOnAccepted { get; set; }

/// <inheritdoc />
/// <inheritdoc/>
protected override bool OnAccepting (CommandEventArgs args)
{
OnAcceptedCount++;
Expand All @@ -273,7 +269,7 @@ protected override bool OnAccepting (CommandEventArgs args)
public int HandlingHotKeyCount { get; set; }
public bool HandleOnHandlingHotKey { get; set; }

/// <inheritdoc />
/// <inheritdoc/>
protected override bool OnHandlingHotKey (CommandEventArgs args)
{
OnHandlingHotKeyCount++;
Expand All @@ -283,12 +279,11 @@ protected override bool OnHandlingHotKey (CommandEventArgs args)

public bool HandleHandlingHotKey { get; set; }


public int OnSelectingCount { get; set; }
public int SelectingCount { get; set; }
public bool HandleOnSelecting { get; set; }

/// <inheritdoc />
/// <inheritdoc/>
protected override bool OnSelecting (CommandEventArgs args)
{
OnSelectingCount++;
Expand All @@ -297,6 +292,5 @@ protected override bool OnSelecting (CommandEventArgs args)
}

public bool HandleSelecting { get; set; }

}
}

0 comments on commit 5a0b350

Please sign in to comment.