Dialog.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. /// <summary>
  34. /// Defines the default border styling for <see cref="Dialog"/>. Can be configured via <see cref="ConfigurationManager"/>.
  35. /// </summary>
  36. [SerializableConfigurationProperty (Scope = typeof (ThemeScope))]
  37. public static Border DefaultBorder { get; set; } = new Border () {
  38. BorderStyle = BorderStyle.Single,
  39. };
  40. internal List<Button> buttons = new List<Button> ();
  41. /// <summary>
  42. /// Initializes a new instance of the <see cref="Dialog"/> class using <see cref="LayoutStyle.Computed"/> positioning
  43. /// and an optional set of <see cref="Button"/>s to display
  44. /// </summary>
  45. /// <param name="title">Title for the dialog.</param>
  46. /// <param name="width">Width for the dialog.</param>
  47. /// <param name="height">Height for the dialog.</param>
  48. /// <param name="buttons">Optional buttons to lay out at the bottom of the dialog.</param>
  49. /// <remarks>
  50. /// if <c>width</c> and <c>height</c> are both 0, the Dialog will be vertically and horizontally centered in the
  51. /// container and the size will be 85% of the container.
  52. /// After initialization use <c>X</c>, <c>Y</c>, <c>Width</c>, and <c>Height</c> to override this with a location or size.
  53. /// </remarks>
  54. /// <remarks>
  55. /// Use the constructor that does not take a <c>width</c> and <c>height</c> instead.
  56. /// </remarks>
  57. public Dialog (ustring title, int width, int height, params Button [] buttons) : base (title: title, padding: 0, border: DefaultBorder)
  58. {
  59. SetInitialProperties (width, height, buttons);
  60. }
  61. private void SetInitialProperties (int width, int height, Button [] buttons)
  62. {
  63. X = Pos.Center ();
  64. Y = Pos.Center ();
  65. if (width == 0 & height == 0) {
  66. Width = Dim.Percent (85);
  67. Height = Dim.Percent (85);
  68. } else {
  69. Width = width;
  70. Height = height;
  71. }
  72. ColorScheme = Colors.Dialog;
  73. Modal = true;
  74. ButtonAlignment = DefaultButtonAlignment;
  75. if (buttons != null) {
  76. foreach (var b in buttons) {
  77. this.buttons.Add (b);
  78. Add (b);
  79. }
  80. }
  81. LayoutStarted += (s, args) => {
  82. LayoutStartedHandler ();
  83. };
  84. }
  85. /// <summary>
  86. /// Initializes a new instance of the <see cref="Dialog"/> class using <see cref="LayoutStyle.Computed"/>.
  87. /// </summary>
  88. /// <remarks>
  89. /// <para>
  90. /// Te Dialog will be vertically and horizontally centered in the container and the size will be 85% of the container.
  91. /// After initialization use <c>X</c>, <c>Y</c>, <c>Width</c>, and <c>Height</c> to override this with a location or size.
  92. /// </para>
  93. /// <para>
  94. /// Use <see cref="AddButton(Button)"/> to add buttons to the dialog.
  95. /// </para>
  96. /// </remarks>
  97. public Dialog () : this (title: ustring.Empty, width: 0, height: 0, buttons: null) { }
  98. /// <summary>
  99. /// Initializes a new instance of the <see cref="Dialog"/> class using <see cref="LayoutStyle.Computed"/> positioning
  100. /// and with an optional set of <see cref="Button"/>s to display
  101. /// </summary>
  102. /// <param name="title">Title for the dialog.</param>
  103. /// <param name="buttons">Optional buttons to lay out at the bottom of the dialog.</param>
  104. /// <remarks>
  105. /// Te Dialog will be vertically and horizontally centered in the container and the size will be 85% of the container.
  106. /// After initialization use <c>X</c>, <c>Y</c>, <c>Width</c>, and <c>Height</c> to override this with a location or size.
  107. /// </remarks>
  108. public Dialog (ustring title, params Button [] buttons) : this (title: title, width: 0, height: 0, buttons: buttons) { }
  109. /// <summary>
  110. /// Adds a <see cref="Button"/> to the <see cref="Dialog"/>, its layout will be controlled by the <see cref="Dialog"/>
  111. /// </summary>
  112. /// <param name="button">Button to add.</param>
  113. public void AddButton (Button button)
  114. {
  115. if (button == null)
  116. return;
  117. buttons.Add (button);
  118. Add (button);
  119. SetNeedsDisplay ();
  120. LayoutSubviews ();
  121. }
  122. // Get the width of all buttons, not including any spacing
  123. internal int GetButtonsWidth ()
  124. {
  125. if (buttons.Count == 0) {
  126. return 0;
  127. }
  128. return buttons.Select (b => b.Frame.Width).Sum ();
  129. }
  130. /// <summary>
  131. /// Determines the horizontal alignment of the Dialog buttons.
  132. /// </summary>
  133. public enum ButtonAlignments {
  134. /// <summary>
  135. /// Center-aligns the buttons (the default).
  136. /// </summary>
  137. Center = 0,
  138. /// <summary>
  139. /// Justifies the buttons
  140. /// </summary>
  141. Justify,
  142. /// <summary>
  143. /// Left-aligns the buttons
  144. /// </summary>
  145. Left,
  146. /// <summary>
  147. /// Right-aligns the buttons
  148. /// </summary>
  149. Right
  150. }
  151. /// <summary>
  152. /// Determines how the <see cref="Dialog"/> <see cref="Button"/>s are aligned along the
  153. /// bottom of the dialog.
  154. /// </summary>
  155. public ButtonAlignments ButtonAlignment { get; set; }
  156. void LayoutStartedHandler ()
  157. {
  158. if (buttons.Count == 0 || !IsInitialized) return;
  159. int shiftLeft = 0;
  160. int buttonsWidth = GetButtonsWidth ();
  161. switch (ButtonAlignment) {
  162. case ButtonAlignments.Center:
  163. // Center Buttons
  164. shiftLeft = (Bounds.Width - buttonsWidth - buttons.Count - 1) / 2 + 1;
  165. for (int i = buttons.Count - 1; i >= 0; i--) {
  166. Button button = buttons [i];
  167. shiftLeft += button.Frame.Width + (i == buttons.Count - 1 ? 0 : 1);
  168. if (shiftLeft > -1) {
  169. button.X = Pos.AnchorEnd (shiftLeft);
  170. } else {
  171. button.X = Bounds.Width - shiftLeft;
  172. }
  173. button.Y = Pos.AnchorEnd (1);
  174. }
  175. break;
  176. case ButtonAlignments.Justify:
  177. // Justify Buttons
  178. // leftmost and rightmost buttons are hard against edges. The rest are evenly spaced.
  179. var spacing = (int)Math.Ceiling ((double)(Bounds.Width - buttonsWidth) / (buttons.Count - 1));
  180. for (int i = buttons.Count - 1; i >= 0; i--) {
  181. Button button = buttons [i];
  182. if (i == buttons.Count - 1) {
  183. shiftLeft += button.Frame.Width;
  184. button.X = Pos.AnchorEnd (shiftLeft);
  185. } else {
  186. if (i == 0) {
  187. // first (leftmost) button - always hard flush left
  188. var left = Bounds.Width + Border.BorderThickness.Horizontal;
  189. button.X = Pos.AnchorEnd (left);
  190. } else {
  191. shiftLeft += button.Frame.Width + (spacing);
  192. button.X = Pos.AnchorEnd (shiftLeft);
  193. }
  194. }
  195. button.Y = Pos.AnchorEnd (1);
  196. }
  197. break;
  198. case ButtonAlignments.Left:
  199. // Left Align Buttons
  200. var prevButton = buttons [0];
  201. prevButton.X = 0;
  202. prevButton.Y = Pos.AnchorEnd (1);
  203. for (int i = 1; i < buttons.Count; i++) {
  204. Button button = buttons [i];
  205. button.X = Pos.Right (prevButton) + 1;
  206. button.Y = Pos.AnchorEnd (1);
  207. prevButton = button;
  208. }
  209. break;
  210. case ButtonAlignments.Right:
  211. // Right align buttons
  212. shiftLeft = buttons [buttons.Count - 1].Frame.Width;
  213. buttons [buttons.Count - 1].X = Pos.AnchorEnd (shiftLeft);
  214. buttons [buttons.Count - 1].Y = Pos.AnchorEnd (1);
  215. for (int i = buttons.Count - 2; i >= 0; i--) {
  216. Button button = buttons [i];
  217. shiftLeft += button.Frame.Width + 1;
  218. button.X = Pos.AnchorEnd (shiftLeft);
  219. button.Y = Pos.AnchorEnd (1);
  220. }
  221. break;
  222. }
  223. }
  224. ///<inheritdoc/>
  225. public override bool ProcessKey (KeyEvent kb)
  226. {
  227. switch (kb.Key) {
  228. case Key.Esc:
  229. Application.RequestStop (this);
  230. return true;
  231. }
  232. return base.ProcessKey (kb);
  233. }
  234. }
  235. }