using Annulus;
using Azimuth;
using Glass.Editing;
using System;
namespace Glass.Testbed
{
///
/// TextBoxes over a spread of behaviors, typed into, selected with the mouse or keyboard, and scrolled. A readout
/// reports every OnTextChanged and OnSubmit.
///
public class TextBoxTestCase : TestCase
{
private const int BoxWidth = 260;
// One instance for every box but the Readline one, the way an app shares one across its whole UI.
private readonly TextBoxBehavior sharedBehavior = new TextBoxBehavior();
private Label readout;
public TextBoxTestCase(string name) : base(name) { }
private TextBox BuildBox(string debugName, string text, TextBoxBehavior behavior,
int maxLength = EditBuffer.NoLimit)
{
var box = new TextBox(behavior, maxLength) { DebugName = debugName, TextBoxMaterial = Widgets.TextBox };
box.Bounds = Rect.FromPositionAndSize(Vector.Zero, Widgets.TextBoxSizeFor(BoxWidth));
box.Text = text;
box.OnTextChanged += changed => readout.Text = $"OnTextChanged: {changed.DebugName} is \"{changed.Text}\"";
box.OnSubmit += submitted => readout.Text = $"OnSubmit: {submitted.DebugName} had \"{submitted.Text}\"";
return box;
}
///
/// A row of with beside it.
///
private void AddRow(TextBox box, string description)
{
var caption = new Label(Widgets.SmallCaptionStyle, description, Constraints.Unbounded);
Vector captionSize = caption.Bounds.Size;
Rect row = ContentAggregator.NewRow(Math.Max(box.Bounds.Height, captionSize.Y));
Rect boxSlot = row.NewCol(box.Bounds.Width, margin: Gutter);
box.Bounds = box.Bounds.WithPosition(boxSlot.TopLeft);
Add(box);
caption.Bounds = caption.Bounds.WithPosition(row.TopLeft + new Vector(0, (row.Height - captionSize.Y) / 2));
Add(caption);
}
protected override void Populate()
{
AddCaption("Click a box to type in it. Drag to select, double click for a word, triple click for all.");
AddCaption("Arrows, Home, End and Ctrl+arrows move (Shift extends). Tab and Shift+Tab change box.");
AddCaption("Ctrl+C, X and V, or Ctrl+Insert, Shift+Delete and Shift+Insert, copy, cut and paste.");
AddCaption("A browser pastes through its own paste event, and may ask permission to copy.");
readout = new Label(Widgets.CaptionStyle, "Nothing yet");
readout.Bounds = ContentAggregator.NewRow(readout.PreferredSize(Constraints.Unbounded).Y);
Add(readout);
AddRow(BuildBox("plain", "Hello, world", sharedBehavior), "Windows keys");
AddRow(BuildBox("long", "The quick brown fox jumps over the lazy dog, and then keeps running",
sharedBehavior),
"Text wider than the box scrolls to keep the caret in view");
AddRow(BuildBox("short", "12345", sharedBehavior, maxLength: 8), "At most eight chars");
var readline = new TextBoxBehavior { Bindings = EditBindings.Readline };
AddRow(BuildBox("readline", "Ctrl+A, E, B, F, K, U, W", readline),
"Readline keys: Ctrl+A and Ctrl+E for line start and end, Ctrl+K and Ctrl+U to kill");
var steady = new TextBoxBehavior { BlinkInterval = Amphora.Duration.Zero };
AddRow(BuildBox("steady", "No blinking caret", steady), "A caret that never blinks");
TextBox disabled = BuildBox("disabled", "Can't be edited", sharedBehavior);
disabled.Enabled = false;
AddRow(disabled, "Disabled");
AddRow(BuildBox("empty", "", sharedBehavior), "Empty");
}
}
}