From fffecde82acc9b142d781f0bb79b3798393de03c Mon Sep 17 00:00:00 2001 From: yoriswind Date: Tue, 1 Dec 2026 19:16:41 -0800 Subject: [PATCH] Keep the Scale Ruler gesture registered by the open dialog --- crates/ui-egui/src/tools/analysis/canvas.rs | 9 ++------- crates/ui-egui/src/tools/analysis/dialogs.rs | 20 ++++++++++++------- .../analysis/scale_ruler_while_open_tests.rs | 12 +++++++++++ 3 files changed, 27 insertions(+), 14 deletions(-) diff --git a/crates/ui-egui/src/tools/analysis/canvas.rs b/crates/ui-egui/src/tools/analysis/canvas.rs index 57995867ee1..a64e782242c 100644 --- a/crates/ui-egui/src/tools/analysis/canvas.rs +++ b/crates/ui-egui/src/tools/analysis/canvas.rs @@ -9,11 +9,10 @@ use craft::behavior::{CancelReason, CountMark, Cursor, CursorBadge, CursorIcon, use craft::color_ui::ui::{ColorSamplerMarks, ColorSamplerTool, ColorSamplers, SamplerEvent}; use craft::draw::ui::{RulerLine, RulerTool}; use craft::ui::canvas::{CanvasTool, NoTool, ToolAction, ToolClaims, ToolContext, ToolHostResponse, ToolKey, ToolPath, ToolPointer, canvas_space}; -use craft::ui::contract::{Access, CanvasGesture, Common, Component, Cx, MeasureCx, PaintUi, Props as _, Role, Show as _}; +use craft::ui::contract::{Access, Common, Component, Cx, MeasureCx, PaintUi, Props as _, Role, Show as _}; use craft::ui::fonts::FontRef; use craft::ui::primitives::{DocRun, DocText, Draw, PaintShape, StrokeWidth}; use craft::ui::runtime::env::Label; -use craft::ui::runtime::sampler; use craft::ui::style::{ComponentStyle, DataColor, StyleColor, StyleCx}; use egui::{Color32, CornerRadius, Pos2, Rect, Response, Vec2, vec2}; use serde_json::json; @@ -141,11 +140,7 @@ pub(crate) fn tool<'a>(scope: &'a ToolScope<'a>, state: &'a mut AnalysisTools, h let tool = scope.with_app(|app| app.ui.tool); let AnalysisTools { ruler, scale, sampler, samplers, count, note } = state; match tool { - Tool::Ruler if scope.with_app(|app| scale_ruler_on(app, host)) => { - // The modal's ParamDialog keeps this canvas open for navigation. Pass the ruler's primary drag too. - sampler::pass_tool_gesture(scope.ctx(), host.canvas_id(), CanvasGesture::PRIMARY); - Box::new(scale.tool.pass(scale.line)) - } + Tool::Ruler if scope.with_app(|app| scale_ruler_on(app, host)) => Box::new(scale.tool.pass(scale.line)), Tool::Ruler => { let line = scope.with_app(|app| ruler_line(app)); Box::new(ruler.pass(line)) diff --git a/crates/ui-egui/src/tools/analysis/dialogs.rs b/crates/ui-egui/src/tools/analysis/dialogs.rs index d6bb2d27228..2537c846e02 100644 --- a/crates/ui-egui/src/tools/analysis/dialogs.rs +++ b/crates/ui-egui/src/tools/analysis/dialogs.rs @@ -3,7 +3,7 @@ use craft::behavior::IconId; use craft::ui::choice::Checkbox; -use craft::ui::contract::{Bind, Props as _, Scroll, Show as _, ShowContainer as _}; +use craft::ui::contract::{Bind, CanvasGesture, Props as _, Sampler as _, Scroll, Show as _, ShowContainer as _}; use craft::ui::dialog::{DialogId, dialog_id}; use craft::ui::display::{Divider, Elide, Icon, Text}; use craft::ui::layout_components::{Row, Stack}; @@ -15,6 +15,7 @@ use photocraft_engine::analysis_cmds; use serde_json::{Value, json}; use crate::PhotocraftApp; +use crate::canvas::document_display::CanvasHost; use crate::dialogs::params; use crate::state::Tool; @@ -97,7 +98,8 @@ pub(crate) fn open(app: &mut PhotocraftApp, kind: Kind) -> Result DialogId { @@ -208,8 +210,12 @@ pub(crate) fn scale_readout(f: &DialogFields, decimal: char) -> String { format!("{} pixel(s) = {} {units}", mark(pixels.to_string()), mark(format!("{length:.4}"))) } -fn scale_ruler_note(ui: &mut egui::Ui) { - Row::new().show(ui, |ui, _| { +fn scale_ruler_note(ui: &mut egui::Ui, canvas: Option) { + Row::new().show(ui, |ui, cx| { + // The dialog runs every pass, even when the canvas replays its cached draw. + if let Some(canvas) = canvas { + cx.pass_canvas_gesture(canvas, CanvasGesture::PRIMARY); + } Icon::new(IconSpec::svg(IconId::new("info"))).show(ui); let note = tl!("The Ruler tool has been activated while this dialog is open. You may use it to measure the pixel length for use in your scale."); let sample: String = note.chars().take(READOUT_CHARS).collect(); @@ -222,7 +228,7 @@ fn options(pairs: &[(&str, &str)]) -> Vec<(String, Label)> { } /// The dialog of `kind` over `fields`. -fn form(kind: Kind, fields: &mut DialogFields) -> ParamDialog<'_> { +fn form(kind: Kind, fields: &mut DialogFields, scale_canvas: Option) -> ParamDialog<'_> { let key = kind.command(); let dialog = match kind { Kind::Scale => { @@ -233,7 +239,7 @@ fn form(kind: Kind, fields: &mut DialogFields) -> ParamDialog<'_> { Param::new("logicalLength", tl!("Logical Length:"), ParamKind::number(Some(0.0001), Some(1e7), 1.0, 1.0, 4)).not_remembered(), Param::new("units", tl!("Logical Units:"), ParamKind::text("pixels")).not_remembered(), ]; - ParamDialog::command(key, tl!("Measurement Scale"), rows).below(|ui, f| { + ParamDialog::command(key, tl!("Measurement Scale"), rows).below(move |ui, f| { // One line: units too long for it are cut with "…", so the dialog keeps its width (#17414). The decimal // mark is the one the fields above write: the system's (any mark, not only `.` or `,`) or the host's. let text = scale_readout(f, Env::of(ui).format().separators().0); @@ -241,7 +247,7 @@ fn form(kind: Kind, fields: &mut DialogFields) -> ParamDialog<'_> { // and long units are cut with "…" instead of widening the dialog. let sample: String = text.chars().take(READOUT_CHARS).collect(); Text::new(Label::from(text)).elide(Elide::End).sample(Label::from(sample)).show(ui); - scale_ruler_note(ui); + scale_ruler_note(ui, scale_canvas); }) } Kind::DataPoints => { diff --git a/crates/ui-egui/src/tools/analysis/scale_ruler_while_open_tests.rs b/crates/ui-egui/src/tools/analysis/scale_ruler_while_open_tests.rs index 2f605a70dbf..4eeff3d3ff6 100644 --- a/crates/ui-egui/src/tools/analysis/scale_ruler_while_open_tests.rs +++ b/crates/ui-egui/src/tools/analysis/scale_ruler_while_open_tests.rs @@ -86,8 +86,11 @@ fn an_exposed_drag_updates_only_pixel_length_and_ok_restores_the_tool() { let primary = CanvasHost::primary(h.state(), 0); assert!(scale_ruler_on(h.state(), primary)); assert!(!scale_ruler_on(h.state(), CanvasHost { idx: 0, window: Some(99) })); + // Let the canvas settle past the lifetime of any registration made when the form opened. + h.run_steps(4); let span = points(&h); drag(&mut h, span); + assert!(crate::canvas::tools::in_gesture(h.state()), "the visible canvas claims the modal ruler drag"); let f = fields(&h); assert_eq!((f.number("pixelLength"), f.number("logicalLength"), f.text("units")), (Some(200.0), Some(3.5), Some("cm"))); assert!(length_has_focus(&h), "the admitted canvas press leaves keyboard focus in the form"); @@ -114,6 +117,7 @@ fn a_nested_prompt_blocks_measurement_and_escape_discards_the_scale_draft() { let alert = crate::dialogs::alerts::alert(h.state_mut(), "Test prompt over Measurement Scale"); h.run_steps(4); drag(&mut h, span); + assert!(!crate::canvas::tools::in_gesture(h.state()), "the nested prompt owns the pointer"); button(&mut h, span[1], false); assert_eq!(fields(&h).number("pixelLength"), Some(42.0)); let ctx = h.ctx.clone(); @@ -123,7 +127,15 @@ fn a_nested_prompt_blocks_measurement_and_escape_discards_the_scale_draft() { assert_eq!(fields(&h).number("pixelLength"), Some(200.0)); h.key_press(Key::Escape); h.run_steps(4); + assert_eq!(fields(&h).number("pixelLength"), Some(42.0), "Escape cancels the held canvas drag before dismissing the form"); button(&mut h, span[1], false); + + let span = points(&h); + drag(&mut h, span); + button(&mut h, span[1], false); + assert_eq!(fields(&h).number("pixelLength"), Some(200.0)); + h.key_press(Key::Escape); + h.run_steps(4); assert!(!h.state().dialog_open()); assert_eq!(h.state().ui.tool, Tool::Brush); assert!(h.state().canvas_tools.analysis.scale.line.is_none());