Dialog.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. Arrangement = ViewArrangement.Movable;
  51. X = Pos.Center ();
  52. Y = Pos.Center ();
  53. ValidatePosDim = true;
  54. Width = Dim.Percent (85);
  55. Height = Dim.Percent (85);
  56. ColorScheme = Colors.ColorSchemes ["Dialog"];
  57. Modal = true;
  58. ButtonAlignment = DefaultButtonAlignment;
  59. AddCommand (Command.QuitToplevel, () =>
  60. {
  61. Canceled = true;
  62. RequestStop ();
  63. return true;
  64. });
  65. KeyBindings.Add (Key.Esc, Command.QuitToplevel);
  66. }
  67. private bool _canceled;
  68. /// <summary>Gets a value indicating whether the <see cref="Dialog"/> was canceled.</summary>
  69. /// <remarks>The default value is <see langword="true"/>.</remarks>
  70. public bool Canceled
  71. {
  72. get
  73. {
  74. #if DEBUG_IDISPOSABLE
  75. if (WasDisposed)
  76. {
  77. throw new ObjectDisposedException (GetType ().FullName);
  78. }
  79. #endif
  80. return _canceled;
  81. }
  82. set
  83. {
  84. #if DEBUG_IDISPOSABLE
  85. if (WasDisposed)
  86. {
  87. throw new ObjectDisposedException (GetType ().FullName);
  88. }
  89. #endif
  90. _canceled = value;
  91. return;
  92. }
  93. }
  94. /// <summary>Determines how the <see cref="Dialog"/> <see cref="Button"/>s are aligned along the bottom of the dialog.</summary>
  95. public ButtonAlignments ButtonAlignment { get; set; }
  96. /// <summary>Optional buttons to lay out at the bottom of the dialog.</summary>
  97. public Button [] Buttons
  98. {
  99. get => _buttons.ToArray ();
  100. init
  101. {
  102. if (value is null)
  103. {
  104. return;
  105. }
  106. foreach (Button b in value)
  107. {
  108. AddButton (b);
  109. }
  110. }
  111. }
  112. /// <summary>The default <see cref="ButtonAlignments"/> for <see cref="Dialog"/>.</summary>
  113. /// <remarks>This property can be set in a Theme.</remarks>
  114. [SerializableConfigurationProperty (Scope = typeof (ThemeScope))]
  115. [JsonConverter (typeof (JsonStringEnumConverter))]
  116. public static ButtonAlignments DefaultButtonAlignment { get; set; } = ButtonAlignments.Center;
  117. /// <summary>
  118. /// Adds a <see cref="Button"/> to the <see cref="Dialog"/>, its layout will be controlled by the
  119. /// <see cref="Dialog"/>
  120. /// </summary>
  121. /// <param name="button">Button to add.</param>
  122. public void AddButton (Button button)
  123. {
  124. if (button is null)
  125. {
  126. return;
  127. }
  128. //button.AutoSize = false; // BUGBUG: v2 - Hack to get around autosize not accounting for Margin?
  129. _buttons.Add (button);
  130. Add (button);
  131. SetNeedsDisplay ();
  132. if (IsInitialized)
  133. {
  134. LayoutSubviews ();
  135. }
  136. }
  137. /// <inheritdoc/>
  138. public override void LayoutSubviews ()
  139. {
  140. if (_inLayout)
  141. {
  142. return;
  143. }
  144. _inLayout = true;
  145. LayoutButtons ();
  146. base.LayoutSubviews ();
  147. _inLayout = false;
  148. }
  149. // Get the width of all buttons, not including any Margin.
  150. internal int GetButtonsWidth ()
  151. {
  152. if (_buttons.Count == 0)
  153. {
  154. return 0;
  155. }
  156. //var widths = buttons.Select (b => b.TextFormatter.GetFormattedSize ().Width + b.BorderFrame.Thickness.Horizontal + b.Padding.Thickness.Horizontal);
  157. IEnumerable<int> widths = _buttons.Select (b => b.Frame.Width);
  158. return widths.Sum ();
  159. }
  160. private void LayoutButtons ()
  161. {
  162. if (_buttons.Count == 0 || !IsInitialized)
  163. {
  164. return;
  165. }
  166. var shiftLeft = 0;
  167. int buttonsWidth = GetButtonsWidth ();
  168. switch (ButtonAlignment)
  169. {
  170. case ButtonAlignments.Center:
  171. // Center Buttons
  172. shiftLeft = (Viewport.Width - buttonsWidth - _buttons.Count - 1) / 2 + 1;
  173. for (int i = _buttons.Count - 1; i >= 0; i--)
  174. {
  175. Button button = _buttons [i];
  176. shiftLeft += button.Frame.Width + (i == _buttons.Count - 1 ? 0 : 1);
  177. if (shiftLeft > -1)
  178. {
  179. button.X = Pos.AnchorEnd (shiftLeft);
  180. }
  181. else
  182. {
  183. button.X = Viewport.Width - shiftLeft;
  184. }
  185. button.Y = Pos.AnchorEnd (1);
  186. }
  187. break;
  188. case ButtonAlignments.Justify:
  189. // Justify Buttons
  190. // leftmost and rightmost buttons are hard against edges. The rest are evenly spaced.
  191. var spacing = (int)Math.Ceiling ((double)(Viewport.Width - buttonsWidth) / (_buttons.Count - 1));
  192. for (int i = _buttons.Count - 1; i >= 0; i--)
  193. {
  194. Button button = _buttons [i];
  195. if (i == _buttons.Count - 1)
  196. {
  197. shiftLeft += button.Frame.Width;
  198. button.X = Pos.AnchorEnd (shiftLeft);
  199. }
  200. else
  201. {
  202. if (i == 0)
  203. {
  204. // first (leftmost) button
  205. int left = Viewport.Width;
  206. button.X = Pos.AnchorEnd (left);
  207. }
  208. else
  209. {
  210. shiftLeft += button.Frame.Width + spacing;
  211. button.X = Pos.AnchorEnd (shiftLeft);
  212. }
  213. }
  214. button.Y = Pos.AnchorEnd (1);
  215. }
  216. break;
  217. case ButtonAlignments.Left:
  218. // Left Align Buttons
  219. Button prevButton = _buttons [0];
  220. prevButton.X = 0;
  221. prevButton.Y = Pos.AnchorEnd (1);
  222. for (var i = 1; i < _buttons.Count; i++)
  223. {
  224. Button button = _buttons [i];
  225. button.X = Pos.Right (prevButton) + 1;
  226. button.Y = Pos.AnchorEnd (1);
  227. prevButton = button;
  228. }
  229. break;
  230. case ButtonAlignments.Right:
  231. // Right align buttons
  232. shiftLeft = _buttons [_buttons.Count - 1].Frame.Width;
  233. _buttons [_buttons.Count - 1].X = Pos.AnchorEnd (shiftLeft);
  234. _buttons [_buttons.Count - 1].Y = Pos.AnchorEnd (1);
  235. for (int i = _buttons.Count - 2; i >= 0; i--)
  236. {
  237. Button button = _buttons [i];
  238. shiftLeft += button.Frame.Width + 1;
  239. button.X = Pos.AnchorEnd (shiftLeft);
  240. button.Y = Pos.AnchorEnd (1);
  241. }
  242. break;
  243. }
  244. }
  245. }