Dialog.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. namespace Terminal.Gui;
  2. /// <summary>
  3. /// The <see cref="Dialog"/> <see cref="View"/> is a <see cref="Window"/> that by default is centered and contains
  4. /// one or more <see cref="Button"/>s. It defaults to the <c>Colors.ColorSchemes ["Dialog"]</c> color scheme and has a
  5. /// 1 cell padding around the edges.
  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. [SerializableConfigurationProperty (Scope = typeof (ThemeScope))]
  19. public static Alignment DefaultButtonAlignment { get; set; } = Alignment.End; // Default is set in config.json
  20. /// <summary>The default <see cref="AlignmentModes"/> for <see cref="Dialog"/>.</summary>
  21. /// <remarks>This property can be set in a Theme.</remarks>
  22. [SerializableConfigurationProperty (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. [SerializableConfigurationProperty (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. [SerializableConfigurationProperty (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. [SerializableConfigurationProperty (Scope = typeof (ThemeScope))]
  40. public new static ShadowStyle DefaultShadow { get; set; } = ShadowStyle.None; // Default is set in config.json
  41. /// <summary>
  42. /// Defines the default border styling for <see cref="Dialog"/>. Can be configured via
  43. /// <see cref="ConfigurationManager"/>.
  44. /// </summary>
  45. [SerializableConfigurationProperty (Scope = typeof (ThemeScope))]
  46. public new static LineStyle DefaultBorderStyle { get; set; } = LineStyle.Single; // Default is set in config.json
  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 there is one. Otherwise,
  55. /// it will be bound by the screen dimensions.
  56. /// </remarks>
  57. public Dialog ()
  58. {
  59. Arrangement = ViewArrangement.Movable | ViewArrangement.Overlapped;
  60. ShadowStyle = DefaultShadow;
  61. BorderStyle = DefaultBorderStyle;
  62. X = Pos.Center ();
  63. Y = Pos.Center ();
  64. Width = Dim.Auto (DimAutoStyle.Auto, Dim.Percent (DefaultMinimumWidth), Dim.Percent (90));
  65. Height = Dim.Auto (DimAutoStyle.Auto, Dim.Percent (DefaultMinimumHeight), Dim.Percent (90));
  66. ColorScheme = Colors.ColorSchemes ["Dialog"];
  67. Modal = true;
  68. ButtonAlignment = DefaultButtonAlignment;
  69. ButtonAlignmentModes = DefaultButtonAlignmentModes;
  70. }
  71. // BUGBUG: We override GetNormal/FocusColor because "Dialog" ColorScheme is goofy.
  72. // BUGBUG: By defn, a Dialog is Modal, and thus HasFocus is always true. OnDrawContent
  73. // BUGBUG: Calls these methods.
  74. // TODO: Fix this in https://github.com/gui-cs/Terminal.Gui/issues/2381
  75. /// <inheritdoc />
  76. public override Attribute GetNormalColor ()
  77. {
  78. return ColorScheme!.Normal;
  79. }
  80. /// <inheritdoc />
  81. public override Attribute GetFocusColor ()
  82. {
  83. return ColorScheme!.Normal;
  84. }
  85. private bool _canceled;
  86. /// <summary>Gets a value indicating whether the <see cref="Dialog"/> was canceled.</summary>
  87. /// <remarks>The default value is <see langword="true"/>.</remarks>
  88. public bool Canceled
  89. {
  90. get
  91. {
  92. #if DEBUG_IDISPOSABLE
  93. if (View.DebugIDisposable && WasDisposed)
  94. {
  95. throw new ObjectDisposedException (GetType ().FullName);
  96. }
  97. #endif
  98. return _canceled;
  99. }
  100. set
  101. {
  102. #if DEBUG_IDISPOSABLE
  103. if (View.DebugIDisposable && WasDisposed)
  104. {
  105. throw new ObjectDisposedException (GetType ().FullName);
  106. }
  107. #endif
  108. _canceled = value;
  109. }
  110. }
  111. // TODO: Update button.X = Pos.Justify when alignment changes
  112. /// <summary>Determines how the <see cref="Dialog"/> <see cref="Button"/>s are aligned along the bottom of the dialog.</summary>
  113. public Alignment ButtonAlignment { get; set; }
  114. /// <summary>
  115. /// Gets or sets the alignment modes for the dialog's buttons.
  116. /// </summary>
  117. public AlignmentModes ButtonAlignmentModes { get; set; }
  118. /// <summary>Optional buttons to lay out at the bottom of the dialog.</summary>
  119. public Button [] Buttons
  120. {
  121. get => _buttons.ToArray ();
  122. init
  123. {
  124. if (value is null)
  125. {
  126. return;
  127. }
  128. foreach (Button b in value)
  129. {
  130. AddButton (b);
  131. }
  132. }
  133. }
  134. /// <summary>
  135. /// Adds a <see cref="Button"/> to the <see cref="Dialog"/>, its layout will be controlled by the
  136. /// <see cref="Dialog"/>
  137. /// </summary>
  138. /// <param name="button">Button to add.</param>
  139. public void AddButton (Button button)
  140. {
  141. if (button is null)
  142. {
  143. return;
  144. }
  145. // Use a distinct GroupId so users can use Pos.Align for other views in the Dialog
  146. button.X = Pos.Align (ButtonAlignment, ButtonAlignmentModes, GetHashCode ());
  147. button.Y = Pos.AnchorEnd ();
  148. _buttons.Add (button);
  149. Add (button);
  150. }
  151. }