From 6476b041442291c32deb99f8c41d5afb8f4d3ed2 Mon Sep 17 00:00:00 2001 From: yoriswind Date: Tue, 17 Nov 2026 02:34:58 -0800 Subject: [PATCH] #8284: restore the Key Color initiator after a rejected tool change --- crates/ui-egui/src/app/commands.rs | 8 +- crates/ui-egui/src/dialogs/tests.rs | 118 ++++++++++++++++++++++++++++ 2 files changed, 125 insertions(+), 1 deletion(-) diff --git a/crates/ui-egui/src/app/commands.rs b/crates/ui-egui/src/app/commands.rs index 79ec296fc2..76f793567c 100644 --- a/crates/ui-egui/src/app/commands.rs +++ b/crates/ui-egui/src/app/commands.rs @@ -424,12 +424,18 @@ impl VectorcraftApp { /// and the tool remembered for the toolbar group and for the Touch workspace's drawing mode /// stays as it was. pub(crate) fn try_select_tool(&mut self, id: &str) -> Result<(), String> { + let back = self.ui.brush_key_color_pick.as_ref().map(|pick| pick.back.clone()); crate::dialogs::cancel_brush_key_color(self, false); // The tip of a pen whose eraser end selected the Eraser doesn't undo this choice, and // neither does letting go of a held tool shortcut. self.pen_end.forget_tool(); self.held_tool.clear(); - self.activate_tool(id, true)?; + if let Err(error) = self.activate_tool(id, true) { + if let Some(back) = back.filter(|_| self.session.tool_id() == "eyedropper") { + self.return_to_tool(&back); + } + return Err(error); + } let now = self.session.tool_id(); if crate::touch::is_drawing_tool(now) { self.ui.touch_drawing_tool = Some(now.to_string()); diff --git a/crates/ui-egui/src/dialogs/tests.rs b/crates/ui-egui/src/dialogs/tests.rs index 08968d5e93..f2199e6700 100644 --- a/crates/ui-egui/src/dialogs/tests.rs +++ b/crates/ui-egui/src/dialogs/tests.rs @@ -3654,3 +3654,121 @@ fn color_halftones_max_radius_stays_in_pixels_whatever_the_units() { assert!(shows, "{typed} shows as {shown}, not in centimetres"); } } + +/// A real Key Color pick returns to its initiating tool when choosing its replacement fails. +#[test] +fn a_failed_tool_choice_restores_the_brush_key_color_initiator() { + use egui::{Event, Modifiers, PointerButton, Pos2, RawInput, Rect, vec2}; + + fn full_frame(app: &mut VectorcraftApp, ctx: &egui::Context, events: Vec) -> egui::FullOutput { + let mut raw = + RawInput { events, focused: true, screen_rect: Some(Rect::from_min_size(Pos2::ZERO, vec2(1600.0, 1100.0))), ..Default::default() }; + app.raw_input_hook(ctx, &mut raw); + let mut output = ctx.run_ui(raw, |ui| { + app.tick(ui.ctx()); + app.ui(ui); + }); + output.textures_delta.clear(); + output + } + + fn arm(app: &mut VectorcraftApp, ctx: &egui::Context) { + for _ in 0..4 { + let _ = full_frame(app, ctx, vec![]); + } + let mut bounds = None; + for _ in 0..8 { + let output = full_frame(app, ctx, vec![]); + bounds = output.platform_output.accesskit_update.as_ref().and_then(|update| { + update + .nodes + .iter() + .find(|(_, node)| node.label() == Some("Eyedropper: click the art to sample the key color")) + .and_then(|(_, node)| node.bounds()) + }); + if bounds.is_some() { + break; + } + } + let bounds = bounds.expect("the real Brush Options pipette button"); + let at = egui::pos2(((bounds.x0 + bounds.x1) * 0.5) as f32, ((bounds.y0 + bounds.y1) * 0.5) as f32); + let _ = full_frame(app, ctx, vec![Event::ModifiersChanged(Modifiers::NONE), Event::PointerMoved(at)]); + for pressed in [true, false] { + let _ = full_frame(app, ctx, vec![Event::PointerButton { pos: at, button: PointerButton::Primary, pressed, modifiers: Modifiers::NONE }]); + } + assert_eq!(app.session.tool_id(), "eyedropper"); + let pick = app.ui.brush_key_color_pick.as_ref().expect("the button owns a live pick"); + assert_eq!(pick.back, "paintbrush"); + assert_eq!(pick.doc, app.session.active().unwrap().uid); + assert!(pick.press.is_none()); + assert_eq!(app.session.tool_options()["brushKeyColor"], true); + assert_eq!(app.session.tool_options()["stop"], "paintbrush"); + } + + let mut app = VectorcraftApp::new(Session::new(), Default::default()); + app.run("file.new", json!({"width": 400, "height": 300})).unwrap(); + app.run("paint.setFill", json!({"color": "#000000"})).unwrap(); + let art = app.run("shape.rectangle", json!({"x": 300, "y": 30, "width": 20, "height": 12})).unwrap()["id"].clone(); + let brush = "Failed tool choice fixture"; + app.run( + "brush.new", + json!({"type": "art", "name": brush, "ids": [art], + "params": {"colorization": {"method": "hueShift", "key": vectorcraft_color::Color::BLACK}}}), + ) + .unwrap(); + app.run("select.none", json!({})).unwrap(); + app.select_tool("paintbrush"); + app.ui.task_bar = false; + app.ui.view.rulers = false; + app.canvas.worker_started = true; + app.session.prefs.interface_language = "en".into(); + crate::i18n::set_current(crate::i18n::Lang::EN); + let ctx = egui::Context::default(); + ctx.enable_accesskit(); + app.run("ui.brushOptions", json!({"name": brush})).unwrap(); + arm(&mut app, &ctx); + let unchanged = |app: &VectorcraftApp| { + let state = app.session.active().unwrap(); + ( + json!({"uid": state.uid, "doc": state.doc, "selection": state.selection, "revision": state.revision, + "undo": state.history.undo.len(), "redo": state.history.redo.len()}), + app.session.paint.clone(), + ) + }; + let before = unchanged(&app); + let draft = app.ui.dialog.as_ref().unwrap().fields.clone(); + let group_tools = app.ui.group_tool.clone(); + let drawing_tool = app.ui.touch_drawing_tool.clone(); + + // The existing failure hook exercises a valid replacement, not an unknown tool id. + app.fail_next_tool_choice = Some("key-color replacement failed".into()); + assert_eq!(app.try_select_tool("rectangle"), Err(String::from("key-color replacement failed"))); + assert!(app.fail_next_tool_choice.is_none()); + assert!(app.ui.brush_key_color_pick.is_none()); + assert_eq!(app.session.tool_options_of("eyedropper")["brushKeyColor"], false); + assert!(app.session.tool_options_of("eyedropper")["stop"].is_null()); + assert_eq!(app.ui.dialog.as_ref().unwrap().fields, draft); + assert_eq!(app.ui.group_tool, group_tools); + assert_eq!(app.ui.touch_drawing_tool, drawing_tool); + assert_eq!(unchanged(&app), before); + // Before the repair, all cleanup checks above pass, but the session stays on Eyedropper. + assert_eq!(app.session.tool_id(), "paintbrush", "a failed replacement must restore the pick's initiating tool"); + + cancel(&mut app); + assert!(app.ui.dialog.is_none()); + assert_eq!(app.session.tool_id(), "paintbrush", "Cancel cannot return an already consumed pick twice"); + assert_eq!(unchanged(&app), before); + app.run("ui.brushOptions", json!({"name": brush})).unwrap(); + arm(&mut app, &ctx); + app.try_select_tool("rectangle").unwrap(); + assert_eq!(app.session.tool_id(), "rectangle", "a successful replacement stays selected"); + assert!(app.ui.brush_key_color_pick.is_none()); + assert_eq!(app.session.tool_options_of("eyedropper")["brushKeyColor"], false); + assert!(app.session.tool_options_of("eyedropper")["stop"].is_null()); + assert_eq!(app.ui.dialog.as_ref().unwrap().fields, draft); + assert_eq!(unchanged(&app), before); + cancel(&mut app); + assert!(app.ui.dialog.is_none()); + assert_eq!(app.session.tool_id(), "rectangle", "Cancel must preserve the successful replacement"); + assert_eq!(unchanged(&app), before); +}