Dialog.cs 8.3 KB

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