Dialog.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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()"/>.
  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.Absolute"/> 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. //LayoutComplete += (sender, a) => AdjustButtonLayout ();
  60. }
  61. /// <summary>
  62. /// Initializes a new instance of the <see cref="Dialog"/> class using <see cref="LayoutStyle.Computed"/> positioning
  63. /// and with an optional set of <see cref="Button"/>s to display
  64. /// </summary>
  65. /// <param name="title">Title for the dialog.</param>
  66. /// <param name="buttons">Optional buttons to lay out at the bottom of the dialog.</param>
  67. /// <remarks>
  68. /// if <c>width</c> and <c>height</c> are both 0, the Dialog will be vertically and horizontally centered in the
  69. /// container and the size will be 85% of the container.
  70. /// After initialzation use <c>X</c>, <c>Y</c>, <c>Width</c>, and <c>Height</c> to override this with a location or size.
  71. /// </remarks>
  72. public Dialog (ustring title, params Button [] buttons) : this (title: title, width: 0, height: 0, buttons: buttons) {
  73. }
  74. /// <summary>
  75. /// Adds a <see cref="Button"/> to the <see cref="Dialog"/>, its layout will be controled by the <see cref="Dialog"/>
  76. /// </summary>
  77. /// <param name="button">Button to add.</param>
  78. public void AddButton (Button button)
  79. {
  80. if (button == null)
  81. return;
  82. buttons.Add (button);
  83. Add (button);
  84. LayoutSubviews ();
  85. }
  86. internal int GetButtonsWidth ()
  87. {
  88. if (buttons.Count == 0) {
  89. return 0;
  90. }
  91. return buttons.Select (b => b.Bounds.Width).Sum () + buttons.Count() - 1;
  92. }
  93. ///<inheritdoc cref="LayoutSubviews"/>
  94. public override void LayoutSubviews ()
  95. {
  96. int buttonsWidth = GetButtonsWidth ();
  97. int shiftLeft = Math.Max ((Bounds.Width - buttonsWidth) / 2 - 2, 0);
  98. for (int i = buttons.Count - 1; i >= 0; i--) {
  99. Button button = buttons [i];
  100. shiftLeft += button.Frame.Width + 1;
  101. button.X = Pos.AnchorEnd (shiftLeft);
  102. button.Y = Pos.AnchorEnd (1);
  103. }
  104. base.LayoutSubviews ();
  105. }
  106. ///<inheritdoc cref="ProcessKey"/>
  107. public override bool ProcessKey (KeyEvent kb)
  108. {
  109. switch (kb.Key) {
  110. case Key.Esc:
  111. Running = false;
  112. return true;
  113. }
  114. return base.ProcessKey (kb);
  115. }
  116. }
  117. }