using System.Text.Json.Serialization;
namespace Terminal.Gui;
///
/// The is a that by default is centered and contains
/// one or more s. It defaults to the Colors.ColorSchemes ["Dialog"] color scheme and has a
/// 1 cell padding around the edges.
///
///
/// To run the modally, create the , and pass it to
/// . 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
/// .
///
public class Dialog : Window
{
/// Determines the horizontal alignment of the Dialog buttons.
public enum ButtonAlignments
{
/// Center-aligns the buttons (the default).
Center = 0,
/// Justifies the buttons
Justify,
/// Left-aligns the buttons
Left,
/// Right-aligns the buttons
Right
}
// TODO: Reenable once border/borderframe design is settled
///
/// Defines the default border styling for . Can be configured via
/// .
///
//[SerializableConfigurationProperty (Scope = typeof (ThemeScope))]
//public static Border DefaultBorder { get; set; } = new Border () {
// LineStyle = LineStyle.Single,
//};
private readonly List _buttons = new ();
private bool _inLayout;
///
/// Initializes a new instance of the class using
/// positioning with no s.
///
///
/// By default, and are set to Pos.Center () and
/// and are set to Width = Dim.Percent (85) , centering the
/// Dialog vertically and horizontally.
///
public Dialog ()
{
X = Pos.Center ();
Y = Pos.Center ();
ValidatePosDim = true;
Width = Dim.Percent (85); // Dim.Auto (min: Dim.Percent (10));
Height = Dim.Percent (85); //Dim.Auto (min: Dim.Percent (50));
ColorScheme = Colors.ColorSchemes ["Dialog"];
Modal = true;
ButtonAlignment = DefaultButtonAlignment;
KeyBindings.Add (Key.Esc, Command.QuitToplevel);
}
/// Determines how the s are aligned along the bottom of the dialog.
public ButtonAlignments ButtonAlignment { get; set; }
/// Optional buttons to lay out at the bottom of the dialog.
public Button [] Buttons
{
get => _buttons.ToArray ();
init
{
if (value is null)
{
return;
}
foreach (Button b in value)
{
AddButton (b);
}
}
}
/// The default for .
/// This property can be set in a Theme.
[SerializableConfigurationProperty (Scope = typeof (ThemeScope))]
[JsonConverter (typeof (JsonStringEnumConverter))]
public static ButtonAlignments DefaultButtonAlignment { get; set; } = ButtonAlignments.Center;
///
/// Adds a to the , its layout will be controlled by the
///
///
/// Button to add.
public void AddButton (Button button)
{
if (button is null)
{
return;
}
//button.AutoSize = false; // BUGBUG: v2 - Hack to get around autosize not accounting for Margin?
_buttons.Add (button);
Add (button);
SetNeedsDisplay ();
if (IsInitialized)
{
LayoutSubviews ();
}
}
///
public override void LayoutSubviews ()
{
if (_inLayout)
{
return;
}
_inLayout = true;
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 widths = _buttons.Select (b => b.Frame.Width);
return widths.Sum ();
}
private void LayoutButtons ()
{
if (_buttons.Count == 0 || !IsInitialized)
{
return;
}
var shiftLeft = 0;
int buttonsWidth = GetButtonsWidth ();
switch (ButtonAlignment)
{
case ButtonAlignments.Center:
// Center Buttons
shiftLeft = (Viewport.Width - buttonsWidth - _buttons.Count - 1) / 2 + 1;
for (int i = _buttons.Count - 1; i >= 0; i--)
{
Button button = _buttons [i];
shiftLeft += button.Frame.Width + (i == _buttons.Count - 1 ? 0 : 1);
if (shiftLeft > -1)
{
button.X = Pos.AnchorEnd (shiftLeft);
}
else
{
button.X = Viewport.Width - shiftLeft;
}
button.Y = Pos.AnchorEnd (1);
}
break;
case ButtonAlignments.Justify:
// Justify Buttons
// leftmost and rightmost buttons are hard against edges. The rest are evenly spaced.
var spacing = (int)Math.Ceiling ((double)(Viewport.Width - buttonsWidth) / (_buttons.Count - 1));
for (int i = _buttons.Count - 1; i >= 0; i--)
{
Button button = _buttons [i];
if (i == _buttons.Count - 1)
{
shiftLeft += button.Frame.Width;
button.X = Pos.AnchorEnd (shiftLeft);
}
else
{
if (i == 0)
{
// first (leftmost) button
int left = Viewport.Width;
button.X = Pos.AnchorEnd (left);
}
else
{
shiftLeft += button.Frame.Width + spacing;
button.X = Pos.AnchorEnd (shiftLeft);
}
}
button.Y = Pos.AnchorEnd (1);
}
break;
case ButtonAlignments.Left:
// Left Align Buttons
Button prevButton = _buttons [0];
prevButton.X = 0;
prevButton.Y = Pos.AnchorEnd (1);
for (var i = 1; i < _buttons.Count; i++)
{
Button button = _buttons [i];
button.X = Pos.Right (prevButton) + 1;
button.Y = Pos.AnchorEnd (1);
prevButton = button;
}
break;
case ButtonAlignments.Right:
// Right align buttons
shiftLeft = _buttons [_buttons.Count - 1].Frame.Width;
_buttons [_buttons.Count - 1].X = Pos.AnchorEnd (shiftLeft);
_buttons [_buttons.Count - 1].Y = Pos.AnchorEnd (1);
for (int i = _buttons.Count - 2; i >= 0; i--)
{
Button button = _buttons [i];
shiftLeft += button.Frame.Width + 1;
button.X = Pos.AnchorEnd (shiftLeft);
button.Y = Pos.AnchorEnd (1);
}
break;
}
}
}