Dialog.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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;
  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. AddCommand (
  71. Command.QuitToplevel,
  72. () =>
  73. {
  74. Canceled = true;
  75. RequestStop ();
  76. return true;
  77. });
  78. KeyBindings.Add (Key.Esc, Command.QuitToplevel);
  79. }
  80. private bool _canceled;
  81. /// <summary>Gets a value indicating whether the <see cref="Dialog"/> was canceled.</summary>
  82. /// <remarks>The default value is <see langword="true"/>.</remarks>
  83. public bool Canceled
  84. {
  85. get
  86. {
  87. #if DEBUG_IDISPOSABLE
  88. if (WasDisposed)
  89. {
  90. throw new ObjectDisposedException (GetType ().FullName);
  91. }
  92. #endif
  93. return _canceled;
  94. }
  95. set
  96. {
  97. #if DEBUG_IDISPOSABLE
  98. if (WasDisposed)
  99. {
  100. throw new ObjectDisposedException (GetType ().FullName);
  101. }
  102. #endif
  103. _canceled = value;
  104. }
  105. }
  106. // TODO: Update button.X = Pos.Justify when alignment changes
  107. /// <summary>Determines how the <see cref="Dialog"/> <see cref="Button"/>s are aligned along the bottom of the dialog.</summary>
  108. public Alignment ButtonAlignment { get; set; }
  109. /// <summary>
  110. /// Gets or sets the alignment modes for the dialog's buttons.
  111. /// </summary>
  112. public AlignmentModes ButtonAlignmentModes { get; set; }
  113. /// <summary>Optional buttons to lay out at the bottom of the dialog.</summary>
  114. public Button [] Buttons
  115. {
  116. get => _buttons.ToArray ();
  117. init
  118. {
  119. if (value is null)
  120. {
  121. return;
  122. }
  123. foreach (Button b in value)
  124. {
  125. AddButton (b);
  126. }
  127. }
  128. }
  129. /// <summary>
  130. /// Adds a <see cref="Button"/> to the <see cref="Dialog"/>, its layout will be controlled by the
  131. /// <see cref="Dialog"/>
  132. /// </summary>
  133. /// <param name="button">Button to add.</param>
  134. public void AddButton (Button button)
  135. {
  136. if (button is null)
  137. {
  138. return;
  139. }
  140. // Use a distinct GroupId so users can use Pos.Align for other views in the Dialog
  141. button.X = Pos.Align (ButtonAlignment, ButtonAlignmentModes, GetHashCode ());
  142. button.Y = Pos.AnchorEnd ();
  143. _buttons.Add (button);
  144. Add (button);
  145. SetNeedsDisplay ();
  146. if (IsInitialized)
  147. {
  148. LayoutSubviews ();
  149. }
  150. }
  151. }