Dialog.cs 8.8 KB

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