Dialog.cs 10 KB

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