MessageBox.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. using System.Text.Json.Serialization;
  2. namespace Terminal.Gui;
  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. [SerializableConfigurationProperty (Scope = typeof (ThemeScope))]
  32. [JsonConverter (typeof (JsonStringEnumConverter))]
  33. public static LineStyle DefaultBorderStyle { get; set; } = LineStyle.Single;
  34. /// <summary>
  35. /// Defines the default minimum MessageBox width, as a percentage of the container width. Can be configured via
  36. /// <see cref="ConfigurationManager"/>.
  37. /// </summary>
  38. [SerializableConfigurationProperty (Scope = typeof (ThemeScope))]
  39. public static int DefaultMinimumWidth { get; set; } = 60;
  40. /// <summary>
  41. /// Defines the default minimum Dialog height, as a percentage of the container width. Can be configured via
  42. /// <see cref="ConfigurationManager"/>.
  43. /// </summary>
  44. [SerializableConfigurationProperty (Scope = typeof (ThemeScope))]
  45. public static int DefaultMinimumHeight { get; set; } = 5;
  46. /// <summary>
  47. /// 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
  48. /// based console where there is no SynchronizationContext or TaskScheduler.
  49. /// </summary>
  50. /// <remarks>
  51. /// Warning: This is a global variable and should be used with caution. It is not thread safe.
  52. /// </remarks>
  53. public static int Clicked { get; private set; } = -1;
  54. /// <summary>
  55. /// Presents an error <see cref="MessageBox"/> with the specified title and message and a list of buttons.
  56. /// </summary>
  57. /// <returns>The index of the selected button, or -1 if the user pressed <see cref="Application.QuitKey"/> to close the MessageBox.</returns>
  58. /// <param name="width">Width for the MessageBox.</param>
  59. /// <param name="height">Height for the MessageBox.</param>
  60. /// <param name="title">Title for the MessageBox.</param>
  61. /// <param name="message">Message to display; might contain multiple lines. The message will be word=wrapped by default.</param>
  62. /// <param name="buttons">Array of buttons to add.</param>
  63. /// <remarks>
  64. /// Use <see cref="ErrorQuery(string, string, string[])"/> instead; it automatically sizes the MessageBox based on
  65. /// the contents.
  66. /// </remarks>
  67. public static int ErrorQuery (int width, int height, string title, string message, params string [] buttons)
  68. {
  69. return QueryFull (true, width, height, title, message, 0, true, buttons);
  70. }
  71. /// <summary>
  72. /// Presents an error <see cref="MessageBox"/> with the specified title and message and a list of buttons to show
  73. /// to the user.
  74. /// </summary>
  75. /// <returns>The index of the selected button, or -1 if the user pressed <see cref="Application.QuitKey"/> to close the MessageBox.</returns>
  76. /// <param name="title">Title for the query.</param>
  77. /// <param name="message">Message to display; might contain multiple lines. The message will be word=wrapped by default.</param>
  78. /// <param name="buttons">Array of buttons to add.</param>
  79. /// <remarks>
  80. /// The message box will be vertically and horizontally centered in the container and the size will be
  81. /// automatically determined from the size of the title, message. and buttons.
  82. /// </remarks>
  83. public static int ErrorQuery (string title, string message, params string [] buttons) { return QueryFull (true, 0, 0, title, message, 0, true, buttons); }
  84. /// <summary>
  85. /// Presents an error <see cref="MessageBox"/> with the specified title and message and a list of buttons.
  86. /// </summary>
  87. /// <returns>The index of the selected button, or -1 if the user pressed <see cref="Application.QuitKey"/> to close the MessageBox.</returns>
  88. /// <param name="width">Width for the MessageBox.</param>
  89. /// <param name="height">Height for the MessageBox.</param>
  90. /// <param name="title">Title for the MessageBox.</param>
  91. /// <param name="message">Message to display; might contain multiple lines. The message will be word=wrapped by default.</param>
  92. /// <param name="defaultButton">Index of the default button.</param>
  93. /// <param name="buttons">Array of buttons to add.</param>
  94. /// <remarks>
  95. /// Use <see cref="ErrorQuery(string, string, string[])"/> instead; it automatically sizes the MessageBox based on
  96. /// the contents.
  97. /// </remarks>
  98. public static int ErrorQuery (
  99. int width,
  100. int height,
  101. string title,
  102. string message,
  103. int defaultButton = 0,
  104. params string [] buttons
  105. )
  106. {
  107. return QueryFull (true, width, height, title, message, defaultButton, true, buttons);
  108. }
  109. /// <summary>
  110. /// Presents an error <see cref="MessageBox"/> with the specified title and message and a list of buttons to show
  111. /// to the user.
  112. /// </summary>
  113. /// <returns>The index of the selected button, or -1 if the user pressed <see cref="Application.QuitKey"/> to close the MessageBox.</returns>
  114. /// <param name="title">Title for the MessageBox.</param>
  115. /// <param name="message">Message to display; might contain multiple lines. The message will be word=wrapped by default.</param>
  116. /// <param name="defaultButton">Index of the default button.</param>
  117. /// <param name="buttons">Array of buttons to add.</param>
  118. /// <remarks>
  119. /// The message box will be vertically and horizontally centered in the container and the size will be
  120. /// automatically determined from the size of the title, message. and buttons.
  121. /// </remarks>
  122. public static int ErrorQuery (string title, string message, int defaultButton = 0, params string [] buttons)
  123. {
  124. return QueryFull (true, 0, 0, title, message, defaultButton, true, buttons);
  125. }
  126. /// <summary>
  127. /// Presents an error <see cref="MessageBox"/> with the specified title and message and a list of buttons to show
  128. /// to the user.
  129. /// </summary>
  130. /// <returns>The index of the selected button, or -1 if the user pressed <see cref="Application.QuitKey"/> to close the MessageBox.</returns>
  131. /// <param name="width">Width for the window.</param>
  132. /// <param name="height">Height for the window.</param>
  133. /// <param name="title">Title for the query.</param>
  134. /// <param name="message">Message to display; might contain multiple lines. The message will be word=wrapped by default.</param>
  135. /// <param name="defaultButton">Index of the default button.</param>
  136. /// <param name="wrapMessage">If wrap the message or not.</param>
  137. /// <param name="buttons">Array of buttons to add.</param>
  138. /// <remarks>
  139. /// Use <see cref="ErrorQuery(string, string, string[])"/> instead; it automatically sizes the MessageBox based on
  140. /// the contents.
  141. /// </remarks>
  142. public static int ErrorQuery (
  143. int width,
  144. int height,
  145. string title,
  146. string message,
  147. int defaultButton = 0,
  148. bool wrapMessage = true,
  149. params string [] buttons
  150. )
  151. {
  152. return QueryFull (true, width, height, title, message, defaultButton, wrapMessage, buttons);
  153. }
  154. /// <summary>
  155. /// Presents an error <see cref="MessageBox"/> with the specified title and message and a list of buttons to show
  156. /// to the user.
  157. /// </summary>
  158. /// <returns>The index of the selected button, or -1 if the user pressed <see cref="Application.QuitKey"/> to close the MessageBox.</returns>
  159. /// <param name="title">Title for the query.</param>
  160. /// <param name="message">Message to display; might contain multiple lines. The message will be word=wrapped by default.</param>
  161. /// <param name="defaultButton">Index of the default button.</param>
  162. /// <param name="wrapMessage">If wrap the message or not. The default is <see langword="true"/></param>
  163. /// <param name="buttons">Array of buttons to add.</param>
  164. /// <remarks>
  165. /// The message box will be vertically and horizontally centered in the container and the size will be
  166. /// automatically determined from the size of the title, message. and buttons.
  167. /// </remarks>
  168. public static int ErrorQuery (
  169. string title,
  170. string message,
  171. int defaultButton = 0,
  172. bool wrapMessage = true,
  173. params string [] buttons
  174. )
  175. {
  176. return QueryFull (true, 0, 0, title, message, defaultButton, wrapMessage, buttons);
  177. }
  178. /// <summary>
  179. /// Presents a <see cref="MessageBox"/> with the specified title and message and a list of buttons.
  180. /// </summary>
  181. /// <returns>The index of the selected button, or -1 if the user pressed <see cref="Application.QuitKey"/> to close the MessageBox.</returns>
  182. /// <param name="width">Width for the MessageBox.</param>
  183. /// <param name="height">Height for the MessageBox.</param>
  184. /// <param name="title">Title for the MessageBox.</param>
  185. /// <param name="message">Message to display; might contain multiple lines. The message will be word=wrapped by default.</param>
  186. /// <param name="buttons">Array of buttons to add.</param>
  187. /// <remarks>
  188. /// Use <see cref="Query(string, string, string[])"/> instead; it automatically sizes the MessageBox based on
  189. /// the contents.
  190. /// </remarks>
  191. public static int Query (int width, int height, string title, string message, params string [] buttons)
  192. {
  193. return QueryFull (false, width, height, title, message, 0, true, buttons);
  194. }
  195. /// <summary>
  196. /// Presents a <see cref="MessageBox"/> with the specified title and message and a list of buttons.
  197. /// </summary>
  198. /// <returns>The index of the selected button, or -1 if the user pressed <see cref="Application.QuitKey"/> to close the MessageBox.</returns>
  199. /// <param name="title">Title for the MessageBox.</param>
  200. /// <param name="message">Message to display; might contain multiple lines. The message will be word=wrapped by default.</param>
  201. /// <param name="buttons">Array of buttons to add.</param>
  202. /// <remarks>
  203. /// <para>
  204. /// The message box will be vertically and horizontally centered in the container and the size will be
  205. /// automatically determined from the size of the title, message. and buttons.
  206. /// </para>
  207. /// <para>
  208. /// Use <see cref="Query(string, string, string[])"/> instead; it automatically sizes the MessageBox based on
  209. /// the contents.
  210. /// </para>
  211. /// </remarks>
  212. public static int Query (string title, string message, params string [] buttons) { return QueryFull (false, 0, 0, title, message, 0, true, buttons); }
  213. /// <summary>
  214. /// Presents a <see cref="MessageBox"/> with the specified title and message and a list of buttons.
  215. /// </summary>
  216. /// <returns>The index of the selected button, or -1 if the user pressed <see cref="Application.QuitKey"/> to close the MessageBox.</returns>
  217. /// <param name="width">Width for the window.</param>
  218. /// <param name="height">Height for the window.</param>
  219. /// <param name="title">Title for the MessageBox.</param>
  220. /// <param name="message">Message to display; might contain multiple lines. The message will be word=wrapped by default.</param>
  221. /// <param name="defaultButton">Index of the default button.</param>
  222. /// <param name="buttons">Array of buttons to add.</param>
  223. /// <remarks>
  224. /// <para>
  225. /// The message box will be vertically and horizontally centered in the container and the size will be
  226. /// automatically determined from the size of the title, message. and buttons.
  227. /// </para>
  228. /// <para>
  229. /// Use <see cref="Query(string, string, string[])"/> instead; it automatically sizes the MessageBox based on
  230. /// the contents.
  231. /// </para>
  232. /// </remarks>
  233. public static int Query (
  234. int width,
  235. int height,
  236. string title,
  237. string message,
  238. int defaultButton = 0,
  239. params string [] buttons
  240. )
  241. {
  242. return QueryFull (false, width, height, title, message, defaultButton, true, buttons);
  243. }
  244. /// <summary>
  245. /// Presents a <see cref="MessageBox"/> with the specified title and message and a list of buttons.
  246. /// </summary>
  247. /// <returns>The index of the selected button, or -1 if the user pressed <see cref="Application.QuitKey"/> to close the MessageBox.</returns>
  248. /// <param name="title">Title for the MessageBox.</param>
  249. /// <param name="message">Message to display; might contain multiple lines. The message will be word=wrapped by default.</param>
  250. /// <param name="defaultButton">Index of the default button.</param>
  251. /// <param name="buttons">Array of buttons to add.</param>
  252. /// <remarks>
  253. /// The message box will be vertically and horizontally centered in the container and the size will be
  254. /// automatically determined from the size of the message and buttons.
  255. /// </remarks>
  256. public static int Query (string title, string message, int defaultButton = 0, params string [] buttons)
  257. {
  258. return QueryFull (false, 0, 0, title, message, defaultButton, true, buttons);
  259. }
  260. /// <summary>
  261. /// Presents a <see cref="MessageBox"/> with the specified title and message and a list of buttons to show
  262. /// to the user.
  263. /// </summary>
  264. /// <returns>The index of the selected button, or -1 if the user pressed <see cref="Application.QuitKey"/> to close the MessageBox.</returns>
  265. /// <param name="width">Width for the window.</param>
  266. /// <param name="height">Height for the window.</param>
  267. /// <param name="title">Title for the query.</param>
  268. /// <param name="message">Message to display, might contain multiple lines.</param>
  269. /// <param name="defaultButton">Index of the default button.</param>
  270. /// <param name="wrapMessage">If wrap the message or not.</param>
  271. /// <param name="buttons">Array of buttons to add.</param>
  272. /// <remarks>
  273. /// Use <see cref="Query(string, string, string[])"/> instead; it automatically sizes the MessageBox based on the
  274. /// contents.
  275. /// </remarks>
  276. public static int Query (
  277. int width,
  278. int height,
  279. string title,
  280. string message,
  281. int defaultButton = 0,
  282. bool wrapMessage = true,
  283. params string [] buttons
  284. )
  285. {
  286. return QueryFull (false, width, height, title, message, defaultButton, wrapMessage, buttons);
  287. }
  288. /// <summary>
  289. /// Presents a <see cref="MessageBox"/> with the specified title and message and a list of buttons to show
  290. /// to the user.
  291. /// </summary>
  292. /// <returns>The index of the selected button, or -1 if the user pressed <see cref="Application.QuitKey"/> to close the MessageBox.</returns>
  293. /// <param name="title">Title for the query.</param>
  294. /// <param name="message">Message to display, might contain multiple lines.</param>
  295. /// <param name="defaultButton">Index of the default button.</param>
  296. /// <param name="wrapMessage">If wrap the message or not.</param>
  297. /// <param name="buttons">Array of buttons to add.</param>
  298. public static int Query (
  299. string title,
  300. string message,
  301. int defaultButton = 0,
  302. bool wrapMessage = true,
  303. params string [] buttons
  304. )
  305. {
  306. return QueryFull (false, 0, 0, title, message, defaultButton, wrapMessage, buttons);
  307. }
  308. private static int QueryFull (
  309. bool useErrorColors,
  310. int width,
  311. int height,
  312. string title,
  313. string message,
  314. int defaultButton = 0,
  315. bool wrapMessage = true,
  316. params string [] buttons
  317. )
  318. {
  319. // Create button array for Dialog
  320. var count = 0;
  321. List<Button> buttonList = new ();
  322. if (buttons is { })
  323. {
  324. if (defaultButton > buttons.Length - 1)
  325. {
  326. defaultButton = buttons.Length - 1;
  327. }
  328. foreach (string s in buttons)
  329. {
  330. var b = new Button
  331. {
  332. Text = s,
  333. };
  334. if (count == defaultButton)
  335. {
  336. b.IsDefault = true;
  337. }
  338. buttonList.Add (b);
  339. count++;
  340. }
  341. }
  342. var d = new Dialog
  343. {
  344. Title = title,
  345. Buttons = buttonList.ToArray (),
  346. ButtonAlignment = Alignment.Center,
  347. ButtonAlignmentModes = AlignmentModes.StartToEnd | AlignmentModes.AddSpaceBetweenItems,
  348. BorderStyle = MessageBox.DefaultBorderStyle,
  349. Width = Dim.Auto (DimAutoStyle.Content, minimumContentDim: 1, Dim.Percent (90)),
  350. Height = Dim.Auto (DimAutoStyle.Content, minimumContentDim: 1, Dim.Percent (90)),
  351. };
  352. if (width != 0)
  353. {
  354. d.Width = width;
  355. }
  356. if (height != 0)
  357. {
  358. d.Height = height;
  359. }
  360. d.ColorScheme = useErrorColors ? Colors.ColorSchemes ["Error"] : Colors.ColorSchemes ["Dialog"];
  361. var messageLabel = new Label
  362. {
  363. HotKeySpecifier = new Rune ('\xFFFF'),
  364. Width = Dim.Auto (DimAutoStyle.Text),
  365. Height = Dim.Auto (DimAutoStyle.Text),
  366. Text = message,
  367. TextAlignment = Alignment.Center,
  368. X = Pos.Center (),
  369. Y = 0,
  370. //ColorScheme = Colors.ColorSchemes ["Error"],
  371. };
  372. messageLabel.TextFormatter.WordWrap = wrapMessage;
  373. messageLabel.TextFormatter.MultiLine = !wrapMessage;
  374. if (wrapMessage)
  375. {
  376. int buttonHeight = buttonList.Count > 0 ? buttonList [0].Frame.Height : 0;
  377. messageLabel.Width = Dim.Fill ();
  378. messageLabel.Height = Dim.Func (() => GetWrapSize ().Height);
  379. Size GetWrapSize ()
  380. {
  381. // A bit of a hack to get the height of the wrapped text.
  382. messageLabel.TextFormatter.Size = d.GetContentSize () with { Height = 1000 };
  383. return messageLabel.TextFormatter.FormatAndGetSize ();
  384. }
  385. }
  386. d.Add (messageLabel);
  387. // Setup actions
  388. Clicked = -1;
  389. for (var n = 0; n < buttonList.Count; n++)
  390. {
  391. int buttonId = n;
  392. Button b = buttonList [n];
  393. b.Accept += (s, e) =>
  394. {
  395. Clicked = buttonId;
  396. Application.RequestStop ();
  397. };
  398. if (b.IsDefault)
  399. {
  400. b.SetFocus ();
  401. }
  402. }
  403. // Run the modal; do not shutdown the mainloop driver when done
  404. Application.Run (d);
  405. d.Dispose ();
  406. return Clicked;
  407. }
  408. }