Dialog.cs 7.0 KB

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