Dialog.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 initialzation 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. if (buttons != null) {
  54. foreach (var b in buttons) {
  55. this.buttons.Add (b);
  56. Add (b);
  57. }
  58. }
  59. LayoutStarted += (args) => {
  60. LayoutStartedHandler ();
  61. };
  62. }
  63. /// <summary>
  64. /// Initializes a new instance of the <see cref="Dialog"/> class using <see cref="LayoutStyle.Computed"/>.
  65. /// </summary>
  66. /// <remarks>
  67. /// <para>
  68. /// Te Dialog will be vertically and horizontally centered in the container and the size will be 85% of the container.
  69. /// After initialzation use <c>X</c>, <c>Y</c>, <c>Width</c>, and <c>Height</c> to override this with a location or size.
  70. /// </para>
  71. /// <para>
  72. /// Use <see cref="AddButton(Button)"/> to add buttons to the dialog.
  73. /// </para>
  74. /// </remarks>
  75. public Dialog () : this (title: string.Empty, width: 0, height: 0, buttons: null) { }
  76. /// <summary>
  77. /// Initializes a new instance of the <see cref="Dialog"/> class using <see cref="LayoutStyle.Computed"/> positioning
  78. /// and with an optional set of <see cref="Button"/>s to display
  79. /// </summary>
  80. /// <param name="title">Title for the dialog.</param>
  81. /// <param name="buttons">Optional buttons to lay out at the bottom of the dialog.</param>
  82. /// <remarks>
  83. /// Te Dialog will be vertically and horizontally centered in the container and the size will be 85% of the container.
  84. /// After initialzation use <c>X</c>, <c>Y</c>, <c>Width</c>, and <c>Height</c> to override this with a location or size.
  85. /// </remarks>
  86. public Dialog (ustring title, params Button [] buttons) : this (title: title, width: 0, height: 0, buttons: buttons) { }
  87. /// <summary>
  88. /// Adds a <see cref="Button"/> to the <see cref="Dialog"/>, its layout will be controlled by the <see cref="Dialog"/>
  89. /// </summary>
  90. /// <param name="button">Button to add.</param>
  91. public void AddButton (Button button)
  92. {
  93. if (button == null)
  94. return;
  95. buttons.Add (button);
  96. Add (button);
  97. SetNeedsDisplay ();
  98. LayoutSubviews ();
  99. }
  100. internal int GetButtonsWidth ()
  101. {
  102. if (buttons.Count == 0) {
  103. return 0;
  104. }
  105. return buttons.Select (b => b.Bounds.Width).Sum () + buttons.Count - 1;
  106. }
  107. void LayoutStartedHandler ()
  108. {
  109. int buttonsWidth = GetButtonsWidth ();
  110. int shiftLeft = Math.Max ((Bounds.Width - buttonsWidth) / 2 - 2, 0);
  111. for (int i = buttons.Count - 1; i >= 0; i--) {
  112. Button button = buttons [i];
  113. shiftLeft += button.Frame.Width + 1;
  114. button.X = Pos.AnchorEnd (shiftLeft);
  115. button.Y = Pos.AnchorEnd (1);
  116. }
  117. }
  118. ///<inheritdoc/>
  119. public override bool ProcessKey (KeyEvent kb)
  120. {
  121. switch (kb.Key) {
  122. case Key.Esc:
  123. Running = false;
  124. return true;
  125. }
  126. return base.ProcessKey (kb);
  127. }
  128. ///<inheritdoc/>
  129. public override bool OnEnter (View view)
  130. {
  131. Application.Driver.SetCursorVisibility (CursorVisibility.Invisible);
  132. return base.OnEnter (view);
  133. }
  134. }
  135. }