using Annulus; using Azimuth; using System.Collections.Generic; namespace Glass.Testbed { /// /// Overlapping Windows to drag, resize, raise and close, one with a menu bar, with a button bringing back /// whatever was closed. /// public class WindowTestCase : TestCase { private const int StageHeight = 360; private static readonly WindowMaterial Frame = new WindowMaterial { Background = UiPacksheetMaterials.Panel, TitleBar = UiPacksheetMaterials.BlueFlatPanel, Content = Material.Invisible, CloseButton = Widgets.CloseButton, }; private static readonly TextMaterial FileLabel = Widgets.MenuLabel("File"); private static readonly string[] FileMenuItems = { "Close" }; private readonly List windows = new List(); public WindowTestCase(string name) : base(name) { } private static void AddContentSizeReadout(Window window, ControlContainer into) { TextMaterial readoutMaterial = Widgets.FieldText(""); var readout = into.NewChild("content size readout"); readout.Material = readoutMaterial; readout.Bounds = Rect.FromPositionAndSize(Vector.Zero, new Vector(160, 40)); void ShowContentSize(Vector contentSize) { readoutMaterial.Text = $"{contentSize.X:F0} x {contentSize.Y:F0}"; } ShowContentSize(window.ContentSize); window.OnContentResized += ShowContentSize; } private Window AddWindow(string title, Window window, Rect bounds) { window.DebugName = title; window.WindowMaterial = Frame; window.ResizeCursors = ResizeCursorMaterials.ByZone; window.Caption.Material = Material.Invisible; window.Bounds = bounds; var titleLabel = window.Caption.CanvasChildren.NewChild("title"); titleLabel.Material = Widgets.WindowTitle(title); titleLabel.Bounds = window.Caption.CanvasRect; window.OnResized += _ => titleLabel.Bounds = window.Caption.CanvasRect; new Panel { Material = Widgets.CloseButtonLabel }.FillParent(window.CloseButton); Add(window); windows.Add(window); return window; } private static MenuButton NewFileMenu(Window window, MenuBar menuBar) { var header = new MenuButton { DebugName = "file menu", Group = menuBar.Group, UncheckedMaterial = Widgets.MenuHeader.Framing(FileLabel), CheckedMaterial = Widgets.OpenMenuHeader.Framing(FileLabel), }; header.Popup.DropdownPopupMaterial = Widgets.PopupList; header.Rebuild(FileMenuItems, item => Widgets.MenuLabel(item)); header.OnItemClicked += _ => window.Close(); Vector size = Vector.Max( header.UncheckedMaterial.PreferredSize(Constraints.Unbounded), header.CheckedMaterial.PreferredSize(Constraints.Unbounded)); header.Bounds = Rect.FromPositionAndSize(Vector.Zero, size); return header; } protected override void Populate() { AddCaption("Drag a title bar to move, an edge or corner to resize; x or File > Close closes a window"); Button reopen = Widgets.ButtonLabel("Reopen closed windows") .BuildControl(Constraints.Unbounded) .WrapIn(UiPacksheetMaterials.Button); reopen.DebugName = "reopen button"; reopen.OnClick += delegate (in MouseState _, MouseButtonFlags __) { foreach (Window window in windows) { window.Visible = true; } }; reopen.Bounds = Rect.FromPositionAndSize( ContentAggregator.NewRow(reopen.Bounds.Height).TopLeft, reopen.Bounds.Size); Add(reopen); Rect stage = ContentAggregator.NewRow(StageHeight); Window resizable = AddWindow("Resizable window", new Window(), Rect.FromPositionAndSize(stage.TopLeft + new Vector(20, 20), new Vector(260, 180))); AddContentSizeReadout(resizable, resizable.CanvasChildren); AddWindow("Fixed size, no close button", new Window { Resizable = false, ShowCloseButton = false }, Rect.FromPositionAndSize(stage.TopLeft + new Vector(160, 110), new Vector(260, 140))); Scalar menuBarHeight = Widgets.MenuHeader.Framing(FileLabel).PreferredSize(Constraints.Unbounded).Y; Window withMenu = AddWindow("With a menu bar", new Window(), Rect.FromPositionAndSize(stage.TopLeft + new Vector(460, 30), new Vector(280, 200))); (MenuBar menuBar, Panel body) = Widgets.AddMenuBar(withMenu, menuBarHeight); menuBar.CanvasChildren.Add(NewFileMenu(withMenu, menuBar)); AddContentSizeReadout(withMenu, body.CanvasChildren); } } }