Selaa lähdekoodia

Added Application.QuitKey property to allow change the quitting application key. (#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.
BDisp 3 vuotta sitten
vanhempi
commit
23d4fa9016

+ 4 - 0
Terminal.Gui/Core/Application.cs

@@ -148,6 +148,10 @@ namespace Terminal.Gui {
 		/// 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

+ 1 - 1
Terminal.Gui/Core/Toplevel.cs

@@ -294,7 +294,7 @@ namespace Terminal.Gui {
 				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 ();

+ 31 - 0
UnitTests/ApplicationTests.cs

@@ -1142,5 +1142,36 @@ namespace Terminal.Gui.Core {
 			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;
+		}
 	}
 }