Dialog.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. namespace Terminal.Gui.Views;
  2. /// <summary>
  3. /// 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(IRunnable, 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.TopRunnableView"/>, 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. ButtonAlignment = DefaultButtonAlignment;
  43. ButtonAlignmentModes = DefaultButtonAlignmentModes;
  44. }
  45. private readonly List<Button> _buttons = [];
  46. private bool _canceled;
  47. /// <summary>
  48. /// Adds a <see cref="Button"/> to the <see cref="Dialog"/>, its layout will be controlled by the
  49. /// <see cref="Dialog"/>
  50. /// </summary>
  51. /// <param name="button">Button to add.</param>
  52. public void AddButton (Button button)
  53. {
  54. // Use a distinct GroupId so users can use Pos.Align for other views in the Dialog
  55. button.X = Pos.Align (ButtonAlignment, ButtonAlignmentModes, GetHashCode ());
  56. button.Y = Pos.AnchorEnd ();
  57. _buttons.Add (button);
  58. Add (button);
  59. }
  60. // TODO: Update button.X = Pos.Justify when alignment changes
  61. /// <summary>Determines how the <see cref="Dialog"/> <see cref="Button"/>s are aligned along the bottom of the dialog.</summary>
  62. public Alignment ButtonAlignment { get; set; }
  63. /// <summary>
  64. /// Gets or sets the alignment modes for the dialog's buttons.
  65. /// </summary>
  66. public AlignmentModes ButtonAlignmentModes { get; set; }
  67. /// <summary>Optional buttons to lay out at the bottom of the dialog.</summary>
  68. public Button [] Buttons
  69. {
  70. get => _buttons.ToArray ();
  71. init
  72. {
  73. foreach (Button b in value)
  74. {
  75. AddButton (b);
  76. }
  77. }
  78. }
  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 { return _canceled; }
  84. set
  85. {
  86. #if DEBUG_IDISPOSABLE
  87. if (EnableDebugIDisposableAsserts && WasDisposed)
  88. {
  89. throw new ObjectDisposedException (GetType ().FullName);
  90. }
  91. #endif
  92. _canceled = value;
  93. }
  94. }
  95. /// <summary>
  96. /// Defines the default border styling for <see cref="Dialog"/>. Can be configured via
  97. /// <see cref="ConfigurationManager"/>.
  98. /// </summary>
  99. [ConfigurationProperty (Scope = typeof (ThemeScope))]
  100. public new static LineStyle DefaultBorderStyle
  101. {
  102. get => _defaultBorderStyle;
  103. set => _defaultBorderStyle = value;
  104. }
  105. /// <summary>The default <see cref="Alignment"/> for <see cref="Dialog"/>.</summary>
  106. /// <remarks>This property can be set in a Theme.</remarks>
  107. [ConfigurationProperty (Scope = typeof (ThemeScope))]
  108. public static Alignment DefaultButtonAlignment
  109. {
  110. get => _defaultButtonAlignment;
  111. set => _defaultButtonAlignment = value;
  112. }
  113. /// <summary>The default <see cref="AlignmentModes"/> for <see cref="Dialog"/>.</summary>
  114. /// <remarks>This property can be set in a Theme.</remarks>
  115. [ConfigurationProperty (Scope = typeof (ThemeScope))]
  116. public static AlignmentModes DefaultButtonAlignmentModes
  117. {
  118. get => _defaultButtonAlignmentModes;
  119. set => _defaultButtonAlignmentModes = value;
  120. }
  121. /// <summary>
  122. /// Defines the default minimum Dialog height, as a percentage of the container width. Can be configured via
  123. /// <see cref="ConfigurationManager"/>.
  124. /// </summary>
  125. [ConfigurationProperty (Scope = typeof (ThemeScope))]
  126. public static int DefaultMinimumHeight
  127. {
  128. get => _defaultMinimumHeight;
  129. set => _defaultMinimumHeight = value;
  130. }
  131. /// <summary>
  132. /// Defines the default minimum Dialog width, as a percentage of the container width. Can be configured via
  133. /// <see cref="ConfigurationManager"/>.
  134. /// </summary>
  135. [ConfigurationProperty (Scope = typeof (ThemeScope))]
  136. public static int DefaultMinimumWidth
  137. {
  138. get => _defaultMinimumWidth;
  139. set => _defaultMinimumWidth = value;
  140. }
  141. /// <summary>
  142. /// Gets or sets whether all <see cref="Window"/>s are shown with a shadow effect by default.
  143. /// </summary>
  144. [ConfigurationProperty (Scope = typeof (ThemeScope))]
  145. public new static ShadowStyle DefaultShadow
  146. {
  147. get => _defaultShadow;
  148. set => _defaultShadow = value;
  149. }
  150. // Dialogs are Modal and Focus is indicated by their Border. The following code ensures the
  151. // Text of the dialog (e.g. for a MessageBox) is always drawn using the Normal Attribute.
  152. private bool _drawingText;
  153. /// <inheritdoc/>
  154. protected override bool OnDrawingText ()
  155. {
  156. _drawingText = true;
  157. return false;
  158. }
  159. /// <inheritdoc/>
  160. protected override void OnDrewText ()
  161. {
  162. _drawingText = false;
  163. }
  164. /// <inheritdoc />
  165. protected override bool OnGettingAttributeForRole (in VisualRole role, ref Attribute currentAttribute)
  166. {
  167. if (_drawingText && role is VisualRole.Focus && Border?.Thickness != Thickness.Empty)
  168. {
  169. currentAttribute = GetScheme ().Normal;
  170. return true;
  171. }
  172. return false;
  173. }
  174. }