diff --git a/crates/color-ui/src/ui/paint_chip.rs b/crates/color-ui/src/ui/paint_chip.rs index 098395b957..c999d0f6da 100644 --- a/crates/color-ui/src/ui/paint_chip.rs +++ b/crates/color-ui/src/ui/paint_chip.rs @@ -19,11 +19,10 @@ 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::primitives::textures::CachedTexture; -use craft_ui::primitives::{ContentPainterCx, PaintShape, StrokeWidth}; +use craft_ui::primitives::{ContentPainterCx, PaintShape, StrokeWidth, WellFill}; use craft_ui::runtime::env::{Env, Label}; use craft_ui::runtime::requests::ColorRequest; use craft_ui::style::{ComponentStyle, DataColor, FocusLook, StyleColor, StyleCx}; @@ -43,8 +42,6 @@ pub use paint_list::{PaintLink, PaintLinkKind, PaintList, PaintListEvent, PaintL const CHIP_WIDTH_IN_HEIGHTS: f32 = 1.5; /// The most tiles a pattern chip draws; a tile so small that more would fit is drawn as one stretched image. const MAX_TILES: f32 = 4096.0; -/// The No Color diagonal stops this many control heights short of the chip's edges. -const DIAGONAL_INSET_IN_HEIGHTS: f32 = 0.15; /// The chip's face: a mixed paint shows the colour well's mixed look. const WELL: PartKey = PartKey::new("well"); /// The swatch list takes its content height up to this many small control heights, then scrolls, as the gradient @@ -206,10 +203,8 @@ pub struct PaintChipStyle { radius: egui::CornerRadius, /// The ring on the chip's outline. edge: StyleColor, - /// The fill of an empty chip: the field's. + /// The fill of a picture chip with no texture yet: the field's. none_fill: StyleColor, - /// The No Color diagonal. - none_mark: StyleColor, } /// The last solid colour the chip held: a switch back to Solid starts from it. @@ -429,7 +424,6 @@ impl Component for PaintChip<'_> { radius: metrics.radius, edge: cx.color(colors.line.control_edge), none_fill: cx.color(colors.surface.sunken), - none_mark: cx.color(colors.status.bad.solid), } } @@ -476,7 +470,7 @@ impl Component for PaintChip<'_> { 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 recent = self.recent; let full_picker = self.full_picker; let cmyk = self.cmyk.unwrap_or_else(|| CmykConverter::coated()); let solid_now = if let Paint::Solid(color) = &paint { Some(*color) } else { None }; @@ -506,11 +500,12 @@ impl Component for PaintChip<'_> { compact_picker(name.clone(), Bind::given(PickedColor::rgb(*color)), sampled).alpha(true).eyedropper(true).show(ui); shown.edit = picked.events; shown.screen_pick = picked.screen_pick; - if !recent_cells.is_empty() { - Text::new(words.recent.clone()).shade(Shade::Faint).show(ui); + if !recent.is_empty() { + // A palette of no swatches: the recent row alone, under its caption. let mut chosen = *color; - let recent = SwatchPalette::new(words.recent.clone(), Bind::bound(&mut chosen), &recent_cells).show(ui); - shown.picked = recent.edit.as_ref().and_then(|edit| edit.value()).copied(); + let row = + SwatchPalette::new(words.recent.clone(), Bind::bound(&mut chosen), &[]).recent(words.recent.clone(), recent).show(ui); + shown.picked = row.picked; } if let Some((groups, state)) = swatches.as_mut() { let groups: &[SwatchGroup] = groups; @@ -602,13 +597,7 @@ impl Component for PaintChip<'_> { } ui.draw().data_fill(&shape, DataColor::new(Color32::from(*color))); } - Paint::None => { - let draw = ui.draw(); - draw.fill(&shape, style.none_fill); - let inner = well.shrink(style.height * DIAGONAL_INSET_IN_HEIGHTS); - let (from, to) = if cx.flow.is_rtl() { (inner.right_bottom(), inner.left_top()) } else { (inner.left_bottom(), inner.right_top()) }; - draw.stroke(&PaintShape::Path(vec![from, to]), StrokeWidth::Medium, style.none_mark); - } + Paint::None => ui.draw().swatch_well(well, style.radius, WellFill::None), Paint::Gradient(preview) => { painter.checker(well); if !preview.stops.is_empty() { diff --git a/crates/color-ui/src/ui/pair.rs b/crates/color-ui/src/ui/pair.rs index e9bb015503..e031376243 100644 --- a/crates/color-ui/src/ui/pair.rs +++ b/crates/color-ui/src/ui/pair.rs @@ -8,7 +8,7 @@ use craft_ui::contract::{ Access, Bind, BindMut, Common, Component, Cx, Edit as _, EditResponse, Focus as _, MeasureCx, Meta as _, Outcome, Overlay, OverlayUi, Overlays as _, PaintUi, PartsCx, Pointer as _, Props as _, Remember as _, Role, WordSetCx as _, }; -use craft_ui::primitives::{DataWidth, Draw, Glyph, IconSpec, PaintShape, StrokeWidth}; +use craft_ui::primitives::{DataWidth, Draw, Glyph, IconSpec, PaintShape, StrokeWidth, WellFill}; use craft_ui::runtime::commands::FireSource; use craft_ui::runtime::env::Label; use craft_ui::style::{ComponentStyle, DataColor, Flags, FocusLook, GlyphRole, StyleColor, StyleCx, SurfaceEdge, SurfaceStyle, UiTokens}; @@ -496,12 +496,7 @@ impl Component for SwatchPair<'_> { match shown.color.same() { Some(color) if shown.ring => paint_ring(&draw, at, *color, style), color => { - // P3(pdf-comments-w5): CM-C25 (`swatch_well` takes a `WellFill`) - draw.swatch_well( - at, - style.radius, - color.copied().map_or(craft_ui::primitives::WellFill::Mixed, craft_ui::primitives::WellFill::Color), - ); + draw.swatch_well(at, style.radius, color.copied().map_or(WellFill::Mixed, WellFill::Color)); draw.stroke(&PaintShape::Rect(at, style.radius), StrokeWidth::Thin, style.mark); draw.stroke(&outline(at, style), StrokeWidth::Thin, style.edge); } diff --git a/crates/color-ui/src/ui/palette.rs b/crates/color-ui/src/ui/palette.rs index dcfca78d02..cce3344b90 100644 --- a/crates/color-ui/src/ui/palette.rs +++ b/crates/color-ui/src/ui/palette.rs @@ -1,13 +1,16 @@ //! `SwatchPalette`: a wrapping grid of colour cells that chooses one colour. Its table of cells is //! `craft_ui::choice::SwatchTable`. -use craft_behavior::{EditEvent, Mixed, Selection}; +use craft_behavior::{EditEvent, IconId, Mixed, Selection}; use craft_theme::Srgba; use craft_ui::choice::SwatchTable; -use craft_ui::contract::{Bind, Held, NoValue, Outcome, Pattern, ShowContainer as _}; +use craft_ui::contract::{ActivateOn, Bind, Held, NoValue, Outcome, Pattern, Show as _, ShowContainer as _}; +use craft_ui::display::{Shade, Text}; use craft_ui::layout_components::Stack; use craft_ui::runtime::env::Label; +use craft_ui::runtime::memory::StageAccess; use craft_ui::runtime::requests::ColorRequest; +use craft_ui::style::TypeRole; use egui::{Response, Ui}; use std::sync::Arc; @@ -21,10 +24,11 @@ mod stories; pub struct SwatchPaletteOutput { /// The palette's response. pub response: Response, - /// The pick of this pass as one `Set` of the swatch's colour; `None` when nothing was picked. The palette has - /// already written it to its binding. + /// The pick of this pass as one `Set` of the swatch's colour; `None` when nothing was picked or the pick was the + /// colour already bound. The palette has already written it to its binding. pub edit: Option>, - /// The request of the Other… row for the host's colour picker, when it was pressed this pass. + /// The request of the Other… cell for the host's colour picker, at the bound colour and keyed by the key given to + /// [`SwatchPalette::other`], when it was activated this pass. pub pick_color: Option, /// Every swatch click of this pass, also a click on the colour already bound, which files no `edit`. pub picked: Option, @@ -41,11 +45,15 @@ impl Outcome for SwatchPaletteOutput { /// A wrapping grid of colour cells from the host's list of named colours that chooses one colour. /// /// - The swatch equal to the colour (compared in 8 bits, the first when several are) is selected; the name is its -/// tooltip. -/// - A click, Space or Enter on a swatch, or a screen reader's click, sets its colour as one `Set`. +/// tooltip. An [`optional`](Self::optional) palette bound to no colour, or to a mixed one, selects none. +/// - A click, Space or Enter on a swatch, or a screen reader's click, sets its colour as one `Set`; a click on the +/// colour already bound sets nothing and still reports `picked`. /// - The palette is one Tab stop: Tab enters on the selected swatch (the first when none is) and the next Tab leaves. /// Arrows move the focus through the cells as the rows lie, Up and Down to the cell above or below; they choose /// nothing. +/// - [`other`](Self::other) adds an Other… cell after the swatches, in the same Tab stop and arrow walk: activating it +/// files `pick_color` and sets nothing. [`recent`](Self::recent) adds a captioned row of recent colours under the +/// grid, its own Tab stop, whose pick sets the colour as a swatch's does. /// - AccessKit: a list box named by the label; each cell a list box option named by its name, with the hex colour as /// its value. #[must_use = "show the palette with `show`"] @@ -54,75 +62,156 @@ pub struct SwatchPalette<'a> { label: Label, color: PaletteColor<'a>, swatches: &'a [(Label, Srgba)], + /// The Other… cell: the key of its colour request and its name. + other: Option<(String, Label)>, + /// The recent row: its caption and colours. + recent: Option<(Label, &'a [Srgba])>, } /// What a palette binds: a colour, or a colour that may be none. enum PaletteColor<'a> { - Color(Bind<'a, Srgba>), + Color(Held<'a, Srgba>), Optional(Bind<'a, Option>), } +impl PaletteColor<'_> { + /// The bound colour as the palette shows it: `Same(None)` for no colour (or an optional binding that holds + /// nothing), `Mixed` for several. `None` while a plain colour's binding holds no single colour, and the palette + /// shows nothing. + fn read(&mut self, cx: &mut impl StageAccess) -> Option>> { + match self { + PaletteColor::Color(held) => held.read(cx).map(|color| Mixed::Same(Some(color))), + PaletteColor::Optional(bind) => Some(bind.reborrow().read(cx).unwrap_or(Mixed::Same(None))), + } + } + + /// Writes `after` over `before` as one `Set` named `name`. + fn write(&mut self, cx: &mut impl StageAccess, before: Mixed>, after: Srgba, name: &str) { + match self { + PaletteColor::Color(held) => { + if let Mixed::Same(Some(before)) = before { + held.write(cx, &before, after, name); + } + } + PaletteColor::Optional(bind) => { + let _ = bind.reborrow().write(cx, &EditEvent::set(before, Some(after), Arc::from(name), None)); + } + } + } +} + +/// What the body picked in its pass. +#[derive(Clone, Copy)] +enum Pick { + /// A swatch or a recent colour. + Color(Srgba), + /// The Other… cell. + Other, +} + +/// The cell of `swatches` whose colour is `color`, compared in 8 bits: the first when several are. +fn cell_of(swatches: &[(Label, Srgba)], color: Option) -> Selection { + color + .and_then(|color| swatches.iter().position(|(_, c)| c.to_rgba() == color.to_rgba())) + .map_or_else(Selection::default, Selection::single) +} + impl<'a> SwatchPalette<'a> { /// A palette named `label` of `swatches`, each a name and a colour, choosing the colour bound to `color`. pub fn new(label: impl Into