Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

fix(ui): Color Picker updates the colour on press, not on release (#6937)

highperfocusedf7e441244f31
diff --git a/crates/ui-egui/src/color_picker_ui.rs b/crates/ui-egui/src/color_picker_ui.rs
index 19272a62e..121b05282 100644
--- a/crates/ui-egui/src/color_picker_ui.rs
+++ b/crates/ui-egui/src/color_picker_ui.rs
@@ -509,7 +509,10 @@ fn color_field(ui: &mut egui::Ui, f: &mut Map<String, Value>) {
ui.painter().rect_stroke(field, 0.0, Stroke::new(1.0, t.field_border), StrokeKind::Outside);
let marker = pos2(field.left() + fx * field.width(), field.top() + fy * field.height());
ui.painter().circle_stroke(marker, 5.0, Stroke::new(1.5, if hsv[2] > 0.6 && hsv[1] < 0.4 { Color32::BLACK } else { Color32::WHITE }));
- if let Some(p) = resp.interact_pointer_pos().filter(|_| resp.dragged() || resp.clicked()) {
+ // Photoshop updates the colour on the press itself, not only when the button is released
+ // (#6937): `is_pointer_button_down_on` is true from the press frame onwards (it covers
+ // dragging too), while `clicked` fires on release and `dragged` only after a move.
+ if let Some(p) = resp.interact_pointer_pos().filter(|_| resp.is_pointer_button_down_on() || resp.clicked()) {
let (x, y) = (((p.x - field.left()) / field.width()).clamp(0.0, 1.0), ((p.y - field.top()) / field.height()).clamp(0.0, 1.0));
let c = field_color(&mode, fz, x, y);
let keep = match mode.as_str() {
@@ -528,7 +531,7 @@ fn color_field(ui: &mut egui::Ui, f: &mut Map<String, Value>) {
let tri = vec![pos2(x, sy), pos2(x - 6.0 * dir, sy - 4.0), pos2(x - 6.0 * dir, sy + 4.0)];
ui.painter().add(egui::Shape::convex_polygon(tri, t.text, Stroke::NONE));
}
- if let Some(p) = sresp.interact_pointer_pos().filter(|_| sresp.dragged() || sresp.clicked()) {
+ if let Some(p) = sresp.interact_pointer_pos().filter(|_| sresp.is_pointer_button_down_on() || sresp.clicked()) {
let z = 1.0 - ((p.y - strip.top()) / strip.height()).clamp(0.0, 1.0);
let h = match mode.as_str() {
"h" => Some([z * 360.0, hsv[1], hsv[2]]),
@@ -895,6 +898,32 @@ mod tests {
assert_eq!(color(&h), "#12abef", "casing is presentation only");
}
+ /// #6937: pressing the hue strip must update the colour on the press frame, not only when
+ /// the button is released (Photoshop updates immediately on click-and-hold).
+ #[test]
+ fn hue_strip_updates_the_color_on_press_not_on_release() {
+ let mut h = picker("#0000ff"); // pure blue: hue 240°
+ let web = h.get_by_label("Only Web Colors").rect();
+ let pick = h.get_by_label("Pick screen color").rect();
+ // The strip is 18 pt wide, 6 pt right of the 200 pt field whose left edge is the column's
+ // (the screen-colour button shares it); its bottom edge is just above the checkbox.
+ // Press near the bottom: hue ~5°, a red the blue starting colour cannot produce via the field.
+ let press = pos2(pick.left() + FIELD + 15.0, web.top() - 20.0);
+ h.event(egui::Event::PointerMoved(press));
+ h.run_steps(1);
+ h.event(egui::Event::PointerButton { pos: press, button: egui::PointerButton::Primary, pressed: true, modifiers: egui::Modifiers::NONE });
+ h.run_steps(1);
+ let held = color(&h);
+ assert_ne!(held, "#0000ff", "holding the button on the strip must already change the colour");
+ // The colour is the red-ish hue near the strip's bottom, not the old blue: had the press
+ // landed on the field instead of the strip, the hue (and the blue channel) would survive.
+ let [r, _, b] = parse_hex(&held).unwrap();
+ assert!(r > 0.5 && b < 0.2, "expected a red-ish hue from the strip, got {held}");
+ h.event(egui::Event::PointerButton { pos: press, button: egui::PointerButton::Primary, pressed: false, modifiers: egui::Modifiers::NONE });
+ h.run_steps(1);
+ assert_eq!(color(&h), held, "releasing must not move the colour again");
+ }
+
#[test]
fn hex_field_takes_typed_digits() {
let mut h = picker("#000000");