Dialog.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. using System.Text.Json.Serialization;
  2. namespace Terminal.Gui;
  3. /// <summary>
  4. /// The <see cref="Dialog"/> <see cref="View"/> is a <see cref="Window"/> that by default is centered and contains
  5. /// one or more <see cref="Button"/>s. It defaults to the <c>Colors.ColorSchemes ["Dialog"]</c> color scheme and has a
  6. /// 1 cell padding around the edges.
  7. /// </summary>
  8. /// <remarks>
  9. /// To run the <see cref="Dialog"/> modally, create the <see cref="Dialog"/>, and pass it to
  10. /// <see cref="Application.Run(Toplevel, Func{Exception, bool}, ConsoleDriver)"/>. This will execute the dialog until it terminates via the
  11. /// [ESC] or [CTRL-Q] key, or when one of the views or buttons added to the dialog calls
  12. /// <see cref="Application.RequestStop"/>.
  13. /// </remarks>
  14. public class Dialog : Window
  15. {
  16. // TODO: Reenable once border/borderframe design is settled
  17. /// <summary>
  18. /// Defines the default border styling for <see cref="Dialog"/>. Can be configured via
  19. /// <see cref="ConfigurationManager"/>.
  20. /// </summary>
  21. //[SerializableConfigurationProperty (Scope = typeof (ThemeScope))]
  22. //public static Border DefaultBorder { get; set; } = new Border () {
  23. // LineStyle = LineStyle.Single,
  24. //};
  25. private readonly List<Button> _buttons = new ();
  26. private bool _inLayout;
  27. /// <summary>
  28. /// Initializes a new instance of the <see cref="Dialog"/> class using <see cref="LayoutStyle.Computed"/>
  29. /// positioning with no <see cref="Button"/>s.
  30. /// </summary>
  31. /// <remarks>
  32. /// By default, <see cref="View.X"/> and <see cref="View.Y"/> are set to <c>Pos.Center ()</c> and
  33. /// <see cref="View.Width"/> and <see cref="View.Height"/> are set to <c>Width = Dim.Percent (85)</c>, centering the
  34. /// Dialog vertically and horizontally.
  35. /// </remarks>
  36. public Dialog ()
  37. {
  38. Arrangement = ViewArrangement.Movable;
  39. X = Pos.Center ();
  40. Y = Pos.Center ();
  41. //ValidatePosDim = true;
  42. Width = Dim.Percent (85);
  43. Height = Dim.Percent (85);
  44. ColorScheme = Colors.ColorSchemes ["Dialog"];
  45. Modal = true;
  46. ButtonAlignment = DefaultButtonAlignment;
  47. AddCommand (
  48. Command.QuitToplevel,
  49. () =>
  50. {
  51. Canceled = true;
  52. RequestStop ();
  53. return true;
  54. });
  55. KeyBindings.Add (Key.Esc, Command.QuitToplevel);
  56. Initialized += Dialog_Initialized; ;
  57. }
  58. private void Dialog_Initialized (object sender, EventArgs e)
  59. {
  60. LayoutButtons ();
  61. }
  62. private bool _canceled;
  63. /// <summary>Gets a value indicating whether the <see cref="Dialog"/> was canceled.</summary>
  64. /// <remarks>The default value is <see langword="true"/>.</remarks>
  65. public bool Canceled
  66. {
  67. get
  68. {
  69. #if DEBUG_IDISPOSABLE
  70. if (WasDisposed)
  71. {
  72. throw new ObjectDisposedException (GetType ().FullName);
  73. }
  74. #endif
  75. return _canceled;
  76. }
  77. set
  78. {
  79. #if DEBUG_IDISPOSABLE
  80. if (WasDisposed)
  81. {
  82. throw new ObjectDisposedException (GetType ().FullName);
  83. }
  84. #endif
  85. _canceled = value;
  86. return;
  87. }
  88. }
  89. // TODO: Update button.X = Pos.Justify when alignment changes
  90. /// <summary>Determines how the <see cref="Dialog"/> <see cref="Button"/>s are aligned along the bottom of the dialog.</summary>
  91. public Alignment ButtonAlignment { get; set; }
  92. /// <summary>Optional buttons to lay out at the bottom of the dialog.</summary>
  93. public Button [] Buttons
  94. {
  95. get => _buttons.ToArray ();
  96. init
  97. {
  98. if (value is null)
  99. {
  100. return;
  101. }
  102. foreach (Button b in value)
  103. {
  104. AddButton (b);
  105. }
  106. }
  107. }
  108. /// <summary>The default <see cref="Alignment"/> for <see cref="Dialog"/>.</summary>
  109. /// <remarks>This property can be set in a Theme.</remarks>
  110. [SerializableConfigurationProperty (Scope = typeof (ThemeScope))]
  111. [JsonConverter (typeof (JsonStringEnumConverter))]
  112. public static Alignment DefaultButtonAlignment { get; set; } = Alignment.Right;
  113. /// <summary>
  114. /// Adds a <see cref="Button"/> to the <see cref="Dialog"/>, its layout will be controlled by the
  115. /// <see cref="Dialog"/>
  116. /// </summary>
  117. /// <param name="button">Button to add.</param>
  118. public void AddButton (Button button)
  119. {
  120. if (button is null)
  121. {
  122. return;
  123. }
  124. button.X = Pos.Align (ButtonAlignment);
  125. button.Y = Pos.AnchorEnd ();
  126. _buttons.Add (button);
  127. Add (button);
  128. SetNeedsDisplay ();
  129. if (IsInitialized)
  130. {
  131. LayoutSubviews ();
  132. }
  133. }
  134. /// <inheritdoc/>
  135. //public override void LayoutSubviews ()
  136. //{
  137. // if (_inLayout)
  138. // {
  139. // return;
  140. // }
  141. // _inLayout = true;
  142. // SetRelativeLayout(SuperView?.ContentSize ?? Driver.Screen.Size);
  143. // LayoutButtons ();
  144. // base.LayoutSubviews ();
  145. // _inLayout = false;
  146. //}
  147. // Get the width of all buttons, not including any Margin.
  148. internal int GetButtonsWidth ()
  149. {
  150. if (_buttons.Count == 0)
  151. {
  152. return 0;
  153. }
  154. //var widths = buttons.Select (b => b.TextFormatter.GetFormattedSize ().Width + b.BorderFrame.Thickness.Horizontal + b.Padding.Thickness.Horizontal);
  155. IEnumerable<int> widths = _buttons.Select (b => b.Frame.Width);
  156. return widths.Sum ();
  157. }
  158. }