diff --git a/crates/mcp/src/ui_catalog.rs b/crates/mcp/src/ui_catalog.rs index e5a1320121..4a6e484386 100644 --- a/crates/mcp/src/ui_catalog.rs +++ b/crates/mcp/src/ui_catalog.rs @@ -716,7 +716,7 @@ pub const UI_COMMANDS: &[(&str, &str, &str, &str)] = &[ "tool.quickPen.flipArc", "Flip Arc", "", - "{} flip the last sharp-corner arc of the path the Quick Pen tool is drawing to the other side of its chord, as C and the toolbar's Flip Arc button do, as one undo step; returns false, changing nothing, when the Quick Pen tool is not active, no path is being drawn or the last piece is not a sharp-corner arc", + "{} flip the last sharp-corner arc of the path the Quick Pen tool is drawing to the other side of its chord, as C and the toolbar's Flip Arc button do, as one undo step; first finishes any pending tool edit (which can change the document), then returns false, changing nothing more, when the Quick Pen tool is not active, no path is being drawn or the last piece is not a sharp-corner arc", ), ( "window.dock", diff --git a/crates/ui-egui/src/canvas/quick_pen_bar.rs b/crates/ui-egui/src/canvas/quick_pen_bar.rs index f9e5bba393..fdca1884de 100644 --- a/crates/ui-egui/src/canvas/quick_pen_bar.rs +++ b/crates/ui-egui/src/canvas/quick_pen_bar.rs @@ -80,6 +80,13 @@ enum Click { Run(&'static str, serde_json::Value), } +/// Is a drag under way (past the drag threshold) that began on the canvas `canvas` outside the +/// toolbar's last place `bar`? A stroke, a Space or middle-button pan and a drag from a path's first +/// point all are; a drag of the toolbar's handle is not. +fn canvas_drag(dragging: bool, press: Option, canvas: egui::Rect, bar: Option) -> bool { + dragging && press.is_some_and(|p| canvas.contains(p) && !bar.is_some_and(|b| b.contains(p))) +} + /// The Quick Pen toolbar, while the Quick Pen tool is active, Window › Quick Pen Toolbar is on and /// no press is dragging on the canvas. Selecting the tool turns Window › Quick Pen Toolbar on /// ([`VectorcraftApp::try_select_tool`]). @@ -87,7 +94,15 @@ pub(super) fn quick_pen_bar(app: &mut VectorcraftApp, ui: &mut Ui, xf: &Xf) { if !app.ui.quick_pen_bar || app.session.tool_id() != quickpen::ID { return; } - if app.session.tool_busy() && ui.input(|i| i.pointer.is_decidedly_dragging()) { + let (dragging, press, down) = ui.input(|i| (i.pointer.is_decidedly_dragging(), i.pointer.press_origin(), i.pointer.any_down())); + // The handle's drag moves the toolbar away from where the press began. + let handle_id = bar_id().with("handle"); + if !down { + ui.ctx().data_mut(|d| d.insert_temp(handle_id, false)); + } + let on_handle = ui.ctx().data(|d| d.get_temp::(handle_id)).unwrap_or(false); + let shown = app.ui.quick_pen_bar_place.shown_at.zip(egui::AreaState::load(ui.ctx(), bar_id()).and_then(|s| s.size)); + if canvas_drag(dragging && !on_handle, press, xf.rect, shown.map(|(at, size)| egui::Rect::from_min_size(xf.rect.min + at, size))) { return; } let Some(doc) = app.session.active().map(|st| st.uid) else { return }; @@ -147,6 +162,7 @@ pub(super) fn quick_pen_bar(app: &mut VectorcraftApp, ui: &mut Ui, xf: &Xf) { let active = grip.hovered() || grip.dragged(); ui.painter().rect_filled(g.shrink2(vec2(0.5, 4.0)), CornerRadius::same(2), if active { t.text_dim } else { t.button_border }); if grip.dragged() { + ui.ctx().data_mut(|d| d.insert_temp(handle_id, true)); ui.ctx().set_cursor_icon(egui::CursorIcon::Grabbing); moved = grip.drag_delta(); } else if grip.hovered() { @@ -233,6 +249,20 @@ mod tests { assert!(!egui::Rect::from_min_size(at, size).contains(end)); } + /// Any drag that began on the canvas hides the toolbar, whether or not the tool has a press + /// (a pan does not); a drag that began on the toolbar, and no drag, do not. + #[test] + fn a_drag_that_began_on_the_canvas_hides_the_toolbar() { + let canvas = egui::Rect::from_min_max(pos2(0.0, 0.0), pos2(800.0, 600.0)); + let bar = Some(egui::Rect::from_min_size(pos2(300.0, 500.0), vec2(180.0, 40.0))); + assert!(canvas_drag(true, Some(pos2(100.0, 100.0)), canvas, bar), "a pan or stroke on the canvas"); + assert!(canvas_drag(true, Some(pos2(100.0, 100.0)), canvas, None), "no toolbar shown yet"); + assert!(!canvas_drag(false, Some(pos2(100.0, 100.0)), canvas, bar), "a click, not a drag"); + assert!(!canvas_drag(true, Some(pos2(310.0, 510.0)), canvas, bar), "the toolbar's own drag"); + assert!(!canvas_drag(true, Some(pos2(900.0, 100.0)), canvas, bar), "a drag in another panel"); + assert!(!canvas_drag(true, None, canvas, bar)); + } + /// A drag's offset moves the toolbar from its place, unless that would put it over the point /// the path goes on from. #[test] diff --git a/crates/ui-egui/src/menus/run.rs b/crates/ui-egui/src/menus/run.rs index e66f984023..659b65b96a 100644 --- a/crates/ui-egui/src/menus/run.rs +++ b/crates/ui-egui/src/menus/run.rs @@ -689,11 +689,14 @@ fn run_ui_part6(app: &mut VectorcraftApp, id: &str, p: &Value) -> Option { - // C's flip, for the toolbar's Flip Arc button: false, with nothing changed, while the - // last piece of the path being drawn is not a sharp-corner arc. Input held for an edit - // first, so the key is made now rather than held. - let r = app.session.finish_tool_edit(); - crate::canvas::apply_requests(app, r); + // C's flip, for the toolbar's Flip Arc button: false, with nothing changed beyond the + // edit finished first, while the last piece of the path being drawn is not a sharp-corner + // arc. Input held for an edit is finished first, so the key is made now rather than + // held; when that fails the error is returned and the flip is not made. + match app.session.finish_tool_edit() { + Ok(reqs) => crate::canvas::apply_requests(app, Ok(reqs)), + Err(e) => return Some(Err(e.to_string())), + } let view = app.view_info(); let key = vectorcraft_tools::ToolKey::Letter('c'); if app.session.tool_id() != vectorcraft_tools::quickpen::ID || !app.session.tool_claims_key(key, view) { diff --git a/crates/ui-egui/src/menus/ui_commands.rs b/crates/ui-egui/src/menus/ui_commands.rs index 313f4bb169..ad062b6cf9 100644 --- a/crates/ui-egui/src/menus/ui_commands.rs +++ b/crates/ui-egui/src/menus/ui_commands.rs @@ -717,7 +717,7 @@ pub const UI_COMMANDS: &[(&str, &str, &str, &str)] = &[ "tool.quickPen.flipArc", "Flip Arc", "", - "{} flip the last sharp-corner arc of the path the Quick Pen tool is drawing to the other side of its chord, as C and the toolbar's Flip Arc button do, as one undo step; returns false, changing nothing, when the Quick Pen tool is not active, no path is being drawn or the last piece is not a sharp-corner arc", + "{} flip the last sharp-corner arc of the path the Quick Pen tool is drawing to the other side of its chord, as C and the toolbar's Flip Arc button do, as one undo step; first finishes any pending tool edit (which can change the document), then returns false, changing nothing more, when the Quick Pen tool is not active, no path is being drawn or the last piece is not a sharp-corner arc", ), ( "window.dock", diff --git a/crates/ui-egui/src/tests/quickpenbar.rs b/crates/ui-egui/src/tests/quickpenbar.rs index f3accab96a..d78423eb6b 100644 --- a/crates/ui-egui/src/tests/quickpenbar.rs +++ b/crates/ui-egui/src/tests/quickpenbar.rs @@ -173,6 +173,26 @@ fn the_toolbar_hides_while_a_press_drags_on_the_canvas() { assert!(bar(&ctx).is_some(), "shown after the drag"); } +/// A middle-button drag on the canvas (a pan), which the tool takes no press from, hides the toolbar +/// too, and it shows again when the button is let go. +#[test] +fn the_toolbar_hides_while_the_canvas_pans() { + let mut app = app(); + let ctx = context(); + settle(&mut app, &ctx); + assert!(bar(&ctx).is_some()); + let (a, b) = (screen(&app, 100.0, 100.0), screen(&app, 200.0, 140.0)); + let press = |pos, pressed| Event::PointerButton { pos, button: PointerButton::Middle, pressed, modifiers: Default::default() }; + frame(&mut app, &ctx, vec![Event::PointerMoved(a), press(a, true)]); + frame(&mut app, &ctx, vec![Event::PointerMoved(a + (b - a) / 2.0)]); + frame(&mut app, &ctx, vec![Event::PointerMoved(b)]); + assert!(!app.session.tool_busy(), "the tool has no press under way"); + assert!(bar(&ctx).is_none(), "hidden mid-pan"); + frame(&mut app, &ctx, vec![press(b, false)]); + settle(&mut app, &ctx); + assert!(bar(&ctx).is_some(), "shown after the pan"); +} + /// On a canvas narrower than the toolbar, its controls wrap onto more rows, so every button lies /// on the canvas. #[test] @@ -250,6 +270,26 @@ fn pos2_of(x: f32, y: f32) -> Pos2 { Pos2::new(x, y) } +/// The toolbar stays shown while its handle is dragged farther than the toolbar's height, when the +/// press is no longer on it. +#[test] +fn the_toolbar_stays_while_its_handle_drags_far() { + let mut app = app(); + let ctx = context(); + settle(&mut app, &ctx); + let button = |pos, pressed| Event::PointerButton { pos, button: PointerButton::Primary, pressed, modifiers: Default::default() }; + let grip = pos2_of(bar(&ctx).unwrap().left() + 10.0, bar(&ctx).unwrap().center().y); + frame(&mut app, &ctx, vec![Event::PointerMoved(grip)]); + frame(&mut app, &ctx, vec![button(grip, true)]); + for k in 1..=6 { + frame(&mut app, &ctx, vec![Event::PointerMoved(grip + vec2(-20.0, -30.0) * k as f32)]); + assert!(bar(&ctx).is_some(), "shown at step {k}"); + } + frame(&mut app, &ctx, vec![button(grip + vec2(-120.0, -180.0), false)]); + settle(&mut app, &ctx); + assert!(bar(&ctx).is_some()); +} + /// The handle drags the toolbar, which keeps that offset as it follows the drawing, but not in its /// resting place with no path being drawn; pinned, it stays put; Reset Toolbar Position puts it /// back beside the drawing, unpinned. diff --git a/docs/mcp.md b/docs/mcp.md index 5507a86963..ed41128a6d 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -7125,8 +7125,9 @@ size of 180°, its 15° steps, the side an arc bulges to and Enter ending the pa tool keeps `action` and `turn` with the preferences (`tool.setOption {tool: "quickPen", key, value}`). Selecting the tool shows its toolbar on the canvas: Straight Line, Continuous Arc and Sharp Corner Arc set `action`, -Flip Arc flips the last sharp-corner arc as C does (`tool.quickPen.flipArc {}`, which returns false and changes nothing -when the Quick Pen tool is not active or the last piece is not a sharp-corner arc), and More Options (…) has Hide +Flip Arc flips the last sharp-corner arc as C does (`tool.quickPen.flipArc {}`, which first finishes any pending tool edit, +which can change the document, then returns false and changes nothing more when the Quick Pen tool is not active +or the last piece is not a sharp-corner arc), and More Options (…) has Hide Toolbar (`window.quickPenToolbar`, which toggles; the toolbar stays hidden until the tool is selected again) and Reset Toolbar Position (`window.quickPenToolbar.reset`), as the help page describes. The handle at its left end drags it. While a path is being drawn, the toolbar sits beside the path's last point, 24