Dialog.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 NStack;
  11. namespace Terminal.Gui {
  12. /// <summary>
  13. /// The <see cref="Dialog"/> <see cref="View"/> is a <see cref="Window"/> that by default is centered and contains one
  14. /// 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.
  15. /// </summary>
  16. /// <remarks>
  17. /// To run the <see cref="Dialog"/> modally, create the <see cref="Dialog"/>, and pass it to <see cref="Application.Run(Func{Exception, bool})"/>.
  18. /// This will execute the dialog until it terminates via the [ESC] or [CTRL-Q] key, or when one of the views
  19. /// or buttons added to the dialog calls <see cref="Application.RequestStop"/>.
  20. /// </remarks>
  21. public class Dialog : Window {
  22. List<Button> buttons = new List<Button> ();
  23. const int padding = 0;
  24. /// <summary>
  25. /// Initializes a new instance of the <see cref="Dialog"/> class using <see cref="LayoutStyle.Computed"/> positioning
  26. /// and an optional set of <see cref="Button"/>s to display
  27. /// </summary>
  28. /// <param name="title">Title for the dialog.</param>
  29. /// <param name="width">Width for the dialog.</param>
  30. /// <param name="height">Height for the dialog.</param>
  31. /// <param name="buttons">Optional buttons to lay out at the bottom of the dialog.</param>
  32. /// <remarks>
  33. /// if <c>width</c> and <c>height</c> are both 0, the Dialog will be vertically and horizontally centered in the
  34. /// container and the size will be 85% of the container.
  35. /// After initialization use <c>X</c>, <c>Y</c>, <c>Width</c>, and <c>Height</c> to override this with a location or size.
  36. /// </remarks>
  37. /// <remarks>
  38. /// Use the constructor that does not take a <c>width</c> and <c>height</c> instead.
  39. /// </remarks>
  40. public Dialog (ustring title, int width, int height, params Button [] buttons) : base (title, padding: padding)
  41. {
  42. X = Pos.Center ();
  43. Y = Pos.Center ();
  44. if (width == 0 & height == 0) {
  45. Width = Dim.Percent (85);
  46. Height = Dim.Percent (85);
  47. } else {
  48. Width = width;
  49. Height = height;
  50. }
  51. ColorScheme = Colors.Dialog;
  52. Modal = true;
  53. Border.Effect3D = true;
  54. if (buttons != null) {
  55. foreach (var b in buttons) {
  56. this.buttons.Add (b);
  57. Add (b);
  58. }
  59. }
  60. LayoutStarted += (args) => {
  61. LayoutStartedHandler ();
  62. };
  63. }
  64. /// <summary>
  65. /// Initializes a new instance of the <see cref="Dialog"/> class using <see cref="LayoutStyle.Computed"/>.
  66. /// </summary>
  67. /// <remarks>
  68. /// <para>
  69. /// Te Dialog will be vertically and horizontally centered in the container and the size will be 85% of the container.
  70. /// After initialization use <c>X</c>, <c>Y</c>, <c>Width</c>, and <c>Height</c> to override this with a location or size.
  71. /// </para>
  72. /// <para>
  73. /// Use <see cref="AddButton(Button)"/> to add buttons to the dialog.
  74. /// </para>
  75. /// </remarks>
  76. public Dialog () : this (title: string.Empty, width: 0, height: 0, buttons: null) { }
  77. /// <summary>
  78. /// Initializes a new instance of the <see cref="Dialog"/> class using <see cref="LayoutStyle.Computed"/> positioning
  79. /// and with an optional set of <see cref="Button"/>s to display
  80. /// </summary>
  81. /// <param name="title">Title for the dialog.</param>
  82. /// <param name="buttons">Optional buttons to lay out at the bottom of the dialog.</param>
  83. /// <remarks>
  84. /// Te Dialog will be vertically and horizontally centered in the container and the size will be 85% of the container.
  85. /// After initialization use <c>X</c>, <c>Y</c>, <c>Width</c>, and <c>Height</c> to override this with a location or size.
  86. /// </remarks>
  87. public Dialog (ustring title, params Button [] buttons) : this (title: title, width: 0, height: 0, buttons: buttons) { }
  88. /// <summary>
  89. /// Adds a <see cref="Button"/> to the <see cref="Dialog"/>, its layout will be controlled by the <see cref="Dialog"/>
  90. /// </summary>
  91. /// <param name="button">Button to add.</param>
  92. public void AddButton (Button button)
  93. {
  94. if (button == null)
  95. return;
  96. buttons.Add (button);
  97. Add (button);
  98. SetNeedsDisplay ();
  99. LayoutSubviews ();
  100. }
  101. // Get the width of all buttons, not including any spacing
  102. internal int GetButtonsWidth ()
  103. {
  104. if (buttons.Count == 0) {
  105. return 0;
  106. }
  107. return buttons.Select (b => b.Bounds.Width).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. private ButtonAlignments buttonAlignment = Dialog.ButtonAlignments.Center;
  131. /// <summary>
  132. /// Determines how the <see cref="Dialog"/> <see cref="Button"/>s are aligned along the
  133. /// bottom of the dialog.
  134. /// </summary>
  135. public ButtonAlignments ButtonAlignment { get => buttonAlignment; set => buttonAlignment = value; }
  136. void LayoutStartedHandler ()
  137. {
  138. if (buttons.Count == 0 || !IsInitialized) return;
  139. int shiftLeft = 0;
  140. int buttonsWidth = GetButtonsWidth ();
  141. switch (ButtonAlignment) {
  142. case ButtonAlignments.Center:
  143. // Center Buttons
  144. shiftLeft = Math.Max ((Bounds.Width - buttonsWidth - buttons.Count - 2) / 2 + 1, 0);
  145. for (int i = buttons.Count - 1; i >= 0; i--) {
  146. Button button = buttons [i];
  147. shiftLeft += button.Frame.Width + (i == buttons.Count - 1 ? 0 : 1);
  148. button.X = Pos.AnchorEnd (shiftLeft);
  149. button.Y = Pos.AnchorEnd (1);
  150. }
  151. break;
  152. case ButtonAlignments.Justify:
  153. // Justify Buttons
  154. // leftmost and rightmost buttons are hard against edges. The rest are evenly spaced.
  155. var spacing = (int)Math.Ceiling ((double)(Bounds.Width - buttonsWidth - 2) / (buttons.Count - 1));
  156. for (int i = buttons.Count - 1; i >= 0; i--) {
  157. Button button = buttons [i];
  158. if (i == buttons.Count - 1) {
  159. shiftLeft += button.Frame.Width;
  160. button.X = Pos.AnchorEnd (shiftLeft);
  161. } else {
  162. if (i == 0) {
  163. // first (leftmost) button - always hard flush left
  164. var left = Bounds.Width - ((Border.DrawMarginFrame ? 2 : 0) + Border.BorderThickness.Left + Border.BorderThickness.Right);
  165. button.X = Pos.AnchorEnd (left);
  166. } else {
  167. shiftLeft += button.Frame.Width + (spacing);
  168. button.X = Pos.AnchorEnd (shiftLeft);
  169. }
  170. }
  171. button.Y = Pos.AnchorEnd (1);
  172. }
  173. break;
  174. case ButtonAlignments.Left:
  175. // Left Align Buttons
  176. var prevButton = buttons [0];
  177. prevButton.X = 0;
  178. prevButton.Y = Pos.AnchorEnd (1);
  179. for (int i = 1; i < buttons.Count; i++) {
  180. Button button = buttons [i];
  181. button.X = Pos.Right (prevButton) + 1;
  182. button.Y = Pos.AnchorEnd (1);
  183. prevButton = button;
  184. }
  185. break;
  186. case ButtonAlignments.Right:
  187. // Right align buttons
  188. shiftLeft = buttons [buttons.Count - 1].Frame.Width;
  189. buttons [buttons.Count - 1].X = Pos.AnchorEnd (shiftLeft);
  190. buttons [buttons.Count - 1].Y = Pos.AnchorEnd (1);
  191. for (int i = buttons.Count - 2; i >= 0; i--) {
  192. Button button = buttons [i];
  193. shiftLeft += button.Frame.Width + 1;
  194. button.X = Pos.AnchorEnd (shiftLeft);
  195. button.Y = Pos.AnchorEnd (1);
  196. }
  197. break;
  198. }
  199. }
  200. ///<inheritdoc/>
  201. public override bool ProcessKey (KeyEvent kb)
  202. {
  203. switch (kb.Key) {
  204. case Key.Esc:
  205. Application.RequestStop (this);
  206. return true;
  207. }
  208. return base.ProcessKey (kb);
  209. }
  210. }
  211. }