Dialog.cs 7.2 KB

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