Skip to content

Commit

Permalink
Added Application.QuitKey property to allow change the quitting appli…
Browse files Browse the repository at this point in the history
…cation key. (gui-cs#1450)

* Added Application.QuitKey property to allow change the quitting application key.

* Fixes QuitKey unit test by reseting his value.

* Locks timeouts until is added.
  • Loading branch information
BDisp committed Oct 2, 2021
1 parent af22a7b commit 28d0d33
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Terminal.Gui/Core/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ public static bool AlwaysSetPosition {
/// Alternative key to navigate backwards through all views. Shift+Ctrl+Tab is always used.
/// </summary>
public static Key AlternateBackwardKey { get; set; } = Key.PageUp | Key.CtrlMask;
/// <summary>
/// Gets or sets the key to quit the application.
/// </summary>
public static Key QuitKey { get; set; } = Key.Q | Key.CtrlMask;

/// <summary>
/// The <see cref="MainLoop"/> driver for the application
Expand Down
2 changes: 1 addition & 1 deletion Terminal.Gui/Core/Toplevel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ public override bool ProcessKey (KeyEvent keyEvent)
return true;

switch (ShortcutHelper.GetModifiersKey (keyEvent)) {
case Key.Q | Key.CtrlMask:
case Key k when k == Application.QuitKey:
// FIXED: stop current execution of this container
if (Application.MdiTop != null) {
Application.MdiTop.RequestStop ();
Expand Down
31 changes: 31 additions & 0 deletions UnitTests/ApplicationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1142,5 +1142,36 @@ public void Internal_Tests ()
Assert.False (Application.ShowChild (Application.Top));
Application.End (Application.Top);
}

[Fact]
[AutoInitShutdown]
public void QuitKey_Getter_Setter ()
{
var top = Application.Top;
var isQuiting = false;

top.Closing += (e) => {
isQuiting = true;
e.Cancel = true;
};

Application.Begin (top);
top.Running = true;

Assert.Equal (Key.Q | Key.CtrlMask, Application.QuitKey);
Application.Driver.SendKeys ('q', ConsoleKey.Q, false, false, true);
Assert.True (isQuiting);

isQuiting = false;
Application.QuitKey = Key.C | Key.CtrlMask;

Application.Driver.SendKeys ('q', ConsoleKey.Q, false, false, true);
Assert.False (isQuiting);
Application.Driver.SendKeys ('c', ConsoleKey.C, false, false, true);
Assert.True (isQuiting);

// Reset the QuitKey to avoid throws errors on another tests
Application.QuitKey = Key.Q | Key.CtrlMask;
}
}
}

0 comments on commit 28d0d33

Please sign in to comment.