Dialog.cs 8.3 KB

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