Dialog.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. /// <summary>The default <see cref="Alignment"/> for <see cref="Dialog"/>.</summary>
  17. /// <remarks>This property can be set in a Theme.</remarks>
  18. [SerializableConfigurationProperty (Scope = typeof (ThemeScope))]
  19. [JsonConverter (typeof (JsonStringEnumConverter))]
  20. public static Alignment DefaultButtonAlignment { get; set; } = Alignment.End;
  21. /// <summary>The default <see cref="Alignment"/> for <see cref="Dialog"/>.</summary>
  22. /// <remarks>This property can be set in a Theme.</remarks>
  23. [SerializableConfigurationProperty (Scope = typeof (ThemeScope))]
  24. [JsonConverter (typeof (JsonStringEnumConverter))]
  25. public static AlignmentModes DefaultButtonAlignmentModes { get; set; } = AlignmentModes.StartToEnd | AlignmentModes.AddSpaceBetweenItems;
  26. /// <summary>
  27. /// Defines the default minimum Dialog width, as a percentage of the container width. Can be configured via
  28. /// <see cref="ConfigurationManager"/>.
  29. /// </summary>
  30. [SerializableConfigurationProperty (Scope = typeof (ThemeScope))]
  31. public static int DefaultMinimumWidth { get; set; } = 25;
  32. /// <summary>
  33. /// Defines the default minimum Dialog height, as a percentage of the container width. Can be configured via
  34. /// <see cref="ConfigurationManager"/>.
  35. /// </summary>
  36. [SerializableConfigurationProperty (Scope = typeof (ThemeScope))]
  37. public static int DefaultMinimumHeight { get; set; } = 25;
  38. // TODO: Reenable once border/borderframe design is settled
  39. /// <summary>
  40. /// Defines the default border styling for <see cref="Dialog"/>. Can be configured via
  41. /// <see cref="ConfigurationManager"/>.
  42. /// </summary>
  43. //[SerializableConfigurationProperty (Scope = typeof (ThemeScope))]
  44. //public static Border DefaultBorder { get; set; } = new Border () {
  45. // LineStyle = LineStyle.Single,
  46. //};
  47. private readonly List<Button> _buttons = new ();
  48. /// <summary>
  49. /// Initializes a new instance of the <see cref="Dialog"/> class using <see cref="LayoutStyle.Computed"/>
  50. /// positioning with no <see cref="Button"/>s.
  51. /// </summary>
  52. /// <remarks>
  53. /// By default, <see cref="View.X"/> and <see cref="View.Y"/> are set to <c>Pos.Center ()</c> and
  54. /// <see cref="View.Width"/> and <see cref="View.Height"/> are set to <c>Width = Dim.Percent (85)</c>, centering the
  55. /// Dialog vertically and horizontally.
  56. /// </remarks>
  57. public Dialog ()
  58. {
  59. Arrangement = ViewArrangement.Movable;
  60. X = Pos.Center ();
  61. Y = Pos.Center ();
  62. Width = Dim.Auto (DimAutoStyle.Content, minimumContentDim: Dim.Percent (DefaultMinimumWidth), Dim.Percent (90));
  63. Height = Dim.Auto (DimAutoStyle.Content, minimumContentDim: Dim.Percent (DefaultMinimumHeight), Dim.Percent (90));
  64. ColorScheme = Colors.ColorSchemes ["Dialog"];
  65. Modal = true;
  66. ButtonAlignment = DefaultButtonAlignment;
  67. ButtonAlignmentModes = DefaultButtonAlignmentModes;
  68. AddCommand (
  69. Command.QuitToplevel,
  70. () =>
  71. {
  72. Canceled = true;
  73. RequestStop ();
  74. return true;
  75. });
  76. KeyBindings.Add (Key.Esc, Command.QuitToplevel);
  77. }
  78. private bool _canceled;
  79. /// <summary>Gets a value indicating whether the <see cref="Dialog"/> was canceled.</summary>
  80. /// <remarks>The default value is <see langword="true"/>.</remarks>
  81. public bool Canceled
  82. {
  83. get
  84. {
  85. #if DEBUG_IDISPOSABLE
  86. if (WasDisposed)
  87. {
  88. throw new ObjectDisposedException (GetType ().FullName);
  89. }
  90. #endif
  91. return _canceled;
  92. }
  93. set
  94. {
  95. #if DEBUG_IDISPOSABLE
  96. if (WasDisposed)
  97. {
  98. throw new ObjectDisposedException (GetType ().FullName);
  99. }
  100. #endif
  101. _canceled = value;
  102. return;
  103. }
  104. }
  105. // TODO: Update button.X = Pos.Justify when alignment changes
  106. /// <summary>Determines how the <see cref="Dialog"/> <see cref="Button"/>s are aligned along the bottom of the dialog.</summary>
  107. public Alignment ButtonAlignment { get; set; }
  108. /// <summary>
  109. /// Gets or sets the alignment modes for the dialog's buttons.
  110. /// </summary>
  111. public AlignmentModes ButtonAlignmentModes { get; set; }
  112. /// <summary>Optional buttons to lay out at the bottom of the dialog.</summary>
  113. public Button [] Buttons
  114. {
  115. get => _buttons.ToArray ();
  116. init
  117. {
  118. if (value is null)
  119. {
  120. return;
  121. }
  122. foreach (Button b in value)
  123. {
  124. AddButton (b);
  125. }
  126. }
  127. }
  128. /// <summary>
  129. /// Adds a <see cref="Button"/> to the <see cref="Dialog"/>, its layout will be controlled by the
  130. /// <see cref="Dialog"/>
  131. /// </summary>
  132. /// <param name="button">Button to add.</param>
  133. public void AddButton (Button button)
  134. {
  135. if (button is null)
  136. {
  137. return;
  138. }
  139. // Use a distinct GroupId so users can use Pos.Align for other views in the Dialog
  140. button.X = Pos.Align (ButtonAlignment, ButtonAlignmentModes, groupId: GetHashCode ());
  141. button.Y = Pos.AnchorEnd ();
  142. _buttons.Add (button);
  143. Add (button);
  144. SetNeedsDisplay ();
  145. if (IsInitialized)
  146. {
  147. LayoutSubviews ();
  148. }
  149. }
  150. }