using Amphora; using Annulus; using Azimuth; using Glass; using System; namespace Prism.Glass { /// /// Two dropdowns that set the space a gradient blends in and which way it goes round the hue circle. The direction /// one is hidden for a space with no hue, and both follow the model. /// public class BlendPicker : Panel { /// /// The names of the spaces, in order. /// public static readonly string[] DefaultSpaceNames = { "Linear light", "Oklab", "Oklch", "HSV", "HSL" }; /// /// The names of the directions, in order. /// public static readonly string[] DefaultDirectionNames = { "Shortest", "Longest", "Increasing", "Decreasing" }; // Set while the dropdowns follow the model, so that doesn't read as the user picking. private bool following; private BlendPickerMaterial blendMaterial; /// /// Gets the gradient being edited. /// public GradientModel Model { get; } /// /// Raised with why, when the model refuses a space or direction that was picked. The dropdowns are put back /// first. /// public event Action OnRefused; /// /// Gets the dropdown that picks the space. /// public Dropdown SpaceDropdown { get; } = new Dropdown(); /// /// Gets the dropdown that picks the hue direction. /// public Dropdown DirectionDropdown { get; } = new Dropdown(); /// /// Returns whether a space has a hue for a blend to go round. /// public static bool HasHue(GradientSpace space) { return space == GradientSpace.Oklch || space == GradientSpace.Hsv || space == GradientSpace.Hsl; } private static void Fill(Dropdown dropdown, string[] names, Func buildLabel) { // A control can be given to one place only, so each name gets a row and a face of its own. dropdown.Rebuild( names, buildRow: name => buildLabel(name), buildFace: name => buildLabel(name)); } /// /// Makes a picker for a gradient. It has no look until it is given a . /// /// /// Makes the control that shows a name, in a row or on a face. Each dropdown is as wide as its widest control. /// /// One name per , or null for the defaults. /// One name per , or null for the defaults. public BlendPicker( GradientModel model, Func buildLabel, string[] spaceNames = null, string[] directionNames = null) { Model = model ?? throw new ArgumentNullException(nameof(model)); if (buildLabel == null) { throw new ArgumentNullException(nameof(buildLabel)); } spaceNames ??= DefaultSpaceNames; directionNames ??= DefaultDirectionNames; CheckNames(spaceNames, Enum.GetValues(typeof(GradientSpace)).Length, nameof(spaceNames)); CheckNames(directionNames, Enum.GetValues(typeof(HueDirection)).Length, nameof(directionNames)); Fill(SpaceDropdown, spaceNames, buildLabel); Fill(DirectionDropdown, directionNames, buildLabel); CanvasChildren.Add(SpaceDropdown); CanvasChildren.Add(DirectionDropdown); SpaceDropdown.OnSelectionChanged += PickSpace; DirectionDropdown.OnSelectionChanged += PickDirection; model.Changed += _ => Follow(); SpaceDropdown.Visible = false; Follow(); Layout(); } private static void CheckNames(string[] names, int expected, string parameterName) { if (names.Length != expected) { throw new ArgumentException($"Needs {expected} names but got {names.Length}.", parameterName); } } // A refused pick puts the dropdowns back on the model first, so a listener sees them as they are left. private void PickSpace(int? index) { if (following || !(index is int space)) { return; } Status picked = Model.SetSpace((GradientSpace)space); if (!picked.IsOk) { Follow(); OnRefused?.Invoke(picked); } } private void PickDirection(int? index) { if (following || !(index is int direction)) { return; } Status picked = Model.SetDirection((HueDirection)direction); if (!picked.IsOk) { Follow(); OnRefused?.Invoke(picked); } } private void Follow() { following = true; SpaceDropdown.Select((int)Model.Space); DirectionDropdown.Select((int)Model.Direction); DirectionDropdown.Visible = blendMaterial.Dropdown.HasValue && HasHue(Model.Space); following = false; } /// /// Gets or sets the look of the dropdowns, which also sizes the picker to them. /// public BlendPickerMaterial BlendMaterial { get => blendMaterial; set { blendMaterial = value; Material = value.Background; if (value.Dropdown.HasValue) { SpaceDropdown.DropdownMaterial = value.Dropdown.Value; DirectionDropdown.DropdownMaterial = value.Dropdown.Value; } SpaceDropdown.Visible = value.Dropdown.HasValue; DirectionDropdown.Visible = value.Dropdown.HasValue && HasHue(Model.Space); Layout(); } } /// /// Sizes the picker to its dropdowns and puts them in place. Setting does it too. /// public void Layout() { Scalar spacing = blendMaterial.Spacing; Vector spaceSize = Vector.Zero; Vector directionSize = Vector.Zero; if (blendMaterial.Dropdown.HasValue) { spaceSize = SpaceDropdown.PreferredSize(Constraints.Unbounded); directionSize = DirectionDropdown.PreferredSize(Constraints.Unbounded); } // The direction dropdown keeps its room when hidden, so choosing a space never moves the picker's edge. var across = new RectAggregator(new Vector(spacing, 0)); across.NewCol(spaceSize.X); across.NewCol(directionSize.X); var inside = new Vector(across.Rect.Width, Scalar.Max(spaceSize.Y, directionSize.Y)); Vector boundsSize = inside; if (Material != null) { boundsSize = Material.BoundsSizeFor(inside); } Bounds = Bounds.WithSize(boundsSize); Rect remainder = CanvasRect; const Shortfall tooSmall = Shortfall.UseWhatIsLeft; Rect spaceCell = remainder.NewCol(spaceSize.X, HorizontalJustification.Left, spacing, tooSmall); Rect directionCell = remainder.NewCol(directionSize.X, HorizontalJustification.Left, 0, tooSmall); SpaceDropdown.Bounds = spaceCell.NewRow(spaceSize.Y, VerticalJustification.Top, 0, tooSmall); DirectionDropdown.Bounds = directionCell.NewRow(directionSize.Y, VerticalJustification.Top, 0, tooSmall); } } }