Dialog.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. using System.Text.Json.Serialization;
  2. namespace Terminal.Gui;
  3. /// <summary>
  4. /// The <see cref="Dialog"/> <see cref="View"/> is a <see cref="Window"/> that by default is centered and contains
  5. /// one or more <see cref="Button"/>s. It defaults to the <c>Colors.ColorSchemes ["Dialog"]</c> color scheme and has a
  6. /// 1 cell padding around the edges.
  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>The default <see cref="Alignment"/> for <see cref="Dialog"/>.</summary>
  18. /// <remarks>This property can be set in a Theme.</remarks>
  19. [SerializableConfigurationProperty (Scope = typeof (ThemeScope))]
  20. [JsonConverter (typeof (JsonStringEnumConverter<Alignment>))]
  21. public static Alignment DefaultButtonAlignment { get; set; } = Alignment.End;
  22. /// <summary>The default <see cref="Alignment"/> for <see cref="Dialog"/>.</summary>
  23. /// <remarks>This property can be set in a Theme.</remarks>
  24. [SerializableConfigurationProperty (Scope = typeof (ThemeScope))]
  25. [JsonConverter (typeof (JsonStringEnumConverter<AlignmentModes>))]
  26. public static AlignmentModes DefaultButtonAlignmentModes { get; set; } = AlignmentModes.StartToEnd | AlignmentModes.AddSpaceBetweenItems;
  27. /// <summary>
  28. /// Defines the default minimum Dialog width, as a percentage of the container width. Can be configured via
  29. /// <see cref="ConfigurationManager"/>.
  30. /// </summary>
  31. [SerializableConfigurationProperty (Scope = typeof (ThemeScope))]
  32. public static int DefaultMinimumWidth { get; set; } = 80;
  33. /// <summary>
  34. /// Defines the default minimum Dialog height, as a percentage of the container width. Can be configured via
  35. /// <see cref="ConfigurationManager"/>.
  36. /// </summary>
  37. [SerializableConfigurationProperty (Scope = typeof (ThemeScope))]
  38. public static int DefaultMinimumHeight { get; set; } = 80;
  39. /// <summary>
  40. /// Gets or sets whether all <see cref="Window"/>s are shown with a shadow effect by default.
  41. /// </summary>
  42. [SerializableConfigurationProperty (Scope = typeof (ThemeScope))]
  43. [JsonConverter (typeof (JsonStringEnumConverter<ShadowStyle>))]
  44. public new static ShadowStyle DefaultShadow { get; set; } = ShadowStyle.None;
  45. /// <summary>
  46. /// Defines the default border styling for <see cref="Dialog"/>. Can be configured via
  47. /// <see cref="ConfigurationManager"/>.
  48. /// </summary>
  49. [SerializableConfigurationProperty (Scope = typeof (ThemeScope))]
  50. [JsonConverter (typeof (JsonStringEnumConverter<LineStyle>))]
  51. public new static LineStyle DefaultBorderStyle { get; set; } = LineStyle.Single;
  52. private readonly List<Button> _buttons = new ();
  53. /// <summary>
  54. /// Initializes a new instance of the <see cref="Dialog"/> class with no <see cref="Button"/>s.
  55. /// </summary>
  56. /// <remarks>
  57. /// By default, <see cref="View.X"/>, <see cref="View.Y"/>, <see cref="View.Width"/>, and <see cref="View.Height"/> are
  58. /// set
  59. /// 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,
  60. /// it will be bound by the screen dimensions.
  61. /// </remarks>
  62. public Dialog ()
  63. {
  64. Arrangement = ViewArrangement.Movable;
  65. ShadowStyle = DefaultShadow;
  66. BorderStyle = DefaultBorderStyle;
  67. X = Pos.Center ();
  68. Y = Pos.Center ();
  69. Width = Dim.Auto (DimAutoStyle.Auto, Dim.Percent (DefaultMinimumWidth), Dim.Percent (90));
  70. Height = Dim.Auto (DimAutoStyle.Auto, Dim.Percent (DefaultMinimumHeight), Dim.Percent (90));
  71. ColorScheme = Colors.ColorSchemes ["Dialog"];
  72. Modal = true;
  73. ButtonAlignment = DefaultButtonAlignment;
  74. ButtonAlignmentModes = DefaultButtonAlignmentModes;
  75. AddCommand (
  76. Command.QuitToplevel,
  77. () =>
  78. {
  79. Canceled = true;
  80. RequestStop ();
  81. return true;
  82. });
  83. KeyBindings.Add (Key.Esc, Command.QuitToplevel);
  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 (WasDisposed)
  94. {
  95. throw new ObjectDisposedException (GetType ().FullName);
  96. }
  97. #endif
  98. return _canceled;
  99. }
  100. set
  101. {
  102. #if DEBUG_IDISPOSABLE
  103. if (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. SetNeedsDisplay ();
  151. if (IsInitialized)
  152. {
  153. LayoutSubviews ();
  154. }
  155. }
  156. }