Dialog.cs 6.4 KB

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