using Annulus; using Azimuth; using System; namespace Glass.Testbed { public class CheckboxTestCase : TestCase { private static readonly Vector BoxSize = Vector.Max( UiPacksheetMaterials.CheckboxUnchecked.PreferredSize(Constraints.Unbounded), UiPacksheetMaterials.CheckboxChecked.PreferredSize(Constraints.Unbounded)); public CheckboxTestCase(string name) : base(name) { } /// /// The row size a labeled checkbox needs to show caption beside the indicator without truncating it. /// private static Vector LabeledRowSize(string caption) { Vector labelSize = Widgets.FieldText(caption).PreferredSize(Constraints.Unbounded); return new Vector(BoxSize.X + labelSize.X, Math.Max(BoxSize.Y, labelSize.Y)); } private void AddBareCheckbox(Rect bounds, bool startChecked, bool enabled) { Checkbox box = new Checkbox { UncheckedMaterial = UiPacksheetMaterials.CheckboxUnchecked, CheckedMaterial = UiPacksheetMaterials.CheckboxChecked, Checked = startChecked, Enabled = enabled, Bounds = bounds, DebugName = "bare checkbox", }; Add(box); } private void AddLabeledCheckbox(Rect bounds, string caption, Anchor indicatorAnchor = Anchor.Left) { Checkbox box = new Checkbox { UncheckedMaterial = UiPacksheetMaterials.CheckboxUnchecked, CheckedMaterial = UiPacksheetMaterials.CheckboxChecked, IndicatorAnchor = indicatorAnchor, Bounds = bounds, DebugName = "labeled checkbox", }; // The label fills the space the indicator leaves, and the whole control is one hit target. Panel label = Widgets.FieldText(caption) .BuildControl(Constraints.Tight(box.CanvasRect.Size), out TextOverflow? truncation); label.DebugName = $"{caption} label"; box.CanvasChildren.Add(label); if (truncation != null) { box.Tooltip = Widgets.Tooltip(caption); } Add(box); } protected override void Populate() { AddCaption("Checkbox is the box graphic only — click to toggle"); Rect bareRow = ContentAggregator.NewRow(BoxSize.Y); AddBareCheckbox(bareRow.NewCol(BoxSize.X, margin: Gutter), startChecked: false, enabled: true); AddBareCheckbox(bareRow.NewCol(BoxSize.X, margin: Gutter), startChecked: true, enabled: true); AddCaption("Disabled, unchecked and checked"); Rect disabledRow = ContentAggregator.NewRow(BoxSize.Y); AddBareCheckbox(disabledRow.NewCol(BoxSize.X, margin: Gutter), startChecked: false, enabled: false); AddBareCheckbox(disabledRow.NewCol(BoxSize.X, margin: Gutter), startChecked: true, enabled: false); AddCaption("Labelled: one control, the label fills the space the indicator leaves"); const string leftCaption = "Toggle me from the text too"; Vector leftSize = LabeledRowSize(leftCaption); Rect leftRow = ContentAggregator.NewRow(leftSize.Y); AddLabeledCheckbox(leftRow.NewCol(leftSize.X), leftCaption); const string rightCaption = "Indicator on the right instead"; Vector rightSize = LabeledRowSize(rightCaption); Rect rightRow = ContentAggregator.NewRow(rightSize.Y); AddLabeledCheckbox(rightRow.NewCol(rightSize.X), rightCaption, Anchor.Right); const string topCaption = "Indicator anchored to the top"; Vector topLabelSize = Widgets.FieldText(topCaption).PreferredSize(Constraints.Unbounded); Rect topRow = ContentAggregator.NewRow(BoxSize.Y + topLabelSize.Y); AddLabeledCheckbox(topRow.NewCol(Math.Max(BoxSize.X, topLabelSize.X)), topCaption, Anchor.Top); // Narrower than the caption below on purpose, to demonstrate truncation — reuses leftSize, sized to a // much shorter caption, rather than a size derived from this one. AddCaption("A caption too long for its row truncates and gains a tooltip"); Rect narrowRow = ContentAggregator.NewRow(leftSize.Y); AddLabeledCheckbox(narrowRow.NewCol(leftSize.X), "This caption is deliberately much wider than the row it has been given to sit in"); } } }