MessageBox.cs 20 KB

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