Dialog.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. /// <para>
  9. /// To run the <see cref="Dialog"/> modally, create the <see cref="Dialog"/>, and pass it to
  10. /// <see cref="IApplication.Run(Toplevel, Func{Exception, bool})"/>. This will execute the dialog until
  11. /// it terminates via the <see cref="Application.QuitKey"/> (`Esc` by default),
  12. /// or when one of the views or buttons added to the dialog calls
  13. /// <see cref="Application.RequestStop"/>.
  14. /// </para>
  15. /// <para>
  16. /// Dialog implements <see cref="IModalRunnable{TResult}"/> with <c>int?</c> as the result type.
  17. /// The <see cref="Result"/> property contains the index of the button that was clicked, or null if canceled.
  18. /// </para>
  19. /// </remarks>
  20. public class Dialog : Window, IModalRunnable<int?>
  21. {
  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.Current"/>, 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. // Subscribe to the button's Accept command to set Result
  61. button.Accepting += Button_Accepting;
  62. }
  63. private void Button_Accepting (object? sender, CommandEventArgs e)
  64. {
  65. // Set Result to the index of the button that was clicked
  66. if (sender is Button button)
  67. {
  68. int index = _buttons.IndexOf (button);
  69. if (index >= 0)
  70. {
  71. Result = index;
  72. // For backward compatibility, set Canceled = false
  73. Canceled = false;
  74. }
  75. }
  76. }
  77. // TODO: Update button.X = Pos.Justify when alignment changes
  78. /// <summary>Determines how the <see cref="Dialog"/> <see cref="Button"/>s are aligned along the bottom of the dialog.</summary>
  79. public Alignment ButtonAlignment { get; set; }
  80. /// <summary>
  81. /// Gets or sets the alignment modes for the dialog's buttons.
  82. /// </summary>
  83. public AlignmentModes ButtonAlignmentModes { get; set; }
  84. /// <summary>Optional buttons to lay out at the bottom of the dialog.</summary>
  85. public Button [] Buttons
  86. {
  87. get => _buttons.ToArray ();
  88. init
  89. {
  90. foreach (Button b in value)
  91. {
  92. AddButton (b);
  93. }
  94. }
  95. }
  96. /// <summary>Gets a value indicating whether the <see cref="Dialog"/> was canceled.</summary>
  97. /// <remarks>
  98. /// <para>The default value is <see langword="true"/>.</para>
  99. /// <para>
  100. /// <b>Obsolete:</b> Use <see cref="Result"/> instead. When <see cref="Result"/> is null, the dialog was canceled.
  101. /// This property is maintained for backward compatibility.
  102. /// </para>
  103. /// </remarks>
  104. [Obsolete ("Use Result property instead. Result == null indicates the dialog was canceled.")]
  105. public bool Canceled
  106. {
  107. get { return _canceled; }
  108. set
  109. {
  110. #if DEBUG_IDISPOSABLE
  111. if (EnableDebugIDisposableAsserts && WasDisposed)
  112. {
  113. throw new ObjectDisposedException (GetType ().FullName);
  114. }
  115. #endif
  116. _canceled = value;
  117. }
  118. }
  119. /// <summary>
  120. /// Gets or sets the result of the modal dialog operation.
  121. /// </summary>
  122. /// <remarks>
  123. /// <para>
  124. /// Contains the zero-based index of the button that was clicked to close the dialog,
  125. /// or null if the dialog was canceled (e.g., ESC key pressed).
  126. /// </para>
  127. /// <para>
  128. /// The button index corresponds to the order buttons were added via <see cref="AddButton"/> or
  129. /// the <see cref="Buttons"/> initializer.
  130. /// </para>
  131. /// <para>
  132. /// For backward compatibility with the <see cref="Canceled"/> property:
  133. /// - <see cref="Result"/> == null means the dialog was canceled (<see cref="Canceled"/> == true)
  134. /// - <see cref="Result"/> != null means a button was clicked (<see cref="Canceled"/> == false)
  135. /// </para>
  136. /// <para>
  137. /// This property implements <see cref="IModalRunnable{TResult}.Result"/> where TResult is <c>int?</c>.
  138. /// </para>
  139. /// </remarks>
  140. public int? Result { get; set; }
  141. /// <summary>
  142. /// Defines the default border styling for <see cref="Dialog"/>. Can be configured via
  143. /// <see cref="ConfigurationManager"/>.
  144. /// </summary>
  145. [ConfigurationProperty (Scope = typeof (ThemeScope))]
  146. public new static LineStyle DefaultBorderStyle { get; set; } = LineStyle.Heavy;
  147. /// <summary>The default <see cref="Alignment"/> for <see cref="Dialog"/>.</summary>
  148. /// <remarks>This property can be set in a Theme.</remarks>
  149. [ConfigurationProperty (Scope = typeof (ThemeScope))]
  150. public static Alignment DefaultButtonAlignment { get; set; } = Alignment.End;
  151. /// <summary>The default <see cref="AlignmentModes"/> for <see cref="Dialog"/>.</summary>
  152. /// <remarks>This property can be set in a Theme.</remarks>
  153. [ConfigurationProperty (Scope = typeof (ThemeScope))]
  154. public static AlignmentModes DefaultButtonAlignmentModes { get; set; } = AlignmentModes.StartToEnd | AlignmentModes.AddSpaceBetweenItems;
  155. /// <summary>
  156. /// Defines the default minimum Dialog height, as a percentage of the container width. Can be configured via
  157. /// <see cref="ConfigurationManager"/>.
  158. /// </summary>
  159. [ConfigurationProperty (Scope = typeof (ThemeScope))]
  160. public static int DefaultMinimumHeight { get; set; } = 80;
  161. /// <summary>
  162. /// Defines the default minimum Dialog width, as a percentage of the container width. Can be configured via
  163. /// <see cref="ConfigurationManager"/>.
  164. /// </summary>
  165. [ConfigurationProperty (Scope = typeof (ThemeScope))]
  166. public static int DefaultMinimumWidth { get; set; } = 80;
  167. /// <summary>
  168. /// Gets or sets whether all <see cref="Window"/>s are shown with a shadow effect by default.
  169. /// </summary>
  170. [ConfigurationProperty (Scope = typeof (ThemeScope))]
  171. public new static ShadowStyle DefaultShadow { get; set; } = ShadowStyle.Transparent;
  172. // Dialogs are Modal and Focus is indicated by their Border. The following code ensures the
  173. // Text of the dialog (e.g. for a MessageBox) is always drawn using the Normal Attribute.
  174. private bool _drawingText;
  175. /// <inheritdoc/>
  176. protected override bool OnDrawingText ()
  177. {
  178. _drawingText = true;
  179. return false;
  180. }
  181. /// <inheritdoc/>
  182. protected override void OnDrewText ()
  183. {
  184. _drawingText = false;
  185. }
  186. /// <inheritdoc />
  187. protected override bool OnGettingAttributeForRole (in VisualRole role, ref Attribute currentAttribute)
  188. {
  189. if (_drawingText && role is VisualRole.Focus && Border?.Thickness != Thickness.Empty)
  190. {
  191. currentAttribute = GetScheme ().Normal;
  192. return true;
  193. }
  194. return false;
  195. }
  196. }