fix(ui): keep the Color Picker preview on the document it was opened on

A command Color Picker now records the document it was opened on, and its
live canvas preview never runs against another document that became
active while the picker was open (its layer ids would point elsewhere).
Covers the tab-switch gap noted in review.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014QvfaMyAX4i4PbKBMnvFj6
This commit is contained in:
saschaweissandClaude Opus 5.5 authored and Claude committed 2026-10-27 01:11:34 +01:00
1 parent 5bea3fd3f5
commit 7786c2247b
3 files changed
+33

No files matched your search

@@ -108,7 +108,13 @@ pub(crate) fn display_doc(app: &mut PhotocraftApp, idx: usize) -> Option<(Arc<Do
return None;
};
let dialog = d.id;
let opened_on = d.fields.get("__document").and_then(Value::as_u64);
let st = app.session.documents().get(idx)?;
// Another document became active while the picker was open: its layer ids mean nothing here.
if opened_on.is_some_and(|id| id != st.doc.id.0) {
forget_shown(app);
return None;
}
let (document, revision) = (st.doc.id, st.revision);
let layers = target_layers(&params);
let key = (egui::Id::unique((document.0, revision, dialog, params.to_string())).value() & (TAG - 1)) | TAG;
@@ -291,3 +291,25 @@ fn ok_commits_the_previewed_gradient_stop_in_one_history_step() {
assert_eq!(stop_rgb(&doc, id, 1), [0, 255, 0]);
assert_eq!(committed(&app).3, history + 1);
}
#[test]
fn the_preview_never_shows_on_a_document_switched_to_while_the_picker_is_open() {
let mut app = app_at(8);
let id = new_type_layer(&mut app, "Hello");
pick(&mut app, "#ff0000");
assert!(display_doc(&mut app, 0).is_some());
// A second document becomes the active tab, with a type layer that may share the id.
app.run("file.new", json!({"width": 400, "height": 200})).unwrap();
let other = new_type_layer(&mut app, "Other");
let idx = app.session.active_index().unwrap();
assert_ne!(idx, 0);
assert!(display_doc(&mut app, idx).is_none(), "the picker belongs to the first document");
assert!(display_doc(&mut app, 0).is_none(), "an inactive tab shows no dialog preview");
assert_eq!(rgb_at(&app.session.active().unwrap().doc, other, 0), [0, 0, 0, 255]);
// Back on the document it was opened on, the preview returns.
assert!(app.session.set_active(0));
let (shown, _) = display_doc(&mut app, 0).expect("preview on its own document");
assert_eq!(rgb_at(&shown, id, 0), [255, 0, 0, 255]);
}
+5
View File
@@ -182,6 +182,11 @@ pub fn open_for_command(app: &mut PhotocraftApp, label: &str, rgb: [f32; 3], com
let mut f = fields_with_web(app, "command", label, rgb);
f.insert("__command".into(), json!(command));
f.insert("__params".into(), params);
// The document the picker was opened on: its live canvas preview never shows on another
// (`color_picker_preview`).
if let Some(st) = app.session.active() {
f.insert("__document".into(), json!(st.doc.id.0));
}
app.ui.open_dialog(DialogKind::Command, f)
}