Merge pull request #4259 from dotCooCoo/fix/color-panel-cancel-restore

color-ui: ramp, hex, readout, gamut chip and well edits start from the host's complete color
This commit is contained in:
BFlat authored and GitHub committed 2026-11-29 20:25:11 +07:00
commit 186a229c8d
4 files changed
+179 -10

No files matched your search

+4 -2
View File
@@ -10,5 +10,7 @@ The colour model and conversions are in `craft-theme`; profiles and transforms i
`ColorPanel::edited` can supply a colour's native components. Editing one channel preserves the other components,
even when the host renders the colour through a different working-space transform. Current host components take
precedence over the panel's remembered values. Canceling a channel gesture restores the host's complete opening
colour, including its display colour, alpha and native components.
precedence over the panel's remembered values. Canceling a channel or ramp gesture restores the host's complete opening
color, including its display color, alpha and native components. The `before` of an edit from the channels, the ramp,
a hex field, the readout, the out-of-gamut chip, the Black and White wells or a Recent Colors chip is that complete
color, so Undo returns to it.
+41 -3
View File
@@ -417,6 +417,8 @@ struct Kept {
dragging: bool,
/// The host's complete colour before a model gesture, independent of the panel's transform.
opening: Option<(GestureId, PanelTarget, PickedColor)>,
/// Both colors, complete, before a gesture of the ramp, a hex field or the readout.
colors_opening: Option<(GestureId, [PickedColor; 2])>,
/// The ramp gesture in progress picks for the colour not edited.
other: bool,
}
@@ -977,6 +979,42 @@ impl Body<'_> {
self.inputs.edited.and_then(Option::as_ref)
}
/// The complete color of `target`: the host's `edited` color with its model values for the edited well, else
/// the well's color.
fn complete(&self, target: PanelTarget) -> PickedColor {
match self.picked() {
Some(picked) if target == self.target => *picked,
_ => PickedColor { color: self.colors[target.index()], model: None },
}
}
/// Pushes `event` for `target` with the complete color it started from as its `before`: the color when its
/// gesture began, kept through the host's live updates, or the current color for a `Set`.
fn push_from_complete(&mut self, target: PanelTarget, mut event: EditEvent<PickedColor>) {
if let EditEvent::Begin { id, .. } = &event {
self.kept.colors_opening = Some((*id, [self.complete(PanelTarget::Foreground), self.complete(PanelTarget::Background)]));
}
let opening = self.kept.colors_opening.filter(|(id, _)| Some(*id) == event.id()).map(|(_, colors)| colors[target.index()]);
match &mut event {
EditEvent::Begin { before, .. } | EditEvent::Commit { before, .. } => {
if let Some(picked) = opening {
*before = Mixed::Same(picked);
}
}
EditEvent::Cancel { before, .. } => {
if let Some(picked) = opening {
*before = Some(Mixed::Same(picked));
}
}
EditEvent::Set { before, .. } => *before = Mixed::Same(self.complete(target)),
_ => {}
}
if matches!(event.phase(), Some(GesturePhase::Commit | GesturePhase::Cancel) | None) {
self.kept.colors_opening = None;
}
self.push(target, event);
}
/// Whether the rows edit anything: the host gave no `edited(None)`.
fn enabled(&self) -> bool {
self.inputs.edited.is_none_or(Option::is_some)
@@ -1090,7 +1128,7 @@ impl Body<'_> {
if ColorWarningChip::new(&warning).arrangement(ColorWarningArrangement::Beside).show(ui).clicked {
let picked = PickedColor { color: warning.nearest().with_alpha(color.a), model: None };
let name = self.words.title.as_str();
self.push(self.target, EditEvent::set(Mixed::Same(PickedColor { color, model: None }), picked, name.into(), None));
self.push_from_complete(self.target, EditEvent::set(Mixed::Same(PickedColor { color, model: None }), picked, name.into(), None));
}
}
});
@@ -1271,7 +1309,7 @@ impl Body<'_> {
let color = self.color();
let edit = ColorReadout::new(self.words.hex_color.clone(), Bind::given(color.to_rgba())).show(ui);
for event in edit.events {
self.push(self.target, event.map(|rgba| PickedColor { color: Srgba::from(rgba).with_alpha(color.a), model: None }));
self.push_from_complete(self.target, event.map(|rgba| PickedColor { color: Srgba::from(rgba).with_alpha(color.a), model: None }));
}
}
@@ -1309,7 +1347,7 @@ impl Body<'_> {
}
let target = if self.kept.other { self.target.other() } else { self.target };
let alpha = self.colors[target.index()].a;
self.push(target, event.map(|pick| PickedColor { color: pick.color.with_alpha(alpha), model: None }));
self.push_from_complete(target, event.map(|pick| PickedColor { color: pick.color.with_alpha(alpha), model: None }));
}
}
+5 -2
View File
@@ -128,7 +128,7 @@ impl Body<'_> {
.show(ui, |ui, _| TextField::hex(words.hex_color.clone(), Bind::given(color.to_rgba())).show(ui))
.inner;
for event in edit.into_iter().flat_map(|edit| edit.events) {
self.push(self.target, event.map(|rgba| PickedColor::rgb(Srgba::from(rgba).with_alpha(color.a))));
self.push_from_complete(self.target, event.map(|rgba| PickedColor::rgb(Srgba::from(rgba).with_alpha(color.a))));
}
}
@@ -206,6 +206,9 @@ impl Body<'_> {
fn set_color(&mut self, target: PanelTarget, color: Srgba) {
let before = self.colors[target.index()];
let name = self.words.title.as_str();
self.push(target, EditEvent::set(Mixed::Same(PickedColor::rgb(before)), PickedColor::rgb(color.with_alpha(before.a)), name.into(), None));
self.push_from_complete(
target,
EditEvent::set(Mixed::Same(PickedColor::rgb(before)), PickedColor::rgb(color.with_alpha(before.a)), name.into(), None),
);
}
}
+129 -3
View File
@@ -343,16 +343,21 @@ fn a_disabled_fill_stroke_rgb_row_does_not_switch_its_units() {
/// A Color panel whose host supplies authoritative native CMYK components.
fn native_cmyk_panel(picked: PickedColor) -> (Fixture, Rc<RefCell<PickedColor>>, Edits) {
native_panel_in(picked, ColorPanelLayout::FillStroke, ColorPanelMode::CmykSliders)
}
/// A Color panel in `layout` and `mode` whose host supplies authoritative native CMYK components.
fn native_panel_in(picked: PickedColor, layout: ColorPanelLayout, mode: ColorPanelMode) -> (Fixture, Rc<RefCell<PickedColor>>, Edits) {
let host = Rc::new(RefCell::new(picked));
let edits: Edits = Default::default();
let (color, output) = (host.clone(), edits.clone());
let mut fixture = Fixture::new(move |ui| {
let picked = *color.borrow();
let mut state = ColorPanelState { mode: ColorPanelMode::CmykSliders, ..Default::default() };
let mut state = ColorPanelState { mode, ..Default::default() };
let shown = ColorPanel::new(Bind::bound(&mut state), [picked.color; 2])
.layout(ColorPanelLayout::FillStroke)
.layout(layout)
.edited(Some(picked))
.shown(ColorPanelShown::Mode(ColorPanelMode::CmykSliders))
.shown(ColorPanelShown::Mode(mode))
.show(ui);
for (_, event) in &shown.edits {
if let Some(picked) = event.landed() {
@@ -433,6 +438,127 @@ fn a_canceled_native_channel_drag_restores_the_hosts_original_color() {
assert_eq!(*host.borrow(), picked, "release after Cancel cannot reapply the preview");
}
/// The color the last finished edit reports it started from: the `before` of its Commit or Set.
fn last_before(edits: &Edits) -> PickedColor {
edits
.borrow()
.iter()
.rev()
.find_map(|(_, event)| match event {
EditEvent::Commit { before: Mixed::Same(before), .. } | EditEvent::Set { before: Mixed::Same(before), .. } => Some(*before),
_ => None,
})
.expect("a finished edit")
}
/// A ramp drag starts from the host's complete color: Cancel restores the native inks and Undo of a committed drag
/// returns to them, even after the host applies the drag's live updates.
#[trial]
fn a_ramp_drag_restores_and_undoes_to_the_hosts_native_color() {
let native = ModelValues::new("cmyk", &[12.345, 45.678, 78.912, 23.456]);
let picked = PickedColor::in_model(Srgba::new(0.2, 0.4, 0.6, 0.7), native);
let (mut fixture, host, edits) = native_cmyk_panel(picked);
let ramp = fixture.by_label("Color Ramp").expect("the ramp");
let from = ramp.rect.left_center() + Vec2::new(10.0, 0.0);
fixture.drag_hold_at(from, Vec2::new(60.0, 0.0)).settle();
assert_ne!(*host.borrow(), picked, "the host applies the live ramp preview");
fixture.key(Key::Escape, Modifiers::NONE).settle();
let canceled = edits
.borrow()
.iter()
.rev()
.find_map(|(_, event)| match event {
EditEvent::Cancel { before: Some(Mixed::Same(before)), .. } => Some(*before),
_ => None,
})
.expect("the ramp drag cancels");
assert_eq!(canceled, picked, "Cancel carries the host's exact opening color and model");
assert_eq!(*host.borrow(), picked, "the host restores the complete opening color");
fixture.release_at(from + Vec2::new(60.0, 0.0)).settle();
fixture.drag_at(from, Vec2::new(60.0, 0.0)).settle();
assert_ne!(*host.borrow(), picked, "the ramp drag lands a new color");
assert_eq!(last_before(&edits), picked, "the committed drag undoes to the host's color and model");
}
/// Without an `edited` color, a canceled ramp drag restores the well's color from before the drag, after the host
/// applies the drag's live updates to its wells.
#[trial]
fn a_canceled_ramp_drag_restores_the_wells_color() {
let start = Srgba::new(0.2, 0.4, 0.6, 0.7);
let wells = Rc::new(Cell::new([start, BLUE]));
let edits: Edits = Default::default();
let (colors, output) = (wells.clone(), edits.clone());
let mut fixture = Fixture::new(move |ui| {
let mut state = ColorPanelState { mode: ColorPanelMode::CmykSliders, ..Default::default() };
let shown = ColorPanel::new(Bind::bound(&mut state), colors.get()).show(ui);
for (target, event) in &shown.edits {
if let Some(picked) = event.landed() {
let mut now = colors.get();
now[usize::from(*target == PanelTarget::Background)] = picked.color;
colors.set(now);
}
}
output.borrow_mut().extend(shown.edits);
});
fixture.room(COLOR_ROOM).settle();
let ramp = fixture.by_label("Color Ramp").expect("the ramp");
let from = ramp.rect.left_center() + Vec2::new(10.0, 0.0);
fixture.drag_hold_at(from, Vec2::new(60.0, 0.0)).settle();
assert_ne!(wells.get()[0], start, "the host applies the live ramp preview");
fixture.key(Key::Escape, Modifiers::NONE).settle();
let canceled = edits.borrow().iter().rev().find_map(|(_, event)| match event {
EditEvent::Cancel { before, .. } => Some(*before),
_ => None,
});
assert_eq!(canceled, Some(Some(Mixed::Same(PickedColor { color: start, model: None }))), "Cancel carries the well's color");
assert_eq!(wells.get(), [start, BLUE], "the host restores the well's color");
}
/// A hex entry, in the cubes' readout and in the FillStroke layout's hex field, undoes to the host's complete color,
/// its native inks included.
#[trial]
fn a_hex_entry_undoes_to_the_hosts_native_color() {
let native = ModelValues::new("cmyk", &[12.345, 45.678, 78.912, 23.456]);
let picked = PickedColor::in_model(Srgba::new(0.2, 0.4, 0.6, 0.7), native);
for (layout, mode) in
[(ColorPanelLayout::ForegroundBackground, ColorPanelMode::HueCube), (ColorPanelLayout::FillStroke, ColorPanelMode::CmykSliders)]
{
let (mut fixture, host, edits) = native_panel_in(picked, layout, mode);
let hex = fixture.by_label("Hex color").expect("the hex field");
fixture.click(&hex);
fixture.key(Key::A, Modifiers::COMMAND);
fixture.type_text("996633");
fixture.key(Key::Enter, Modifiers::NONE);
fixture.settle();
assert_ne!(*host.borrow(), picked, "{layout:?}: the hex entry lands a new color");
assert_eq!(last_before(&edits), picked, "{layout:?}: the hex entry undoes to the host's color and model");
}
}
/// A click on the FillStroke layout's White well undoes to the host's complete color, its native inks included.
#[trial]
fn a_white_well_pick_undoes_to_the_hosts_native_color() {
let native = ModelValues::new("cmyk", &[12.345, 45.678, 78.912, 23.456]);
let picked = PickedColor::in_model(Srgba::new(0.2, 0.4, 0.6, 0.7), native);
let (mut fixture, host, edits) = native_cmyk_panel(picked);
let white = fixture.by_label("White").expect("the White well");
fixture.click(&white).settle();
assert_ne!(*host.borrow(), picked, "the White well lands white");
assert_eq!(last_before(&edits), picked, "the White well's pick undoes to the host's color and model");
}
/// A click on the out-of-gamut chip undoes to the host's complete color, its native inks included.
#[trial]
fn a_gamut_chip_pick_undoes_to_the_hosts_native_color() {
let native = ModelValues::new("cmyk", &[12.345, 45.678, 78.912, 23.456]);
let picked = PickedColor::in_model(Srgba::rgb(0.0, 0.0, 1.0), native);
let (mut fixture, host, edits) = native_cmyk_panel(picked);
let chip = fixture.by_label("Out of gamut for printing: click for the nearest printable color").expect("the out-of-gamut chip");
fixture.click(&chip).settle();
assert_ne!(*host.borrow(), picked, "the chip lands the nearest printable color");
assert_eq!(last_before(&edits), picked, "the chip's pick undoes to the host's color and model");
}
/// The rect of the FillStroke spectrum in Web Color for a foreground of `color`.
fn spectrum_rect(color: Srgba) -> egui::Rect {
let mut fixture = Fixture::new(move |ui| {