|
|
@@ -1,59 +1,109 @@
|
|
|
-#nullable disable
|
|
|
-
|
|
|
namespace Terminal.Gui.Views;
|
|
|
|
|
|
/// <summary>
|
|
|
-/// MessageBox displays a modal message to the user, with a title, a message and a series of options that the user
|
|
|
-/// can choose from.
|
|
|
+/// Displays a modal message box with a title, message, and buttons. Returns the index of the selected button,
|
|
|
+/// or <see langword="null"/> if the user cancels with <see cref="Application.QuitKey"/>.
|
|
|
/// </summary>
|
|
|
-/// <para>
|
|
|
-/// The difference between the <see cref="Query(string, string, string[])"/> and
|
|
|
-/// <see cref="ErrorQuery(string, string, string[])"/> method is the default set of colors used for the message box.
|
|
|
-/// </para>
|
|
|
-/// <para>
|
|
|
-/// The following example pops up a <see cref="MessageBox"/> with the specified title and text, plus two
|
|
|
-/// <see cref="Button"/>s. The value -1 is returned when the user cancels the <see cref="MessageBox"/> by pressing the
|
|
|
-/// ESC key.
|
|
|
-/// </para>
|
|
|
-/// <example>
|
|
|
-/// <code lang="c#">
|
|
|
-/// var n = MessageBox.Query ("Quit Demo", "Are you sure you want to quit this demo?", "Yes", "No");
|
|
|
-/// if (n == 0)
|
|
|
-/// quit = true;
|
|
|
-/// else
|
|
|
-/// quit = false;
|
|
|
-/// </code>
|
|
|
-/// </example>
|
|
|
+/// <remarks>
|
|
|
+/// <para>
|
|
|
+/// MessageBox provides static methods for displaying modal dialogs with customizable buttons and messages.
|
|
|
+/// All methods return <see langword="int?"/> where the value is the 0-based index of the button pressed,
|
|
|
+/// or <see langword="null"/> if the user pressed <see cref="Application.QuitKey"/> (typically Esc).
|
|
|
+/// </para>
|
|
|
+/// <para>
|
|
|
+/// <see cref="Query(IApplication?, string, string, string[])"/> uses the default Dialog color scheme.
|
|
|
+/// <see cref="ErrorQuery(IApplication?, string, string, string[])"/> uses the Error color scheme.
|
|
|
+/// </para>
|
|
|
+/// <para>
|
|
|
+/// <b>Important:</b> All MessageBox methods require an <see cref="IApplication"/> instance to be passed.
|
|
|
+/// This enables proper modal dialog management and respects the application's lifecycle. Pass your
|
|
|
+/// application instance (from <see cref="Application.Create()"/>) or use the legacy
|
|
|
+/// <see cref="ApplicationImpl.Instance"/> if using the static Application pattern.
|
|
|
+/// </para>
|
|
|
+/// <para>
|
|
|
+/// Example using instance-based pattern:
|
|
|
+/// <code>
|
|
|
+/// IApplication app = Application.Create();
|
|
|
+/// app.Init();
|
|
|
+///
|
|
|
+/// int? result = MessageBox.Query(app, "Quit Demo", "Are you sure you want to quit?", "Yes", "No");
|
|
|
+/// if (result == 0) // User clicked "Yes"
|
|
|
+/// app.RequestStop();
|
|
|
+/// else if (result == null) // User pressed Esc
|
|
|
+/// // Handle cancellation
|
|
|
+///
|
|
|
+/// app.Shutdown();
|
|
|
+/// </code>
|
|
|
+/// </para>
|
|
|
+/// <para>
|
|
|
+/// Example using legacy static pattern:
|
|
|
+/// <code>
|
|
|
+/// Application.Init();
|
|
|
+///
|
|
|
+/// int? result = MessageBox.Query(ApplicationImpl.Instance, "Quit Demo", "Are you sure?", "Yes", "No");
|
|
|
+/// if (result == 0) // User clicked "Yes"
|
|
|
+/// Application.RequestStop();
|
|
|
+///
|
|
|
+/// Application.Shutdown();
|
|
|
+/// </code>
|
|
|
+/// </para>
|
|
|
+/// <para>
|
|
|
+/// The <see cref="Clicked"/> property provides a global variable alternative for web-based consoles
|
|
|
+/// without SynchronizationContext. However, using the return value is preferred as it's more thread-safe
|
|
|
+/// and follows modern async patterns.
|
|
|
+/// </para>
|
|
|
+/// </remarks>
|
|
|
public static class MessageBox
|
|
|
{
|
|
|
+ private static LineStyle _defaultBorderStyle = LineStyle.Heavy; // Resources/config.json overrides
|
|
|
+ private static Alignment _defaultButtonAlignment = Alignment.Center; // Resources/config.json overrides
|
|
|
+ private static int _defaultMinimumWidth = 0; // Resources/config.json overrides
|
|
|
+ private static int _defaultMinimumHeight = 0; // Resources/config.json overrides
|
|
|
+
|
|
|
/// <summary>
|
|
|
/// Defines the default border styling for <see cref="MessageBox"/>. Can be configured via
|
|
|
/// <see cref="ConfigurationManager"/>.
|
|
|
/// </summary>
|
|
|
[ConfigurationProperty (Scope = typeof (ThemeScope))]
|
|
|
- public static LineStyle DefaultBorderStyle { get; set; } = LineStyle.Heavy;
|
|
|
+ public static LineStyle DefaultBorderStyle
|
|
|
+ {
|
|
|
+ get => _defaultBorderStyle;
|
|
|
+ set => _defaultBorderStyle = value;
|
|
|
+ }
|
|
|
|
|
|
/// <summary>The default <see cref="Alignment"/> for <see cref="Dialog"/>.</summary>
|
|
|
/// <remarks>This property can be set in a Theme.</remarks>
|
|
|
[ConfigurationProperty (Scope = typeof (ThemeScope))]
|
|
|
- public static Alignment DefaultButtonAlignment { get; set; } = Alignment.Center;
|
|
|
+ public static Alignment DefaultButtonAlignment
|
|
|
+ {
|
|
|
+ get => _defaultButtonAlignment;
|
|
|
+ set => _defaultButtonAlignment = value;
|
|
|
+ }
|
|
|
|
|
|
/// <summary>
|
|
|
/// Defines the default minimum MessageBox width, as a percentage of the screen width. Can be configured via
|
|
|
/// <see cref="ConfigurationManager"/>.
|
|
|
/// </summary>
|
|
|
[ConfigurationProperty (Scope = typeof (ThemeScope))]
|
|
|
- public static int DefaultMinimumWidth { get; set; } = 0;
|
|
|
+ public static int DefaultMinimumWidth
|
|
|
+ {
|
|
|
+ get => _defaultMinimumWidth;
|
|
|
+ set => _defaultMinimumWidth = value;
|
|
|
+ }
|
|
|
|
|
|
/// <summary>
|
|
|
/// Defines the default minimum Dialog height, as a percentage of the screen width. Can be configured via
|
|
|
/// <see cref="ConfigurationManager"/>.
|
|
|
/// </summary>
|
|
|
[ConfigurationProperty (Scope = typeof (ThemeScope))]
|
|
|
- public static int DefaultMinimumHeight { get; set; } = 0;
|
|
|
+ public static int DefaultMinimumHeight
|
|
|
+ {
|
|
|
+ get => _defaultMinimumHeight;
|
|
|
+ set => _defaultMinimumHeight = value;
|
|
|
+ }
|
|
|
+
|
|
|
/// <summary>
|
|
|
- /// The index of the selected button, or -1 if the user pressed <see cref="Application.QuitKey"/> to close the MessageBox. This is useful for web
|
|
|
- /// based console where there is no SynchronizationContext or TaskScheduler.
|
|
|
+ /// The index of the selected button, or <see langword="null"/> if the user pressed <see cref="Application.QuitKey"/>.
|
|
|
/// </summary>
|
|
|
/// <remarks>
|
|
|
/// <para>
|
|
|
@@ -65,55 +115,97 @@ public static class MessageBox
|
|
|
/// non-global alternative for custom dialog implementations.
|
|
|
/// </para>
|
|
|
/// </remarks>
|
|
|
- public static int Clicked { get; private set; } = -1;
|
|
|
+ public static int? Clicked { get; private set; }
|
|
|
|
|
|
/// <summary>
|
|
|
- /// Presents an error <see cref="MessageBox"/> with the specified title and message and a list of buttons.
|
|
|
+ /// Displays an error <see cref="MessageBox"/> with fixed dimensions.
|
|
|
/// </summary>
|
|
|
- /// <returns>The index of the selected button, or -1 if the user pressed <see cref="Application.QuitKey"/> to close the MessageBox.</returns>
|
|
|
+ /// <param name="app">The application instance. If <see langword="null"/>, uses <see cref="IApplication.TopRunnable"/>.</param>
|
|
|
/// <param name="width">Width for the MessageBox.</param>
|
|
|
/// <param name="height">Height for the MessageBox.</param>
|
|
|
/// <param name="title">Title for the MessageBox.</param>
|
|
|
- /// <param name="message">Message to display; might contain multiple lines. The message will be word=wrapped by default.</param>
|
|
|
- /// <param name="buttons">Array of buttons to add.</param>
|
|
|
+ /// <param name="message">Message to display. May contain multiple lines and will be word-wrapped.</param>
|
|
|
+ /// <param name="buttons">Array of button labels.</param>
|
|
|
+ /// <returns>
|
|
|
+ /// The index of the selected button, or <see langword="null"/> if the user pressed
|
|
|
+ /// <see cref="Application.QuitKey"/>.
|
|
|
+ /// </returns>
|
|
|
+ /// <exception cref="ArgumentNullException">Thrown if <paramref name="app"/> is <see langword="null"/>.</exception>
|
|
|
/// <remarks>
|
|
|
- /// Use <see cref="ErrorQuery(string, string, string[])"/> instead; it automatically sizes the MessageBox based on
|
|
|
- /// the contents.
|
|
|
+ /// Consider using <see cref="ErrorQuery(IApplication?, string, string, string[])"/> which automatically sizes the
|
|
|
+ /// MessageBox.
|
|
|
/// </remarks>
|
|
|
- public static int ErrorQuery (int width, int height, string title, string message, params string [] buttons)
|
|
|
+ public static int? ErrorQuery (
|
|
|
+ IApplication? app,
|
|
|
+ int width,
|
|
|
+ int height,
|
|
|
+ string title,
|
|
|
+ string message,
|
|
|
+ params string [] buttons
|
|
|
+ )
|
|
|
{
|
|
|
- return QueryFull (true, width, height, title, message, 0, true, buttons);
|
|
|
+ return QueryFull (
|
|
|
+ app,
|
|
|
+ true,
|
|
|
+ width,
|
|
|
+ height,
|
|
|
+ title,
|
|
|
+ message,
|
|
|
+ 0,
|
|
|
+ true,
|
|
|
+ buttons);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
- /// Presents an error <see cref="MessageBox"/> with the specified title and message and a list of buttons to show
|
|
|
- /// to the user.
|
|
|
+ /// Displays an auto-sized error <see cref="MessageBox"/>.
|
|
|
/// </summary>
|
|
|
- /// <returns>The index of the selected button, or -1 if the user pressed <see cref="Application.QuitKey"/> to close the MessageBox.</returns>
|
|
|
- /// <param name="title">Title for the query.</param>
|
|
|
- /// <param name="message">Message to display; might contain multiple lines. The message will be word=wrapped by default.</param>
|
|
|
- /// <param name="buttons">Array of buttons to add.</param>
|
|
|
+ /// <param name="app">The application instance. If <see langword="null"/>, uses <see cref="IApplication.TopRunnable"/>.</param>
|
|
|
+ /// <param name="title">Title for the MessageBox.</param>
|
|
|
+ /// <param name="message">Message to display. May contain multiple lines and will be word-wrapped.</param>
|
|
|
+ /// <param name="buttons">Array of button labels.</param>
|
|
|
+ /// <returns>
|
|
|
+ /// The index of the selected button, or <see langword="null"/> if the user pressed
|
|
|
+ /// <see cref="Application.QuitKey"/>.
|
|
|
+ /// </returns>
|
|
|
+ /// <exception cref="ArgumentNullException">Thrown if <paramref name="app"/> is <see langword="null"/>.</exception>
|
|
|
/// <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.
|
|
|
+ /// The MessageBox is centered and auto-sized based on title, message, and buttons.
|
|
|
/// </remarks>
|
|
|
- public static int ErrorQuery (string title, string message, params string [] buttons) { return QueryFull (true, 0, 0, title, message, 0, true, buttons); }
|
|
|
+ public static int? ErrorQuery (IApplication? app, string title, string message, params string [] buttons)
|
|
|
+ {
|
|
|
+ return QueryFull (
|
|
|
+ app,
|
|
|
+ true,
|
|
|
+ 0,
|
|
|
+ 0,
|
|
|
+ title,
|
|
|
+ message,
|
|
|
+ 0,
|
|
|
+ true,
|
|
|
+ buttons);
|
|
|
+ }
|
|
|
|
|
|
/// <summary>
|
|
|
- /// Presents an error <see cref="MessageBox"/> with the specified title and message and a list of buttons.
|
|
|
+ /// Displays an error <see cref="MessageBox"/> with fixed dimensions and a default button.
|
|
|
/// </summary>
|
|
|
- /// <returns>The index of the selected button, or -1 if the user pressed <see cref="Application.QuitKey"/> to close the MessageBox.</returns>
|
|
|
+ /// <param name="app">The application instance. If <see langword="null"/>, uses <see cref="IApplication.TopRunnable"/>.</param>
|
|
|
/// <param name="width">Width for the MessageBox.</param>
|
|
|
/// <param name="height">Height for the MessageBox.</param>
|
|
|
/// <param name="title">Title for the MessageBox.</param>
|
|
|
- /// <param name="message">Message to display; might contain multiple lines. The message will be word=wrapped by default.</param>
|
|
|
- /// <param name="defaultButton">Index of the default button.</param>
|
|
|
- /// <param name="buttons">Array of buttons to add.</param>
|
|
|
+ /// <param name="message">Message to display. May contain multiple lines and will be word-wrapped.</param>
|
|
|
+ /// <param name="defaultButton">Index of the default button (0-based).</param>
|
|
|
+ /// <param name="buttons">Array of button labels.</param>
|
|
|
+ /// <returns>
|
|
|
+ /// The index of the selected button, or <see langword="null"/> if the user pressed
|
|
|
+ /// <see cref="Application.QuitKey"/>.
|
|
|
+ /// </returns>
|
|
|
+ /// <exception cref="ArgumentNullException">Thrown if <paramref name="app"/> is <see langword="null"/>.</exception>
|
|
|
/// <remarks>
|
|
|
- /// Use <see cref="ErrorQuery(string, string, string[])"/> instead; it automatically sizes the MessageBox based on
|
|
|
- /// the contents.
|
|
|
+ /// Consider using <see cref="ErrorQuery(IApplication?, string, string, int, string[])"/> which automatically sizes the
|
|
|
+ /// MessageBox.
|
|
|
/// </remarks>
|
|
|
- public static int ErrorQuery (
|
|
|
+ public static int? ErrorQuery (
|
|
|
+ IApplication? app,
|
|
|
int width,
|
|
|
int height,
|
|
|
string title,
|
|
|
@@ -122,44 +214,73 @@ public static class MessageBox
|
|
|
params string [] buttons
|
|
|
)
|
|
|
{
|
|
|
- return QueryFull (true, width, height, title, message, defaultButton, true, buttons);
|
|
|
+ return QueryFull (
|
|
|
+ app,
|
|
|
+ true,
|
|
|
+ width,
|
|
|
+ height,
|
|
|
+ title,
|
|
|
+ message,
|
|
|
+ defaultButton,
|
|
|
+ true,
|
|
|
+ buttons);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
- /// Presents an error <see cref="MessageBox"/> with the specified title and message and a list of buttons to show
|
|
|
- /// to the user.
|
|
|
+ /// Displays an auto-sized error <see cref="MessageBox"/> with a default button.
|
|
|
/// </summary>
|
|
|
- /// <returns>The index of the selected button, or -1 if the user pressed <see cref="Application.QuitKey"/> to close the MessageBox.</returns>
|
|
|
+ /// <param name="app">The application instance. If <see langword="null"/>, uses <see cref="IApplication.TopRunnable"/>.</param>
|
|
|
/// <param name="title">Title for the MessageBox.</param>
|
|
|
- /// <param name="message">Message to display; might contain multiple lines. The message will be word=wrapped by default.</param>
|
|
|
- /// <param name="defaultButton">Index of the default button.</param>
|
|
|
- /// <param name="buttons">Array of buttons to add.</param>
|
|
|
+ /// <param name="message">Message to display. May contain multiple lines and will be word-wrapped.</param>
|
|
|
+ /// <param name="defaultButton">Index of the default button (0-based).</param>
|
|
|
+ /// <param name="buttons">Array of button labels.</param>
|
|
|
+ /// <returns>
|
|
|
+ /// The index of the selected button, or <see langword="null"/> if the user pressed
|
|
|
+ /// <see cref="Application.QuitKey"/>.
|
|
|
+ /// </returns>
|
|
|
+ /// <exception cref="ArgumentNullException">Thrown if <paramref name="app"/> is <see langword="null"/>.</exception>
|
|
|
/// <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.
|
|
|
+ /// The MessageBox is centered and auto-sized based on title, message, and buttons.
|
|
|
/// </remarks>
|
|
|
- public static int ErrorQuery (string title, string message, int defaultButton = 0, params string [] buttons)
|
|
|
+ public static int? ErrorQuery (IApplication? app, string title, string message, int defaultButton = 0, params string [] buttons)
|
|
|
{
|
|
|
- return QueryFull (true, 0, 0, title, message, defaultButton, true, buttons);
|
|
|
+ return QueryFull (
|
|
|
+ app,
|
|
|
+ true,
|
|
|
+ 0,
|
|
|
+ 0,
|
|
|
+ title,
|
|
|
+ message,
|
|
|
+ defaultButton,
|
|
|
+ true,
|
|
|
+ buttons);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
- /// Presents an error <see cref="MessageBox"/> with the specified title and message and a list of buttons to show
|
|
|
- /// to the user.
|
|
|
+ /// Displays an error <see cref="MessageBox"/> with fixed dimensions, a default button, and word-wrap control.
|
|
|
/// </summary>
|
|
|
- /// <returns>The index of the selected button, or -1 if the user pressed <see cref="Application.QuitKey"/> to close the MessageBox.</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. The message will be word=wrapped by default.</param>
|
|
|
- /// <param name="defaultButton">Index of the default button.</param>
|
|
|
- /// <param name="wrapMessage">If wrap the message or not.</param>
|
|
|
- /// <param name="buttons">Array of buttons to add.</param>
|
|
|
+ /// <param name="app">The application instance. If <see langword="null"/>, uses <see cref="IApplication.TopRunnable"/>.</param>
|
|
|
+ /// <param name="width">Width for the MessageBox.</param>
|
|
|
+ /// <param name="height">Height for the MessageBox.</param>
|
|
|
+ /// <param name="title">Title for the MessageBox.</param>
|
|
|
+ /// <param name="message">Message to display. May contain multiple lines.</param>
|
|
|
+ /// <param name="defaultButton">Index of the default button (0-based).</param>
|
|
|
+ /// <param name="wrapMessage">
|
|
|
+ /// If <see langword="true"/>, word-wraps the message; otherwise displays as-is with multi-line
|
|
|
+ /// support.
|
|
|
+ /// </param>
|
|
|
+ /// <param name="buttons">Array of button labels.</param>
|
|
|
+ /// <returns>
|
|
|
+ /// The index of the selected button, or <see langword="null"/> if the user pressed
|
|
|
+ /// <see cref="Application.QuitKey"/>.
|
|
|
+ /// </returns>
|
|
|
+ /// <exception cref="ArgumentNullException">Thrown if <paramref name="app"/> is <see langword="null"/>.</exception>
|
|
|
/// <remarks>
|
|
|
- /// Use <see cref="ErrorQuery(string, string, string[])"/> instead; it automatically sizes the MessageBox based on
|
|
|
- /// the contents.
|
|
|
+ /// Consider using <see cref="ErrorQuery(IApplication?, string, string, int, bool, string[])"/> which automatically
|
|
|
+ /// sizes the MessageBox.
|
|
|
/// </remarks>
|
|
|
- public static int ErrorQuery (
|
|
|
+ public static int? ErrorQuery (
|
|
|
+ IApplication? app,
|
|
|
int width,
|
|
|
int height,
|
|
|
string title,
|
|
|
@@ -169,24 +290,40 @@ public static class MessageBox
|
|
|
params string [] buttons
|
|
|
)
|
|
|
{
|
|
|
- return QueryFull (true, width, height, title, message, defaultButton, wrapMessage, buttons);
|
|
|
+ return QueryFull (
|
|
|
+ app,
|
|
|
+ true,
|
|
|
+ width,
|
|
|
+ height,
|
|
|
+ title,
|
|
|
+ message,
|
|
|
+ defaultButton,
|
|
|
+ wrapMessage,
|
|
|
+ buttons);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
- /// Presents an error <see cref="MessageBox"/> with the specified title and message and a list of buttons to show
|
|
|
- /// to the user.
|
|
|
+ /// Displays an auto-sized error <see cref="MessageBox"/> with a default button and word-wrap control.
|
|
|
/// </summary>
|
|
|
- /// <returns>The index of the selected button, or -1 if the user pressed <see cref="Application.QuitKey"/> to close the MessageBox.</returns>
|
|
|
- /// <param name="title">Title for the query.</param>
|
|
|
- /// <param name="message">Message to display; might contain multiple lines. The message will be word=wrapped by default.</param>
|
|
|
- /// <param name="defaultButton">Index of the default button.</param>
|
|
|
- /// <param name="wrapMessage">If wrap the message or not. The default is <see langword="true"/></param>
|
|
|
- /// <param name="buttons">Array of buttons to add.</param>
|
|
|
+ /// <param name="app">The application instance. If <see langword="null"/>, uses <see cref="IApplication.TopRunnable"/>.</param>
|
|
|
+ /// <param name="title">Title for the MessageBox.</param>
|
|
|
+ /// <param name="message">Message to display. May contain multiple lines.</param>
|
|
|
+ /// <param name="defaultButton">Index of the default button (0-based).</param>
|
|
|
+ /// <param name="wrapMessage">
|
|
|
+ /// If <see langword="true"/>, word-wraps the message; otherwise displays as-is with multi-line
|
|
|
+ /// support.
|
|
|
+ /// </param>
|
|
|
+ /// <param name="buttons">Array of button labels.</param>
|
|
|
+ /// <returns>
|
|
|
+ /// The index of the selected button, or <see langword="null"/> if the user pressed
|
|
|
+ /// <see cref="Application.QuitKey"/>.
|
|
|
+ /// </returns>
|
|
|
+ /// <exception cref="ArgumentNullException">Thrown if <paramref name="app"/> is <see langword="null"/>.</exception>
|
|
|
/// <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.
|
|
|
+ /// The MessageBox is centered and auto-sized based on title, message, and buttons.
|
|
|
/// </remarks>
|
|
|
- public static int ErrorQuery (
|
|
|
+ public static int? ErrorQuery (
|
|
|
+ IApplication? app,
|
|
|
string title,
|
|
|
string message,
|
|
|
int defaultButton = 0,
|
|
|
@@ -194,67 +331,100 @@ public static class MessageBox
|
|
|
params string [] buttons
|
|
|
)
|
|
|
{
|
|
|
- return QueryFull (true, 0, 0, title, message, defaultButton, wrapMessage, buttons);
|
|
|
+ return QueryFull (
|
|
|
+ app,
|
|
|
+ true,
|
|
|
+ 0,
|
|
|
+ 0,
|
|
|
+ title,
|
|
|
+ message,
|
|
|
+ defaultButton,
|
|
|
+ wrapMessage,
|
|
|
+ buttons);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
- /// Presents a <see cref="MessageBox"/> with the specified title and message and a list of buttons.
|
|
|
+ /// Displays a <see cref="MessageBox"/> with fixed dimensions.
|
|
|
/// </summary>
|
|
|
- /// <returns>The index of the selected button, or -1 if the user pressed <see cref="Application.QuitKey"/> to close the MessageBox.</returns>
|
|
|
+ /// <param name="app">The application instance. If <see langword="null"/>, uses <see cref="IApplication.TopRunnable"/>.</param>
|
|
|
/// <param name="width">Width for the MessageBox.</param>
|
|
|
/// <param name="height">Height for the MessageBox.</param>
|
|
|
/// <param name="title">Title for the MessageBox.</param>
|
|
|
- /// <param name="message">Message to display; might contain multiple lines. The message will be word=wrapped by default.</param>
|
|
|
- /// <param name="buttons">Array of buttons to add.</param>
|
|
|
+ /// <param name="message">Message to display. May contain multiple lines and will be word-wrapped.</param>
|
|
|
+ /// <param name="buttons">Array of button labels.</param>
|
|
|
+ /// <returns>
|
|
|
+ /// The index of the selected button, or <see langword="null"/> if the user pressed
|
|
|
+ /// <see cref="Application.QuitKey"/>.
|
|
|
+ /// </returns>
|
|
|
+ /// <exception cref="ArgumentNullException">Thrown if <paramref name="app"/> is <see langword="null"/>.</exception>
|
|
|
/// <remarks>
|
|
|
- /// Use <see cref="Query(string, string, string[])"/> instead; it automatically sizes the MessageBox based on
|
|
|
- /// the contents.
|
|
|
+ /// Consider using <see cref="Query(IApplication?, string, string, string[])"/> which automatically sizes the
|
|
|
+ /// MessageBox.
|
|
|
/// </remarks>
|
|
|
- public static int Query (int width, int height, string title, string message, params string [] buttons)
|
|
|
+ public static int? Query (IApplication? app, int width, int height, string title, string message, params string [] buttons)
|
|
|
{
|
|
|
- return QueryFull (false, width, height, title, message, 0, true, buttons);
|
|
|
+ return QueryFull (
|
|
|
+ app,
|
|
|
+ false,
|
|
|
+ width,
|
|
|
+ height,
|
|
|
+ title,
|
|
|
+ message,
|
|
|
+ 0,
|
|
|
+ true,
|
|
|
+ buttons);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
- /// Presents a <see cref="MessageBox"/> with the specified title and message and a list of buttons.
|
|
|
+ /// Displays an auto-sized <see cref="MessageBox"/>.
|
|
|
/// </summary>
|
|
|
- /// <returns>The index of the selected button, or -1 if the user pressed <see cref="Application.QuitKey"/> to close the MessageBox.</returns>
|
|
|
+ /// <param name="app">The application instance. If <see langword="null"/>, uses <see cref="IApplication.TopRunnable"/>.</param>
|
|
|
/// <param name="title">Title for the MessageBox.</param>
|
|
|
- /// <param name="message">Message to display; might contain multiple lines. The message will be word=wrapped by default.</param>
|
|
|
- /// <param name="buttons">Array of buttons to add.</param>
|
|
|
+ /// <param name="message">Message to display. May contain multiple lines and will be word-wrapped.</param>
|
|
|
+ /// <param name="buttons">Array of button labels.</param>
|
|
|
+ /// <returns>
|
|
|
+ /// The index of the selected button, or <see langword="null"/> if the user pressed
|
|
|
+ /// <see cref="Application.QuitKey"/>.
|
|
|
+ /// </returns>
|
|
|
+ /// <exception cref="ArgumentNullException">Thrown if <paramref name="app"/> is <see langword="null"/>.</exception>
|
|
|
/// <remarks>
|
|
|
- /// <para>
|
|
|
- /// 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.
|
|
|
- /// </para>
|
|
|
- /// <para>
|
|
|
- /// Use <see cref="Query(string, string, string[])"/> instead; it automatically sizes the MessageBox based on
|
|
|
- /// the contents.
|
|
|
- /// </para>
|
|
|
+ /// The MessageBox is centered and auto-sized based on title, message, and buttons.
|
|
|
/// </remarks>
|
|
|
- public static int Query (string title, string message, params string [] buttons) { return QueryFull (false, 0, 0, title, message, 0, true, buttons); }
|
|
|
+ public static int? Query (IApplication? app, string title, string message, params string [] buttons)
|
|
|
+ {
|
|
|
+ return QueryFull (
|
|
|
+ app,
|
|
|
+ false,
|
|
|
+ 0,
|
|
|
+ 0,
|
|
|
+ title,
|
|
|
+ message,
|
|
|
+ 0,
|
|
|
+ true,
|
|
|
+ buttons);
|
|
|
+ }
|
|
|
|
|
|
/// <summary>
|
|
|
- /// Presents a <see cref="MessageBox"/> with the specified title and message and a list of buttons.
|
|
|
+ /// Displays a <see cref="MessageBox"/> with fixed dimensions and a default button.
|
|
|
/// </summary>
|
|
|
- /// <returns>The index of the selected button, or -1 if the user pressed <see cref="Application.QuitKey"/> to close the MessageBox.</returns>
|
|
|
- /// <param name="width">Width for the window.</param>
|
|
|
- /// <param name="height">Height for the window.</param>
|
|
|
+ /// <param name="app">The application instance. If <see langword="null"/>, uses <see cref="IApplication.TopRunnable"/>.</param>
|
|
|
+ /// <param name="width">Width for the MessageBox.</param>
|
|
|
+ /// <param name="height">Height for the MessageBox.</param>
|
|
|
/// <param name="title">Title for the MessageBox.</param>
|
|
|
- /// <param name="message">Message to display; might contain multiple lines. The message will be word=wrapped by default.</param>
|
|
|
- /// <param name="defaultButton">Index of the default button.</param>
|
|
|
- /// <param name="buttons">Array of buttons to add.</param>
|
|
|
+ /// <param name="message">Message to display. May contain multiple lines and will be word-wrapped.</param>
|
|
|
+ /// <param name="defaultButton">Index of the default button (0-based).</param>
|
|
|
+ /// <param name="buttons">Array of button labels.</param>
|
|
|
+ /// <returns>
|
|
|
+ /// The index of the selected button, or <see langword="null"/> if the user pressed
|
|
|
+ /// <see cref="Application.QuitKey"/>.
|
|
|
+ /// </returns>
|
|
|
+ /// <exception cref="ArgumentNullException">Thrown if <paramref name="app"/> is <see langword="null"/>.</exception>
|
|
|
/// <remarks>
|
|
|
- /// <para>
|
|
|
- /// 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.
|
|
|
- /// </para>
|
|
|
- /// <para>
|
|
|
- /// Use <see cref="Query(string, string, string[])"/> instead; it automatically sizes the MessageBox based on
|
|
|
- /// the contents.
|
|
|
- /// </para>
|
|
|
+ /// Consider using <see cref="Query(IApplication?, string, string, int, string[])"/> which automatically sizes the
|
|
|
+ /// MessageBox.
|
|
|
/// </remarks>
|
|
|
- public static int Query (
|
|
|
+ public static int? Query (
|
|
|
+ IApplication? app,
|
|
|
int width,
|
|
|
int height,
|
|
|
string title,
|
|
|
@@ -263,43 +433,73 @@ public static class MessageBox
|
|
|
params string [] buttons
|
|
|
)
|
|
|
{
|
|
|
- return QueryFull (false, width, height, title, message, defaultButton, true, buttons);
|
|
|
+ return QueryFull (
|
|
|
+ app,
|
|
|
+ false,
|
|
|
+ width,
|
|
|
+ height,
|
|
|
+ title,
|
|
|
+ message,
|
|
|
+ defaultButton,
|
|
|
+ true,
|
|
|
+ buttons);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
- /// Presents a <see cref="MessageBox"/> with the specified title and message and a list of buttons.
|
|
|
+ /// Displays an auto-sized <see cref="MessageBox"/> with a default button.
|
|
|
/// </summary>
|
|
|
- /// <returns>The index of the selected button, or -1 if the user pressed <see cref="Application.QuitKey"/> to close the MessageBox.</returns>
|
|
|
+ /// <param name="app">The application instance. If <see langword="null"/>, uses <see cref="IApplication.TopRunnable"/>.</param>
|
|
|
/// <param name="title">Title for the MessageBox.</param>
|
|
|
- /// <param name="message">Message to display; might contain multiple lines. The message will be word=wrapped by default.</param>
|
|
|
- /// <param name="defaultButton">Index of the default button.</param>
|
|
|
- /// <param name="buttons">Array of buttons to add.</param>
|
|
|
+ /// <param name="message">Message to display. May contain multiple lines and will be word-wrapped.</param>
|
|
|
+ /// <param name="defaultButton">Index of the default button (0-based).</param>
|
|
|
+ /// <param name="buttons">Array of button labels.</param>
|
|
|
+ /// <returns>
|
|
|
+ /// The index of the selected button, or <see langword="null"/> if the user pressed
|
|
|
+ /// <see cref="Application.QuitKey"/>.
|
|
|
+ /// </returns>
|
|
|
+ /// <exception cref="ArgumentNullException">Thrown if <paramref name="app"/> is <see langword="null"/>.</exception>
|
|
|
/// <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.
|
|
|
+ /// The MessageBox is centered and auto-sized based on title, message, and buttons.
|
|
|
/// </remarks>
|
|
|
- public static int Query (string title, string message, int defaultButton = 0, params string [] buttons)
|
|
|
+ public static int? Query (IApplication? app, string title, string message, int defaultButton = 0, params string [] buttons)
|
|
|
{
|
|
|
- return QueryFull (false, 0, 0, title, message, defaultButton, true, buttons);
|
|
|
+ return QueryFull (
|
|
|
+ app,
|
|
|
+ false,
|
|
|
+ 0,
|
|
|
+ 0,
|
|
|
+ title,
|
|
|
+ message,
|
|
|
+ defaultButton,
|
|
|
+ true,
|
|
|
+ buttons);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
- /// Presents a <see cref="MessageBox"/> with the specified title and message and a list of buttons to show
|
|
|
- /// to the user.
|
|
|
+ /// Displays a <see cref="MessageBox"/> with fixed dimensions, a default button, and word-wrap control.
|
|
|
/// </summary>
|
|
|
- /// <returns>The index of the selected button, or -1 if the user pressed <see cref="Application.QuitKey"/> to close the MessageBox.</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="wrapMessage">If wrap the message or not.</param>
|
|
|
- /// <param name="buttons">Array of buttons to add.</param>
|
|
|
+ /// <param name="app">The application instance. If <see langword="null"/>, uses <see cref="IApplication.TopRunnable"/>.</param>
|
|
|
+ /// <param name="width">Width for the MessageBox.</param>
|
|
|
+ /// <param name="height">Height for the MessageBox.</param>
|
|
|
+ /// <param name="title">Title for the MessageBox.</param>
|
|
|
+ /// <param name="message">Message to display. May contain multiple lines.</param>
|
|
|
+ /// <param name="defaultButton">Index of the default button (0-based).</param>
|
|
|
+ /// <param name="wrapMessage">
|
|
|
+ /// If <see langword="true"/>, word-wraps the message; otherwise displays as-is with multi-line
|
|
|
+ /// support.
|
|
|
+ /// </param>
|
|
|
+ /// <param name="buttons">Array of button labels.</param>
|
|
|
+ /// <returns>
|
|
|
+ /// The index of the selected button, or <see langword="null"/> if the user pressed
|
|
|
+ /// <see cref="Application.QuitKey"/>.
|
|
|
+ /// </returns>
|
|
|
+ /// <exception cref="ArgumentNullException">Thrown if <paramref name="app"/> is <see langword="null"/>.</exception>
|
|
|
/// <remarks>
|
|
|
- /// Use <see cref="Query(string, string, string[])"/> instead; it automatically sizes the MessageBox based on the
|
|
|
- /// contents.
|
|
|
+ /// Consider using <see cref="Query(IApplication?, string, string, int, bool, string[])"/> which automatically sizes
|
|
|
+ /// the MessageBox.
|
|
|
/// </remarks>
|
|
|
- public static int Query (
|
|
|
+ public static int? Query (
|
|
|
+ IApplication? app,
|
|
|
int width,
|
|
|
int height,
|
|
|
string title,
|
|
|
@@ -309,20 +509,40 @@ public static class MessageBox
|
|
|
params string [] buttons
|
|
|
)
|
|
|
{
|
|
|
- return QueryFull (false, width, height, title, message, defaultButton, wrapMessage, buttons);
|
|
|
+ return QueryFull (
|
|
|
+ app,
|
|
|
+ false,
|
|
|
+ width,
|
|
|
+ height,
|
|
|
+ title,
|
|
|
+ message,
|
|
|
+ defaultButton,
|
|
|
+ wrapMessage,
|
|
|
+ buttons);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
- /// Presents a <see cref="MessageBox"/> with the specified title and message and a list of buttons to show
|
|
|
- /// to the user.
|
|
|
+ /// Displays an auto-sized <see cref="MessageBox"/> with a default button and word-wrap control.
|
|
|
/// </summary>
|
|
|
- /// <returns>The index of the selected button, or -1 if the user pressed <see cref="Application.QuitKey"/> to close the MessageBox.</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="wrapMessage">If wrap the message or not.</param>
|
|
|
- /// <param name="buttons">Array of buttons to add.</param>
|
|
|
- public static int Query (
|
|
|
+ /// <param name="app">The application instance. If <see langword="null"/>, uses <see cref="IApplication.TopRunnable"/>.</param>
|
|
|
+ /// <param name="title">Title for the MessageBox.</param>
|
|
|
+ /// <param name="message">Message to display. May contain multiple lines.</param>
|
|
|
+ /// <param name="defaultButton">Index of the default button (0-based).</param>
|
|
|
+ /// <param name="wrapMessage">
|
|
|
+ /// If <see langword="true"/>, word-wraps the message; otherwise displays as-is with multi-line
|
|
|
+ /// support.
|
|
|
+ /// </param>
|
|
|
+ /// <param name="buttons">Array of button labels.</param>
|
|
|
+ /// <returns>
|
|
|
+ /// The index of the selected button, or <see langword="null"/> if the user pressed
|
|
|
+ /// <see cref="Application.QuitKey"/>.
|
|
|
+ /// </returns>
|
|
|
+ /// <exception cref="ArgumentNullException">Thrown if <paramref name="app"/> is <see langword="null"/>.</exception>
|
|
|
+ /// <remarks>
|
|
|
+ /// The MessageBox is centered and auto-sized based on title, message, and buttons.
|
|
|
+ /// </remarks>
|
|
|
+ public static int? Query (
|
|
|
+ IApplication? app,
|
|
|
string title,
|
|
|
string message,
|
|
|
int defaultButton = 0,
|
|
|
@@ -330,10 +550,20 @@ public static class MessageBox
|
|
|
params string [] buttons
|
|
|
)
|
|
|
{
|
|
|
- return QueryFull (false, 0, 0, title, message, defaultButton, wrapMessage, buttons);
|
|
|
+ return QueryFull (
|
|
|
+ app,
|
|
|
+ false,
|
|
|
+ 0,
|
|
|
+ 0,
|
|
|
+ title,
|
|
|
+ message,
|
|
|
+ defaultButton,
|
|
|
+ wrapMessage,
|
|
|
+ buttons);
|
|
|
}
|
|
|
|
|
|
- private static int QueryFull (
|
|
|
+ private static int? QueryFull (
|
|
|
+ IApplication? app,
|
|
|
bool useErrorColors,
|
|
|
int width,
|
|
|
int height,
|
|
|
@@ -344,6 +574,8 @@ public static class MessageBox
|
|
|
params string [] buttons
|
|
|
)
|
|
|
{
|
|
|
+ ArgumentNullException.ThrowIfNull (app);
|
|
|
+
|
|
|
// Create button array for Dialog
|
|
|
var count = 0;
|
|
|
List<Button> buttonList = new ();
|
|
|
@@ -384,8 +616,9 @@ public static class MessageBox
|
|
|
e.Handled = true;
|
|
|
}
|
|
|
|
|
|
- Application.RequestStop ();
|
|
|
- };
|
|
|
+ (s as View)?.App?.RequestStop ();
|
|
|
+ };
|
|
|
+ }
|
|
|
|
|
|
buttonList.Add (b);
|
|
|
count++;
|
|
|
@@ -395,20 +628,21 @@ public static class MessageBox
|
|
|
var d = new Dialog
|
|
|
{
|
|
|
Title = title,
|
|
|
- ButtonAlignment = MessageBox.DefaultButtonAlignment,
|
|
|
+ ButtonAlignment = DefaultButtonAlignment,
|
|
|
ButtonAlignmentModes = AlignmentModes.StartToEnd | AlignmentModes.AddSpaceBetweenItems,
|
|
|
- BorderStyle = MessageBox.DefaultBorderStyle,
|
|
|
- Buttons = buttonList.ToArray (),
|
|
|
+ BorderStyle = DefaultBorderStyle,
|
|
|
+ Buttons = buttonList.ToArray ()
|
|
|
};
|
|
|
|
|
|
- d.Width = Dim.Auto (DimAutoStyle.Auto,
|
|
|
- minimumContentDim: Dim.Func (_ => (int)((Application.Screen.Width - d.GetAdornmentsThickness ().Horizontal) * (DefaultMinimumWidth / 100f))),
|
|
|
- maximumContentDim: Dim.Func (_ => (int)((Application.Screen.Width - d.GetAdornmentsThickness ().Horizontal) * 0.9f)));
|
|
|
-
|
|
|
- d.Height = Dim.Auto (DimAutoStyle.Auto,
|
|
|
- minimumContentDim: Dim.Func (_ => (int)((Application.Screen.Height - d.GetAdornmentsThickness ().Vertical) * (DefaultMinimumHeight / 100f))),
|
|
|
- maximumContentDim: Dim.Func (_ => (int)((Application.Screen.Height - d.GetAdornmentsThickness ().Vertical) * 0.9f)));
|
|
|
+ d.Width = Dim.Auto (
|
|
|
+ DimAutoStyle.Auto,
|
|
|
+ Dim.Func (_ => (int)((app.Screen.Width - d.GetAdornmentsThickness ().Horizontal) * (DefaultMinimumWidth / 100f))),
|
|
|
+ Dim.Func (_ => (int)((app.Screen.Width - d.GetAdornmentsThickness ().Horizontal) * 0.9f)));
|
|
|
|
|
|
+ d.Height = Dim.Auto (
|
|
|
+ DimAutoStyle.Auto,
|
|
|
+ Dim.Func (_ => (int)((app.Screen.Height - d.GetAdornmentsThickness ().Vertical) * (DefaultMinimumHeight / 100f))),
|
|
|
+ Dim.Func (_ => (int)((app.Screen.Height - d.GetAdornmentsThickness ().Vertical) * 0.9f)));
|
|
|
|
|
|
if (width != 0)
|
|
|
{
|
|
|
@@ -422,7 +656,7 @@ public static class MessageBox
|
|
|
|
|
|
d.SchemeName = useErrorColors ? SchemeManager.SchemesToSchemeName (Schemes.Error) : SchemeManager.SchemesToSchemeName (Schemes.Dialog);
|
|
|
|
|
|
- d.HotKeySpecifier = new Rune ('\xFFFF');
|
|
|
+ d.HotKeySpecifier = new ('\xFFFF');
|
|
|
d.Text = message;
|
|
|
d.TextAlignment = Alignment.Center;
|
|
|
d.VerticalTextAlignment = Alignment.Start;
|
|
|
@@ -430,8 +664,8 @@ public static class MessageBox
|
|
|
d.TextFormatter.MultiLine = !wrapMessage;
|
|
|
|
|
|
// Run the modal; do not shut down the mainloop driver when done
|
|
|
- Application.Run (d);
|
|
|
-
|
|
|
+ app.Run (d);
|
|
|
+
|
|
|
// Use Dialog.Result instead of manually tracking with Clicked
|
|
|
// Dialog automatically extracts which button was clicked in OnIsRunningChanging
|
|
|
int result = d.Result ?? -1;
|