//
// Dialog.cs: Dialog box
//
// Authors:
// Miguel de Icaza (miguel@gnome.org)
//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Serialization;
using NStack;
using Terminal.Gui;
using static Terminal.Gui.ConfigurationManager;
namespace Terminal.Gui {
///
/// The is a that by default is centered and contains one
/// or more s. It defaults to the 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 {
///
/// 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;
///
/// Defines the default border styling for . Can be configured via .
///
[SerializableConfigurationProperty (Scope = typeof (ThemeScope))]
public static Border DefaultBorder { get; set; } = new Border () {
BorderStyle = BorderStyle.Single,
};
internal List buttons = new List ();
///
/// Initializes a new instance of the class using positioning
/// and an optional set of s to display
///
/// Title for the dialog.
/// Width for the dialog.
/// Height for the dialog.
/// Optional buttons to lay out at the bottom of the dialog.
///
/// if width and height are both 0, the Dialog will be vertically and horizontally centered in the
/// container and the size will be 85% of the container.
/// After initialization use X , Y , Width , and Height to override this with a location or size.
///
///
/// Use the constructor that does not take a width and height instead.
///
public Dialog (ustring title, int width, int height, params Button [] buttons) : base (title: title, padding: 0, border: DefaultBorder)
{
SetInitialProperties (width, height, buttons);
}
private void SetInitialProperties (int width, int height, Button [] buttons)
{
X = Pos.Center ();
Y = Pos.Center ();
if (width == 0 & height == 0) {
Width = Dim.Percent (85);
Height = Dim.Percent (85);
} else {
Width = width;
Height = height;
}
ColorScheme = Colors.Dialog;
Modal = true;
ButtonAlignment = DefaultButtonAlignment;
if (buttons != null) {
foreach (var b in buttons) {
this.buttons.Add (b);
Add (b);
}
}
LayoutStarted += (s, args) => {
LayoutStartedHandler ();
};
}
///
/// Initializes a new instance of the class using .
///
///
///
/// Te Dialog will be vertically and horizontally centered in the container and the size will be 85% of the container.
/// After initialization use X , Y , Width , and Height to override this with a location or size.
///
///
/// Use to add buttons to the dialog.
///
///
public Dialog () : this (title: ustring.Empty, width: 0, height: 0, buttons: null) { }
///
/// Initializes a new instance of the class using positioning
/// and with an optional set of s to display
///
/// Title for the dialog.
/// Optional buttons to lay out at the bottom of the dialog.
///
/// Te Dialog will be vertically and horizontally centered in the container and the size will be 85% of the container.
/// After initialization use X , Y , Width , and Height to override this with a location or size.
///
public Dialog (ustring title, params Button [] buttons) : this (title: title, width: 0, height: 0, buttons: buttons) { }
///
/// Adds a to the , its layout will be controlled by the
///
/// Button to add.
public void AddButton (Button button)
{
if (button == null)
return;
buttons.Add (button);
Add (button);
SetNeedsDisplay ();
LayoutSubviews ();
}
// Get the width of all buttons, not including any spacing
internal int GetButtonsWidth ()
{
if (buttons.Count == 0) {
return 0;
}
return buttons.Select (b => b.Frame.Width).Sum ();
}
///
/// 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
}
///
/// Determines how the s are aligned along the
/// bottom of the dialog.
///
public ButtonAlignments ButtonAlignment { get; set; }
void LayoutStartedHandler ()
{
if (buttons.Count == 0 || !IsInitialized) return;
int shiftLeft = 0;
int buttonsWidth = GetButtonsWidth ();
switch (ButtonAlignment) {
case ButtonAlignments.Center:
// Center Buttons
shiftLeft = (Bounds.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 = Bounds.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)(Bounds.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 - always hard flush left
var left = Bounds.Width + Border.BorderThickness.Horizontal;
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
var prevButton = buttons [0];
prevButton.X = 0;
prevButton.Y = Pos.AnchorEnd (1);
for (int 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;
}
}
///
public override bool ProcessKey (KeyEvent kb)
{
switch (kb.Key) {
case Key.Esc:
Application.RequestStop (this);
return true;
}
return base.ProcessKey (kb);
}
}
}