Dialog.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. namespace Terminal.Gui;
  2. /// <summary>
  3. /// The <see cref="Dialog"/> <see cref="View"/> is a <see cref="Window"/> that by default is centered and contains
  4. /// one or more <see cref="Button"/>s. It defaults to the <c>Colors.ColorSchemes ["Dialog"]</c> color scheme and has a
  5. /// 1 cell padding around the edges.
  6. /// </summary>
  7. /// <remarks>
  8. /// To run the <see cref="Dialog"/> modally, create the <see cref="Dialog"/>, and pass it to
  9. /// <see cref="Application.Run(Toplevel, Func{Exception, bool})"/>. This will execute the dialog until
  10. /// it terminates via the <see cref="Application.QuitKey"/> (`Esc` by default),
  11. /// 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. public static Alignment DefaultButtonAlignment { get; set; } = Alignment.End; // Default is set in config.json
  20. /// <summary>The default <see cref="AlignmentModes"/> for <see cref="Dialog"/>.</summary>
  21. /// <remarks>This property can be set in a Theme.</remarks>
  22. [SerializableConfigurationProperty (Scope = typeof (ThemeScope))]
  23. public static AlignmentModes DefaultButtonAlignmentModes { get; set; } = AlignmentModes.StartToEnd | AlignmentModes.AddSpaceBetweenItems;
  24. /// <summary>
  25. /// Defines the default minimum Dialog width, as a percentage of the container width. Can be configured via
  26. /// <see cref="ConfigurationManager"/>.
  27. /// </summary>
  28. [SerializableConfigurationProperty (Scope = typeof (ThemeScope))]
  29. public static int DefaultMinimumWidth { get; set; } = 80;
  30. /// <summary>
  31. /// Defines the default minimum Dialog height, as a percentage of the container width. Can be configured via
  32. /// <see cref="ConfigurationManager"/>.
  33. /// </summary>
  34. [SerializableConfigurationProperty (Scope = typeof (ThemeScope))]
  35. public static int DefaultMinimumHeight { get; set; } = 80;
  36. /// <summary>
  37. /// Gets or sets whether all <see cref="Window"/>s are shown with a shadow effect by default.
  38. /// </summary>
  39. [SerializableConfigurationProperty (Scope = typeof (ThemeScope))]
  40. public new static ShadowStyle DefaultShadow { get; set; } = ShadowStyle.None; // Default is set in config.json
  41. /// <summary>
  42. /// Defines the default border styling for <see cref="Dialog"/>. Can be configured via
  43. /// <see cref="ConfigurationManager"/>.
  44. /// </summary>
  45. [SerializableConfigurationProperty (Scope = typeof (ThemeScope))]
  46. public new static LineStyle DefaultBorderStyle { get; set; } = LineStyle.Single; // Default is set in config.json
  47. private readonly List<Button> _buttons = new ();
  48. /// <summary>
  49. /// Initializes a new instance of the <see cref="Dialog"/> class with no <see cref="Button"/>s.
  50. /// </summary>
  51. /// <remarks>
  52. /// By default, <see cref="View.X"/>, <see cref="View.Y"/>, <see cref="View.Width"/>, and <see cref="View.Height"/> are
  53. /// set
  54. /// such that the <see cref="Dialog"/> will be centered in, and no larger than 90% of <see cref="Application.Top"/>, if there is one. Otherwise,
  55. /// it will be bound by the screen dimensions.
  56. /// </remarks>
  57. public Dialog ()
  58. {
  59. Arrangement = ViewArrangement.Movable;
  60. ShadowStyle = DefaultShadow;
  61. BorderStyle = DefaultBorderStyle;
  62. X = Pos.Center ();
  63. Y = Pos.Center ();
  64. Width = Dim.Auto (DimAutoStyle.Auto, Dim.Percent (DefaultMinimumWidth), Dim.Percent (90));
  65. Height = Dim.Auto (DimAutoStyle.Auto, Dim.Percent (DefaultMinimumHeight), Dim.Percent (90));
  66. ColorScheme = Colors.ColorSchemes ["Dialog"];
  67. Modal = true;
  68. ButtonAlignment = DefaultButtonAlignment;
  69. ButtonAlignmentModes = DefaultButtonAlignmentModes;
  70. AddCommand (
  71. Command.QuitToplevel,
  72. () =>
  73. {
  74. Canceled = true;
  75. RequestStop ();
  76. return true;
  77. });
  78. KeyBindings.Add (Key.Esc, Command.QuitToplevel);
  79. }
  80. // BUGBUG: We override GetNormal/FocusColor because "Dialog" ColorScheme is goofy.
  81. // BUGBUG: By defn, a Dialog is Modal, and thus HasFocus is always true. OnDrawContent
  82. // BUGBUG: Calls these methods.
  83. // TODO: Fix this in https://github.com/gui-cs/Terminal.Gui/issues/2381
  84. /// <inheritdoc />
  85. public override Attribute GetNormalColor ()
  86. {
  87. return ColorScheme.Normal;
  88. }
  89. /// <inheritdoc />
  90. public override Attribute GetFocusColor ()
  91. {
  92. return ColorScheme.Normal;
  93. }
  94. private bool _canceled;
  95. /// <summary>Gets a value indicating whether the <see cref="Dialog"/> was canceled.</summary>
  96. /// <remarks>The default value is <see langword="true"/>.</remarks>
  97. public bool Canceled
  98. {
  99. get
  100. {
  101. #if DEBUG_IDISPOSABLE
  102. if (WasDisposed)
  103. {
  104. throw new ObjectDisposedException (GetType ().FullName);
  105. }
  106. #endif
  107. return _canceled;
  108. }
  109. set
  110. {
  111. #if DEBUG_IDISPOSABLE
  112. if (WasDisposed)
  113. {
  114. throw new ObjectDisposedException (GetType ().FullName);
  115. }
  116. #endif
  117. _canceled = value;
  118. }
  119. }
  120. // TODO: Update button.X = Pos.Justify when alignment changes
  121. /// <summary>Determines how the <see cref="Dialog"/> <see cref="Button"/>s are aligned along the bottom of the dialog.</summary>
  122. public Alignment ButtonAlignment { get; set; }
  123. /// <summary>
  124. /// Gets or sets the alignment modes for the dialog's buttons.
  125. /// </summary>
  126. public AlignmentModes ButtonAlignmentModes { get; set; }
  127. /// <summary>Optional buttons to lay out at the bottom of the dialog.</summary>
  128. public Button [] Buttons
  129. {
  130. get => _buttons.ToArray ();
  131. init
  132. {
  133. if (value is null)
  134. {
  135. return;
  136. }
  137. foreach (Button b in value)
  138. {
  139. AddButton (b);
  140. }
  141. }
  142. }
  143. /// <summary>
  144. /// Adds a <see cref="Button"/> to the <see cref="Dialog"/>, its layout will be controlled by the
  145. /// <see cref="Dialog"/>
  146. /// </summary>
  147. /// <param name="button">Button to add.</param>
  148. public void AddButton (Button button)
  149. {
  150. if (button is null)
  151. {
  152. return;
  153. }
  154. // Use a distinct GroupId so users can use Pos.Align for other views in the Dialog
  155. button.X = Pos.Align (ButtonAlignment, ButtonAlignmentModes, GetHashCode ());
  156. button.Y = Pos.AnchorEnd ();
  157. _buttons.Add (button);
  158. Add (button);
  159. }
  160. }