using Amphora; using Annulus; using Azimuth; using Glass; using System; namespace Prism.Glass { /// /// A dropdown that sets what a function does between its points, and follows the model. A mode the model refuses /// is shown as the mode it kept. /// public class FunctionModePicker : Panel { /// /// The names of the modes, in order. /// public static readonly string[] DefaultModeNames = { "Linear", "Smooth", "Step" }; // Set while the dropdown follows the model, so that doesn't read as the user picking. private bool following; private FunctionModePickerMaterial pickerMaterial; /// /// Raised with why, when the model refuses a mode that was picked. The dropdown is put back first. /// public event Action OnRefused; /// /// Gets the function being edited. /// public FunctionModel Model { get; } /// /// Gets the dropdown that picks the mode. /// public Dropdown ModeDropdown { get; } = new Dropdown(); /// /// Makes a picker for a function. It has no look until it is given a . /// /// /// Makes the control that shows a name, in a row or on the face. The dropdown is as wide as its widest /// control. /// /// One name per , or null for the defaults. public FunctionModePicker(FunctionModel model, Func buildLabel, string[] modeNames = null) { Model = model ?? throw new ArgumentNullException(nameof(model)); if (buildLabel == null) { throw new ArgumentNullException(nameof(buildLabel)); } modeNames ??= DefaultModeNames; int expected = Enum.GetValues(typeof(FunctionMode)).Length; if (modeNames.Length != expected) { throw new ArgumentException($"Needs {expected} names but got {modeNames.Length}.", nameof(modeNames)); } // A control can be given to one place only, so each name gets a row and a face of its own. ModeDropdown.Rebuild( modeNames, buildRow: name => buildLabel(name), buildFace: name => buildLabel(name)); CanvasChildren.Add(ModeDropdown); ModeDropdown.OnSelectionChanged += PickMode; model.Changed += _ => Follow(); ModeDropdown.Visible = false; Follow(); Layout(); } private void Follow() { following = true; ModeDropdown.Select((int)Model.Mode); following = false; } private void PickMode(int? index) { if (following || index is not int mode) { return; } Status picked = Model.SetMode((FunctionMode)mode); if (!picked.IsOk) { // A refusal raises no change, so nothing else would put the dropdown back. Follow(); OnRefused?.Invoke(picked); } } /// /// Gets or sets the look of the dropdown, which also sizes the picker to it. /// public FunctionModePickerMaterial PickerMaterial { get => pickerMaterial; set { pickerMaterial = value; Material = value.Background; if (value.Dropdown.HasValue) { ModeDropdown.DropdownMaterial = value.Dropdown.Value; } ModeDropdown.Visible = value.Dropdown.HasValue; Layout(); } } /// /// Sizes the picker to its dropdown and puts it in place. Setting does it too. /// public void Layout() { Vector inside = Vector.Zero; if (pickerMaterial.Dropdown.HasValue) { inside = ModeDropdown.PreferredSize(Constraints.Unbounded); } Vector boundsSize = inside; if (Material != null) { boundsSize = Material.BoundsSizeFor(inside); } Bounds = Bounds.WithSize(boundsSize); Rect remainder = CanvasRect; Rect row = remainder.NewRow(inside.Y, VerticalJustification.Top, 0, Shortfall.UseWhatIsLeft); ModeDropdown.Bounds = row.NewCol(inside.X, HorizontalJustification.Left, 0, Shortfall.UseWhatIsLeft); } } }