Dialog.cs 6.1 KB

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