diff --git a/Cargo.lock b/Cargo.lock index 39b7cf0f5..96a9f4e10 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4252,6 +4252,7 @@ name = "craft-tracked" version = "0.1.0" dependencies = [ "craft-ui-macros", + "serde", ] [[package]] diff --git a/crates/color-ui/src/gradient/value.rs b/crates/color-ui/src/gradient/value.rs index a0d70a8fc..76819b4c2 100644 --- a/crates/color-ui/src/gradient/value.rs +++ b/crates/color-ui/src/gradient/value.rs @@ -7,6 +7,7 @@ use serde::{Deserialize, Serialize}; /// How a gradient's opacity is stored, which decides the rows of its stop bar. #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] +#[cfg_attr(feature = "ui", derive(craft_ui::contract::MemoProps))] #[non_exhaustive] pub enum StopRows { /// Opacity stops in their own row above the ramp, colour stops below; a colour's alpha is @@ -36,6 +37,7 @@ pub enum StopRow { /// A colour stop. #[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)] +#[cfg_attr(feature = "ui", derive(craft_ui::contract::MemoProps))] #[serde(rename_all = "camelCase")] #[non_exhaustive] pub struct ColorStop { @@ -56,6 +58,7 @@ pub struct ColorStop { /// An opacity stop of a [`StopRows::Split`] gradient. #[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)] +#[cfg_attr(feature = "ui", derive(craft_ui::contract::MemoProps))] #[serde(rename_all = "camelCase")] #[non_exhaustive] pub struct OpacityStop { @@ -120,6 +123,7 @@ impl OpacityStop { /// A gradient's stops, each list sorted by location. #[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] +#[cfg_attr(feature = "ui", derive(craft_ui::contract::MemoProps))] #[serde(rename_all = "camelCase")] #[non_exhaustive] pub struct GradientStops { diff --git a/crates/color-ui/src/palette.rs b/crates/color-ui/src/palette.rs index b8d97267e..fb7b69230 100644 --- a/crates/color-ui/src/palette.rs +++ b/crates/color-ui/src/palette.rs @@ -97,6 +97,7 @@ impl SwatchFormat { /// A swatch colour in its own model. #[derive(Clone, Copy, Debug, PartialEq, serde::Serialize, serde::Deserialize)] +#[cfg_attr(feature = "ui", derive(craft_ui::contract::MemoProps))] #[serde(tag = "model", content = "values", rename_all = "camelCase")] pub enum SwatchColor { /// Encoded RGB, 0–1 each, in the RGB working space. @@ -148,6 +149,7 @@ impl SwatchColor { /// How an `.ase` file marks a colour. #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)] +#[cfg_attr(feature = "ui", derive(craft_ui::contract::MemoProps))] #[serde(rename_all = "camelCase")] pub enum SwatchKind { #[default] @@ -161,6 +163,7 @@ pub enum SwatchKind { /// One named swatch. #[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)] +#[cfg_attr(feature = "ui", derive(craft_ui::contract::MemoProps))] pub struct SwatchEntry { /// The swatch's label. pub name: String, @@ -181,6 +184,7 @@ impl SwatchEntry { /// A named group of swatches, as the Swatches panel shows them. A file without groups reads as /// one group named after the file. #[derive(Clone, Debug, Default, PartialEq, serde::Serialize, serde::Deserialize)] +#[cfg_attr(feature = "ui", derive(craft_ui::contract::MemoProps))] pub struct SwatchGroup { /// The group's label. pub name: String, diff --git a/crates/color-ui/src/ui/mod.rs b/crates/color-ui/src/ui/mod.rs index 4f08f463d..7d3c18cc6 100644 --- a/crates/color-ui/src/ui/mod.rs +++ b/crates/color-ui/src/ui/mod.rs @@ -87,7 +87,7 @@ pub use screen_picker::{CaptureResult, CaptureSource, LOUPE_PIXELS, LOUPE_ZOOM, pub use strip::{ColorStrip, ColorStripStyle}; pub use swatches::{ MAX_GROUPS, MAX_SWATCHES, SwatchAt, SwatchEdit, SwatchKey, SwatchLibrary, SwatchesCommand, SwatchesFileOutput, SwatchesMenu, SwatchesMenuOutput, - SwatchesPanel, SwatchesPanelOutput, SwatchesState, SwatchesWords, swatch_rows, + SwatchesPanel, SwatchesPanelOutput, SwatchesState, SwatchesWords, swatch_rows, swatch_source, }; pub use warning::{ColorWarning, ColorWarningArrangement, ColorWarningChip, ColorWarningChipOutput, ColorWarningChipStyle}; pub use wheel::{ diff --git a/crates/color-ui/src/ui/paint_chip.rs b/crates/color-ui/src/ui/paint_chip.rs index 8db24aab3..2893b5a41 100644 --- a/crates/color-ui/src/ui/paint_chip.rs +++ b/crates/color-ui/src/ui/paint_chip.rs @@ -14,14 +14,14 @@ use craft_behavior::{Axis, EditEvent, EditEvents, Metrics, PartKey}; use craft_cms::convert::CmykConverter; use craft_theme::Srgba; use craft_ui::buttons::Button; -use craft_ui::choice::{ChoiceWords, Segmented, Swatch, SwatchFace}; +use craft_ui::choice::{ChoiceWords, Segmented, Swatch}; use craft_ui::contract::{ Access, Activation, Bind, Common, Component, Cx, DisabledLook, Edit as _, EditResponse, Entry, Outcome, Overlay, OverlayUi, Overlays as _, Pages, PaintUi, PartsCx, Pointer as _, PopupKind, Props as _, Remember as _, Role, Show as _, ShowContainer as _, WordSetCx as _, }; use craft_ui::display::{Shade, Text}; use craft_ui::layout_components::Row; -use craft_ui::patterns::presets::{DefaultOpen, PresetBrowser, PresetEvent, PresetState, ThumbInfo}; +use craft_ui::patterns::presets::{DefaultOpen, PresetBrowser, PresetEvent, PresetState}; use craft_ui::primitives::textures::CachedTexture; use craft_ui::primitives::{ContentPainterCx, PaintShape, StrokeWidth}; use craft_ui::runtime::env::{Env, Label}; @@ -30,7 +30,7 @@ use craft_ui::style::{ComponentStyle, DataColor, FocusLook, StyleColor, StyleCx} use egui::{Color32, Pos2, Rect, Response, Ui, pos2, vec2}; use super::popover::compact_picker; -use super::{PickedColor, ScreenPickGesture, SwatchKey, SwatchPalette, SwatchesWords, swatch_rows}; +use super::{PickedColor, ScreenPickGesture, SwatchKey, SwatchPalette, swatch_source}; use crate::palette::{SwatchColor, SwatchGroup}; pub(crate) mod paint_list; @@ -475,7 +475,6 @@ impl Component for PaintChip<'_> { let solid = cx.memory::().0.unwrap_or(self.fallback); let current = FillKind::of(&paint); let sampled = self.sampled; - let swatch_words = cx.words::(); let recent_cells: Vec<(Label, Srgba)> = self.recent.iter().map(|color| (Label::from(color.to_hex()), *color)).collect(); let full_picker = self.full_picker; let cmyk = self.cmyk.unwrap_or_else(|| CmykConverter::coated()); @@ -514,21 +513,14 @@ impl Component for PaintChip<'_> { } if let Some((groups, state)) = swatches.as_mut() { let groups: &[SwatchGroup] = groups; - let rows = swatch_rows(groups, &swatch_words); + let source = swatch_source(groups, cmyk); let colors: HashMap<&str, &SwatchColor> = groups.iter().flat_map(|g| g.entries.iter().map(|e| (e.name.as_str(), &e.color))).collect(); - let out = PresetBrowser::new(words.swatches.clone(), state.reborrowed(), &rows) + let out = PresetBrowser::new(words.swatches.clone(), state.reborrowed(), &source) .toolbar(false) .row_menu(false) .default_open(DefaultOpen::None) .room_height(Env::of(ui).tokens.size.control.sm * SWATCH_ROWS) - .thumb(|painter: &ContentPainterCx<'_>, rect: Rect, info: ThumbInfo<'_, SwatchKey>| { - if let SwatchKey::Swatch(name) = info.key - && let Some(color) = colors.get(name.as_str()) - { - SwatchFace::new(color.to_srgb(cmyk)).selected(info.selected).paint(painter, rect); - } - }) .show(ui); for event in out.events { if let PresetEvent::Select { key: SwatchKey::Swatch(name), .. } = event diff --git a/crates/color-ui/src/ui/swatches.rs b/crates/color-ui/src/ui/swatches.rs index 17bed44a5..d66a11bab 100644 --- a/crates/color-ui/src/ui/swatches.rs +++ b/crates/color-ui/src/ui/swatches.rs @@ -11,7 +11,7 @@ pub(crate) mod panel; #[cfg(feature = "suite")] pub(crate) mod stories; -pub use library::{SwatchAt, SwatchEdit, SwatchKey, SwatchLibrary, SwatchesState, swatch_rows}; +pub use library::{SwatchAt, SwatchEdit, SwatchKey, SwatchLibrary, SwatchesState, swatch_rows, swatch_source}; pub use menu::{SwatchesCommand, SwatchesFileOutput, SwatchesMenu, SwatchesMenuOutput}; pub use panel::{SwatchesPanel, SwatchesPanelOutput}; diff --git a/crates/color-ui/src/ui/swatches/library.rs b/crates/color-ui/src/ui/swatches/library.rs index b5ca7d726..2ab31c803 100644 --- a/crates/color-ui/src/ui/swatches/library.rs +++ b/crates/color-ui/src/ui/swatches/library.rs @@ -2,8 +2,15 @@ use std::collections::HashSet; +use craft_cms::convert::CmykConverter; +use craft_theme::Srgba; +use craft_ui::choice::SwatchFace; use craft_ui::contract::ReorderMove; -use craft_ui::patterns::presets::{PresetRow, PresetState}; +use craft_ui::patterns::presets::{PresetFns, PresetRow, PresetSource, PresetState, ThumbInfo}; +use craft_ui::primitives::ContentPainterCx; +use craft_ui::runtime::env::Env; +use egui::Rect; +use std::collections::BTreeMap; use super::{MAX_GROUPS, MAX_SWATCHES, SWATCH_ZOOM, SwatchesWords}; use crate::palette::{SwatchColor, SwatchEntry, SwatchGroup, SwatchReport}; @@ -11,7 +18,7 @@ use crate::ui::PickedColor; /// What the browser names its rows by. Swatch names are unique across the library and group names among groups, so /// both survive reordering. -#[derive(Clone, Debug, PartialEq, Eq, Hash)] +#[derive(Clone, Debug, PartialEq, Eq, Hash, craft_ui::contract::MemoProps)] #[non_exhaustive] pub enum SwatchKey { /// A group, by name. @@ -556,8 +563,27 @@ impl SwatchLibrary { } } -/// The rows of the swatch list: each group at depth 0 with its count, each swatch at depth 1 with "name, value" as its -/// tooltip and accessible name. `SwatchesPanel` and `PaintChip`'s swatches list build their rows with it. +/// The swatch list's inputs: the library's groups and each swatch's colour as drawn, by name. +#[derive(craft_ui::contract::MemoProps)] +struct SwatchInputs<'a> { + groups: &'a [SwatchGroup], + drawn: BTreeMap<&'a str, Srgba>, +} + +/// The presets of the swatch list: each group at depth 0 with its count, each swatch at depth 1 with "name, value" as +/// its tooltip and accessible name, painted in its colour through `cmyk`. `SwatchesPanel` and `PaintChip`'s swatches +/// list show it. +pub fn swatch_source<'a>(groups: &'a [SwatchGroup], cmyk: &CmykConverter) -> PresetSource<'a, SwatchKey> { + let drawn = groups.iter().flat_map(|g| g.entries.iter().map(|e| (e.name.as_str(), e.color.to_srgb(cmyk)))).collect(); + PresetSource::new(SwatchInputs { groups, drawn }, PresetFns::new(source_rows).thumb(paint_swatch)) +} + +fn source_rows(inputs: &SwatchInputs<'_>, env: &Env) -> Vec> { + swatch_rows(inputs.groups, &env.words::()) +} + +/// The rows the Swatches panel lists for `groups`: each group's header, then its swatches, each named by its name and +/// colour value. pub fn swatch_rows(groups: &[SwatchGroup], words: &SwatchesWords) -> Vec> { let mut rows = Vec::with_capacity(groups.len() + groups.iter().map(|g| g.entries.len()).sum::()); for group in groups { @@ -570,6 +596,14 @@ pub fn swatch_rows(groups: &[SwatchGroup], words: &SwatchesWords) -> Vec, cx: &ContentPainterCx<'_>, rect: Rect, info: ThumbInfo<'_, SwatchKey>) { + if let SwatchKey::Swatch(name) = info.key + && let Some(color) = inputs.drawn.get(name.as_str()) + { + SwatchFace::new(*color).selected(info.selected).paint(cx, rect); + } +} + #[cfg(test)] pub(crate) mod tests { use super::*; diff --git a/crates/color-ui/src/ui/swatches/panel.rs b/crates/color-ui/src/ui/swatches/panel.rs index f0bef8ce9..1347ece3d 100644 --- a/crates/color-ui/src/ui/swatches/panel.rs +++ b/crates/color-ui/src/ui/swatches/panel.rs @@ -1,24 +1,20 @@ //! `SwatchesPanel`: the panel body, its state and what it reports. -use std::collections::HashMap; - use craft_cms::convert::CmykConverter; use craft_i18n::Format; use craft_theme::Srgba; use craft_theme::variant::Tone; use craft_ui::bars::StatusMessage; -use craft_ui::choice::SwatchFace; use craft_ui::contract::{Bind, Held, NoValue, Outcome, Pattern, ShowContainer as _, request_rename}; use craft_ui::layout_components::Stack; -use craft_ui::patterns::presets::{PresetBrowser, PresetEvent, PresetFileEvent, PresetFooterOptions, PresetReorder, PresetWords, ThumbInfo}; -use craft_ui::primitives::ContentPainterCx; +use craft_ui::patterns::presets::{PresetBrowser, PresetEvent, PresetFileEvent, PresetFooterOptions, PresetReorder, PresetWords}; use craft_ui::runtime::env::{Count, Env}; -use egui::{Rect, Response, Ui}; +use egui::{Response, Ui}; use super::library::{find_swatch, has}; use super::menu::event_edits; -use super::{SWATCH_ZOOM, SwatchAt, SwatchEdit, SwatchKey, SwatchesFileOutput, SwatchesState, SwatchesWords, swatch_rows}; -use crate::palette::{SwatchColor, SwatchGroup, SwatchNote, SwatchReport}; +use super::{SWATCH_ZOOM, SwatchAt, SwatchEdit, SwatchKey, SwatchesFileOutput, SwatchesState, SwatchesWords, swatch_source}; +use crate::palette::{SwatchGroup, SwatchNote, SwatchReport}; /// What [`SwatchesPanel::show`] reports. #[derive(Debug)] @@ -99,21 +95,13 @@ impl<'a> SwatchesPanel<'a> { if st.selected.as_ref().is_some_and(|k| !has(groups, k)) { st.selected = None; } - let rows = swatch_rows(groups, &words); + let source = swatch_source(groups, cmyk); let out = { - let colors: HashMap<&str, &SwatchColor> = groups.iter().flat_map(|g| g.entries.iter().map(|e| (e.name.as_str(), &e.color))).collect(); - let mut browser = PresetBrowser::new(words.title.clone(), Bind::bound(&mut st.browser), &rows) + let mut browser = PresetBrowser::new(words.title.clone(), Bind::bound(&mut st.browser), &source) .thumb_range(SWATCH_ZOOM) .toolbar(true) .reorder(PresetReorder::items_and_groups()) - .footer(PresetFooterOptions::new().can_delete(st.selected.is_some())) - .thumb(|cx: &ContentPainterCx<'_>, rect: Rect, info: ThumbInfo<'_, SwatchKey>| { - if let SwatchKey::Swatch(name) = info.key - && let Some(color) = colors.get(name.as_str()) - { - SwatchFace::new(color.to_srgb(cmyk)).selected(info.selected).paint(cx, rect); - } - }); + .footer(PresetFooterOptions::new().can_delete(st.selected.is_some())); if let Some(key) = &st.selected { browser = browser.selected(key.clone()); } diff --git a/crates/craft/Cargo.toml b/crates/craft/Cargo.toml index 6a27a4e70..4eaeaa1cf 100644 --- a/crates/craft/Cargo.toml +++ b/crates/craft/Cargo.toml @@ -910,6 +910,7 @@ timeline-otio = ["timeline", "craft-timeline/otio"] timeline-xml = ["timeline", "craft-timeline/xml"] tracked = ["dep:craft-tracked"] +tracked-serde = ["tracked", "craft-tracked/serde"] ui-app = ["ui", "craft-ui/app"] ui-cloud = ["ui", "craft-ui/cloud"] diff --git a/crates/design-lint/src/workspace.rs b/crates/design-lint/src/workspace.rs index 93407230a..a2436797c 100644 --- a/crates/design-lint/src/workspace.rs +++ b/crates/design-lint/src/workspace.rs @@ -161,6 +161,9 @@ const NO_GLOBAL_STATE: &[(&str, &str)] = &[ ("crates/ui-patterns/src/patterns/presets/model/library.rs", "MemoProps"), // Settled fact: a name check keys by its inputs and its `fn` behind `dyn NameChecker`, as `Bind::check_with` does. ("crates/ui-patterns/src/patterns/presets/named.rs", "MemoProps"), + // Settled fact: `PresetSource` erases its inputs behind a trait object; it keys on those inputs (themselves + // `MemoProps`) and on the addresses of its capture-free `fn`s, which stand for the code that reads them. + ("crates/ui-patterns/src/patterns/presets/source.rs", "MemoProps"), // Settled fact: constant font bytes, decompressed once from compile-time input and immutable after init. ("crates/ui-contract/src/fonts/defaults.rs", "HACK"), ("crates/ui-contract/src/fonts/defaults.rs", "EGUI_ICONS"), diff --git a/crates/draw/src/ui/brush_presets/panel.rs b/crates/draw/src/ui/brush_presets/panel.rs index 293b35c55..e81884ca9 100644 --- a/crates/draw/src/ui/brush_presets/panel.rs +++ b/crates/draw/src/ui/brush_presets/panel.rs @@ -3,9 +3,9 @@ use std::hash::Hash; use craft_behavior::{EditEvents, IconId}; -use craft_ui::contract::{Bind, EditResponse, Outcome, Props as _, ShowContainer as _}; +use craft_ui::contract::{Bind, EditResponse, MemoProps, Outcome, Props as _, ShowContainer as _}; use craft_ui::layout_components::Stack; -use craft_ui::patterns::presets::{PresetBrowser, PresetEvent, PresetFooterEvent, PresetFooterOptions, PresetReorder, PresetRow, PresetState}; +use craft_ui::patterns::presets::{PresetBrowser, PresetEvent, PresetFooterEvent, PresetFooterOptions, PresetReorder, PresetSource, PresetState}; use craft_ui::runtime::drag::DropZone; use craft_ui::runtime::env::{Env, Label}; use craft_ui::sections::FooterAction; @@ -29,9 +29,9 @@ pub(super) const BRUSH_THUMBS: TileZoom = #[must_use = "show the panel with `show`"] #[derive(craft_ui::contract::Pattern, craft_ui::contract::Props)] #[pattern(probe(BrushesPanel<'static, u32>))] -pub struct BrushesPanel<'a, K: Clone + Eq + Hash + Send + Sync + 'static> { +pub struct BrushesPanel<'a, K: Clone + Eq + Hash + Send + Sync + MemoProps + 'static> { label: Label, - rows: &'a [PresetRow], + source: &'a PresetSource<'a, K>, state: Bind<'a, PresetState>, diameter: Bind<'a, f64>, max_size: f64, @@ -67,16 +67,22 @@ impl Outcome for BrushesPanelOutput { } } -impl<'a, K: Clone + Eq + Hash + Send + Sync + 'static> BrushesPanel<'a, K> { - /// A panel named `label` over the presets in `rows`, with its view in `state` and the brush +impl<'a, K: Clone + Eq + Hash + Send + Sync + MemoProps + 'static> BrushesPanel<'a, K> { + /// A panel named `label` over the presets of `source`, with its view in `state` and the brush /// size in `diameter` up to `max_size`. - pub fn new(label: impl Into