Dialog.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 System.Text;
  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. if (IsInitialized) {
  96. LayoutSubviews ();
  97. }
  98. }
  99. // Get the width of all buttons, not including any Margin.
  100. internal int GetButtonsWidth ()
  101. {
  102. if (buttons.Count == 0) {
  103. return 0;
  104. }
  105. //var widths = buttons.Select (b => b.TextFormatter.GetFormattedSize ().Width + b.BorderFrame.Thickness.Horizontal + b.Padding.Thickness.Horizontal);
  106. var widths = buttons.Select (b => b.Frame.Width);
  107. return widths.Sum ();
  108. }
  109. /// <summary>
  110. /// Determines the horizontal alignment of the Dialog buttons.
  111. /// </summary>
  112. public enum ButtonAlignments {
  113. /// <summary>
  114. /// Center-aligns the buttons (the default).
  115. /// </summary>
  116. Center = 0,
  117. /// <summary>
  118. /// Justifies the buttons
  119. /// </summary>
  120. Justify,
  121. /// <summary>
  122. /// Left-aligns the buttons
  123. /// </summary>
  124. Left,
  125. /// <summary>
  126. /// Right-aligns the buttons
  127. /// </summary>
  128. Right
  129. }
  130. /// <summary>
  131. /// Determines how the <see cref="Dialog"/> <see cref="Button"/>s are aligned along the
  132. /// bottom of the dialog.
  133. /// </summary>
  134. public ButtonAlignments ButtonAlignment { get; set; }
  135. void LayoutButtons ()
  136. {
  137. if (buttons.Count == 0 || !IsInitialized) return;
  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. Button 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. var spacing = (int)Math.Ceiling ((double)(Bounds.Width - buttonsWidth) / (buttons.Count - 1));
  159. for (int i = buttons.Count - 1; i >= 0; i--) {
  160. Button 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. var 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. Button 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. Button 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. ///<inheritdoc/>
  204. public override bool ProcessKey (KeyEvent kb)
  205. {
  206. switch (kb.Key) {
  207. case Key.Esc:
  208. Application.RequestStop (this);
  209. return true;
  210. }
  211. return base.ProcessKey (kb);
  212. }
  213. }
  214. }