Dialog.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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))]
  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))]
  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; } = 25;
  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; } = 25;
  39. // TODO: Reenable once border/borderframe design is settled
  40. /// <summary>
  41. /// Defines the default border styling for <see cref="Dialog"/>. Can be configured via
  42. /// <see cref="ConfigurationManager"/>.
  43. /// </summary>
  44. //[SerializableConfigurationProperty (Scope = typeof (ThemeScope))]
  45. //public static Border DefaultBorder { get; set; } = new Border () {
  46. // LineStyle = LineStyle.Single,
  47. //};
  48. private readonly List<Button> _buttons = new ();
  49. /// <summary>
  50. /// Initializes a new instance of the <see cref="Dialog"/> class with no <see cref="Button"/>s.
  51. /// </summary>
  52. /// <remarks>
  53. /// By default, <see cref="View.X"/>, <see cref="View.Y"/>, <see cref="View.Width"/>, and <see cref="View.Height"/> are
  54. /// set
  55. /// 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,
  56. /// it will be bound by the screen dimensions.
  57. /// </remarks>
  58. public Dialog ()
  59. {
  60. Arrangement = ViewArrangement.Movable;
  61. X = Pos.Center ();
  62. Y = Pos.Center ();
  63. Width = Dim.Auto (DimAutoStyle.Content, Dim.Percent (DefaultMinimumWidth), Dim.Percent (90));
  64. Height = Dim.Auto (DimAutoStyle.Content, Dim.Percent (DefaultMinimumHeight), Dim.Percent (90));
  65. ColorScheme = Colors.ColorSchemes ["Dialog"];
  66. Modal = true;
  67. ButtonAlignment = DefaultButtonAlignment;
  68. ButtonAlignmentModes = DefaultButtonAlignmentModes;
  69. AddCommand (
  70. Command.QuitToplevel,
  71. () =>
  72. {
  73. Canceled = true;
  74. RequestStop ();
  75. return true;
  76. });
  77. KeyBindings.Add (Key.Esc, Command.QuitToplevel);
  78. }
  79. private bool _canceled;
  80. /// <summary>Gets a value indicating whether the <see cref="Dialog"/> was canceled.</summary>
  81. /// <remarks>The default value is <see langword="true"/>.</remarks>
  82. public bool Canceled
  83. {
  84. get
  85. {
  86. #if DEBUG_IDISPOSABLE
  87. if (WasDisposed)
  88. {
  89. throw new ObjectDisposedException (GetType ().FullName);
  90. }
  91. #endif
  92. return _canceled;
  93. }
  94. set
  95. {
  96. #if DEBUG_IDISPOSABLE
  97. if (WasDisposed)
  98. {
  99. throw new ObjectDisposedException (GetType ().FullName);
  100. }
  101. #endif
  102. _canceled = value;
  103. }
  104. }
  105. // TODO: Update button.X = Pos.Justify when alignment changes
  106. /// <summary>Determines how the <see cref="Dialog"/> <see cref="Button"/>s are aligned along the bottom of the dialog.</summary>
  107. public Alignment ButtonAlignment { get; set; }
  108. /// <summary>
  109. /// Gets or sets the alignment modes for the dialog's buttons.
  110. /// </summary>
  111. public AlignmentModes ButtonAlignmentModes { get; set; }
  112. /// <summary>Optional buttons to lay out at the bottom of the dialog.</summary>
  113. public Button [] Buttons
  114. {
  115. get => _buttons.ToArray ();
  116. init
  117. {
  118. if (value is null)
  119. {
  120. return;
  121. }
  122. foreach (Button b in value)
  123. {
  124. AddButton (b);
  125. }
  126. }
  127. }
  128. /// <summary>
  129. /// Adds a <see cref="Button"/> to the <see cref="Dialog"/>, its layout will be controlled by the
  130. /// <see cref="Dialog"/>
  131. /// </summary>
  132. /// <param name="button">Button to add.</param>
  133. public void AddButton (Button button)
  134. {
  135. if (button is null)
  136. {
  137. return;
  138. }
  139. // Use a distinct GroupId so users can use Pos.Align for other views in the Dialog
  140. button.X = Pos.Align (ButtonAlignment, ButtonAlignmentModes, GetHashCode ());
  141. button.Y = Pos.AnchorEnd ();
  142. _buttons.Add (button);
  143. Add (button);
  144. SetNeedsDisplay ();
  145. if (IsInitialized)
  146. {
  147. LayoutSubviews ();
  148. }
  149. }
  150. }