Dialog.cs 7.2 KB

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