Dialog.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. internal int GetButtonsWidth ()
  102. {
  103. if (buttons.Count == 0) {
  104. return 0;
  105. }
  106. return buttons.Select (b => b.Bounds.Width).Sum () + buttons.Count - 1;
  107. }
  108. void LayoutStartedHandler ()
  109. {
  110. int buttonsWidth = GetButtonsWidth ();
  111. int shiftLeft = Math.Max ((Bounds.Width - buttonsWidth) / 2 - 2, 0);
  112. for (int i = buttons.Count - 1; i >= 0; i--) {
  113. Button button = buttons [i];
  114. shiftLeft += button.Frame.Width + 1;
  115. button.X = Pos.AnchorEnd (shiftLeft);
  116. button.Y = Pos.AnchorEnd (1);
  117. }
  118. }
  119. ///<inheritdoc/>
  120. public override bool ProcessKey (KeyEvent kb)
  121. {
  122. switch (kb.Key) {
  123. case Key.Esc:
  124. Running = false;
  125. return true;
  126. }
  127. return base.ProcessKey (kb);
  128. }
  129. }
  130. }