MessageBox.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. namespace Terminal.Gui;
  2. /// <summary>
  3. /// MessageBox displays a modal message to the user, with a title, a message and a series of options that the user
  4. /// can choose from.
  5. /// </summary>
  6. /// <para>
  7. /// The difference between the <see cref="Query(string, string, string[])"/> and
  8. /// <see cref="ErrorQuery(string, string, string[])"/> method is the default set of colors used for the message box.
  9. /// </para>
  10. /// <para>
  11. /// The following example pops up a <see cref="MessageBox"/> with the specified title and text, plus two
  12. /// <see cref="Button"/>s. The value -1 is returned when the user cancels the <see cref="MessageBox"/> by pressing the
  13. /// ESC key.
  14. /// </para>
  15. /// <example>
  16. /// <code lang="c#">
  17. /// var n = MessageBox.Query ("Quit Demo", "Are you sure you want to quit this demo?", "Yes", "No");
  18. /// if (n == 0)
  19. /// quit = true;
  20. /// else
  21. /// quit = false;
  22. /// </code>
  23. /// </example>
  24. public static class MessageBox
  25. {
  26. /// <summary>
  27. /// Defines the default border styling for <see cref="MessageBox"/>. Can be configured via
  28. /// <see cref="ConfigurationManager"/>.
  29. /// </summary>
  30. [SerializableConfigurationProperty (Scope = typeof (ThemeScope))]
  31. public static LineStyle DefaultBorderStyle { get; set; } = LineStyle.Single; // Default is set in config.json
  32. /// <summary>The default <see cref="Alignment"/> for <see cref="Dialog"/>.</summary>
  33. /// <remarks>This property can be set in a Theme.</remarks>
  34. [SerializableConfigurationProperty (Scope = typeof (ThemeScope))]
  35. public static Alignment DefaultButtonAlignment { get; set; } = Alignment.Center; // Default is set in config.json
  36. /// <summary>
  37. /// Defines the default minimum MessageBox width, as a percentage of the screen width. Can be configured via
  38. /// <see cref="ConfigurationManager"/>.
  39. /// </summary>
  40. [SerializableConfigurationProperty (Scope = typeof (ThemeScope))]
  41. public static int DefaultMinimumWidth { get; set; } = 0;
  42. /// <summary>
  43. /// Defines the default minimum Dialog height, as a percentage of the screen width. Can be configured via
  44. /// <see cref="ConfigurationManager"/>.
  45. /// </summary>
  46. [SerializableConfigurationProperty (Scope = typeof (ThemeScope))]
  47. public static int DefaultMinimumHeight { get; set; } = 0;
  48. /// <summary>
  49. /// 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
  50. /// based console where there is no SynchronizationContext or TaskScheduler.
  51. /// </summary>
  52. /// <remarks>
  53. /// Warning: This is a global variable and should be used with caution. It is not thread safe.
  54. /// </remarks>
  55. public static int Clicked { get; private set; } = -1;
  56. /// <summary>
  57. /// Presents an error <see cref="MessageBox"/> with the specified title and message and a list of buttons.
  58. /// </summary>
  59. /// <returns>The index of the selected button, or -1 if the user pressed <see cref="Application.QuitKey"/> to close the MessageBox.</returns>
  60. /// <param name="width">Width for the MessageBox.</param>
  61. /// <param name="height">Height for the MessageBox.</param>
  62. /// <param name="title">Title for the MessageBox.</param>
  63. /// <param name="message">Message to display; might contain multiple lines. The message will be word=wrapped by default.</param>
  64. /// <param name="buttons">Array of buttons to add.</param>
  65. /// <remarks>
  66. /// Use <see cref="ErrorQuery(string, string, string[])"/> instead; it automatically sizes the MessageBox based on
  67. /// the contents.
  68. /// </remarks>
  69. public static int ErrorQuery (int width, int height, string title, string message, params string [] buttons)
  70. {
  71. return QueryFull (true, width, height, title, message, 0, true, buttons);
  72. }
  73. /// <summary>
  74. /// Presents an error <see cref="MessageBox"/> with the specified title and message and a list of buttons to show
  75. /// to the user.
  76. /// </summary>
  77. /// <returns>The index of the selected button, or -1 if the user pressed <see cref="Application.QuitKey"/> to close the MessageBox.</returns>
  78. /// <param name="title">Title for the query.</param>
  79. /// <param name="message">Message to display; might contain multiple lines. The message will be word=wrapped by default.</param>
  80. /// <param name="buttons">Array of buttons to add.</param>
  81. /// <remarks>
  82. /// The message box will be vertically and horizontally centered in the container and the size will be
  83. /// automatically determined from the size of the title, message. and buttons.
  84. /// </remarks>
  85. public static int ErrorQuery (string title, string message, params string [] buttons) { return QueryFull (true, 0, 0, title, message, 0, true, buttons); }
  86. /// <summary>
  87. /// Presents an error <see cref="MessageBox"/> with the specified title and message and a list of buttons.
  88. /// </summary>
  89. /// <returns>The index of the selected button, or -1 if the user pressed <see cref="Application.QuitKey"/> to close the MessageBox.</returns>
  90. /// <param name="width">Width for the MessageBox.</param>
  91. /// <param name="height">Height for the MessageBox.</param>
  92. /// <param name="title">Title for the MessageBox.</param>
  93. /// <param name="message">Message to display; might contain multiple lines. The message will be word=wrapped by default.</param>
  94. /// <param name="defaultButton">Index of the default button.</param>
  95. /// <param name="buttons">Array of buttons to add.</param>
  96. /// <remarks>
  97. /// Use <see cref="ErrorQuery(string, string, string[])"/> instead; it automatically sizes the MessageBox based on
  98. /// the contents.
  99. /// </remarks>
  100. public static int ErrorQuery (
  101. int width,
  102. int height,
  103. string title,
  104. string message,
  105. int defaultButton = 0,
  106. params string [] buttons
  107. )
  108. {
  109. return QueryFull (true, width, height, title, message, defaultButton, true, buttons);
  110. }
  111. /// <summary>
  112. /// Presents an error <see cref="MessageBox"/> with the specified title and message and a list of buttons to show
  113. /// to the user.
  114. /// </summary>
  115. /// <returns>The index of the selected button, or -1 if the user pressed <see cref="Application.QuitKey"/> to close the MessageBox.</returns>
  116. /// <param name="title">Title for the MessageBox.</param>
  117. /// <param name="message">Message to display; might contain multiple lines. The message will be word=wrapped by default.</param>
  118. /// <param name="defaultButton">Index of the default button.</param>
  119. /// <param name="buttons">Array of buttons to add.</param>
  120. /// <remarks>
  121. /// The message box will be vertically and horizontally centered in the container and the size will be
  122. /// automatically determined from the size of the title, message. and buttons.
  123. /// </remarks>
  124. public static int ErrorQuery (string title, string message, int defaultButton = 0, params string [] buttons)
  125. {
  126. return QueryFull (true, 0, 0, title, message, defaultButton, true, buttons);
  127. }
  128. /// <summary>
  129. /// Presents an error <see cref="MessageBox"/> with the specified title and message and a list of buttons to show
  130. /// to the user.
  131. /// </summary>
  132. /// <returns>The index of the selected button, or -1 if the user pressed <see cref="Application.QuitKey"/> to close the MessageBox.</returns>
  133. /// <param name="width">Width for the window.</param>
  134. /// <param name="height">Height for the window.</param>
  135. /// <param name="title">Title for the query.</param>
  136. /// <param name="message">Message to display; might contain multiple lines. The message will be word=wrapped by default.</param>
  137. /// <param name="defaultButton">Index of the default button.</param>
  138. /// <param name="wrapMessage">If wrap the message or not.</param>
  139. /// <param name="buttons">Array of buttons to add.</param>
  140. /// <remarks>
  141. /// Use <see cref="ErrorQuery(string, string, string[])"/> instead; it automatically sizes the MessageBox based on
  142. /// the contents.
  143. /// </remarks>
  144. public static int ErrorQuery (
  145. int width,
  146. int height,
  147. string title,
  148. string message,
  149. int defaultButton = 0,
  150. bool wrapMessage = true,
  151. params string [] buttons
  152. )
  153. {
  154. return QueryFull (true, width, height, title, message, defaultButton, wrapMessage, buttons);
  155. }
  156. /// <summary>
  157. /// Presents an error <see cref="MessageBox"/> with the specified title and message and a list of buttons to show
  158. /// to the user.
  159. /// </summary>
  160. /// <returns>The index of the selected button, or -1 if the user pressed <see cref="Application.QuitKey"/> to close the MessageBox.</returns>
  161. /// <param name="title">Title for the query.</param>
  162. /// <param name="message">Message to display; might contain multiple lines. The message will be word=wrapped by default.</param>
  163. /// <param name="defaultButton">Index of the default button.</param>
  164. /// <param name="wrapMessage">If wrap the message or not. The default is <see langword="true"/></param>
  165. /// <param name="buttons">Array of buttons to add.</param>
  166. /// <remarks>
  167. /// The message box will be vertically and horizontally centered in the container and the size will be
  168. /// automatically determined from the size of the title, message. and buttons.
  169. /// </remarks>
  170. public static int ErrorQuery (
  171. string title,
  172. string message,
  173. int defaultButton = 0,
  174. bool wrapMessage = true,
  175. params string [] buttons
  176. )
  177. {
  178. return QueryFull (true, 0, 0, title, message, defaultButton, wrapMessage, buttons);
  179. }
  180. /// <summary>
  181. /// Presents a <see cref="MessageBox"/> with the specified title and message and a list of buttons.
  182. /// </summary>
  183. /// <returns>The index of the selected button, or -1 if the user pressed <see cref="Application.QuitKey"/> to close the MessageBox.</returns>
  184. /// <param name="width">Width for the MessageBox.</param>
  185. /// <param name="height">Height for the MessageBox.</param>
  186. /// <param name="title">Title for the MessageBox.</param>
  187. /// <param name="message">Message to display; might contain multiple lines. The message will be word=wrapped by default.</param>
  188. /// <param name="buttons">Array of buttons to add.</param>
  189. /// <remarks>
  190. /// Use <see cref="Query(string, string, string[])"/> instead; it automatically sizes the MessageBox based on
  191. /// the contents.
  192. /// </remarks>
  193. public static int Query (int width, int height, string title, string message, params string [] buttons)
  194. {
  195. return QueryFull (false, width, height, title, message, 0, true, buttons);
  196. }
  197. /// <summary>
  198. /// Presents a <see cref="MessageBox"/> with the specified title and message and a list of buttons.
  199. /// </summary>
  200. /// <returns>The index of the selected button, or -1 if the user pressed <see cref="Application.QuitKey"/> to close the MessageBox.</returns>
  201. /// <param name="title">Title for the MessageBox.</param>
  202. /// <param name="message">Message to display; might contain multiple lines. The message will be word=wrapped by default.</param>
  203. /// <param name="buttons">Array of buttons to add.</param>
  204. /// <remarks>
  205. /// <para>
  206. /// The message box will be vertically and horizontally centered in the container and the size will be
  207. /// automatically determined from the size of the title, message. and buttons.
  208. /// </para>
  209. /// <para>
  210. /// Use <see cref="Query(string, string, string[])"/> instead; it automatically sizes the MessageBox based on
  211. /// the contents.
  212. /// </para>
  213. /// </remarks>
  214. public static int Query (string title, string message, params string [] buttons) { return QueryFull (false, 0, 0, title, message, 0, true, buttons); }
  215. /// <summary>
  216. /// Presents a <see cref="MessageBox"/> with the specified title and message and a list of buttons.
  217. /// </summary>
  218. /// <returns>The index of the selected button, or -1 if the user pressed <see cref="Application.QuitKey"/> to close the MessageBox.</returns>
  219. /// <param name="width">Width for the window.</param>
  220. /// <param name="height">Height for the window.</param>
  221. /// <param name="title">Title for the MessageBox.</param>
  222. /// <param name="message">Message to display; might contain multiple lines. The message will be word=wrapped by default.</param>
  223. /// <param name="defaultButton">Index of the default button.</param>
  224. /// <param name="buttons">Array of buttons to add.</param>
  225. /// <remarks>
  226. /// <para>
  227. /// The message box will be vertically and horizontally centered in the container and the size will be
  228. /// automatically determined from the size of the title, message. and buttons.
  229. /// </para>
  230. /// <para>
  231. /// Use <see cref="Query(string, string, string[])"/> instead; it automatically sizes the MessageBox based on
  232. /// the contents.
  233. /// </para>
  234. /// </remarks>
  235. public static int Query (
  236. int width,
  237. int height,
  238. string title,
  239. string message,
  240. int defaultButton = 0,
  241. params string [] buttons
  242. )
  243. {
  244. return QueryFull (false, width, height, title, message, defaultButton, true, buttons);
  245. }
  246. /// <summary>
  247. /// Presents a <see cref="MessageBox"/> with the specified title and message and a list of buttons.
  248. /// </summary>
  249. /// <returns>The index of the selected button, or -1 if the user pressed <see cref="Application.QuitKey"/> to close the MessageBox.</returns>
  250. /// <param name="title">Title for the MessageBox.</param>
  251. /// <param name="message">Message to display; might contain multiple lines. The message will be word=wrapped by default.</param>
  252. /// <param name="defaultButton">Index of the default button.</param>
  253. /// <param name="buttons">Array of buttons to add.</param>
  254. /// <remarks>
  255. /// The message box will be vertically and horizontally centered in the container and the size will be
  256. /// automatically determined from the size of the message and buttons.
  257. /// </remarks>
  258. public static int Query (string title, string message, int defaultButton = 0, params string [] buttons)
  259. {
  260. return QueryFull (false, 0, 0, title, message, defaultButton, true, buttons);
  261. }
  262. /// <summary>
  263. /// Presents a <see cref="MessageBox"/> with the specified title and message and a list of buttons to show
  264. /// to the user.
  265. /// </summary>
  266. /// <returns>The index of the selected button, or -1 if the user pressed <see cref="Application.QuitKey"/> to close the MessageBox.</returns>
  267. /// <param name="width">Width for the window.</param>
  268. /// <param name="height">Height for the window.</param>
  269. /// <param name="title">Title for the query.</param>
  270. /// <param name="message">Message to display, might contain multiple lines.</param>
  271. /// <param name="defaultButton">Index of the default button.</param>
  272. /// <param name="wrapMessage">If wrap the message or not.</param>
  273. /// <param name="buttons">Array of buttons to add.</param>
  274. /// <remarks>
  275. /// Use <see cref="Query(string, string, string[])"/> instead; it automatically sizes the MessageBox based on the
  276. /// contents.
  277. /// </remarks>
  278. public static int Query (
  279. int width,
  280. int height,
  281. string title,
  282. string message,
  283. int defaultButton = 0,
  284. bool wrapMessage = true,
  285. params string [] buttons
  286. )
  287. {
  288. return QueryFull (false, width, height, title, message, defaultButton, wrapMessage, buttons);
  289. }
  290. /// <summary>
  291. /// Presents a <see cref="MessageBox"/> with the specified title and message and a list of buttons to show
  292. /// to the user.
  293. /// </summary>
  294. /// <returns>The index of the selected button, or -1 if the user pressed <see cref="Application.QuitKey"/> to close the MessageBox.</returns>
  295. /// <param name="title">Title for the query.</param>
  296. /// <param name="message">Message to display, might contain multiple lines.</param>
  297. /// <param name="defaultButton">Index of the default button.</param>
  298. /// <param name="wrapMessage">If wrap the message or not.</param>
  299. /// <param name="buttons">Array of buttons to add.</param>
  300. public static int Query (
  301. string title,
  302. string message,
  303. int defaultButton = 0,
  304. bool wrapMessage = true,
  305. params string [] buttons
  306. )
  307. {
  308. return QueryFull (false, 0, 0, title, message, defaultButton, wrapMessage, buttons);
  309. }
  310. private static int QueryFull (
  311. bool useErrorColors,
  312. int width,
  313. int height,
  314. string title,
  315. string message,
  316. int defaultButton = 0,
  317. bool wrapMessage = true,
  318. params string [] buttons
  319. )
  320. {
  321. // Create button array for Dialog
  322. var count = 0;
  323. List<Button> buttonList = new ();
  324. Clicked = -1;
  325. if (buttons is { })
  326. {
  327. if (defaultButton > buttons.Length - 1)
  328. {
  329. defaultButton = buttons.Length - 1;
  330. }
  331. foreach (string s in buttons)
  332. {
  333. var b = new Button
  334. {
  335. Text = s,
  336. Data = count,
  337. };
  338. if (count == defaultButton)
  339. {
  340. b.IsDefault = true;
  341. b.Accepting += (s, e) =>
  342. {
  343. // TODO: With https://github.com/gui-cs/Terminal.Gui/issues/3778 we can simplify this
  344. if (e.Context.Data is Button button)
  345. {
  346. Clicked = (int)button.Data!;
  347. }
  348. else if (e.Context.KeyBinding?.BoundView is Button btn)
  349. {
  350. Clicked = (int)btn.Data!;
  351. }
  352. e.Cancel = true;
  353. Application.RequestStop ();
  354. };
  355. }
  356. buttonList.Add (b);
  357. count++;
  358. }
  359. }
  360. var d = new Dialog
  361. {
  362. Title = title,
  363. ButtonAlignment = MessageBox.DefaultButtonAlignment,
  364. ButtonAlignmentModes = AlignmentModes.StartToEnd | AlignmentModes.AddSpaceBetweenItems,
  365. BorderStyle = MessageBox.DefaultBorderStyle,
  366. Buttons = buttonList.ToArray (),
  367. };
  368. d.Width = Dim.Auto (DimAutoStyle.Auto,
  369. minimumContentDim: Dim.Func (() => (int)((Application.Screen.Width - d.GetAdornmentsThickness ().Horizontal) * (DefaultMinimumWidth / 100f))),
  370. maximumContentDim: Dim.Func (() => (int)((Application.Screen.Width - d.GetAdornmentsThickness ().Horizontal) * 0.9f)));
  371. d.Height = Dim.Auto (DimAutoStyle.Auto,
  372. minimumContentDim: Dim.Func (() => (int)((Application.Screen.Height - d.GetAdornmentsThickness ().Vertical) * (DefaultMinimumHeight / 100f))),
  373. maximumContentDim: Dim.Func (() => (int)((Application.Screen.Height - d.GetAdornmentsThickness ().Vertical) * 0.9f)));
  374. if (width != 0)
  375. {
  376. d.Width = width;
  377. }
  378. if (height != 0)
  379. {
  380. d.Height = height;
  381. }
  382. d.ColorScheme = useErrorColors ? Colors.ColorSchemes ["Error"] : Colors.ColorSchemes ["Dialog"];
  383. d.HotKeySpecifier = new Rune ('\xFFFF');
  384. d.Text = message;
  385. d.TextAlignment = Alignment.Center;
  386. d.VerticalTextAlignment = Alignment.Start;
  387. d.TextFormatter.WordWrap = wrapMessage;
  388. d.TextFormatter.MultiLine = !wrapMessage;
  389. // Run the modal; do not shut down the mainloop driver when done
  390. Application.Run (d);
  391. d.Dispose ();
  392. return Clicked;
  393. }
  394. }