MessageBox.cs 20 KB

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