From 9c98223114bb33d334eff4e9d8affeb74ce81dc3 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 25 Nov 2026 14:14:29 -0800 Subject: [PATCH 01/24] color-ui: a double click right of an R, G or B field switches the Color panel's RGB units ColorPanelState::rgb_percent switched the R, G and B rows between 0 to 255 and percent, but nothing in the panel wrote it: ChannelRow reported no pointer signal after its number field. - ChannelRow::units_cell(true) adds a blank cell after the number field. It is highlighted under the pointer, takes no keyboard focus, and reports a double click in ChannelRowOutput::units_double_clicked. In a disabled container it reports nothing. ChannelRow::show now returns ChannelRowOutput, with the edit in `edit`. - ColorPanel::rgb_units_cell(true) gives the R, G and B rows that cell. A double click on any of the three switches rgb_percent, and all three rows change together. It is off by default, so a host turns it on together with a command that writes rgb_percent. - Web Color's rows keep 0 to 255 while rgb_percent is on. Before, they showed percent as well, because they use the same RGB model. --- crates/color-ui/src/ui/harmony.rs | 2 +- crates/color-ui/src/ui/mod.rs | 4 +- crates/color-ui/src/ui/panel.rs | 41 ++++- crates/color-ui/src/ui/panel/fill_stroke.rs | 2 +- crates/color-ui/src/ui/picker/channel.rs | 161 +++++++++++++++++--- crates/color-ui/src/ui/picker/compact.rs | 4 +- crates/color-ui/src/ui/picker/mod.rs | 2 +- crates/color-ui/src/ui/picker/stories.rs | 10 +- crates/color-ui/tests/it/panels.rs | 117 +++++++++++++- 9 files changed, 301 insertions(+), 42 deletions(-) diff --git a/crates/color-ui/src/ui/harmony.rs b/crates/color-ui/src/ui/harmony.rs index a153276ada..b0830ae86c 100644 --- a/crates/color-ui/src/ui/harmony.rs +++ b/crates/color-ui/src/ui/harmony.rs @@ -231,7 +231,7 @@ impl<'a> HarmonyWheel<'a> { let value = (f64::from(level) * 100.0).round(); let rule = ChannelRule { range: 0.0..=100.0, step: 1.0, decimals: 0 }; Stack::new().enabled(selected.is_some()).show(ui, |ui, _| { - let row = ChannelRow::new(slider_name.clone(), Bind::given(value), rule.clone()).track(track.clone()).show(ui); + let row = ChannelRow::new(slider_name.clone(), Bind::given(value), rule.clone()).track(track.clone()).show(ui).edit; if let (Some(i), Some(row)) = (selected, row) { for event in row.events { edits.push(( diff --git a/crates/color-ui/src/ui/mod.rs b/crates/color-ui/src/ui/mod.rs index 54fb837833..5126f1b044 100644 --- a/crates/color-ui/src/ui/mod.rs +++ b/crates/color-ui/src/ui/mod.rs @@ -74,8 +74,8 @@ pub use panel::{ ColorPanelShown, ColorPanelState, ColorPanelWords, PanelTarget, TintRow, }; pub use picker::{ - ChannelRow, ChannelRule, ChannelUnit, ColorConstraint, ColorPicker, ColorPickerLayout, ColorPickerOutput, ColorPickerWords, PickedColor, - ScreenPickGesture, TrackRamp, + ChannelRow, ChannelRowOutput, ChannelRule, ChannelUnit, ColorConstraint, ColorPicker, ColorPickerLayout, ColorPickerOutput, ColorPickerWords, + PickedColor, ScreenPickGesture, TrackRamp, }; pub use picker_dialog::{ ColorPickerDialog, ColorPickerDialogLayout, ColorPickerDialogWords, ColorPickerMemory, ColorPickerResult, ColorPickerTab, PickerLibrary, diff --git a/crates/color-ui/src/ui/panel.rs b/crates/color-ui/src/ui/panel.rs index 90c92271b2..634926af6d 100644 --- a/crates/color-ui/src/ui/panel.rs +++ b/crates/color-ui/src/ui/panel.rs @@ -245,7 +245,8 @@ pub struct ColorPanelState { pub hide_options: bool, /// The FillStroke layout shows the Recent Colors slot. pub recent_colors: bool, - /// The R, G and B rows count percents, 0 to 100, in place of 0 to 255. + /// The R, G and B rows count percents, 0 to 100, in place of 0 to 255; Web Color's rows keep 0 to 255. A double + /// click on a units cell ([`ColorPanel::rgb_units_cell`]) switches it. pub rgb_percent: bool, } @@ -444,6 +445,7 @@ pub struct ColorPanel<'a> { cmyk: Option<&'a CmykConverter>, rgb: Option<&'a dyn ColorModel>, dynamic: bool, + rgb_units: bool, layout: ColorPanelLayout, wells: Option<([PairWell; 2], Bind<'a, Well>)>, edited: Option>, @@ -484,6 +486,7 @@ impl<'a> ColorPanel<'a> { cmyk: None, rgb: None, dynamic: true, + rgb_units: false, layout: ColorPanelLayout::default(), wells: None, edited: None, @@ -515,6 +518,14 @@ impl<'a> ColorPanel<'a> { self } + /// A units cell right of each R, G and B field (not Web Color's): a double click on it switches + /// [`ColorPanelState::rgb_percent`]. The cell has no keyboard stop, so a host that turns it on offers the same + /// switch as a command that writes `rgb_percent`. Off by default. + pub fn rgb_units_cell(mut self, on: bool) -> Self { + self.rgb_units = on; + self + } + /// How the panel arranges its body; the Photoshop foreground and background arrangement by default. pub fn layout(mut self, layout: ColorPanelLayout) -> Self { self.layout = layout; @@ -561,7 +572,7 @@ impl<'a> ColorPanel<'a> { /// Shows the panel and returns this pass's edits and requests. pub fn show(self, ui: &mut Ui) -> ColorPanelOutput { - let ColorPanel { state, colors, cmyk, rgb, dynamic, layout, wells, edited, shown: rows, recent, recent_setup, gamut } = self; + let ColorPanel { state, colors, cmyk, rgb, dynamic, rgb_units, layout, wells, edited, shown: rows, recent, recent_setup, gamut } = self; let env = Env::of(ui); let (words, dialog) = (env.words::(), env.words::()); let mut held = Held::new(state); @@ -593,6 +604,7 @@ impl<'a> ColorPanel<'a> { rgb, gamut: &gamut, dynamic, + rgb_units, words: &words, dialog: &dialog, inputs: Inputs { @@ -891,6 +903,8 @@ struct Body<'a> { rgb: &'a dyn ColorModel, gamut: &'a Gamut<'a>, dynamic: bool, + /// The R, G and B rows have units cells ([`ColorPanel::rgb_units_cell`]). + rgb_units: bool, words: &'a ColorPanelWords, dialog: &'a ColorPickerDialogWords, /// The state as this pass leaves it. @@ -1110,15 +1124,18 @@ impl Body<'_> { self.kept.tracks.retain(|(id, target, _)| (*id, *target) != slot); self.kept.tracks.push((slot.0, slot.1, current)); } + // The R, G and B rows are not Web Color's. The units are read once, so a double click on one units cell + // switches all three rows together, from the next pass. + let rgb = !web && model.id() == self.rgb.id(); + let rgb_percent = self.now.rgb_percent; for (i, ch) in model.channels().iter().enumerate() { let mut rule = ChannelRule::of(ch); if web { rule.step = WEB_STEP; } - // The R, G and B rows count percents of the channel's range; an edit goes back through `scale`. - // PLAN: a double click on the units cell flips `rgb_percent` while the rows are enabled; `ChannelRow` - // (color-w2) reports no signal for its units cell, so the panel cannot read it. - let percent = self.now.rgb_percent && model.id() == self.rgb.id(); + // With `rgb_percent` the R, G and B rows count percents of the channel's range; an edit goes back through + // `scale`. + let percent = rgb && rgb_percent; let scale = if percent { 100.0 / *rule.range.end() } else { 1.0 }; let unit = if percent { ChannelUnit::Percent } else { ChannelUnit::of(ch.unit) }; if percent { @@ -1134,8 +1151,16 @@ impl Body<'_> { // The field shows the channel's places, as Photoshop's; the edits start from the unrounded values. let places = 10f64.powi(i32::from(rule.decimals)); let shown = (f64::from(current.values[i]) * scale * places).round() / places; - let row = ChannelRow::new(channel_name(model, i, &[model]), Bind::given(shown), rule).short(ch.label).unit(unit).track(track).show(ui); - for event in row.into_iter().flat_map(|row| row.events) { + let row = ChannelRow::new(channel_name(model, i, &[model]), Bind::given(shown), rule) + .short(ch.label) + .unit(unit) + .track(track) + .units_cell(rgb && self.rgb_units) + .show(ui); + if row.units_double_clicked { + self.now.rgb_percent = !rgb_percent; + } + for event in row.edit.into_iter().flat_map(|row| row.events) { self.channel_event(model, current, i, event.map(|x| x / scale)); } } diff --git a/crates/color-ui/src/ui/panel/fill_stroke.rs b/crates/color-ui/src/ui/panel/fill_stroke.rs index a3ff66c160..e043371abb 100644 --- a/crates/color-ui/src/ui/panel/fill_stroke.rs +++ b/crates/color-ui/src/ui/panel/fill_stroke.rs @@ -87,7 +87,7 @@ impl Body<'_> { .unit(ChannelUnit::Percent) .track(vec![Srgba::rgb(1.0, 1.0, 1.0), tint.base]) .show(ui); - events.extend(row.into_iter().flat_map(|row| row.events)); + events.extend(row.edit.into_iter().flat_map(|row| row.events)); }); }); // A pass has one tint edit: the last of its events. diff --git a/crates/color-ui/src/ui/picker/channel.rs b/crates/color-ui/src/ui/picker/channel.rs index 34807fb607..ae1b25dc39 100644 --- a/crates/color-ui/src/ui/picker/channel.rs +++ b/crates/color-ui/src/ui/picker/channel.rs @@ -3,12 +3,14 @@ use std::ops::RangeInclusive; -use craft_behavior::{Axis, Sense}; +use craft_behavior::{Axis, Metrics, PartKey, PressPhase, Sense}; use craft_theme::{Channel, ColorModel, Srgba}; use craft_ui::contract::{ - Bind, Common, Component, Cx, DisabledLook, EditResponse, GlyphLabel, LabelKind, MeasureCx, Role, Show as _, ShowContainer as _, SideLabel, Sizing, + Bind, Common, Component, Cx, DisabledLook, EditResponse, Gap, GlyphLabel, LabelKind, MeasureCx, NoValue, Outcome, PartsCx, Pointer as _, Role, + Show as _, ShowContainer as _, SideLabel, Sizing, }; use craft_ui::form::{Field, SliderField, SliderFieldLayout}; +use craft_ui::layout_components::Row; use craft_ui::primitives::{PaintShape, StrokeWidth}; use craft_ui::runtime::env::{Label, UnitKind}; use craft_ui::style::{ComponentStyle, DataColor, StyleColor, StyleCx}; @@ -181,12 +183,45 @@ pub struct ChannelRow<'a> { checker: bool, unit: ChannelUnit, marks: &'a [f64], + /// Whether a units cell follows the number field. + units_cell: bool, +} + +/// What a [`ChannelRow`] reports. +#[non_exhaustive] +pub struct ChannelRowOutput { + /// The edit of the slider or the field, `None` when the row did not run. + pub edit: Option>, + /// The units cell ([`ChannelRow::units_cell`]) was double-clicked in this pass: the host switches the units the + /// row counts in. + pub units_double_clicked: bool, } impl<'a> ChannelRow<'a> { /// A row named `label` over `value`, ruled by `rule`. pub fn new(label: impl Into