Replace Color: a first Add to Sample click sets the From colour (CodeRabbit review on crates/ui-egui/src/dialogs/filter_dialog.rs:763)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Hector VillarrealandClaude Opus 5.5 committed 2026-11-05 21:15:00 -06:00
1 parent 02d244a8f7
commit 9b9deca07d
2 files changed
+34 -2

No files matched your search

+7 -2
View File
@@ -794,8 +794,13 @@ pub(crate) fn color_eyedropper_sample(app: &mut PhotocraftApp, x: f64, y: f64, m
};
let Some(rgb) = crate::canvas::composite_color(app, x, y) else { return true };
let Some(mut f) = app.ui.dialogs.last().map(|d| d.fields.clone()) else { return true };
let sample = !mods.shift && !mods.alt && f.get("__tool").and_then(Value::as_str).is_none_or(|t| t == "sample");
if sample {
let tool = f.get("__tool").and_then(Value::as_str);
let sample = !mods.shift && !mods.alt && tool.is_none_or(|t| t == "sample");
// A first Add to Sample, before any sample, starts from the clicked colour too: one point
// matches through `color`, and Color Range's matcher starts its range there as well.
let add = mods.shift || (!mods.alt && tool == Some("add"));
let first_add = add && f.get("points").and_then(Value::as_array).is_none_or(Vec::is_empty);
if sample || first_add {
f.insert("color".into(), json!(crate::color_picker_ui::hex(rgb)));
}
// Where it was picked: Localized Color Clusters match the colour near this point (#4457);
@@ -1589,6 +1589,33 @@ fn replace_color_shift_and_alt_clicks_add_and_subtract_samples() {
assert_eq!((field(&app, "points"), field(&app, "subtractPoints")), (json!([[7.0, 7.0]]), json!([])));
}
/// A first click with Add to Sample, before any plain sample, takes the clicked colour as the
/// From colour, so the single-sample command replaces it, not the foreground colour (CodeRabbit
/// review on #10443). Color Range's matcher, used from the second sample on, also starts its
/// range at the first added colour.
#[test]
fn replace_color_first_add_to_sample_click_sets_the_from_colour() {
let mut s = photocraft_engine::Session::new();
s.execute("file.new", json!({"width": 16, "height": 16, "background": "#00ff00"})).unwrap();
let mut app = PhotocraftApp::new(s, crate::Services::default());
let id = open(&mut app, REPLACE_COLOR).unwrap();
let field = |app: &PhotocraftApp, k: &str| app.ui.dialogs.last().unwrap().fields.get(k).cloned().unwrap_or(Value::Null);
assert_ne!(field(&app, "color"), json!("#00ff00"), "the dialog starts from the foreground colour");
for (tool, mods) in [("sample", egui::Modifiers::SHIFT), ("add", egui::Modifiers::NONE)] {
let mut fields = app.ui.dialogs.last().unwrap().fields.clone();
reset_fields(&app, &mut fields);
fields.insert("__tool".into(), json!(tool));
app.ui.dialog_mut(id).unwrap().fields = fields;
assert!(color_eyedropper_sample(&mut app, 4.0, 4.0, mods));
assert_eq!(field(&app, "points"), json!([[4.0, 4.0]]));
assert_eq!(field(&app, "color"), json!("#00ff00"), "{tool}");
}
// A later added sample leaves the From colour alone.
app.ui.dialog_mut(id).unwrap().fields.insert("color".into(), json!("#123456"));
assert!(color_eyedropper_sample(&mut app, 6.0, 6.0, egui::Modifiers::SHIFT));
assert_eq!(field(&app, "color"), json!("#123456"));
}
/// Reset (Alt+Cancel) forgets the click order with the samples, so samples added afterwards
/// carry an order that fits them and the command takes it (CodeRabbit review on #10443).
#[test]