using System; using System.Collections.Generic; using Annulus; using Azimuth; using Glass; namespace Clave.Glass { /// /// The Undo and Reset buttons of a bindings screen, side by side, apart from the screen so they can stay in view /// while it scrolls. It sizes itself to its buttons. /// public class KeyBindingsCommands : Control { private readonly KeyBindingsEditor editor; public KeyBindingsCommands(KeyBindingsEditor editor, KeyBindingsMaterial material) { this.editor = editor ?? throw new ArgumentNullException(nameof(editor)); SetMaterial(global::Glass.Material.Invisible); KeyBindingsText text = material.TextOrEnglish; UndoButton = CommandButton(material, text.Undo); UndoButton.OnActivate += (ButtonBase _, in KeyboardSnapshot __, KeyboardManager ___) => editor.Undo(); ResetButton = CommandButton(material, text.ResetToDefaults); ResetButton.OnActivate += (ButtonBase _, in KeyboardSnapshot __, KeyboardManager ___) => editor.ResetToDefaults(); foreach (KeyBindings source in editor.Sources) { source.Rebound += _ => ShowChanges(); } // Measure and arrange at once, since the buttons never change size. var sizes = new List { UndoButton.Size, ResetButton.Size }; Bounds = Rect.FromPositionAndSize(Vector.Zero, StripLayout.SizeOf(sizes, material.Spacing)); StripLayout.Arrange(new Control[] { UndoButton, ResetButton }, sizes, Rect.FromPositionAndSize(Vector.Zero, CanvasRect.Size), material.Spacing); ShowChanges(); } /// /// Gets the button that takes back the latest change, disabled while there is none. /// public Button UndoButton { get; } public Button ResetButton { get; } private Button CommandButton(KeyBindingsMaterial material, string text) { var label = new Label(material.ButtonTextStyle ?? new TextMaterialStyle(), text); Vector labelSize = label.PreferredSize(Constraints.Unbounded); var button = new Button(ButtonBehavior.Default) { ButtonMaterial = material.CommandButton }; button.Bounds = Rect.FromPositionAndSize(Vector.Zero, material.CommandButton.BoundsSizeFor(labelSize)); button.CanvasChildren.Add(label); label.Bounds = Rect.FromPositionAndSize(Vector.Zero, labelSize); CanvasChildren.Add(button); return button; } private void ShowChanges() { UndoButton.Enabled = editor.CanUndo; } } }