Dialog.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. //
  2. // Dialog.cs: Dialog box
  3. //
  4. // Authors:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text.Json.Serialization;
  11. using NStack;
  12. using Terminal.Gui;
  13. using static Terminal.Gui.ConfigurationManager;
  14. namespace Terminal.Gui {
  15. /// <summary>
  16. /// The <see cref="Dialog"/> <see cref="View"/> is a <see cref="Window"/> that by default is centered and contains one
  17. /// 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.
  18. /// </summary>
  19. /// <remarks>
  20. /// To run the <see cref="Dialog"/> modally, create the <see cref="Dialog"/>, and pass it to <see cref="Application.Run(Func{Exception, bool})"/>.
  21. /// This will execute the dialog until it terminates via the [ESC] or [CTRL-Q] key, or when one of the views
  22. /// or buttons added to the dialog calls <see cref="Application.RequestStop"/>.
  23. /// </remarks>
  24. public class Dialog : Window {
  25. /// <summary>
  26. /// The default <see cref="ButtonAlignments"/> for <see cref="Dialog"/>.
  27. /// </summary>
  28. /// <remarks>
  29. /// This property can be set in a Theme.
  30. /// </remarks>
  31. [SerializableConfigurationProperty (Scope = typeof (ThemeScope)), JsonConverter (typeof (JsonStringEnumConverter))]
  32. public static ButtonAlignments DefaultButtonAlignment { get; set; } = ButtonAlignments.Center;
  33. // TODO: Reenable once border/borderframe design is settled
  34. /// <summary>
  35. /// Defines the default border styling for <see cref="Dialog"/>. Can be configured via <see cref="ConfigurationManager"/>.
  36. /// </summary>
  37. //[SerializableConfigurationProperty (Scope = typeof (ThemeScope))]
  38. //public static Border DefaultBorder { get; set; } = new Border () {
  39. // LineStyle = LineStyle.Single,
  40. //};
  41. internal List<Button> buttons = new List<Button> ();
  42. /// <summary>
  43. /// Initializes a new instance of the <see cref="Dialog"/> class using <see cref="LayoutStyle.Computed"/> positioning
  44. /// with no <see cref="Button"/>s.
  45. /// </summary>
  46. /// <remarks>
  47. /// 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
  48. /// to <c>Width = Dim.Percent (85)</c>, centering the Dialog vertically and horizontally.
  49. /// </remarks>
  50. public Dialog () : this (null) { }
  51. /// <summary>
  52. /// Initializes a new instance of the <see cref="Dialog"/> class using <see cref="LayoutStyle.Computed"/> positioning
  53. /// and an optional set of <see cref="Button"/>s to display
  54. /// </summary>
  55. /// <param name="buttons">Optional buttons to lay out at the bottom of the dialog.</param>
  56. /// <remarks>
  57. /// 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
  58. /// to <c>Width = Dim.Percent (85)</c>, centering the Dialog vertically and horizontally.
  59. /// </remarks>
  60. public Dialog (params Button [] buttons) : base ()
  61. {
  62. SetInitialProperties (buttons);
  63. }
  64. private void SetInitialProperties (Button [] buttons)
  65. {
  66. X = Pos.Center ();
  67. Y = Pos.Center ();
  68. Width = Dim.Percent (85);
  69. Height = Dim.Percent (85);
  70. ColorScheme = Colors.Dialog;
  71. Modal = true;
  72. ButtonAlignment = DefaultButtonAlignment;
  73. if (buttons != null) {
  74. foreach (var b in buttons) {
  75. AddButton (b);
  76. }
  77. }
  78. LayoutComplete += (s, args) => {
  79. LayoutButtons ();
  80. };
  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. LayoutSubviews ();
  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) return;
  136. int shiftLeft = 0;
  137. int buttonsWidth = GetButtonsWidth ();
  138. switch (ButtonAlignment) {
  139. case ButtonAlignments.Center:
  140. // Center Buttons
  141. shiftLeft = (Bounds.Width - buttonsWidth - buttons.Count - 1) / 2 + 1;
  142. for (int i = buttons.Count - 1; i >= 0; i--) {
  143. Button button = buttons [i];
  144. shiftLeft += button.Frame.Width + (i == buttons.Count - 1 ? 0 : 1);
  145. if (shiftLeft > -1) {
  146. button.X = Pos.AnchorEnd (shiftLeft);
  147. } else {
  148. button.X = Bounds.Width - shiftLeft;
  149. }
  150. button.Y = Pos.AnchorEnd (1);
  151. }
  152. break;
  153. case ButtonAlignments.Justify:
  154. // Justify Buttons
  155. // leftmost and rightmost buttons are hard against edges. The rest are evenly spaced.
  156. var spacing = (int)Math.Ceiling ((double)(Bounds.Width - buttonsWidth) / (buttons.Count - 1));
  157. for (int i = buttons.Count - 1; i >= 0; i--) {
  158. Button button = buttons [i];
  159. if (i == buttons.Count - 1) {
  160. shiftLeft += button.Frame.Width;
  161. button.X = Pos.AnchorEnd (shiftLeft);
  162. } else {
  163. if (i == 0) {
  164. // first (leftmost) button
  165. var left = Bounds.Width;
  166. button.X = Pos.AnchorEnd (left);
  167. } else {
  168. shiftLeft += button.Frame.Width + (spacing);
  169. button.X = Pos.AnchorEnd (shiftLeft);
  170. }
  171. }
  172. button.Y = Pos.AnchorEnd (1);
  173. }
  174. break;
  175. case ButtonAlignments.Left:
  176. // Left Align Buttons
  177. var prevButton = buttons [0];
  178. prevButton.X = 0;
  179. prevButton.Y = Pos.AnchorEnd (1);
  180. for (int i = 1; i < buttons.Count; i++) {
  181. Button button = buttons [i];
  182. button.X = Pos.Right (prevButton) + 1;
  183. button.Y = Pos.AnchorEnd (1);
  184. prevButton = button;
  185. }
  186. break;
  187. case ButtonAlignments.Right:
  188. // Right align buttons
  189. shiftLeft = buttons [buttons.Count - 1].Frame.Width;
  190. buttons [buttons.Count - 1].X = Pos.AnchorEnd (shiftLeft);
  191. buttons [buttons.Count - 1].Y = Pos.AnchorEnd (1);
  192. for (int i = buttons.Count - 2; i >= 0; i--) {
  193. Button button = buttons [i];
  194. shiftLeft += button.Frame.Width + 1;
  195. button.X = Pos.AnchorEnd (shiftLeft);
  196. button.Y = Pos.AnchorEnd (1);
  197. }
  198. break;
  199. }
  200. }
  201. ///<inheritdoc/>
  202. public override bool ProcessKey (KeyEvent kb)
  203. {
  204. switch (kb.Key) {
  205. case Key.Esc:
  206. Application.RequestStop (this);
  207. return true;
  208. }
  209. return base.ProcessKey (kb);
  210. }
  211. }
  212. }