Dialog.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. using System.Text.Json.Serialization;
  2. namespace Terminal.Gui;
  3. /// <summary>
  4. /// The <see cref="Dialog"/> <see cref="View"/> is a <see cref="Window"/> that by default is centered and contains
  5. /// one or more <see cref="Button"/>s. It defaults to the <c>Colors.ColorSchemes ["Dialog"]</c> color scheme and has a
  6. /// 1 cell padding around the edges.
  7. /// </summary>
  8. /// <remarks>
  9. /// To run the <see cref="Dialog"/> modally, create the <see cref="Dialog"/>, and pass it to
  10. /// <see cref="Application.Run(Toplevel, Func{Exception, bool}, ConsoleDriver)"/>. This will execute the dialog until it terminates via the
  11. /// [ESC] or [CTRL-Q] key, or when one of the views or buttons added to the dialog calls
  12. /// <see cref="Application.RequestStop"/>.
  13. /// </remarks>
  14. public class Dialog : Window
  15. {
  16. /// <summary>Determines the horizontal alignment of the Dialog buttons.</summary>
  17. public enum ButtonAlignments
  18. {
  19. /// <summary>Center-aligns the buttons (the default).</summary>
  20. Center = 0,
  21. /// <summary>Justifies the buttons</summary>
  22. Justify,
  23. /// <summary>Left-aligns the buttons</summary>
  24. Left,
  25. /// <summary>Right-aligns the buttons</summary>
  26. Right
  27. }
  28. // TODO: Reenable once border/borderframe design is settled
  29. /// <summary>
  30. /// Defines the default border styling for <see cref="Dialog"/>. Can be configured via
  31. /// <see cref="ConfigurationManager"/>.
  32. /// </summary>
  33. //[SerializableConfigurationProperty (Scope = typeof (ThemeScope))]
  34. //public static Border DefaultBorder { get; set; } = new Border () {
  35. // LineStyle = LineStyle.Single,
  36. //};
  37. private readonly List<Button> _buttons = new ();
  38. private bool _inLayout;
  39. /// <summary>
  40. /// Initializes a new instance of the <see cref="Dialog"/> class using <see cref="LayoutStyle.Computed"/>
  41. /// positioning with no <see cref="Button"/>s.
  42. /// </summary>
  43. /// <remarks>
  44. /// By default, <see cref="View.X"/> and <see cref="View.Y"/> are set to <c>Pos.Center ()</c> and
  45. /// <see cref="View.Width"/> and <see cref="View.Height"/> are set to <c>Width = Dim.Percent (85)</c>, centering the
  46. /// Dialog vertically and horizontally.
  47. /// </remarks>
  48. public Dialog ()
  49. {
  50. X = Pos.Center ();
  51. Y = Pos.Center ();
  52. ValidatePosDim = true;
  53. #if DIMAUTO
  54. Width = Dim.Auto (min: Dim.Percent (10));
  55. Height = Dim.Auto (min: Dim.Percent (50));
  56. #else
  57. Width = Dim.Percent (85);
  58. Height = Dim.Percent (85);
  59. #endif
  60. ColorScheme = Colors.ColorSchemes ["Dialog"];
  61. Modal = true;
  62. ButtonAlignment = DefaultButtonAlignment;
  63. AddCommand (Command.QuitToplevel, () =>
  64. {
  65. Canceled = true;
  66. RequestStop ();
  67. return true;
  68. });
  69. KeyBindings.Add (Key.Esc, Command.QuitToplevel);
  70. }
  71. private bool _canceled;
  72. /// <summary>Gets a value indicating whether the <see cref="Dialog"/> was canceled.</summary>
  73. /// <remarks>The default value is <see langword="true"/>.</remarks>
  74. public bool Canceled
  75. {
  76. get
  77. {
  78. #if DEBUG_IDISPOSABLE
  79. if (WasDisposed)
  80. {
  81. throw new ObjectDisposedException (GetType ().FullName);
  82. }
  83. #endif
  84. return _canceled;
  85. }
  86. set
  87. {
  88. #if DEBUG_IDISPOSABLE
  89. if (WasDisposed)
  90. {
  91. throw new ObjectDisposedException (GetType ().FullName);
  92. }
  93. #endif
  94. _canceled = value;
  95. return;
  96. }
  97. }
  98. /// <summary>Determines how the <see cref="Dialog"/> <see cref="Button"/>s are aligned along the bottom of the dialog.</summary>
  99. public ButtonAlignments ButtonAlignment { get; set; }
  100. /// <summary>Optional buttons to lay out at the bottom of the dialog.</summary>
  101. public Button [] Buttons
  102. {
  103. get => _buttons.ToArray ();
  104. init
  105. {
  106. if (value is null)
  107. {
  108. return;
  109. }
  110. foreach (Button b in value)
  111. {
  112. AddButton (b);
  113. }
  114. }
  115. }
  116. /// <summary>The default <see cref="ButtonAlignments"/> for <see cref="Dialog"/>.</summary>
  117. /// <remarks>This property can be set in a Theme.</remarks>
  118. [SerializableConfigurationProperty (Scope = typeof (ThemeScope))]
  119. [JsonConverter (typeof (JsonStringEnumConverter))]
  120. public static ButtonAlignments DefaultButtonAlignment { get; set; } = ButtonAlignments.Center;
  121. /// <summary>
  122. /// Adds a <see cref="Button"/> to the <see cref="Dialog"/>, its layout will be controlled by the
  123. /// <see cref="Dialog"/>
  124. /// </summary>
  125. /// <param name="button">Button to add.</param>
  126. public void AddButton (Button button)
  127. {
  128. if (button is null)
  129. {
  130. return;
  131. }
  132. //button.AutoSize = false; // BUGBUG: v2 - Hack to get around autosize not accounting for Margin?
  133. _buttons.Add (button);
  134. Add (button);
  135. SetNeedsDisplay ();
  136. if (IsInitialized)
  137. {
  138. LayoutSubviews ();
  139. }
  140. }
  141. /// <inheritdoc/>
  142. public override void LayoutSubviews ()
  143. {
  144. if (_inLayout)
  145. {
  146. return;
  147. }
  148. _inLayout = true;
  149. LayoutButtons ();
  150. base.LayoutSubviews ();
  151. _inLayout = false;
  152. }
  153. // Get the width of all buttons, not including any Margin.
  154. internal int GetButtonsWidth ()
  155. {
  156. if (_buttons.Count == 0)
  157. {
  158. return 0;
  159. }
  160. //var widths = buttons.Select (b => b.TextFormatter.GetFormattedSize ().Width + b.BorderFrame.Thickness.Horizontal + b.Padding.Thickness.Horizontal);
  161. IEnumerable<int> widths = _buttons.Select (b => b.Frame.Width);
  162. return widths.Sum ();
  163. }
  164. private void LayoutButtons ()
  165. {
  166. if (_buttons.Count == 0 || !IsInitialized)
  167. {
  168. return;
  169. }
  170. var shiftLeft = 0;
  171. int buttonsWidth = GetButtonsWidth ();
  172. switch (ButtonAlignment)
  173. {
  174. case ButtonAlignments.Center:
  175. // Center Buttons
  176. shiftLeft = (Viewport.Width - buttonsWidth - _buttons.Count - 1) / 2 + 1;
  177. for (int i = _buttons.Count - 1; i >= 0; i--)
  178. {
  179. Button button = _buttons [i];
  180. shiftLeft += button.Frame.Width + (i == _buttons.Count - 1 ? 0 : 1);
  181. if (shiftLeft > -1)
  182. {
  183. button.X = Pos.AnchorEnd (shiftLeft);
  184. }
  185. else
  186. {
  187. button.X = Viewport.Width - shiftLeft;
  188. }
  189. button.Y = Pos.AnchorEnd (1);
  190. }
  191. break;
  192. case ButtonAlignments.Justify:
  193. // Justify Buttons
  194. // leftmost and rightmost buttons are hard against edges. The rest are evenly spaced.
  195. var spacing = (int)Math.Ceiling ((double)(Viewport.Width - buttonsWidth) / (_buttons.Count - 1));
  196. for (int i = _buttons.Count - 1; i >= 0; i--)
  197. {
  198. Button button = _buttons [i];
  199. if (i == _buttons.Count - 1)
  200. {
  201. shiftLeft += button.Frame.Width;
  202. button.X = Pos.AnchorEnd (shiftLeft);
  203. }
  204. else
  205. {
  206. if (i == 0)
  207. {
  208. // first (leftmost) button
  209. int left = Viewport.Width;
  210. button.X = Pos.AnchorEnd (left);
  211. }
  212. else
  213. {
  214. shiftLeft += button.Frame.Width + spacing;
  215. button.X = Pos.AnchorEnd (shiftLeft);
  216. }
  217. }
  218. button.Y = Pos.AnchorEnd (1);
  219. }
  220. break;
  221. case ButtonAlignments.Left:
  222. // Left Align Buttons
  223. Button prevButton = _buttons [0];
  224. prevButton.X = 0;
  225. prevButton.Y = Pos.AnchorEnd (1);
  226. for (var i = 1; i < _buttons.Count; i++)
  227. {
  228. Button button = _buttons [i];
  229. button.X = Pos.Right (prevButton) + 1;
  230. button.Y = Pos.AnchorEnd (1);
  231. prevButton = button;
  232. }
  233. break;
  234. case ButtonAlignments.Right:
  235. // Right align buttons
  236. shiftLeft = _buttons [_buttons.Count - 1].Frame.Width;
  237. _buttons [_buttons.Count - 1].X = Pos.AnchorEnd (shiftLeft);
  238. _buttons [_buttons.Count - 1].Y = Pos.AnchorEnd (1);
  239. for (int i = _buttons.Count - 2; i >= 0; i--)
  240. {
  241. Button button = _buttons [i];
  242. shiftLeft += button.Frame.Width + 1;
  243. button.X = Pos.AnchorEnd (shiftLeft);
  244. button.Y = Pos.AnchorEnd (1);
  245. }
  246. break;
  247. }
  248. }
  249. }