Dialog.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.Json.Serialization;
  5. namespace Terminal.Gui;
  6. /// <summary>
  7. /// The <see cref="Dialog"/> <see cref="View"/> is a <see cref="Window"/> that by default is centered and contains one
  8. /// or more <see cref="Button"/>s. It defaults to the <c>Colors.ColorSchemes ["Dialog"]</c> color scheme and has a 1 cell padding around the edges.
  9. /// </summary>
  10. /// <remarks>
  11. /// To run the <see cref="Dialog"/> modally, create the <see cref="Dialog"/>, and pass it to <see cref="Application.Run(Func{Exception, bool})"/>.
  12. /// This will execute the dialog until it terminates via the [ESC] or [CTRL-Q] key, or when one of the views
  13. /// or buttons added to the dialog calls <see cref="Application.RequestStop"/>.
  14. /// </remarks>
  15. public class Dialog : Window {
  16. /// <summary>
  17. /// The default <see cref="ButtonAlignments"/> for <see cref="Dialog"/>.
  18. /// </summary>
  19. /// <remarks>
  20. /// This property can be set in a Theme.
  21. /// </remarks>
  22. [SerializableConfigurationProperty (Scope = typeof (ThemeScope))]
  23. [JsonConverter (typeof (JsonStringEnumConverter))]
  24. public static ButtonAlignments DefaultButtonAlignment { get; set; } = ButtonAlignments.Center;
  25. // TODO: Reenable once border/borderframe design is settled
  26. /// <summary>
  27. /// Defines the default border styling for <see cref="Dialog"/>. Can be configured via <see cref="ConfigurationManager"/>.
  28. /// </summary>
  29. //[SerializableConfigurationProperty (Scope = typeof (ThemeScope))]
  30. //public static Border DefaultBorder { get; set; } = new Border () {
  31. // LineStyle = LineStyle.Single,
  32. //};
  33. internal List<Button> buttons = new ();
  34. /// <summary>
  35. /// Initializes a new instance of the <see cref="Dialog"/> class using <see cref="LayoutStyle.Computed"/> positioning
  36. /// with no <see cref="Button"/>s.
  37. /// </summary>
  38. /// <remarks>
  39. /// By default, <see cref="View.X"/> and <see cref="View.Y"/> are set to <c>Pos.Center ()</c> and <see cref="View.Width"/> and <see cref="View.Height"/> are set
  40. /// to <c>Width = Dim.Percent (85)</c>, centering the Dialog vertically and horizontally.
  41. /// </remarks>
  42. public Dialog () : this (null) { }
  43. /// <summary>
  44. /// Initializes a new instance of the <see cref="Dialog"/> class using <see cref="LayoutStyle.Computed"/> positioning
  45. /// and an optional set of <see cref="Button"/>s to display
  46. /// </summary>
  47. /// <param name="buttons">Optional buttons to lay out at the bottom of the dialog.</param>
  48. /// <remarks>
  49. /// By default, <see cref="View.X"/> and <see cref="View.Y"/> are set to <c>Pos.Center ()</c> and <see cref="View.Width"/> and <see cref="View.Height"/> are set
  50. /// to <c>Width = Dim.Percent (85)</c>, centering the Dialog vertically and horizontally.
  51. /// </remarks>
  52. public Dialog (params Button [] buttons) : base () => SetInitialProperties (buttons);
  53. void SetInitialProperties (Button [] buttons)
  54. {
  55. X = Pos.Center ();
  56. Y = Pos.Center ();
  57. ValidatePosDim = true;
  58. Width = Dim.Percent (85);// Dim.Auto (min: Dim.Percent (10));
  59. Height = Dim.Percent (85);//Dim.Auto (min: Dim.Percent (50));
  60. ColorScheme = Colors.ColorSchemes ["Dialog"];
  61. Modal = true;
  62. ButtonAlignment = DefaultButtonAlignment;
  63. KeyBindings.Add (Key.Esc, Command.QuitToplevel);
  64. if (buttons != null) {
  65. foreach (var b in buttons) {
  66. AddButton (b);
  67. }
  68. }
  69. }
  70. bool inLayout = false;
  71. /// <inheritdoc />
  72. public override void LayoutSubviews ()
  73. {
  74. if (inLayout) {
  75. return;
  76. }
  77. inLayout = true;
  78. LayoutButtons ();
  79. base.LayoutSubviews ();
  80. inLayout = false;
  81. }
  82. /// <summary>
  83. /// Adds a <see cref="Button"/> to the <see cref="Dialog"/>, its layout will be controlled by the <see cref="Dialog"/>
  84. /// </summary>
  85. /// <param name="button">Button to add.</param>
  86. public void AddButton (Button button)
  87. {
  88. if (button == null) {
  89. return;
  90. }
  91. //button.AutoSize = false; // BUGBUG: v2 - Hack to get around autosize not accounting for Margin?
  92. buttons.Add (button);
  93. Add (button);
  94. SetNeedsDisplay ();
  95. if (IsInitialized) {
  96. LayoutSubviews ();
  97. }
  98. }
  99. // Get the width of all buttons, not including any Margin.
  100. internal int GetButtonsWidth ()
  101. {
  102. if (buttons.Count == 0) {
  103. return 0;
  104. }
  105. //var widths = buttons.Select (b => b.TextFormatter.GetFormattedSize ().Width + b.BorderFrame.Thickness.Horizontal + b.Padding.Thickness.Horizontal);
  106. var widths = buttons.Select (b => b.Frame.Width);
  107. return widths.Sum ();
  108. }
  109. /// <summary>
  110. /// Determines the horizontal alignment of the Dialog buttons.
  111. /// </summary>
  112. public enum ButtonAlignments {
  113. /// <summary>
  114. /// Center-aligns the buttons (the default).
  115. /// </summary>
  116. Center = 0,
  117. /// <summary>
  118. /// Justifies the buttons
  119. /// </summary>
  120. Justify,
  121. /// <summary>
  122. /// Left-aligns the buttons
  123. /// </summary>
  124. Left,
  125. /// <summary>
  126. /// Right-aligns the buttons
  127. /// </summary>
  128. Right
  129. }
  130. /// <summary>
  131. /// Determines how the <see cref="Dialog"/> <see cref="Button"/>s are aligned along the
  132. /// bottom of the dialog.
  133. /// </summary>
  134. public ButtonAlignments ButtonAlignment { get; set; }
  135. void LayoutButtons ()
  136. {
  137. if (buttons.Count == 0 || !IsInitialized) {
  138. return;
  139. }
  140. int shiftLeft = 0;
  141. int buttonsWidth = GetButtonsWidth ();
  142. switch (ButtonAlignment) {
  143. case ButtonAlignments.Center:
  144. // Center Buttons
  145. shiftLeft = (Bounds.Width - buttonsWidth - buttons.Count - 1) / 2 + 1;
  146. for (int i = buttons.Count - 1; i >= 0; i--) {
  147. var button = buttons [i];
  148. shiftLeft += button.Frame.Width + (i == buttons.Count - 1 ? 0 : 1);
  149. if (shiftLeft > -1) {
  150. button.X = Pos.AnchorEnd (shiftLeft);
  151. } else {
  152. button.X = Bounds.Width - shiftLeft;
  153. }
  154. button.Y = Pos.AnchorEnd (1);
  155. }
  156. break;
  157. case ButtonAlignments.Justify:
  158. // Justify Buttons
  159. // leftmost and rightmost buttons are hard against edges. The rest are evenly spaced.
  160. int spacing = (int)Math.Ceiling ((double)(Bounds.Width - buttonsWidth) / (buttons.Count - 1));
  161. for (int i = buttons.Count - 1; i >= 0; i--) {
  162. var button = buttons [i];
  163. if (i == buttons.Count - 1) {
  164. shiftLeft += button.Frame.Width;
  165. button.X = Pos.AnchorEnd (shiftLeft);
  166. } else {
  167. if (i == 0) {
  168. // first (leftmost) button
  169. int left = Bounds.Width;
  170. button.X = Pos.AnchorEnd (left);
  171. } else {
  172. shiftLeft += button.Frame.Width + spacing;
  173. button.X = Pos.AnchorEnd (shiftLeft);
  174. }
  175. }
  176. button.Y = Pos.AnchorEnd (1);
  177. }
  178. break;
  179. case ButtonAlignments.Left:
  180. // Left Align Buttons
  181. var prevButton = buttons [0];
  182. prevButton.X = 0;
  183. prevButton.Y = Pos.AnchorEnd (1);
  184. for (int i = 1; i < buttons.Count; i++) {
  185. var button = buttons [i];
  186. button.X = Pos.Right (prevButton) + 1;
  187. button.Y = Pos.AnchorEnd (1);
  188. prevButton = button;
  189. }
  190. break;
  191. case ButtonAlignments.Right:
  192. // Right align buttons
  193. shiftLeft = buttons [buttons.Count - 1].Frame.Width;
  194. buttons [buttons.Count - 1].X = Pos.AnchorEnd (shiftLeft);
  195. buttons [buttons.Count - 1].Y = Pos.AnchorEnd (1);
  196. for (int i = buttons.Count - 2; i >= 0; i--) {
  197. var button = buttons [i];
  198. shiftLeft += button.Frame.Width + 1;
  199. button.X = Pos.AnchorEnd (shiftLeft);
  200. button.Y = Pos.AnchorEnd (1);
  201. }
  202. break;
  203. }
  204. }
  205. }