Dialog.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. internal 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 = (Bounds.Width - buttonsWidth - buttons.Count - 2) / 2 + 1;
  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. if (shiftLeft > -1) {
  149. button.X = Pos.AnchorEnd (shiftLeft);
  150. } else {
  151. button.X = Frame.Width - shiftLeft;
  152. }
  153. button.Y = Pos.AnchorEnd (1);
  154. }
  155. break;
  156. case ButtonAlignments.Justify:
  157. // Justify Buttons
  158. // leftmost and rightmost buttons are hard against edges. The rest are evenly spaced.
  159. var spacing = (int)Math.Ceiling ((double)(Bounds.Width - buttonsWidth - (Border.DrawMarginFrame ? 2 : 0)) / (buttons.Count - 1));
  160. for (int i = buttons.Count - 1; i >= 0; i--) {
  161. Button button = buttons [i];
  162. if (i == buttons.Count - 1) {
  163. shiftLeft += button.Frame.Width;
  164. button.X = Pos.AnchorEnd (shiftLeft);
  165. } else {
  166. if (i == 0) {
  167. // first (leftmost) button - always hard flush left
  168. var left = Bounds.Width - ((Border.DrawMarginFrame ? 2 : 0) + Border.BorderThickness.Left + Border.BorderThickness.Right);
  169. button.X = Pos.AnchorEnd (Math.Max (left, 0));
  170. } else {
  171. shiftLeft += button.Frame.Width + (spacing);
  172. button.X = Pos.AnchorEnd (shiftLeft);
  173. }
  174. }
  175. button.Y = Pos.AnchorEnd (1);
  176. }
  177. break;
  178. case ButtonAlignments.Left:
  179. // Left Align Buttons
  180. var prevButton = buttons [0];
  181. prevButton.X = 0;
  182. prevButton.Y = Pos.AnchorEnd (1);
  183. for (int i = 1; i < buttons.Count; i++) {
  184. Button button = buttons [i];
  185. button.X = Pos.Right (prevButton) + 1;
  186. button.Y = Pos.AnchorEnd (1);
  187. prevButton = button;
  188. }
  189. break;
  190. case ButtonAlignments.Right:
  191. // Right align buttons
  192. shiftLeft = buttons [buttons.Count - 1].Frame.Width;
  193. buttons [buttons.Count - 1].X = Pos.AnchorEnd (shiftLeft);
  194. buttons [buttons.Count - 1].Y = Pos.AnchorEnd (1);
  195. for (int i = buttons.Count - 2; i >= 0; i--) {
  196. Button button = buttons [i];
  197. shiftLeft += button.Frame.Width + 1;
  198. button.X = Pos.AnchorEnd (shiftLeft);
  199. button.Y = Pos.AnchorEnd (1);
  200. }
  201. break;
  202. }
  203. }
  204. ///<inheritdoc/>
  205. public override bool ProcessKey (KeyEvent kb)
  206. {
  207. switch (kb.Key) {
  208. case Key.Esc:
  209. Application.RequestStop (this);
  210. return true;
  211. }
  212. return base.ProcessKey (kb);
  213. }
  214. }
  215. }