Dialog.cs 9.4 KB

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