Dialog.cs 7.0 KB

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