diff --git a/crates/ui-egui/src/eyedropper.rs b/crates/ui-egui/src/eyedropper.rs index 4dc62ea58..28e6932d3 100644 --- a/crates/ui-egui/src/eyedropper.rs +++ b/crates/ui-egui/src/eyedropper.rs @@ -17,7 +17,8 @@ const SCALE: f64 = 2.0; /// The target and the single dropper tool retained until picking or cancellation ends the arming. pub struct Armed { target: Target, - // PLAN: plan error: Armed.slide is unused; the sample carries its point and the engine uses current targets. + /// The presentation the snapshot shows: the pick applies to the active presentation, so a switch ends it. + doc: u64, tool: Option>, /// No canvas pass has settled since arming: the press in that pass is the one that armed it. fresh: bool, @@ -38,7 +39,7 @@ pub fn arm(app: &mut SlideApp, target: Target) { app.set_status("The Eyedropper takes colours from a slide"); return; } - let slide = d.selection.slide; + let (doc, slide) = (d.uid, d.selection.slide); let image = Arc::new(deckcraft_render::render_slide(&d.doc, slide, &deckcraft_render::RenderOpts { scale: SCALE, ..Default::default() })); let tool = DropperTool::new(move |[x, y]| { let (x, y) = (x * SCALE, y * SCALE); @@ -53,12 +54,18 @@ pub fn arm(app: &mut SlideApp, target: Target) { Target::Menu(_) => "Eyedropper: click the slide (Esc cancels)", Target::TransparentColor => "Set Transparent Color: click the colour in the picture (Esc cancels)", }; - app.eyedropper = Some(Armed { target, tool: Some(tool), fresh: true }); + app.eyedropper = Some(Armed { target, doc, tool: Some(tool), fresh: true }); app.set_status(status); } /// Takes the arming's tool for the canvas pass without rebuilding its sampler or gesture state. +/// An arming from a presentation that is no longer active ends here. pub fn tool(app: &mut SlideApp) -> Option> { + let active = app.session.active().map(|d| d.uid); + if app.eyedropper.as_ref().is_some_and(|armed| Some(armed.doc) != active) { + app.eyedropper = None; + return None; + } app.eyedropper.as_mut()?.tool.take() } diff --git a/crates/ui-egui/src/eyedropper_tests.rs b/crates/ui-egui/src/eyedropper_tests.rs index 2c1e39972..35e672fda 100644 --- a/crates/ui-egui/src/eyedropper_tests.rs +++ b/crates/ui-egui/src/eyedropper_tests.rs @@ -164,3 +164,34 @@ fn set_transparent_color_takes_the_colour_clicked_in_the_picture() { let [r, g, b, _] = blue; assert_eq!(clear, Some(Some(deckcraft_color::Rgba::rgb(r, g, b)))); } + +/// An arming belongs to the presentation it was armed in: after a switch to another window, a click +/// there must not set the transparent colour of that presentation's selected picture. +#[test] +fn a_document_switch_ends_set_transparent_color() { + let png = deckcraft_render::Image { width: 1, height: 1, pixels: vec![0, 0, 255, 255] }.to_png(); + let picture = json!({"data": deckcraft_engine::cmd::base64_encode(&png), "name": "a.png", "rect": [100, 100, 400, 200]}); + let mut session = Session::with_new(); + session.execute("slide.new", &json!({"layout": "blank"})).unwrap(); + session.execute("insert.picture", &picture).unwrap(); + session.execute("file.new", &json!({})).unwrap(); + session.execute("slide.new", &json!({"layout": "blank"})).unwrap(); + session.execute("insert.picture", &picture).unwrap(); + session.execute("window.next", &json!({"index": 0})).unwrap(); + let mut h = crate::test_harness::harness_with(session, Services::default(), Harness::builder().with_size(egui::vec2(1600.0, 1200.0)), |_| {}); + rerun_input_passes(&h); + h.run_steps(4); + eyedropper::arm(h.state_mut(), Target::TransparentColor); + h.run_steps(3); + assert!(h.state().eyedropper.is_some(), "the command arms the pointer"); + h.state_mut().session.execute("window.next", &json!({"index": 1})).unwrap(); + h.run_steps(3); + let inside = at(&h, 300.0, 200.0); + click(&mut h, inside); + assert!(h.state().eyedropper.is_none(), "the switch ends the arming"); + let transparent = h.state().session.documents().iter().flat_map(|d| d.shapes()).any(|sh| match &sh.kind { + deckcraft_model::ShapeKind::Picture { fill, .. } => fill.adjust.clear_color.is_some(), + _ => false, + }); + assert!(!transparent, "no picture took a transparent colour"); +}