Dialog.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. namespace Terminal.Gui.Views;
  2. /// <summary>
  3. /// A <see cref="Toplevel.Modal"/> <see cref="Window"/>. Supports a simple API for adding <see cref="Button"/>s
  4. /// across the bottom. By default, the <see cref="Dialog"/> is centered and used the <see cref="Schemes.Dialog"/>
  5. /// scheme.
  6. /// </summary>
  7. /// <remarks>
  8. /// To run the <see cref="Dialog"/> modally, create the <see cref="Dialog"/>, and pass it to
  9. /// <see cref="IApplication.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="IApplication.RequestStop()"/>.
  13. /// </remarks>
  14. public class Dialog : Window
  15. {
  16. private static LineStyle _defaultBorderStyle = LineStyle.Heavy; // Resources/config.json overrides
  17. private static Alignment _defaultButtonAlignment = Alignment.End; // Resources/config.json overrides
  18. private static AlignmentModes _defaultButtonAlignmentModes = AlignmentModes.StartToEnd | AlignmentModes.AddSpaceBetweenItems; // Resources/config.json overrides
  19. private static int _defaultMinimumHeight = 80; // Resources/config.json overrides
  20. private static int _defaultMinimumWidth = 80; // Resources/config.json overrides
  21. private static ShadowStyle _defaultShadow = ShadowStyle.Transparent; // Resources/config.json overrides
  22. /// <summary>
  23. /// Initializes a new instance of the <see cref="Dialog"/> class with no <see cref="Button"/>s.
  24. /// </summary>
  25. /// <remarks>
  26. /// By default, <see cref="View.X"/>, <see cref="View.Y"/>, <see cref="View.Width"/>, and <see cref="View.Height"/> are
  27. /// set
  28. /// such that the <see cref="Dialog"/> will be centered in, and no larger than 90% of <see cref="IApplication.TopRunnable"/>, if
  29. /// there is one. Otherwise,
  30. /// it will be bound by the screen dimensions.
  31. /// </remarks>
  32. public Dialog ()
  33. {
  34. Arrangement = ViewArrangement.Movable | ViewArrangement.Overlapped;
  35. base.ShadowStyle = DefaultShadow;
  36. BorderStyle = DefaultBorderStyle;
  37. X = Pos.Center ();
  38. Y = Pos.Center ();
  39. Width = Dim.Auto (DimAutoStyle.Auto, Dim.Percent (DefaultMinimumWidth), Dim.Percent (90));
  40. Height = Dim.Auto (DimAutoStyle.Auto, Dim.Percent (DefaultMinimumHeight), Dim.Percent (90));
  41. SchemeName = SchemeManager.SchemesToSchemeName (Schemes.Dialog);
  42. Modal = true;
  43. ButtonAlignment = DefaultButtonAlignment;
  44. ButtonAlignmentModes = DefaultButtonAlignmentModes;
  45. }
  46. private readonly List<Button> _buttons = [];
  47. private bool _canceled;
  48. /// <summary>
  49. /// Adds a <see cref="Button"/> to the <see cref="Dialog"/>, its layout will be controlled by the
  50. /// <see cref="Dialog"/>
  51. /// </summary>
  52. /// <param name="button">Button to add.</param>
  53. public void AddButton (Button button)
  54. {
  55. // Use a distinct GroupId so users can use Pos.Align for other views in the Dialog
  56. button.X = Pos.Align (ButtonAlignment, ButtonAlignmentModes, GetHashCode ());
  57. button.Y = Pos.AnchorEnd ();
  58. _buttons.Add (button);
  59. Add (button);
  60. }
  61. // TODO: Update button.X = Pos.Justify when alignment changes
  62. /// <summary>Determines how the <see cref="Dialog"/> <see cref="Button"/>s are aligned along the bottom of the dialog.</summary>
  63. public Alignment ButtonAlignment { get; set; }
  64. /// <summary>
  65. /// Gets or sets the alignment modes for the dialog's buttons.
  66. /// </summary>
  67. public AlignmentModes ButtonAlignmentModes { get; set; }
  68. /// <summary>Optional buttons to lay out at the bottom of the dialog.</summary>
  69. public Button [] Buttons
  70. {
  71. get => _buttons.ToArray ();
  72. init
  73. {
  74. foreach (Button b in value)
  75. {
  76. AddButton (b);
  77. }
  78. }
  79. }
  80. /// <summary>Gets a value indicating whether the <see cref="Dialog"/> was canceled.</summary>
  81. /// <remarks>The default value is <see langword="true"/>.</remarks>
  82. public bool Canceled
  83. {
  84. get { return _canceled; }
  85. set
  86. {
  87. #if DEBUG_IDISPOSABLE
  88. if (EnableDebugIDisposableAsserts && WasDisposed)
  89. {
  90. throw new ObjectDisposedException (GetType ().FullName);
  91. }
  92. #endif
  93. _canceled = value;
  94. }
  95. }
  96. /// <summary>
  97. /// Defines the default border styling for <see cref="Dialog"/>. Can be configured via
  98. /// <see cref="ConfigurationManager"/>.
  99. /// </summary>
  100. [ConfigurationProperty (Scope = typeof (ThemeScope))]
  101. public new static LineStyle DefaultBorderStyle
  102. {
  103. get => _defaultBorderStyle;
  104. set => _defaultBorderStyle = value;
  105. }
  106. /// <summary>The default <see cref="Alignment"/> for <see cref="Dialog"/>.</summary>
  107. /// <remarks>This property can be set in a Theme.</remarks>
  108. [ConfigurationProperty (Scope = typeof (ThemeScope))]
  109. public static Alignment DefaultButtonAlignment
  110. {
  111. get => _defaultButtonAlignment;
  112. set => _defaultButtonAlignment = value;
  113. }
  114. /// <summary>The default <see cref="AlignmentModes"/> for <see cref="Dialog"/>.</summary>
  115. /// <remarks>This property can be set in a Theme.</remarks>
  116. [ConfigurationProperty (Scope = typeof (ThemeScope))]
  117. public static AlignmentModes DefaultButtonAlignmentModes
  118. {
  119. get => _defaultButtonAlignmentModes;
  120. set => _defaultButtonAlignmentModes = value;
  121. }
  122. /// <summary>
  123. /// Defines the default minimum Dialog height, as a percentage of the container width. Can be configured via
  124. /// <see cref="ConfigurationManager"/>.
  125. /// </summary>
  126. [ConfigurationProperty (Scope = typeof (ThemeScope))]
  127. public static int DefaultMinimumHeight
  128. {
  129. get => _defaultMinimumHeight;
  130. set => _defaultMinimumHeight = value;
  131. }
  132. /// <summary>
  133. /// Defines the default minimum Dialog width, as a percentage of the container width. Can be configured via
  134. /// <see cref="ConfigurationManager"/>.
  135. /// </summary>
  136. [ConfigurationProperty (Scope = typeof (ThemeScope))]
  137. public static int DefaultMinimumWidth
  138. {
  139. get => _defaultMinimumWidth;
  140. set => _defaultMinimumWidth = value;
  141. }
  142. /// <summary>
  143. /// Gets or sets whether all <see cref="Window"/>s are shown with a shadow effect by default.
  144. /// </summary>
  145. [ConfigurationProperty (Scope = typeof (ThemeScope))]
  146. public new static ShadowStyle DefaultShadow
  147. {
  148. get => _defaultShadow;
  149. set => _defaultShadow = value;
  150. }
  151. // Dialogs are Modal and Focus is indicated by their Border. The following code ensures the
  152. // Text of the dialog (e.g. for a MessageBox) is always drawn using the Normal Attribute.
  153. private bool _drawingText;
  154. /// <inheritdoc/>
  155. protected override bool OnDrawingText ()
  156. {
  157. _drawingText = true;
  158. return false;
  159. }
  160. /// <inheritdoc/>
  161. protected override void OnDrewText ()
  162. {
  163. _drawingText = false;
  164. }
  165. /// <inheritdoc />
  166. protected override bool OnGettingAttributeForRole (in VisualRole role, ref Attribute currentAttribute)
  167. {
  168. if (_drawingText && role is VisualRole.Focus && Border?.Thickness != Thickness.Empty)
  169. {
  170. currentAttribute = GetScheme ().Normal;
  171. return true;
  172. }
  173. return false;
  174. }
  175. }