Dialog.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. /// <summary>
  27. /// Initializes a new instance of the <see cref="Dialog"/> class using <see cref="LayoutStyle.Computed"/>
  28. /// positioning with no <see cref="Button"/>s.
  29. /// </summary>
  30. /// <remarks>
  31. /// By default, <see cref="View.X"/> and <see cref="View.Y"/> are set to <c>Pos.Center ()</c> and
  32. /// <see cref="View.Width"/> and <see cref="View.Height"/> are set to <c>Width = Dim.Percent (85)</c>, centering the
  33. /// Dialog vertically and horizontally.
  34. /// </remarks>
  35. public Dialog ()
  36. {
  37. Arrangement = ViewArrangement.Movable;
  38. X = Pos.Center ();
  39. Y = Pos.Center ();
  40. //ValidatePosDim = true;
  41. Width = Dim.Percent (85);
  42. Height = Dim.Percent (85);
  43. ColorScheme = Colors.ColorSchemes ["Dialog"];
  44. Modal = true;
  45. ButtonAlignment = DefaultButtonAlignment;
  46. ButtonAlignmentModes = DefaultButtonAlignmentModes;
  47. AddCommand (
  48. Command.QuitToplevel,
  49. () =>
  50. {
  51. Canceled = true;
  52. RequestStop ();
  53. return true;
  54. });
  55. KeyBindings.Add (Key.Esc, Command.QuitToplevel);
  56. }
  57. private bool _canceled;
  58. /// <summary>Gets a value indicating whether the <see cref="Dialog"/> was canceled.</summary>
  59. /// <remarks>The default value is <see langword="true"/>.</remarks>
  60. public bool Canceled
  61. {
  62. get
  63. {
  64. #if DEBUG_IDISPOSABLE
  65. if (WasDisposed)
  66. {
  67. throw new ObjectDisposedException (GetType ().FullName);
  68. }
  69. #endif
  70. return _canceled;
  71. }
  72. set
  73. {
  74. #if DEBUG_IDISPOSABLE
  75. if (WasDisposed)
  76. {
  77. throw new ObjectDisposedException (GetType ().FullName);
  78. }
  79. #endif
  80. _canceled = value;
  81. return;
  82. }
  83. }
  84. // TODO: Update button.X = Pos.Justify when alignment changes
  85. /// <summary>Determines how the <see cref="Dialog"/> <see cref="Button"/>s are aligned along the bottom of the dialog.</summary>
  86. public Alignment ButtonAlignment { get; set; }
  87. /// <summary>
  88. /// Gets or sets the alignment modes for the dialog's buttons.
  89. /// </summary>
  90. public AlignmentModes ButtonAlignmentModes { get; set; }
  91. /// <summary>Optional buttons to lay out at the bottom of the dialog.</summary>
  92. public Button [] Buttons
  93. {
  94. get => _buttons.ToArray ();
  95. init
  96. {
  97. if (value is null)
  98. {
  99. return;
  100. }
  101. foreach (Button b in value)
  102. {
  103. AddButton (b);
  104. }
  105. }
  106. }
  107. /// <summary>The default <see cref="Alignment"/> for <see cref="Dialog"/>.</summary>
  108. /// <remarks>This property can be set in a Theme.</remarks>
  109. [SerializableConfigurationProperty (Scope = typeof (ThemeScope))]
  110. [JsonConverter (typeof (JsonStringEnumConverter))]
  111. public static Alignment DefaultButtonAlignment { get; set; } = Alignment.End;
  112. /// <summary>The default <see cref="Alignment"/> for <see cref="Dialog"/>.</summary>
  113. /// <remarks>This property can be set in a Theme.</remarks>
  114. [SerializableConfigurationProperty (Scope = typeof (ThemeScope))]
  115. [JsonConverter (typeof (JsonStringEnumConverter))]
  116. public static AlignmentModes DefaultButtonAlignmentModes { get; set; } = AlignmentModes.StartToEnd | AlignmentModes.AddSpaceBetweenItems;
  117. /// <summary>
  118. /// Adds a <see cref="Button"/> to the <see cref="Dialog"/>, its layout will be controlled by the
  119. /// <see cref="Dialog"/>
  120. /// </summary>
  121. /// <param name="button">Button to add.</param>
  122. public void AddButton (Button button)
  123. {
  124. if (button is null)
  125. {
  126. return;
  127. }
  128. // Use a distinct GroupId so users can use Pos.Align for other views in the Dialog
  129. button.X = Pos.Align (ButtonAlignment, ButtonAlignmentModes, groupId: GetHashCode ());
  130. button.Y = Pos.AnchorEnd ();
  131. _buttons.Add (button);
  132. Add (button);
  133. SetNeedsDisplay ();
  134. if (IsInitialized)
  135. {
  136. LayoutSubviews ();
  137. }
  138. }
  139. /// <inheritdoc/>
  140. //public override void LayoutSubviews ()
  141. //{
  142. // if (_inLayout)
  143. // {
  144. // return;
  145. // }
  146. // _inLayout = true;
  147. // SetRelativeLayout(SuperView?.ContentSize ?? Driver.Screen.Size);
  148. // LayoutButtons ();
  149. // base.LayoutSubviews ();
  150. // _inLayout = false;
  151. //}
  152. // Get the width of all buttons, not including any Margin.
  153. internal int GetButtonsWidth ()
  154. {
  155. if (_buttons.Count == 0)
  156. {
  157. return 0;
  158. }
  159. //var widths = buttons.Select (b => b.TextFormatter.GetFormattedSize ().Width + b.BorderFrame.Thickness.Horizontal + b.Padding.Thickness.Horizontal);
  160. IEnumerable<int> widths = _buttons.Select (b => b.Frame.Width);
  161. return widths.Sum ();
  162. }
  163. }