Dialog.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. // TODO: Update button.X = Pos.Justify when alignment changes
  83. /// <summary>Determines how the <see cref="Dialog"/> <see cref="Button"/>s are aligned along the bottom of the dialog.</summary>
  84. public Justification ButtonAlignment { get; set; }
  85. /// <summary>Optional buttons to lay out at the bottom of the dialog.</summary>
  86. public Button [] Buttons
  87. {
  88. get => _buttons.ToArray ();
  89. init
  90. {
  91. if (value is null)
  92. {
  93. return;
  94. }
  95. foreach (Button b in value)
  96. {
  97. AddButton (b);
  98. }
  99. }
  100. }
  101. /// <summary>The default <see cref="Justification"/> for <see cref="Dialog"/>.</summary>
  102. /// <remarks>This property can be set in a Theme.</remarks>
  103. [SerializableConfigurationProperty (Scope = typeof (ThemeScope))]
  104. [JsonConverter (typeof (JsonStringEnumConverter))]
  105. public static Justification DefaultButtonAlignment { get; set; } = Justification.Centered;
  106. /// <summary>
  107. /// Adds a <see cref="Button"/> to the <see cref="Dialog"/>, its layout will be controlled by the
  108. /// <see cref="Dialog"/>
  109. /// </summary>
  110. /// <param name="button">Button to add.</param>
  111. public void AddButton (Button button)
  112. {
  113. if (button is null)
  114. {
  115. return;
  116. }
  117. button.X = Pos.Justify (ButtonAlignment);
  118. button.Y = Pos.AnchorEnd () - 1;
  119. _buttons.Add (button);
  120. Add (button);
  121. SetNeedsDisplay ();
  122. if (IsInitialized)
  123. {
  124. LayoutSubviews ();
  125. }
  126. }
  127. /// <inheritdoc/>
  128. //public override void LayoutSubviews ()
  129. //{
  130. // if (_inLayout)
  131. // {
  132. // return;
  133. // }
  134. // _inLayout = true;
  135. // //LayoutButtons ();
  136. // base.LayoutSubviews ();
  137. // _inLayout = false;
  138. //}
  139. // Get the width of all buttons, not including any Margin.
  140. internal int GetButtonsWidth ()
  141. {
  142. if (_buttons.Count == 0)
  143. {
  144. return 0;
  145. }
  146. //var widths = buttons.Select (b => b.TextFormatter.GetFormattedSize ().Width + b.BorderFrame.Thickness.Horizontal + b.Padding.Thickness.Horizontal);
  147. IEnumerable<int> widths = _buttons.Select (b => b.Frame.Width);
  148. return widths.Sum ();
  149. }
  150. //private void LayoutButtons ()
  151. //{
  152. // if (_buttons.Count == 0 || !IsInitialized)
  153. // {
  154. // return;
  155. // }
  156. // var shiftLeft = 0;
  157. // int buttonsWidth = GetButtonsWidth ();
  158. // switch (ButtonAlignment)
  159. // {
  160. // case Justification.Centered:
  161. // // Center Buttons
  162. // shiftLeft = (Viewport.Width - buttonsWidth - _buttons.Count - 1) / 2 + 1;
  163. // for (int i = _buttons.Count - 1; i >= 0; i--)
  164. // {
  165. // Button button = _buttons [i];
  166. // shiftLeft += button.Frame.Width + (i == _buttons.Count - 1 ? 0 : 1);
  167. // if (shiftLeft > -1)
  168. // {
  169. // button.X = Pos.AnchorEnd (shiftLeft);
  170. // }
  171. // else
  172. // {
  173. // button.X = Viewport.Width - shiftLeft;
  174. // }
  175. // button.Y = Pos.AnchorEnd (1);
  176. // }
  177. // break;
  178. // case Justification.Justified:
  179. // // Justify Buttons
  180. // // leftmost and rightmost buttons are hard against edges. The rest are evenly spaced.
  181. // var spacing = (int)Math.Ceiling ((double)(Viewport.Width - buttonsWidth) / (_buttons.Count - 1));
  182. // for (int i = _buttons.Count - 1; i >= 0; i--)
  183. // {
  184. // Button button = _buttons [i];
  185. // if (i == _buttons.Count - 1)
  186. // {
  187. // shiftLeft += button.Frame.Width;
  188. // button.X = Pos.AnchorEnd (shiftLeft);
  189. // }
  190. // else
  191. // {
  192. // if (i == 0)
  193. // {
  194. // // first (leftmost) button
  195. // int left = Viewport.Width;
  196. // button.X = Pos.AnchorEnd (left);
  197. // }
  198. // else
  199. // {
  200. // shiftLeft += button.Frame.Width + spacing;
  201. // button.X = Pos.AnchorEnd (shiftLeft);
  202. // }
  203. // }
  204. // button.Y = Pos.AnchorEnd (1);
  205. // }
  206. // break;
  207. // case Justification.Left:
  208. // // Left Align Buttons
  209. // Button prevButton = _buttons [0];
  210. // prevButton.X = 0;
  211. // prevButton.Y = Pos.AnchorEnd (1);
  212. // for (var i = 1; i < _buttons.Count; i++)
  213. // {
  214. // Button button = _buttons [i];
  215. // button.X = Pos.Right (prevButton) + 1;
  216. // button.Y = Pos.AnchorEnd (1);
  217. // prevButton = button;
  218. // }
  219. // break;
  220. // case Justification.Right:
  221. // // Right align buttons
  222. // shiftLeft = _buttons [_buttons.Count - 1].Frame.Width;
  223. // _buttons [_buttons.Count - 1].X = Pos.AnchorEnd (shiftLeft);
  224. // _buttons [_buttons.Count - 1].Y = Pos.AnchorEnd (1);
  225. // for (int i = _buttons.Count - 2; i >= 0; i--)
  226. // {
  227. // Button button = _buttons [i];
  228. // shiftLeft += button.Frame.Width + 1;
  229. // button.X = Pos.AnchorEnd (shiftLeft);
  230. // button.Y = Pos.AnchorEnd (1);
  231. // }
  232. // break;
  233. // }
  234. //}
  235. }