From 39056798ee8e346bdaadbc7608dbc5eb6499158b Mon Sep 17 00:00:00 2001 From: aiongg Date: Sat, 14 Nov 2026 16:25:28 +0000 Subject: [PATCH] panels-w7: written from its sheet --- crates/color/README.md | 2 +- crates/color/src/ui/area.rs | 59 +++-- crates/color/src/ui/hud.rs | 239 ++++++++++++++++-- crates/color/src/ui/hud/stories.rs | 23 ++ crates/color/src/ui/info.rs | 103 ++++++-- crates/color/src/ui/info/stories.rs | 38 +++ crates/color/src/ui/mod.rs | 7 +- crates/color/src/ui/strip.rs | 46 ++-- crates/color/tests/it/color_parts.rs | 113 ++++++++- crates/color/tests/it/main.rs | 2 + .../ui-widgets/src/value_drag/point_picker.rs | 31 +-- 11 files changed, 564 insertions(+), 99 deletions(-) create mode 100644 crates/color/src/ui/hud/stories.rs create mode 100644 crates/color/src/ui/info/stories.rs diff --git a/crates/color/README.md b/crates/color/README.md index 2f4a1d0a5..24dd3bd49 100644 --- a/crates/color/README.md +++ b/crates/color/README.md @@ -1,6 +1,6 @@ # craft-color -Colour for Craft apps. With the `ui` feature, `craft_color::ui` has the colour picker (`ColorPicker`, `ColorPopover`, `ColorArea`, `ColorStrip`, `ColorWheel`, `ColorDisc`, `ColorRamp`), the wells (`SwatchPair`, `Well`), `SwatchPalette` tables and the `HexField`. It also has the colour features built on them: the Color Picker dialog (`ColorPickerDialog`), dialog droppers (`DropperButton`, `DropperState`), the Eyedropper's options and ring, Color Sampler points, the screen colour picker (`ScreenColorPicker`), `ColorChoice`, `PaintChip` and `PaintList` (with their `PaintKindLabels` and `PaintListLabels`), the Color and Swatches panels (`ColorPanel`, `SwatchesPanel`), the profile mismatch prompt, the harmony wheel and Color Guide panel, and the `LutPicker`. +Colour for Craft apps. With the `ui` feature, `craft_color::ui` has the colour picker (`ColorPicker`, `ColorPopover`, `ColorArea`, `ColorStrip`, `ColorWheel`, `ColorDisc`, `ColorRamp`), the wells (`SwatchPair`, `Well`), `SwatchPalette` tables and the `HexField`. It also has the colour features built on them: the Color Picker dialog (`ColorPickerDialog`), dialog droppers (`DropperButton`, `DropperState`), the Eyedropper's options and ring, Color Sampler points, the screen colour picker (`ScreenColorPicker`), `ColorChoice`, `PaintChip` and `PaintList` (with their `PaintKindLabels` and `PaintListLabels`), the Color and Swatches panels (`ColorPanel`, `SwatchesPanel`), the Info panel (`InfoPanel`), the HUD Color Picker over a canvas (`ColorHud`), the profile mismatch prompt, the harmony wheel and Color Guide panel, the `LutPicker`, and `ui::gradient` (`GradientEditor`, `GradientStops`, the stop rules, `GradientRamp`). Stories: `patterns/color/{picker-dialog, panel, swatches, eyedropper-bar, samplers, paint-chip, choice, harmony, lut-picker, profile-mismatch}` show each of those features in its states. The two dialogs run on a `DialogHost` (a "Show dialog" button in an Overview tile). The screen eyedropper is off in the stories, so none starts a screen capture. diff --git a/crates/color/src/ui/area.rs b/crates/color/src/ui/area.rs index 12fc357f8..b4b1e26f7 100644 --- a/crates/color/src/ui/area.rs +++ b/crates/color/src/ui/area.rs @@ -2,7 +2,7 @@ use super::channels::{QUANTIZE_CELLS, channel_range, channel_step, quantized, settled, snapped, spoken}; use crate::AreaMode; -use craft_behavior::{Metrics, Mixed, Sense}; +use craft_behavior::{Metrics, Mixed, Proposal, Sense}; use craft_theme::ModelValues; use craft_ui::contract::{ Access, Bind, BindMut, Common, Component, Cx, DisabledLook, Edit as _, EditResponse, MeasureCx, PaintUi, Props as _, Role, Sizing, WordSetCx as _, @@ -11,7 +11,7 @@ use craft_ui::primitives::ContentPainterCx; use craft_ui::runtime::env::Label; use craft_ui::style::{ComponentStyle, StyleCx}; use craft_ui::value_drag::{PointMark, PointPicker}; -use egui::{Mesh, Rect, Vec2, pos2}; +use egui::{Mesh, Pos2, Rect, Vec2, pos2}; #[cfg(feature = "screen-testing")] mod stories; @@ -75,10 +75,32 @@ impl<'a> ColorArea<'a> { /// The values the pointer at `at` picks in `rect`: the picker's own point-to-value mapping, then the settle and /// quantise steps. `ColorHud` drives it with the host's pointer. - #[allow(dead_code)] // P3(panels-w7): pn.C32 - pub(crate) fn value_at(&self, rect: Rect, values: &ModelValues, at: egui::Pos2) -> ModelValues { - let _ = (rect, values, at); - todo!("pn.C32") + pub(crate) fn value_at(&self, rect: Rect, values: &ModelValues, at: Pos2) -> ModelValues { + let mut mine = *values; + if let Some(point) = self.picker(Some(values), false).value_at(rect, at) { + self.settle_into(&mut mine, point.map(Some)); + } + mine + } + + /// Where `values` stands in `rect`: the picker's own value-to-point mapping. + pub(crate) fn point_in(&self, rect: Rect, values: &ModelValues) -> Pos2 { + let [across, down] = self.channels(); + self.picker(Some(values), false).position([values.values[across], values.values[down]].map(f64::from), rect) + } + + /// Writes the area's two components from `point` (an axis that is `None` stays) onto `mine`, settled on the + /// channel's step and then on the quantised colour: the one rule for what the area reports. + fn settle_into(&self, mine: &mut ModelValues, point: [Option; 2]) { + let model = self.mode.model(); + for (channel, value) in self.channels().into_iter().zip(point) { + if let Some(value) = value { + mine.values[channel] = settled(model, channel, value as f32); + } + } + if let Some(levels) = self.levels() { + *mine = quantized(model, mine, levels); + } } /// The picker over `shown`, whose backdrop paints the field of `base`. @@ -127,9 +149,14 @@ impl Component for ColorArea<'_> { } fn measure(&mut self, cx: &MeasureCx<'_, Self>) -> Vec2 { - // An area that fills its room (the Color panel's, as tall as its group leaves it) keeps no plot size of its own. + // An area that fills its room (the Color panel's, as tall as its group leaves it) is at least a hit target and + // square on the width it is offered. if cx.resolved.sizing() == Some(Sizing::Fill) { - return Vec2::splat(cx.tokens.size.hit.target); + return match cx.proposal { + Proposal::Min => Vec2::splat(cx.tokens.size.hit.target), + Proposal::Width(width) | Proposal::Size(Vec2 { x: width, .. }) => Vec2::splat(width), + _ => Vec2::splat(cx.tokens.size.plot.width), + }; } let mut picker = self.picker(cx.value(), false); cx.child("picker", &mut picker, cx.proposal) @@ -152,22 +179,10 @@ impl Component for ColorArea<'_> { } fn paint(&mut self, ui: &mut PaintUi<'_>, cx: &Cx<'_, Self>) { - let model = self.mode.model(); - let [across, down] = self.channels(); let picker = self.picker(cx.value(), true).label(cx.label()); let edit = ui.inner("picker", cx.rect, picker); - let levels = self.levels(); - let start = ModelValues::new(model.id(), &[]); - cx.adopt(&edit, &start, |point, mine| { - for (channel, axis) in [(across, 0), (down, 1)] { - if let Some(value) = point[axis].same() { - mine.values[channel] = settled(model, channel, *value as f32); - } - } - if let Some(levels) = levels { - *mine = quantized(model, mine, levels); - } - }); + let start = ModelValues::new(self.mode.model().id(), &[]); + cx.adopt(&edit, &start, |point, mine| self.settle_into(mine, [0, 1].map(|axis| point[axis].same().copied()))); } fn finish(self, cx: Cx<'_, Self>) -> EditResponse { diff --git a/crates/color/src/ui/hud.rs b/crates/color/src/ui/hud.rs index beadbc606..f723acb1c 100644 --- a/crates/color/src/ui/hud.rs +++ b/crates/color/src/ui/hud.rs @@ -1,18 +1,92 @@ //! [`ColorHud`]: the colour area and hue strip in a popover at a point, driven by the host's press. -use craft_behavior::EditEvents; +use super::strip; +use super::{ColorArea, ColorStrip}; +use crate::AreaMode; +use craft_behavior::{EditEvents, Metrics, Sense}; use craft_theme::Srgba; -use craft_ui::contract::{Bind, Pattern}; -use craft_ui::runtime::env::Label; +use craft_ui::contract::{ + Bind, BindMut, Common, Component, Cx, DisabledLook, Edit as _, EditResponse, EscapeEdit, MeasureCx, Outcome, PaintUi, Pattern, Props as _, Role, + Show as _, ShowContainer as _, +}; +use craft_ui::popover::Popover; +use craft_ui::runtime::env::{Env, Label}; use craft_ui::runtime::overlay::CloseReason; -use egui::{Pos2, Response, Ui}; +use craft_ui::style::{ComponentStyle, StyleCx, UiTokens}; +use egui::{Pos2, Rect, Response, Ui, Vec2, pos2, vec2}; + +#[cfg(feature = "screen-testing")] +mod stories; + +/// The model the HUD picks in: saturation and brightness across and down, hue in the strip. +const MODE: AreaMode = AreaMode::Hue; + +/// The colour of a HUD whose binding shows none. +const BLACK: Srgba = Srgba::rgb(0.0, 0.0, 0.0); + +/// The sizes of the HUD's content, from tokens, so that nothing is measured in an earlier pass: the area is a plot +/// square, the strip stands to its right. +#[derive(Clone, Copy, Debug, PartialEq)] +struct Geometry { + plot: f32, + strip_width: f32, + gap: f32, +} + +impl Geometry { + fn of(tokens: &UiTokens) -> Self { + // The strip's colours are `size.icon.lg` wide with a pointer triangle's room on each side (its own padding). + Geometry { plot: tokens.size.plot.width, strip_width: tokens.size.icon.lg + 2.0 * tokens.size.handle.sm, gap: tokens.space.gap.inline } + } + + fn size(self) -> Vec2 { + vec2(self.plot + self.gap + self.strip_width, self.plot) + } + + /// The area's rect in content whose top left is `min`. + fn area(self, min: Pos2) -> Rect { + Rect::from_min_size(min, Vec2::splat(self.plot)) + } + + /// The strip's rect in content whose top left is `min`. + fn strip(self, min: Pos2) -> Rect { + Rect::from_min_size(pos2(min.x + self.plot + self.gap, min.y), vec2(self.strip_width, self.plot)) + } +} + +/// Where the popover hangs so that the start colour's point in the area sits at `at`. +struct Placement { + /// The popover's top left. + origin: Pos2, + /// The top left of its content. + content: Pos2, + geometry: Geometry, +} + +impl Placement { + fn of(tokens: &UiTokens, at: Pos2, start: Option) -> Self { + let geometry = Geometry::of(tokens); + let area = geometry.area(Pos2::ZERO); + let point = match start { + Some(color) => { + let values = MODE.model().from_srgb(color); + ColorArea::new("", MODE, Bind::given(values)).point_in(area, &values) + } + None => area.center(), + }; + // The popover's padding is the popup padding (`POPUP_PAD`, the control pad). + let pad = tokens.space.pad.control; + Placement { origin: pos2(at.x - pad - point.x, at.y - pad - point.y), content: pos2(at.x - point.x, at.y - point.y), geometry } + } +} /// The area and hue strip in a popover at `at`, with the start colour under it, driven by the host's press: the pick /// maps `pointer` through `ColorArea` and the strip's `value_at`; no pick while `pointer` is still `at`; one gesture /// per open; Escape or `held(false)` commits and closes. Every input is a prop of the pass; the HUD keeps no state. +/// +/// The host shows the HUD while its press is held and stops showing it once `closed` is reported. #[must_use = "show the HUD with `show`"] #[derive(Pattern)] -#[allow(dead_code)] // P3(panels-w7): pn.C31 pub struct ColorHud<'a> { label: Label, at: Pos2, @@ -25,35 +99,48 @@ pub struct ColorHud<'a> { impl<'a> ColorHud<'a> { /// A HUD named `label` at `at` over `color`. pub fn new(label: impl Into