Dialog.cs 6.4 KB

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