Dialog.cs 6.4 KB

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