MessageBox.cs 20 KB

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