123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- using System.Text.Json.Serialization;
- namespace Terminal.Gui;
- /// <summary>
- /// The <see cref="Dialog"/> <see cref="View"/> is a <see cref="Window"/> that by default is centered and contains
- /// one or more <see cref="Button"/>s. It defaults to the <c>Colors.ColorSchemes ["Dialog"]</c> color scheme and has a
- /// 1 cell padding around the edges.
- /// </summary>
- /// <remarks>
- /// To run the <see cref="Dialog"/> modally, create the <see cref="Dialog"/>, and pass it to
- /// <see cref="Application.Run(Toplevel, Func{Exception, bool}, ConsoleDriver)"/>. This will execute the dialog until it terminates via the
- /// [ESC] or [CTRL-Q] key, or when one of the views or buttons added to the dialog calls
- /// <see cref="Application.RequestStop"/>.
- /// </remarks>
- public class Dialog : Window
- {
- // TODO: Reenable once border/borderframe design is settled
- /// <summary>
- /// Defines the default border styling for <see cref="Dialog"/>. Can be configured via
- /// <see cref="ConfigurationManager"/>.
- /// </summary>
- //[SerializableConfigurationProperty (Scope = typeof (ThemeScope))]
- //public static Border DefaultBorder { get; set; } = new Border () {
- // LineStyle = LineStyle.Single,
- //};
- private readonly List<Button> _buttons = new ();
- /// <summary>
- /// Initializes a new instance of the <see cref="Dialog"/> class using <see cref="LayoutStyle.Computed"/>
- /// positioning with no <see cref="Button"/>s.
- /// </summary>
- /// <remarks>
- /// By default, <see cref="View.X"/> and <see cref="View.Y"/> are set to <c>Pos.Center ()</c> and
- /// <see cref="View.Width"/> and <see cref="View.Height"/> are set to <c>Width = Dim.Percent (85)</c>, centering the
- /// Dialog vertically and horizontally.
- /// </remarks>
- public Dialog ()
- {
- Arrangement = ViewArrangement.Movable;
- X = Pos.Center ();
- Y = Pos.Center ();
- //ValidatePosDim = true;
- Width = Dim.Percent (85);
- Height = Dim.Percent (85);
- ColorScheme = Colors.ColorSchemes ["Dialog"];
- Modal = true;
- ButtonAlignment = DefaultButtonAlignment;
- ButtonAlignmentModes = DefaultButtonAlignmentModes;
- AddCommand (
- Command.QuitToplevel,
- () =>
- {
- Canceled = true;
- RequestStop ();
- return true;
- });
- KeyBindings.Add (Key.Esc, Command.QuitToplevel);
- }
- private bool _canceled;
- /// <summary>Gets a value indicating whether the <see cref="Dialog"/> was canceled.</summary>
- /// <remarks>The default value is <see langword="true"/>.</remarks>
- public bool Canceled
- {
- get
- {
- #if DEBUG_IDISPOSABLE
- if (WasDisposed)
- {
- throw new ObjectDisposedException (GetType ().FullName);
- }
- #endif
- return _canceled;
- }
- set
- {
- #if DEBUG_IDISPOSABLE
- if (WasDisposed)
- {
- throw new ObjectDisposedException (GetType ().FullName);
- }
- #endif
- _canceled = value;
- return;
- }
- }
- // TODO: Update button.X = Pos.Justify when alignment changes
- /// <summary>Determines how the <see cref="Dialog"/> <see cref="Button"/>s are aligned along the bottom of the dialog.</summary>
- public Alignment ButtonAlignment { get; set; }
- /// <summary>
- /// Gets or sets the alignment modes for the dialog's buttons.
- /// </summary>
- public AlignmentModes ButtonAlignmentModes { get; set; }
- /// <summary>Optional buttons to lay out at the bottom of the dialog.</summary>
- public Button [] Buttons
- {
- get => _buttons.ToArray ();
- init
- {
- if (value is null)
- {
- return;
- }
- foreach (Button b in value)
- {
- AddButton (b);
- }
- }
- }
- /// <summary>The default <see cref="Alignment"/> for <see cref="Dialog"/>.</summary>
- /// <remarks>This property can be set in a Theme.</remarks>
- [SerializableConfigurationProperty (Scope = typeof (ThemeScope))]
- [JsonConverter (typeof (JsonStringEnumConverter))]
- public static Alignment DefaultButtonAlignment { get; set; } = Alignment.End;
- /// <summary>The default <see cref="Alignment"/> for <see cref="Dialog"/>.</summary>
- /// <remarks>This property can be set in a Theme.</remarks>
- [SerializableConfigurationProperty (Scope = typeof (ThemeScope))]
- [JsonConverter (typeof (JsonStringEnumConverter))]
- public static AlignmentModes DefaultButtonAlignmentModes { get; set; } = AlignmentModes.StartToEnd | AlignmentModes.AddSpaceBetweenItems;
- /// <summary>
- /// Adds a <see cref="Button"/> to the <see cref="Dialog"/>, its layout will be controlled by the
- /// <see cref="Dialog"/>
- /// </summary>
- /// <param name="button">Button to add.</param>
- public void AddButton (Button button)
- {
- if (button is null)
- {
- return;
- }
- // Use a distinct GroupId so users can use Pos.Align for other views in the Dialog
- button.X = Pos.Align (ButtonAlignment, ButtonAlignmentModes, groupId: GetHashCode ());
- button.Y = Pos.AnchorEnd ();
- _buttons.Add (button);
- Add (button);
- SetNeedsDisplay ();
- if (IsInitialized)
- {
- LayoutSubviews ();
- }
- }
- /// <inheritdoc/>
- //public override void LayoutSubviews ()
- //{
- // if (_inLayout)
- // {
- // return;
- // }
- // _inLayout = true;
- // SetRelativeLayout(SuperView?.ContentSize ?? Driver.Screen.Size);
- // LayoutButtons ();
- // base.LayoutSubviews ();
- // _inLayout = false;
- //}
- // Get the width of all buttons, not including any Margin.
- internal int GetButtonsWidth ()
- {
- if (_buttons.Count == 0)
- {
- return 0;
- }
- //var widths = buttons.Select (b => b.TextFormatter.GetFormattedSize ().Width + b.BorderFrame.Thickness.Horizontal + b.Padding.Thickness.Horizontal);
- IEnumerable<int> widths = _buttons.Select (b => b.Frame.Width);
- return widths.Sum ();
- }
- }
|