Переглянути джерело

Fixes #1159. Dialog must have a default button if none is provided. (#1160)

* Fixes #1159. Dialog must have a default button if none is provided.

* Reverting this because someone may don't want buttons at all. Thanks @tig

* Allow to specify which default button for MessageBox.

* Added a check if default button is greater than the buttons length and changing the MessageBox scenario.
BDisp 4 роки тому
батько
коміт
1a9720f471

+ 3 - 3
Terminal.Gui/Windows/Dialog.cs

@@ -34,7 +34,7 @@ namespace Terminal.Gui {
 		/// <remarks>
 		/// if <c>width</c> and <c>height</c> are both 0, the Dialog will be vertically and horizontally centered in the
 		/// container and the size will be 85% of the container. 
-		/// After initialzation use <c>X</c>, <c>Y</c>, <c>Width</c>, and <c>Height</c> to override this with a location or size.
+		/// After initialization use <c>X</c>, <c>Y</c>, <c>Width</c>, and <c>Height</c> to override this with a location or size.
 		/// </remarks>
 		/// <remarks>
 		/// Use the constructor that does not take a <c>width</c> and <c>height</c> instead.
@@ -73,7 +73,7 @@ namespace Terminal.Gui {
 		/// <remarks>
 		/// <para>
 		/// Te Dialog will be vertically and horizontally centered in the container and the size will be 85% of the container. 
-		/// After initialzation use <c>X</c>, <c>Y</c>, <c>Width</c>, and <c>Height</c> to override this with a location or size.
+		/// After initialization use <c>X</c>, <c>Y</c>, <c>Width</c>, and <c>Height</c> to override this with a location or size.
 		/// </para>
 		/// <para>
 		/// Use <see cref="AddButton(Button)"/> to add buttons to the dialog.
@@ -89,7 +89,7 @@ namespace Terminal.Gui {
 		/// <param name="buttons">Optional buttons to lay out at the bottom of the dialog.</param>
 		/// <remarks>
 		/// Te Dialog will be vertically and horizontally centered in the container and the size will be 85% of the container. 
-		/// After initialzation use <c>X</c>, <c>Y</c>, <c>Width</c>, and <c>Height</c> to override this with a location or size.
+		/// After initialization use <c>X</c>, <c>Y</c>, <c>Width</c>, and <c>Height</c> to override this with a location or size.
 		/// </remarks>
 		public Dialog (ustring title, params Button [] buttons) : this (title: title, width: 0, height: 0, buttons: buttons) { }
 

+ 85 - 8
Terminal.Gui/Windows/MessageBox.cs

@@ -39,7 +39,7 @@ namespace Terminal.Gui {
 		/// </remarks>
 		public static int Query (int width, int height, ustring title, ustring message, params ustring [] buttons)
 		{
-			return QueryFull (false, width, height, title, message, buttons);
+			return QueryFull (false, width, height, title, message, 0, buttons);
 		}
 
 		/// <summary>
@@ -55,7 +55,7 @@ namespace Terminal.Gui {
 		/// </remarks>
 		public static int Query (ustring title, ustring message, params ustring [] buttons)
 		{
-			return QueryFull (false, 0, 0, title, message, buttons);
+			return QueryFull (false, 0, 0, title, message, 0, buttons);
 		}
 
 		/// <summary>
@@ -72,7 +72,7 @@ namespace Terminal.Gui {
 		/// </remarks>
 		public static int ErrorQuery (int width, int height, ustring title, ustring message, params ustring [] buttons)
 		{
-			return QueryFull (true, width, height, title, message, buttons);
+			return QueryFull (true, width, height, title, message, 0, buttons);
 		}
 
 		/// <summary>
@@ -88,10 +88,80 @@ namespace Terminal.Gui {
 		/// </remarks>
 		public static int ErrorQuery (ustring title, ustring message, params ustring [] buttons)
 		{
-			return QueryFull (true, 0, 0, title, message, buttons);
+			return QueryFull (true, 0, 0, title, message, 0, buttons);
 		}
 
-		static int QueryFull (bool useErrorColors, int width, int height, ustring title, ustring message, params ustring [] buttons)
+		/// <summary>
+		/// Presents a normal <see cref="MessageBox"/> with the specified title and message and a list of buttons to show to the user.
+		/// </summary>
+		/// <returns>The index of the selected button, or -1 if the user pressed ESC to close the dialog.</returns>
+		/// <param name="width">Width for the window.</param>
+		/// <param name="height">Height for the window.</param>
+		/// <param name="title">Title for the query.</param>
+		/// <param name="message">Message to display, might contain multiple lines.</param>
+		/// <param name="defaultButton">Index of the default button.</param>
+		/// <param name="buttons">Array of buttons to add.</param>
+		/// <remarks>
+		/// Use <see cref="Query(ustring, ustring, ustring[])"/> instead; it automatically sizes the MessageBox based on the contents.
+		/// </remarks>
+		public static int Query (int width, int height, ustring title, ustring message, int defaultButton = 0, params ustring [] buttons)
+		{
+			return QueryFull (false, width, height, title, message, defaultButton, buttons);
+		}
+
+		/// <summary>
+		/// Presents an error <see cref="MessageBox"/> with the specified title and message and a list of buttons to show to the user.
+		/// </summary>
+		/// <returns>The index of the selected button, or -1 if the user pressed ESC to close the dialog.</returns>
+		/// <param name="title">Title for the query.</param>
+		/// <param name="message">Message to display, might contain multiple lines.</param>
+		/// <param name="defaultButton">Index of the default button.</param>
+		/// <param name="buttons">Array of buttons to add.</param>
+		/// <remarks>
+		/// The message box will be vertically and horizontally centered in the container and the size will be automatically determined
+		/// from the size of the message and buttons.
+		/// </remarks>
+		public static int Query (ustring title, ustring message, int defaultButton = 0, params ustring [] buttons)
+		{
+			return QueryFull (false, 0, 0, title, message, defaultButton, buttons);
+		}
+
+		/// <summary>
+		/// Presents an error <see cref="MessageBox"/> with the specified title and message and a list of buttons to show to the user.
+		/// </summary>
+		/// <returns>The index of the selected button, or -1 if the user pressed ESC to close the dialog.</returns>
+		/// <param name="width">Width for the window.</param>
+		/// <param name="height">Height for the window.</param>
+		/// <param name="title">Title for the query.</param>
+		/// <param name="message">Message to display, might contain multiple lines.</param>
+		/// <param name="defaultButton">Index of the default button.</param>
+		/// <param name="buttons">Array of buttons to add.</param>
+		/// <remarks>
+		/// Use <see cref="ErrorQuery(ustring, ustring, ustring[])"/> instead; it automatically sizes the MessageBox based on the contents.
+		/// </remarks>
+		public static int ErrorQuery (int width, int height, ustring title, ustring message, int defaultButton = 0, params ustring [] buttons)
+		{
+			return QueryFull (true, width, height, title, message, defaultButton, buttons);
+		}
+
+		/// <summary>
+		/// Presents an error <see cref="MessageBox"/> with the specified title and message and a list of buttons to show to the user.
+		/// </summary>
+		/// <returns>The index of the selected button, or -1 if the user pressed ESC to close the dialog.</returns>
+		/// <param name="title">Title for the query.</param>
+		/// <param name="message">Message to display, might contain multiple lines.</param>
+		/// <param name="defaultButton">Index of the default button.</param>
+		/// <param name="buttons">Array of buttons to add.</param>
+		/// <remarks>
+		/// The message box will be vertically and horizontally centered in the container and the size will be automatically determined
+		/// from the size of the title, message. and buttons.
+		/// </remarks>
+		public static int ErrorQuery (ustring title, ustring message, int defaultButton = 0, params ustring [] buttons)
+		{
+			return QueryFull (true, 0, 0, title, message, defaultButton, buttons);
+		}
+
+		static int QueryFull (bool useErrorColors, int width, int height, ustring title, ustring message, int defaultButton = 0, params ustring [] buttons)
 		{
 			const int defaultWidth = 50;
 			int textWidth = TextFormatter.MaxWidth (message, width == 0 ? defaultWidth : width);
@@ -101,9 +171,12 @@ namespace Terminal.Gui {
 			// Create button array for Dialog
 			int count = 0;
 			List<Button> buttonList = new List<Button> ();
+			if (buttons != null && defaultButton > buttons.Length - 1) {
+				defaultButton = buttons.Length - 1;
+			}
 			foreach (var s in buttons) {
 				var b = new Button (s);
-				if (count == 0) {
+				if (count == defaultButton) {
 					b.IsDefault = true;
 				}
 				buttonList.Add (b);
@@ -142,13 +215,17 @@ namespace Terminal.Gui {
 			int clicked = -1;
 			for (int n = 0; n < buttonList.Count; n++) {
 				int buttonId = n;
-				buttonList [n].Clicked += () => {
+				var b = buttonList [n];
+				b.Clicked += () => {
 					clicked = buttonId;
 					Application.RequestStop ();
 				};
+				if (b.IsDefault) {
+					b.SetFocus ();
+				}
 			}
 
-			// Rin the modal; do not shutdown the mainloop driver when done
+			// Run the modal; do not shutdown the mainloop driver when done
 			Application.Run (d);
 			return clicked;
 		}

+ 21 - 3
UICatalog/Scenarios/MessageBoxes.cs

@@ -110,6 +110,22 @@ namespace UICatalog {
 			};
 			frame.Add (numButtonsEdit);
 
+			label = new Label ("Default Button:") {
+				X = 0,
+				Y = Pos.Bottom (label),
+				Width = Dim.Width (label),
+				Height = 1,
+				TextAlignment = Terminal.Gui.TextAlignment.Right,
+			};
+			frame.Add (label);
+			var defaultButtonEdit = new TextField ("0") {
+				X = Pos.Right (label) + 1,
+				Y = Pos.Top (label),
+				Width = 5,
+				Height = 1
+			};
+			frame.Add (defaultButtonEdit);
+
 			label = new Label ("Style:") {
 				X = 0,
 				Y = Pos.Bottom (label),
@@ -127,7 +143,7 @@ namespace UICatalog {
 			void Top_Loaded ()
 			{
 				frame.Height = Dim.Height (widthEdit) + Dim.Height (heightEdit) + Dim.Height (titleEdit) + Dim.Height (messageEdit)
-					+ Dim.Height (numButtonsEdit) + Dim.Height (styleRadioGroup) + 2;
+					+ Dim.Height (numButtonsEdit) + Dim.Height (defaultButtonEdit) + Dim.Height (styleRadioGroup) + 2;
 				Top.Loaded -= Top_Loaded;
 			}
 			Top.Loaded += Top_Loaded;
@@ -145,6 +161,7 @@ namespace UICatalog {
 				Width = 25,
 				Height = 1,
 				ColorScheme = Colors.Error,
+				TextAlignment = Terminal.Gui.TextAlignment.Centered
 			};
 
 			//var btnText = new [] { "_Zero", "_One", "T_wo", "_Three", "_Four", "Fi_ve", "Si_x", "_Seven", "_Eight", "_Nine" };
@@ -159,6 +176,7 @@ namespace UICatalog {
 					int width = int.Parse (widthEdit.Text.ToString ());
 					int height = int.Parse (heightEdit.Text.ToString ());
 					int numButtons = int.Parse (numButtonsEdit.Text.ToString ());
+					int defaultButton = int.Parse (defaultButtonEdit.Text.ToString ());
 
 					var btns = new List<ustring> ();
 					for (int i = 0; i < numButtons; i++) {
@@ -166,9 +184,9 @@ namespace UICatalog {
 						btns.Add (NumberToWords.Convert (i));
 					}
 					if (styleRadioGroup.SelectedItem == 0) {
-						buttonPressedLabel.Text = $"{MessageBox.Query (width, height, titleEdit.Text.ToString (), messageEdit.Text.ToString (), btns.ToArray ())}";
+						buttonPressedLabel.Text = $"{MessageBox.Query (width, height, titleEdit.Text.ToString (), messageEdit.Text.ToString (), defaultButton, btns.ToArray ())}";
 					} else {
-						buttonPressedLabel.Text = $"{MessageBox.ErrorQuery (width, height, titleEdit.Text.ToString (), messageEdit.Text.ToString (), btns.ToArray ())}";
+						buttonPressedLabel.Text = $"{MessageBox.ErrorQuery (width, height, titleEdit.Text.ToString (), messageEdit.Text.ToString (), defaultButton, btns.ToArray ())}";
 					}
 				} catch (FormatException) {
 					buttonPressedLabel.Text = "Invalid Options";