MessageBox.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. namespace Terminal.Gui.Views;
  2. /// <summary>
  3. /// Displays a modal message box with a title, message, and buttons. Returns the index of the selected button,
  4. /// or <see langword="null"/> if the user cancels with <see cref="Application.QuitKey"/>.
  5. /// </summary>
  6. /// <remarks>
  7. /// <para>
  8. /// MessageBox provides static methods for displaying modal dialogs with customizable buttons and messages.
  9. /// All methods return <see langword="int?"/> where the value is the 0-based index of the button pressed,
  10. /// or <see langword="null"/> if the user pressed <see cref="Application.QuitKey"/> (typically Esc).
  11. /// </para>
  12. /// <para>
  13. /// <see cref="Query(IApplication?, string, string, string[])"/> uses the default Dialog color scheme.
  14. /// <see cref="ErrorQuery(IApplication?, string, string, string[])"/> uses the Error color scheme.
  15. /// </para>
  16. /// <para>
  17. /// <b>Important:</b> All MessageBox methods require an <see cref="IApplication"/> instance to be passed.
  18. /// This enables proper modal dialog management and respects the application's lifecycle. Pass your
  19. /// application instance (from <see cref="Application.Create()"/>) or use the legacy
  20. /// <see cref="Application.Instance"/> if using the static Application pattern.
  21. /// </para>
  22. /// <para>
  23. /// Example using instance-based pattern:
  24. /// <code>
  25. /// IApplication app = Application.Create();
  26. /// app.Init();
  27. ///
  28. /// int? result = MessageBox.Query(app, "Quit Demo", "Are you sure you want to quit?", "Yes", "No");
  29. /// if (result == 0) // User clicked "Yes"
  30. /// app.RequestStop();
  31. /// else if (result == null) // User pressed Esc
  32. /// // Handle cancellation
  33. ///
  34. /// app.Shutdown();
  35. /// </code>
  36. /// </para>
  37. /// <para>
  38. /// Example using legacy static pattern:
  39. /// <code>
  40. /// Application.Init();
  41. ///
  42. /// int? result = MessageBox.Query(ApplicationImpl.Instance, "Quit Demo", "Are you sure?", "Yes", "No");
  43. /// if (result == 0) // User clicked "Yes"
  44. /// Application.RequestStop();
  45. ///
  46. /// Application.Shutdown();
  47. /// </code>
  48. /// </para>
  49. /// <para>
  50. /// The <see cref="Clicked"/> property provides a global variable alternative for web-based consoles
  51. /// without SynchronizationContext. However, using the return value is preferred as it's more thread-safe
  52. /// and follows modern async patterns.
  53. /// </para>
  54. /// </remarks>
  55. public static class MessageBox
  56. {
  57. private static LineStyle _defaultBorderStyle = LineStyle.Heavy; // Resources/config.json overrides
  58. private static Alignment _defaultButtonAlignment = Alignment.Center; // Resources/config.json overrides
  59. private static int _defaultMinimumWidth = 0; // Resources/config.json overrides
  60. private static int _defaultMinimumHeight = 0; // Resources/config.json overrides
  61. /// <summary>
  62. /// Defines the default border styling for <see cref="MessageBox"/>. Can be configured via
  63. /// <see cref="ConfigurationManager"/>.
  64. /// </summary>
  65. [ConfigurationProperty (Scope = typeof (ThemeScope))]
  66. public static LineStyle DefaultBorderStyle
  67. {
  68. get => _defaultBorderStyle;
  69. set => _defaultBorderStyle = value;
  70. }
  71. /// <summary>The default <see cref="Alignment"/> for <see cref="Dialog"/>.</summary>
  72. /// <remarks>This property can be set in a Theme.</remarks>
  73. [ConfigurationProperty (Scope = typeof (ThemeScope))]
  74. public static Alignment DefaultButtonAlignment
  75. {
  76. get => _defaultButtonAlignment;
  77. set => _defaultButtonAlignment = value;
  78. }
  79. /// <summary>
  80. /// Defines the default minimum MessageBox width, as a percentage of the screen width. Can be configured via
  81. /// <see cref="ConfigurationManager"/>.
  82. /// </summary>
  83. [ConfigurationProperty (Scope = typeof (ThemeScope))]
  84. public static int DefaultMinimumWidth
  85. {
  86. get => _defaultMinimumWidth;
  87. set => _defaultMinimumWidth = value;
  88. }
  89. /// <summary>
  90. /// Defines the default minimum Dialog height, as a percentage of the screen width. Can be configured via
  91. /// <see cref="ConfigurationManager"/>.
  92. /// </summary>
  93. [ConfigurationProperty (Scope = typeof (ThemeScope))]
  94. public static int DefaultMinimumHeight
  95. {
  96. get => _defaultMinimumHeight;
  97. set => _defaultMinimumHeight = value;
  98. }
  99. /// <summary>
  100. /// The index of the selected button, or <see langword="null"/> if the user pressed <see cref="Application.QuitKey"/>.
  101. /// </summary>
  102. /// <remarks>
  103. /// This global variable is useful for web-based consoles without a SynchronizationContext or TaskScheduler.
  104. /// Warning: Not thread-safe.
  105. /// </remarks>
  106. public static int? Clicked { get; private set; }
  107. /// <summary>
  108. /// Displays an error <see cref="MessageBox"/> with fixed dimensions.
  109. /// </summary>
  110. /// <param name="app">The application instance. If <see langword="null"/>, uses <see cref="IApplication.TopRunnableView"/>.</param>
  111. /// <param name="width">Width for the MessageBox.</param>
  112. /// <param name="height">Height for the MessageBox.</param>
  113. /// <param name="title">Title for the MessageBox.</param>
  114. /// <param name="message">Message to display. May contain multiple lines and will be word-wrapped.</param>
  115. /// <param name="buttons">Array of button labels.</param>
  116. /// <returns>
  117. /// The index of the selected button, or <see langword="null"/> if the user pressed
  118. /// <see cref="Application.QuitKey"/>.
  119. /// </returns>
  120. /// <exception cref="ArgumentNullException">Thrown if <paramref name="app"/> is <see langword="null"/>.</exception>
  121. /// <remarks>
  122. /// Consider using <see cref="ErrorQuery(IApplication?, string, string, string[])"/> which automatically sizes the
  123. /// MessageBox.
  124. /// </remarks>
  125. public static int? ErrorQuery (
  126. IApplication? app,
  127. int width,
  128. int height,
  129. string title,
  130. string message,
  131. params string [] buttons
  132. )
  133. {
  134. return QueryFull (
  135. app,
  136. true,
  137. width,
  138. height,
  139. title,
  140. message,
  141. 0,
  142. true,
  143. buttons);
  144. }
  145. /// <summary>
  146. /// Displays an auto-sized error <see cref="MessageBox"/>.
  147. /// </summary>
  148. /// <param name="app">The application instance. If <see langword="null"/>, uses <see cref="IApplication.TopRunnableView"/>.</param>
  149. /// <param name="title">Title for the MessageBox.</param>
  150. /// <param name="message">Message to display. May contain multiple lines and will be word-wrapped.</param>
  151. /// <param name="buttons">Array of button labels.</param>
  152. /// <returns>
  153. /// The index of the selected button, or <see langword="null"/> if the user pressed
  154. /// <see cref="Application.QuitKey"/>.
  155. /// </returns>
  156. /// <exception cref="ArgumentNullException">Thrown if <paramref name="app"/> is <see langword="null"/>.</exception>
  157. /// <remarks>
  158. /// The MessageBox is centered and auto-sized based on title, message, and buttons.
  159. /// </remarks>
  160. public static int? ErrorQuery (IApplication? app, string title, string message, params string [] buttons)
  161. {
  162. return QueryFull (
  163. app,
  164. true,
  165. 0,
  166. 0,
  167. title,
  168. message,
  169. 0,
  170. true,
  171. buttons);
  172. }
  173. /// <summary>
  174. /// Displays an error <see cref="MessageBox"/> with fixed dimensions and a default button.
  175. /// </summary>
  176. /// <param name="app">The application instance. If <see langword="null"/>, uses <see cref="IApplication.TopRunnableView"/>.</param>
  177. /// <param name="width">Width for the MessageBox.</param>
  178. /// <param name="height">Height for the MessageBox.</param>
  179. /// <param name="title">Title for the MessageBox.</param>
  180. /// <param name="message">Message to display. May contain multiple lines and will be word-wrapped.</param>
  181. /// <param name="defaultButton">Index of the default button (0-based).</param>
  182. /// <param name="buttons">Array of button labels.</param>
  183. /// <returns>
  184. /// The index of the selected button, or <see langword="null"/> if the user pressed
  185. /// <see cref="Application.QuitKey"/>.
  186. /// </returns>
  187. /// <exception cref="ArgumentNullException">Thrown if <paramref name="app"/> is <see langword="null"/>.</exception>
  188. /// <remarks>
  189. /// Consider using <see cref="ErrorQuery(IApplication?, string, string, int, string[])"/> which automatically sizes the
  190. /// MessageBox.
  191. /// </remarks>
  192. public static int? ErrorQuery (
  193. IApplication? app,
  194. int width,
  195. int height,
  196. string title,
  197. string message,
  198. int defaultButton = 0,
  199. params string [] buttons
  200. )
  201. {
  202. return QueryFull (
  203. app,
  204. true,
  205. width,
  206. height,
  207. title,
  208. message,
  209. defaultButton,
  210. true,
  211. buttons);
  212. }
  213. /// <summary>
  214. /// Displays an auto-sized error <see cref="MessageBox"/> with a default button.
  215. /// </summary>
  216. /// <param name="app">The application instance. If <see langword="null"/>, uses <see cref="IApplication.TopRunnableView"/>.</param>
  217. /// <param name="title">Title for the MessageBox.</param>
  218. /// <param name="message">Message to display. May contain multiple lines and will be word-wrapped.</param>
  219. /// <param name="defaultButton">Index of the default button (0-based).</param>
  220. /// <param name="buttons">Array of button labels.</param>
  221. /// <returns>
  222. /// The index of the selected button, or <see langword="null"/> if the user pressed
  223. /// <see cref="Application.QuitKey"/>.
  224. /// </returns>
  225. /// <exception cref="ArgumentNullException">Thrown if <paramref name="app"/> is <see langword="null"/>.</exception>
  226. /// <remarks>
  227. /// The MessageBox is centered and auto-sized based on title, message, and buttons.
  228. /// </remarks>
  229. public static int? ErrorQuery (IApplication? app, string title, string message, int defaultButton = 0, params string [] buttons)
  230. {
  231. return QueryFull (
  232. app,
  233. true,
  234. 0,
  235. 0,
  236. title,
  237. message,
  238. defaultButton,
  239. true,
  240. buttons);
  241. }
  242. /// <summary>
  243. /// Displays an error <see cref="MessageBox"/> with fixed dimensions, a default button, and word-wrap control.
  244. /// </summary>
  245. /// <param name="app">The application instance. If <see langword="null"/>, uses <see cref="IApplication.TopRunnableView"/>.</param>
  246. /// <param name="width">Width for the MessageBox.</param>
  247. /// <param name="height">Height for the MessageBox.</param>
  248. /// <param name="title">Title for the MessageBox.</param>
  249. /// <param name="message">Message to display. May contain multiple lines.</param>
  250. /// <param name="defaultButton">Index of the default button (0-based).</param>
  251. /// <param name="wrapMessage">
  252. /// If <see langword="true"/>, word-wraps the message; otherwise displays as-is with multi-line
  253. /// support.
  254. /// </param>
  255. /// <param name="buttons">Array of button labels.</param>
  256. /// <returns>
  257. /// The index of the selected button, or <see langword="null"/> if the user pressed
  258. /// <see cref="Application.QuitKey"/>.
  259. /// </returns>
  260. /// <exception cref="ArgumentNullException">Thrown if <paramref name="app"/> is <see langword="null"/>.</exception>
  261. /// <remarks>
  262. /// Consider using <see cref="ErrorQuery(IApplication?, string, string, int, bool, string[])"/> which automatically
  263. /// sizes the MessageBox.
  264. /// </remarks>
  265. public static int? ErrorQuery (
  266. IApplication? app,
  267. int width,
  268. int height,
  269. string title,
  270. string message,
  271. int defaultButton = 0,
  272. bool wrapMessage = true,
  273. params string [] buttons
  274. )
  275. {
  276. return QueryFull (
  277. app,
  278. true,
  279. width,
  280. height,
  281. title,
  282. message,
  283. defaultButton,
  284. wrapMessage,
  285. buttons);
  286. }
  287. /// <summary>
  288. /// Displays an auto-sized error <see cref="MessageBox"/> with a default button and word-wrap control.
  289. /// </summary>
  290. /// <param name="app">The application instance. If <see langword="null"/>, uses <see cref="IApplication.TopRunnableView"/>.</param>
  291. /// <param name="title">Title for the MessageBox.</param>
  292. /// <param name="message">Message to display. May contain multiple lines.</param>
  293. /// <param name="defaultButton">Index of the default button (0-based).</param>
  294. /// <param name="wrapMessage">
  295. /// If <see langword="true"/>, word-wraps the message; otherwise displays as-is with multi-line
  296. /// support.
  297. /// </param>
  298. /// <param name="buttons">Array of button labels.</param>
  299. /// <returns>
  300. /// The index of the selected button, or <see langword="null"/> if the user pressed
  301. /// <see cref="Application.QuitKey"/>.
  302. /// </returns>
  303. /// <exception cref="ArgumentNullException">Thrown if <paramref name="app"/> is <see langword="null"/>.</exception>
  304. /// <remarks>
  305. /// The MessageBox is centered and auto-sized based on title, message, and buttons.
  306. /// </remarks>
  307. public static int? ErrorQuery (
  308. IApplication? app,
  309. string title,
  310. string message,
  311. int defaultButton = 0,
  312. bool wrapMessage = true,
  313. params string [] buttons
  314. )
  315. {
  316. return QueryFull (
  317. app,
  318. true,
  319. 0,
  320. 0,
  321. title,
  322. message,
  323. defaultButton,
  324. wrapMessage,
  325. buttons);
  326. }
  327. /// <summary>
  328. /// Displays a <see cref="MessageBox"/> with fixed dimensions.
  329. /// </summary>
  330. /// <param name="app">The application instance. If <see langword="null"/>, uses <see cref="IApplication.TopRunnableView"/>.</param>
  331. /// <param name="width">Width for the MessageBox.</param>
  332. /// <param name="height">Height for the MessageBox.</param>
  333. /// <param name="title">Title for the MessageBox.</param>
  334. /// <param name="message">Message to display. May contain multiple lines and will be word-wrapped.</param>
  335. /// <param name="buttons">Array of button labels.</param>
  336. /// <returns>
  337. /// The index of the selected button, or <see langword="null"/> if the user pressed
  338. /// <see cref="Application.QuitKey"/>.
  339. /// </returns>
  340. /// <exception cref="ArgumentNullException">Thrown if <paramref name="app"/> is <see langword="null"/>.</exception>
  341. /// <remarks>
  342. /// Consider using <see cref="Query(IApplication?, string, string, string[])"/> which automatically sizes the
  343. /// MessageBox.
  344. /// </remarks>
  345. public static int? Query (IApplication? app, int width, int height, string title, string message, params string [] buttons)
  346. {
  347. return QueryFull (
  348. app,
  349. false,
  350. width,
  351. height,
  352. title,
  353. message,
  354. 0,
  355. true,
  356. buttons);
  357. }
  358. /// <summary>
  359. /// Displays an auto-sized <see cref="MessageBox"/>.
  360. /// </summary>
  361. /// <param name="app">The application instance. If <see langword="null"/>, uses <see cref="IApplication.TopRunnableView"/>.</param>
  362. /// <param name="title">Title for the MessageBox.</param>
  363. /// <param name="message">Message to display. May contain multiple lines and will be word-wrapped.</param>
  364. /// <param name="buttons">Array of button labels.</param>
  365. /// <returns>
  366. /// The index of the selected button, or <see langword="null"/> if the user pressed
  367. /// <see cref="Application.QuitKey"/>.
  368. /// </returns>
  369. /// <exception cref="ArgumentNullException">Thrown if <paramref name="app"/> is <see langword="null"/>.</exception>
  370. /// <remarks>
  371. /// The MessageBox is centered and auto-sized based on title, message, and buttons.
  372. /// </remarks>
  373. public static int? Query (IApplication? app, string title, string message, params string [] buttons)
  374. {
  375. return QueryFull (
  376. app,
  377. false,
  378. 0,
  379. 0,
  380. title,
  381. message,
  382. 0,
  383. true,
  384. buttons);
  385. }
  386. /// <summary>
  387. /// Displays a <see cref="MessageBox"/> with fixed dimensions and a default button.
  388. /// </summary>
  389. /// <param name="app">The application instance. If <see langword="null"/>, uses <see cref="IApplication.TopRunnableView"/>.</param>
  390. /// <param name="width">Width for the MessageBox.</param>
  391. /// <param name="height">Height for the MessageBox.</param>
  392. /// <param name="title">Title for the MessageBox.</param>
  393. /// <param name="message">Message to display. May contain multiple lines and will be word-wrapped.</param>
  394. /// <param name="defaultButton">Index of the default button (0-based).</param>
  395. /// <param name="buttons">Array of button labels.</param>
  396. /// <returns>
  397. /// The index of the selected button, or <see langword="null"/> if the user pressed
  398. /// <see cref="Application.QuitKey"/>.
  399. /// </returns>
  400. /// <exception cref="ArgumentNullException">Thrown if <paramref name="app"/> is <see langword="null"/>.</exception>
  401. /// <remarks>
  402. /// Consider using <see cref="Query(IApplication?, string, string, int, string[])"/> which automatically sizes the
  403. /// MessageBox.
  404. /// </remarks>
  405. public static int? Query (
  406. IApplication? app,
  407. int width,
  408. int height,
  409. string title,
  410. string message,
  411. int defaultButton = 0,
  412. params string [] buttons
  413. )
  414. {
  415. return QueryFull (
  416. app,
  417. false,
  418. width,
  419. height,
  420. title,
  421. message,
  422. defaultButton,
  423. true,
  424. buttons);
  425. }
  426. /// <summary>
  427. /// Displays an auto-sized <see cref="MessageBox"/> with a default button.
  428. /// </summary>
  429. /// <param name="app">The application instance. If <see langword="null"/>, uses <see cref="IApplication.TopRunnableView"/>.</param>
  430. /// <param name="title">Title for the MessageBox.</param>
  431. /// <param name="message">Message to display. May contain multiple lines and will be word-wrapped.</param>
  432. /// <param name="defaultButton">Index of the default button (0-based).</param>
  433. /// <param name="buttons">Array of button labels.</param>
  434. /// <returns>
  435. /// The index of the selected button, or <see langword="null"/> if the user pressed
  436. /// <see cref="Application.QuitKey"/>.
  437. /// </returns>
  438. /// <exception cref="ArgumentNullException">Thrown if <paramref name="app"/> is <see langword="null"/>.</exception>
  439. /// <remarks>
  440. /// The MessageBox is centered and auto-sized based on title, message, and buttons.
  441. /// </remarks>
  442. public static int? Query (IApplication? app, string title, string message, int defaultButton = 0, params string [] buttons)
  443. {
  444. return QueryFull (
  445. app,
  446. false,
  447. 0,
  448. 0,
  449. title,
  450. message,
  451. defaultButton,
  452. true,
  453. buttons);
  454. }
  455. /// <summary>
  456. /// Displays a <see cref="MessageBox"/> with fixed dimensions, a default button, and word-wrap control.
  457. /// </summary>
  458. /// <param name="app">The application instance. If <see langword="null"/>, uses <see cref="IApplication.TopRunnableView"/>.</param>
  459. /// <param name="width">Width for the MessageBox.</param>
  460. /// <param name="height">Height for the MessageBox.</param>
  461. /// <param name="title">Title for the MessageBox.</param>
  462. /// <param name="message">Message to display. May contain multiple lines.</param>
  463. /// <param name="defaultButton">Index of the default button (0-based).</param>
  464. /// <param name="wrapMessage">
  465. /// If <see langword="true"/>, word-wraps the message; otherwise displays as-is with multi-line
  466. /// support.
  467. /// </param>
  468. /// <param name="buttons">Array of button labels.</param>
  469. /// <returns>
  470. /// The index of the selected button, or <see langword="null"/> if the user pressed
  471. /// <see cref="Application.QuitKey"/>.
  472. /// </returns>
  473. /// <exception cref="ArgumentNullException">Thrown if <paramref name="app"/> is <see langword="null"/>.</exception>
  474. /// <remarks>
  475. /// Consider using <see cref="Query(IApplication?, string, string, int, bool, string[])"/> which automatically sizes
  476. /// the MessageBox.
  477. /// </remarks>
  478. public static int? Query (
  479. IApplication? app,
  480. int width,
  481. int height,
  482. string title,
  483. string message,
  484. int defaultButton = 0,
  485. bool wrapMessage = true,
  486. params string [] buttons
  487. )
  488. {
  489. return QueryFull (
  490. app,
  491. false,
  492. width,
  493. height,
  494. title,
  495. message,
  496. defaultButton,
  497. wrapMessage,
  498. buttons);
  499. }
  500. /// <summary>
  501. /// Displays an auto-sized <see cref="MessageBox"/> with a default button and word-wrap control.
  502. /// </summary>
  503. /// <param name="app">The application instance. If <see langword="null"/>, uses <see cref="IApplication.TopRunnableView"/>.</param>
  504. /// <param name="title">Title for the MessageBox.</param>
  505. /// <param name="message">Message to display. May contain multiple lines.</param>
  506. /// <param name="defaultButton">Index of the default button (0-based).</param>
  507. /// <param name="wrapMessage">
  508. /// If <see langword="true"/>, word-wraps the message; otherwise displays as-is with multi-line
  509. /// support.
  510. /// </param>
  511. /// <param name="buttons">Array of button labels.</param>
  512. /// <returns>
  513. /// The index of the selected button, or <see langword="null"/> if the user pressed
  514. /// <see cref="Application.QuitKey"/>.
  515. /// </returns>
  516. /// <exception cref="ArgumentNullException">Thrown if <paramref name="app"/> is <see langword="null"/>.</exception>
  517. /// <remarks>
  518. /// The MessageBox is centered and auto-sized based on title, message, and buttons.
  519. /// </remarks>
  520. public static int? Query (
  521. IApplication? app,
  522. string title,
  523. string message,
  524. int defaultButton = 0,
  525. bool wrapMessage = true,
  526. params string [] buttons
  527. )
  528. {
  529. return QueryFull (
  530. app,
  531. false,
  532. 0,
  533. 0,
  534. title,
  535. message,
  536. defaultButton,
  537. wrapMessage,
  538. buttons);
  539. }
  540. private static int? QueryFull (
  541. IApplication? app,
  542. bool useErrorColors,
  543. int width,
  544. int height,
  545. string title,
  546. string message,
  547. int defaultButton = 0,
  548. bool wrapMessage = true,
  549. params string [] buttons
  550. )
  551. {
  552. ArgumentNullException.ThrowIfNull (app);
  553. // Create button array for Dialog
  554. var count = 0;
  555. List<Button> buttonList = new ();
  556. Clicked = null;
  557. if (buttons is { })
  558. {
  559. if (defaultButton > buttons.Length - 1)
  560. {
  561. defaultButton = buttons.Length - 1;
  562. }
  563. foreach (string s in buttons)
  564. {
  565. var b = new Button
  566. {
  567. Text = s,
  568. Data = count
  569. };
  570. if (count == defaultButton)
  571. {
  572. b.IsDefault = true;
  573. b.Accepting += (s, e) =>
  574. {
  575. if (e?.Context?.Source is Button button)
  576. {
  577. Clicked = (int)button.Data!;
  578. }
  579. else
  580. {
  581. Clicked = defaultButton;
  582. }
  583. if (e is { })
  584. {
  585. e.Handled = true;
  586. }
  587. (s as View)?.App?.RequestStop ();
  588. };
  589. }
  590. buttonList.Add (b);
  591. count++;
  592. }
  593. }
  594. var d = new Dialog
  595. {
  596. Title = title,
  597. ButtonAlignment = DefaultButtonAlignment,
  598. ButtonAlignmentModes = AlignmentModes.StartToEnd | AlignmentModes.AddSpaceBetweenItems,
  599. BorderStyle = DefaultBorderStyle,
  600. Buttons = buttonList.ToArray ()
  601. };
  602. d.Width = Dim.Auto (
  603. DimAutoStyle.Auto,
  604. Dim.Func (_ => (int)((app.Screen.Width - d.GetAdornmentsThickness ().Horizontal) * (DefaultMinimumWidth / 100f))),
  605. Dim.Func (_ => (int)((app.Screen.Width - d.GetAdornmentsThickness ().Horizontal) * 0.9f)));
  606. d.Height = Dim.Auto (
  607. DimAutoStyle.Auto,
  608. Dim.Func (_ => (int)((app.Screen.Height - d.GetAdornmentsThickness ().Vertical) * (DefaultMinimumHeight / 100f))),
  609. Dim.Func (_ => (int)((app.Screen.Height - d.GetAdornmentsThickness ().Vertical) * 0.9f)));
  610. if (width != 0)
  611. {
  612. d.Width = width;
  613. }
  614. if (height != 0)
  615. {
  616. d.Height = height;
  617. }
  618. d.SchemeName = useErrorColors ? SchemeManager.SchemesToSchemeName (Schemes.Error) : SchemeManager.SchemesToSchemeName (Schemes.Dialog);
  619. d.HotKeySpecifier = new ('\xFFFF');
  620. d.Text = message;
  621. d.TextAlignment = Alignment.Center;
  622. d.VerticalTextAlignment = Alignment.Start;
  623. d.TextFormatter.WordWrap = wrapMessage;
  624. d.TextFormatter.MultiLine = !wrapMessage;
  625. // Run the modal; do not shut down the mainloop driver when done
  626. app.Run (d);
  627. d.Dispose ();
  628. return Clicked;
  629. }
  630. }