From 18c66c5e20ccae167ea2ba5c4210446778f417aa Mon Sep 17 00:00:00 2001 From: DWAA1660 Date: Sat, 5 Dec 2026 18:27:43 -0700 Subject: [PATCH 1/3] feat(ink): restyle picked-up ink and move lasso selections (#664) Choosing a pen while ink is picked up now gives that ink the pen's kind, colour and thickness, as OneNote does; the ink stays picked up and Undo takes it back. Ink held by Lasso Select can now be moved, stretched and deleted from its box like Select's, and a press elsewhere still draws a new loop. Co-Authored-By: Claude Opus 5.5 --- crates/ui-egui/src/app.rs | 26 +++++++- crates/ui-egui/src/inkpad.rs | 40 ++++++++++--- crates/ui-egui/tests/ui.rs | 113 +++++++++++++++++++++++++++++++++++ i18n/ar-XB.tsv | 1 + i18n/en-XA.tsv | 1 + i18n/en.tsv | 1 + i18n/nl.tsv | 1 + 7 files changed, 174 insertions(+), 9 deletions(-) diff --git a/crates/ui-egui/src/app.rs b/crates/ui-egui/src/app.rs index 330a57a5..4789063f 100644 --- a/crates/ui-egui/src/app.rs +++ b/crates/ui-egui/src/app.rs @@ -8848,8 +8848,14 @@ impl NoteCraftApp { ribbon::Act::InkPointer(p) => self.pointer = p, ribbon::Act::InkPen(i) => { self.nib = i.min(self.rack.len().saturating_sub(1)); - self.pointer = ribbon::Pointer::Draw; self.remember_rack(); + // With ink picked up, the pen chosen restyles it, as in OneNote (issue #664): the + // ink stays picked up and the pointer stays as it was, to go on editing it. + if self.pad.as_ref().is_some_and(|pad| pad.holding() > 0) { + self.restyle_held_ink(); + } else { + self.pointer = ribbon::Pointer::Draw; + } } ribbon::Act::InkRack(edit) => self.edit_rack(edit), ribbon::Act::Table(what) => self.edit_table(what), @@ -9737,6 +9743,22 @@ impl NoteCraftApp { }); } + /// Gives the ink picked up the chosen pen's kind, colour and thickness (issue #664), and writes + /// the drawing. + fn restyle_held_ink(&mut self) { + self.take_ink_save(true); + let Some(nib) = self.rack.get(self.nib).copied() else { return }; + let Some(pad) = self.pad.as_mut().filter(|pad| pad.holding() > 0) else { return }; + if !pad.restyle_held(nib.tool, nib.colour, nib.width()) { + return; + } + match pad.save() { + Ok(()) => self.say(crate::tl_id!("ui.ink_restyled")), + Err(e) => self.complain(&crate::i18n::format_message_id("ui.cannot_write_the_drawing_error", &[("error", &e.to_string())])), + } + self.tell_kept_ink(); + } + /// Draw > Rotate (issue #544): turns or mirrors the ink picked up, as one group about its /// middle, and writes the drawing. It stays picked up, to be turned again or moved. fn turn_ink(&mut self, how: notecraft_ink::pick::Turn) { @@ -18590,6 +18612,8 @@ impl NoteCraftApp { "bounds": self.pad.as_ref().and_then(crate::inkpad::Pad::bounds), "gain": self.gain(), "strokes": self.pad.as_ref().map(crate::inkpad::Pad::strokes), + // Each stroke's colour, in order, so a restyle can be seen (issue #664). + "strokeColours": self.pad.as_ref().map(|p| p.drawing().strokes.iter().map(|s| s.colour.hex()).collect::>()), "inked": self.pad.as_ref().is_some_and(|p| !p.is_empty()), // The readings of the newest stroke, in page points (issues #44, #293). "lastStroke": self.pad.as_ref().map(crate::inkpad::Pad::last_stroke), diff --git a/crates/ui-egui/src/inkpad.rs b/crates/ui-egui/src/inkpad.rs index 2854a9bc..cb2cda5c 100644 --- a/crates/ui-egui/src/inkpad.rs +++ b/crates/ui-egui/src/inkpad.rs @@ -608,6 +608,27 @@ impl Pad { self.delete_held() } + /// Gives the held ink a pen's kind, colour and thickness (issue #664) and says whether any + /// changed. It stays held, and the change is one step for undo. + pub fn restyle_held(&mut self, tool: Tool, colour: Rgb, width: f32) -> bool { + let mut changed = false; + let width = Stroke::new(tool, colour, width).width; + for &i in &self.held { + if let Some(stroke) = self.drawing.strokes.get_mut(i) { + if stroke.tool != tool || stroke.colour != colour || (stroke.width - width).abs() > f32::EPSILON { + (stroke.tool, stroke.colour, stroke.width) = (tool, colour, width); + changed = true; + } + } + } + self.dirty |= changed; + self.touched |= changed; + if changed { + self.settle(); + } + changed + } + /// Throws away the held ink and says whether any went. pub fn delete_held(&mut self) -> bool { let went = pick::remove(&mut self.drawing, &self.held) > 0; @@ -1046,11 +1067,21 @@ pub fn show( let colour = ui.visuals().text_color().gamma_multiply(0.6); painter.circle_stroke(p, r.max(1.5), egui::Stroke::new(1.0, colour)); } + // Ink the lasso holds is moved, stretched and deleted as in Select (issues #664, #715): its box + // and its Delete take the pointer over the ink layer, and a press anywhere else draws a new loop. + let mut held_changed = false; + if pen.pointer == Pointer::Lasso && pad.loop_now.is_empty() { + if pad.held.is_empty() { + ui.data_mut(|d| d.remove::(held_area_id())); + } else { + held_changed = held_in_write(ui, body, origin, pad, zoom, modal_owns_keys, t); + } + } // Not egui's drag, which only begins once the pointer has travelled far enough: a pen // put down and lifted in one place is a dot, and a dot is ink. let down = resp.is_pointer_button_down_on(); let at = ui.ctx().pointer_interact_pos().filter(|p| body.contains(*p)).map(|p| ((p.x - origin.x) / zoom, (p.y - origin.y) / zoom)); - let mut changed = false; + let mut changed = held_changed; match pen.pointer { Pointer::Erase => { if let Some((x, y)) = at.filter(|_| down) { @@ -1168,13 +1199,6 @@ pub fn show( // Write and Pick were answered above, with the page. Pointer::Write | Pointer::Pick | Pointer::Pan | Pointer::Laser | Pointer::Eyedropper(_) => {} } - // Ink the lasso holds offers Delete beside it too, for a finger or a mouse (issue #715). - if pad.loop_now.is_empty() - && let Some(held) = held_box(pad, origin, zoom) - && held_delete_button(ui, held, body, t).0 - { - changed |= pad.delete_held(); - } // Delete throws away whatever is held, unless the keyboard belongs to a dialog. if !modal_owns_keys && !pad.held.is_empty() && ui.input(|i| i.key_pressed(egui::Key::Delete) || i.key_pressed(egui::Key::Backspace)) { changed |= pad.delete_held(); diff --git a/crates/ui-egui/tests/ui.rs b/crates/ui-egui/tests/ui.rs index 17d4651a..e8f1bb86 100644 --- a/crates/ui-egui/tests/ui.rs +++ b/crates/ui-egui/tests/ui.rs @@ -23621,3 +23621,116 @@ fn held_ink_offers_a_visible_delete_that_undo_reverses() { settle(&mut h); assert_eq!(ink(&mut h)["strokes"].as_u64(), Some(0), "the last stroke went too"); } + +/// Draws one stroke with the black pen across the page at `y` below its top. Gives the page. +fn draw_one_black_stroke(h: &mut Harness<'_, NoteCraftApp>, y: f32) -> egui::Rect { + h.get_by_label("Draw").click(); + h.run(); + h.get_by_label("Pen black").click(); + h.run(); + let body = h.state().page_rect().expect("the page"); + let (x, y) = (body.min.x + 40.0, body.min.y + y); + ask(h, "ui.press", json!({"x": x, "y": y})).expect("down"); + pump(h); + for step in 1..=4 { + ask(h, "ui.move", json!({"x": x + step as f32 * 20.0, "y": y})).expect("move"); + pump(h); + } + ask(h, "ui.release", json!({"x": x + 80.0, "y": y})).expect("up"); + pump(h); + for _ in 0..20 { + settle(h); + if ask(h, "ui.inspect", json!({})).expect("inspect")["ink"]["strokes"].as_u64() == Some(1) { + break; + } + } + body +} + +/// Issue #664: with ink picked up, choosing a pen gives the picked-up ink that pen's colour and +/// thickness, as OneNote does, rather than leaving it as it was and starting to draw. The ink stays +/// picked up, and Undo takes the change back. +#[test] +fn choosing_a_pen_restyles_the_ink_that_is_picked_up() { + let (_t, mut app, s) = sample_app("ui-held-ink-restyle"); + app.open_path(&s.md_page.to_string_lossy()); + let mut h = harness(app, LIGHT); + settle(&mut h); + let body = draw_one_black_stroke(&mut h, 120.0); + let ink = |h: &mut Harness<'_, NoteCraftApp>| ask(h, "ui.inspect", json!({})).expect("inspect")["ink"].clone(); + let black = ink(&mut h)["strokeColours"].clone(); + assert_eq!(black.as_array().map(Vec::len), Some(1), "one stroke: {}", ink(&mut h)); + + h.get_by_label("Select").click(); + pump(&mut h); + ask(&mut h, "ui.click", json!({"x": body.min.x + 80.0, "y": body.min.y + 120.0})).expect("pick"); + settle(&mut h); + let picked = ink(&mut h); + assert_eq!(picked["holding"].as_u64(), Some(1), "the stroke is picked up: {picked}"); + + h.get_by_label("Pen red").click(); + settle(&mut h); + let after = ink(&mut h); + assert_eq!(after["strokeColours"], json!([after["colour"]]), "the stroke took the red pen's colour: {after}"); + assert_ne!(after["strokeColours"], black, "and is no longer black"); + assert_eq!(after["holding"].as_u64(), Some(1), "it stays picked up"); + assert_eq!(after["pointer"], picked["pointer"], "choosing the pen did not start drawing"); + + ask(&mut h, "ui.undo", json!({})).expect("undo"); + settle(&mut h); + assert_eq!(ink(&mut h)["strokeColours"], black, "undo puts the colour back"); +} + +/// Issue #664: ink picked up with Lasso Select can be moved by dragging inside its box, as with +/// Select, and a loop drawn elsewhere still picks up something else. +#[test] +fn ink_picked_up_with_the_lasso_can_be_moved() { + let (_t, mut app, s) = sample_app("ui-lasso-move"); + app.open_path(&s.md_page.to_string_lossy()); + let mut h = harness(app, LIGHT); + settle(&mut h); + let body = draw_one_black_stroke(&mut h, 260.0); + let ink = |h: &mut Harness<'_, NoteCraftApp>| ask(h, "ui.inspect", json!({})).expect("inspect")["ink"].clone(); + let left = |v: &Value| v["bounds"][0].as_f64().unwrap_or_default(); + let before = left(&ink(&mut h)); + + h.get_by_label("Lasso Select").click(); + pump(&mut h); + let (lx, ly) = (body.min.x + 20.0, body.min.y + 230.0); + ask(&mut h, "ui.press", json!({"x": lx, "y": ly})).expect("down"); + pump(&mut h); + for (dx, dy) in [(180.0, 0.0), (180.0, 70.0), (0.0, 70.0), (0.0, 2.0)] { + ask(&mut h, "ui.move", json!({"x": lx + dx, "y": ly + dy})).expect("move"); + pump(&mut h); + } + ask(&mut h, "ui.release", json!({"x": lx, "y": ly + 2.0})).expect("up"); + settle(&mut h); + assert_eq!(ink(&mut h)["holding"].as_u64(), Some(1), "the lasso took the stroke"); + + // A drag from the middle of the stroke moves it, and draws no new loop. + let (sx, sy) = (body.min.x + 80.0, body.min.y + 260.0); + ask(&mut h, "ui.press", json!({"x": sx, "y": sy})).expect("down"); + pump(&mut h); + for step in 1..=5 { + ask(&mut h, "ui.move", json!({"x": sx + step as f32 * 20.0, "y": sy + step as f32 * 10.0})).expect("move"); + pump(&mut h); + } + ask(&mut h, "ui.release", json!({"x": sx + 100.0, "y": sy + 50.0})).expect("up"); + settle(&mut h); + let moved = ink(&mut h); + assert!((left(&moved) - before - 100.0).abs() < 4.0, "the stroke moved 100 points right: {before} -> {}", left(&moved)); + assert_eq!(moved["holding"].as_u64(), Some(1), "and is still picked up"); + assert_eq!(moved["strokes"].as_u64(), Some(1), "nothing was drawn"); + + // A loop round bare page picks up nothing, and lets go of the stroke. + let (lx, ly) = (body.min.x + 300.0, body.min.y + 120.0); + ask(&mut h, "ui.press", json!({"x": lx, "y": ly})).expect("down"); + pump(&mut h); + for (dx, dy) in [(80.0, 0.0), (80.0, 60.0), (0.0, 60.0), (0.0, 2.0)] { + ask(&mut h, "ui.move", json!({"x": lx + dx, "y": ly + dy})).expect("move"); + pump(&mut h); + } + ask(&mut h, "ui.release", json!({"x": lx, "y": ly + 2.0})).expect("up"); + settle(&mut h); + assert_eq!(ink(&mut h)["holding"].as_u64(), Some(0), "a loop elsewhere picks up what is inside it: nothing"); +} diff --git a/i18n/ar-XB.tsv b/i18n/ar-XB.tsv index eeb6474c..0a5953f5 100644 --- a/i18n/ar-XB.tsv +++ b/i18n/ar-XB.tsv @@ -1770,6 +1770,7 @@ @id ui.eraser_chosen [[RLI]]$$${eraser} in hand.@@@[[PDI]] @id ui.rotate_ink [[RLI]]$$$Rotate@@@[[PDI]] @id ui.rotate_ink_tip [[RLI]]$$$Turn or mirror the ink picked up@@@[[PDI]] +@id ui.ink_restyled [[RLI]]$$$The ink picked up now uses the chosen pen.@@@[[PDI]] @id ui.ink_turned [[RLI]]$$$The ink picked up is turned.@@@[[PDI]] @id ui.pick_up_ink_first [[RLI]]$$$Pick up some ink with Select or Lasso Select first.@@@[[PDI]] @id ui.new_docked_window [[RLI]]$$$New docked window@@@[[PDI]] diff --git a/i18n/en-XA.tsv b/i18n/en-XA.tsv index 10d6ff4c..a14c0695 100644 --- a/i18n/en-XA.tsv +++ b/i18n/en-XA.tsv @@ -1770,6 +1770,7 @@ @id ui.eraser_chosen $$${eraser} in hand.@@@ @id ui.rotate_ink $$$Rotate@@@ @id ui.rotate_ink_tip $$$Turn or mirror the ink picked up@@@ +@id ui.ink_restyled $$$The ink picked up now uses the chosen pen.@@@ @id ui.ink_turned $$$The ink picked up is turned.@@@ @id ui.pick_up_ink_first $$$Pick up some ink with Select or Lasso Select first.@@@ @id ui.new_docked_window $$$New docked window@@@ diff --git a/i18n/en.tsv b/i18n/en.tsv index 7977a6ac..edf3a009 100644 --- a/i18n/en.tsv +++ b/i18n/en.tsv @@ -1770,6 +1770,7 @@ @id ui.eraser_chosen {eraser} in hand. @id ui.rotate_ink Rotate @id ui.rotate_ink_tip Turn or mirror the ink picked up +@id ui.ink_restyled The ink picked up now uses the chosen pen. @id ui.ink_turned The ink picked up is turned. @id ui.pick_up_ink_first Pick up some ink with Select or Lasso Select first. @id ui.new_docked_window New docked window diff --git a/i18n/nl.tsv b/i18n/nl.tsv index 53309a1d..ba8d7693 100644 --- a/i18n/nl.tsv +++ b/i18n/nl.tsv @@ -1770,6 +1770,7 @@ @id ui.eraser_chosen {eraser} geselecteerd. @id ui.rotate_ink Draaien @id ui.rotate_ink_tip Geselecteerde inkt draaien of spiegelen +@id ui.ink_restyled De geselecteerde inkt gebruikt nu de gekozen pen. @id ui.ink_turned De geselecteerde inkt is gedraaid. @id ui.pick_up_ink_first Selecteer eerst inkt met Selecteren of Lasso selecteren. @id ui.new_docked_window Nieuw vastgezet venster From 31ee9e5731660d0f825c48b6c934310b4a125dde Mon Sep 17 00:00:00 2001 From: DWAA1660 Date: Sat, 5 Dec 2026 18:38:56 -0700 Subject: [PATCH 2/3] feat(touch): open the page menu with a long press (#714) A finger held still past the long-press time and lifted in place now opens the page's menu there, as a right click does, in Type and in pen-only Draw; a long press that drags still selects. A stylus held still opens it too where a pen does not draw (Select, the panning hand, Type without automatic switching). A finger that draws opens none. Co-Authored-By: Claude Opus 5.5 --- crates/ui-egui/src/app.rs | 4 + crates/ui-egui/src/touch.rs | 170 +++++++++++++++++++++++++++++++++--- crates/ui-egui/tests/ui.rs | 41 +++++++++ 3 files changed, 205 insertions(+), 10 deletions(-) diff --git a/crates/ui-egui/src/app.rs b/crates/ui-egui/src/app.rs index 4789063f..e490fcdc 100644 --- a/crates/ui-egui/src/app.rs +++ b/crates/ui-egui/src/app.rs @@ -3499,6 +3499,10 @@ impl NoteCraftApp { let unobscured = |at| ctx.layer_id_at(at).is_none_or(|layer| layer.order < egui::Order::Foreground); let on_page = |at| page.is_some_and(|page| page.contains(at)) && unobscured(at); let on_text = |at| type_tap && self.ink_text_targets.iter().any(|rect| rect.contains(at)); + // A stylus held still opens the page's menu only where a pen on the page does not draw (#714). + self.touch.pen_menu = page.is_some() + && (matches!(self.pointer, ribbon::Pointer::Pick | ribbon::Pointer::Pan) + || (self.pointer == ribbon::Pointer::Write && !self.auto_switch)); let file = self.backstage.is_some() && self.dialog.is_none(); let tap = self.touch.route(&mut raw.events, time, |at| { if on_page(at) { diff --git a/crates/ui-egui/src/touch.rs b/crates/ui-egui/src/touch.rs index e0308461..07d07232 100644 --- a/crates/ui-egui/src/touch.rs +++ b/crates/ui-egui/src/touch.rs @@ -17,6 +17,9 @@ //! to the original tool with the press at the point of contact. A long press in Type still //! lets the finger act as a mouse to select. In pen-only Draw, holding on the page keeps //! the finger available to pan. +//! - A finger held still for a long press and lifted where it came down opens the page's menu +//! there, as a right click does (issue #714), unless it draws. So does a stylus held still where +//! a pen does not draw (`pen_menu`). //! - A second finger turns a held or panning finger into a two-finger gesture: egui's own pinch //! and two-finger scroll take it from there. //! - A pen (a contact that reports a force) always writes. Every finger on the glass when it @@ -63,7 +66,9 @@ enum Finger { /// A palm under a pen: nothing of it goes further until it lifts. Palm, /// As the window layer sends it: a mouse. - Mouse { last: Pos2, ink_start: Option }, + /// `still` is where a long press made it a mouse, while it has not travelled from there: lifted + /// so, it opens the page's menu (issue #714). + Mouse { last: Pos2, ink_start: Option, still: Option }, } /// The pen on the glass. @@ -73,6 +78,11 @@ struct Pen { /// Whether the window layer follows a finger, so the pen's pointer is made here. `None` /// until the events after the pen's first contact say. made: Option, + /// Where and when it came down, and whether it has stayed there where it does not draw: a + /// stylus held still and lifted opens the page's menu (issue #714). + start: Pos2, + since: f64, + still: bool, } /// What happens to the pointer events that follow a touch in the frame's events: the window @@ -100,6 +110,12 @@ pub struct Router { fingers: Vec<(TouchId, Finger)>, pen: Option, finger_has_inked: bool, + /// Where a long press asked for the page's menu this frame, sent as a right click at the end. + menu_at: Option, + /// Whether a stylus held still and lifted opens the page's menu: set by the window when a pen + /// on the page does not draw (Select, the panning hand, Type without automatic switching), so + /// a long press never leaves a stroke behind (issue #714). + pub(crate) pen_menu: bool, } impl Router { @@ -117,7 +133,7 @@ impl Router { let pen = force.is_some() || self.pen.is_some_and(|p| p.id == id); follow = if pen { tap_to_type = None; - self.pen_touch(event, id, phase, pos, &mut out) + self.pen_touch(event, id, phase, pos, time, &mut out) } else { if phase == TouchPhase::Start { tap_to_type = None; @@ -174,9 +190,18 @@ impl Router { out.push(Event::PointerMoved(start)); out.push(press_event(start, true, modifiers)); } - *finger = Finger::Mouse { last: start, ink_start: (drag == Drag::Ink && press.is_some()).then_some(start) }; + *finger = Finger::Mouse { + last: start, + ink_start: (drag == Drag::Ink && press.is_some()).then_some(start), + still: (drag != Drag::Ink).then_some(start), + }; } } + // A long press lifted in place opens the page's menu where it came down, as a right click + // does, after the contact's own events (issue #714). + if let Some(at) = self.menu_at.take() { + events_push_menu(&mut out, at); + } *events = out; tap_to_type } @@ -287,7 +312,7 @@ impl Router { self.set(id, Finger::Held { start: pos, since: time, press: None, drag, type_tap }); Follow::Hold(id) } else { - self.set(id, Finger::Mouse { last: pos, ink_start: (drag == Drag::Ink).then_some(pos) }); + self.set(id, Finger::Mouse { last: pos, ink_start: (drag == Drag::Ink).then_some(pos), still: None }); Follow::Pass } } @@ -305,7 +330,7 @@ impl Router { out.push(press_event(start, true, modifiers)); self.finger_has_inked |= drag == Drag::Ink; } - self.set(id, Finger::Mouse { last: pos, ink_start: None }); + self.set(id, Finger::Mouse { last: pos, ink_start: None, still: None }); Follow::Pass } Some(Finger::Held { .. }) => { @@ -323,10 +348,11 @@ impl Router { Follow::MovesOnly } Some(Finger::Palm) => Follow::Drop, - Some(Finger::Mouse { ink_start, .. }) => { + Some(Finger::Mouse { ink_start, still, .. }) => { out.push(event); self.finger_has_inked |= ink_start.is_some_and(|start| start.distance(pos) > SLOP); - self.set(id, Finger::Mouse { last: pos, ink_start }); + let still = still.filter(|at| at.distance(pos) <= SLOP); + self.set(id, Finger::Mouse { last: pos, ink_start, still }); Follow::Pass } None => { @@ -340,6 +366,11 @@ impl Router { match state { Some(Finger::Held { start, since, press, drag, type_tap }) => { out.push(event); + // Where a finger only pans it never became a mouse: held still long enough + // and lifted in place, it opens the menu, and nothing is clicked (issue #714). + if lifted && drag == Drag::Pan && time - since >= LONG_PRESS && start.distance(pos) <= SLOP { + self.menu_at = Some(start); + } let Some(modifiers) = press.filter(|_| lifted) else { return Follow::MovesOnly }; let short = time - since < LONG_PRESS && start.distance(pos) <= SLOP; if type_tap && short { @@ -363,7 +394,14 @@ impl Router { Follow::MovesOnly } Some(Finger::Palm) => Follow::Drop, - Some(Finger::Mouse { .. }) | None => { + Some(Finger::Mouse { still, .. }) => { + if lifted && let Some(at) = still.filter(|at| at.distance(pos) <= SLOP) { + self.menu_at = Some(at); + } + out.push(event); + Follow::Pass + } + None => { out.push(event); Follow::Pass } @@ -372,7 +410,8 @@ impl Router { } } - fn pen_touch(&mut self, event: Event, id: TouchId, phase: TouchPhase, pos: Pos2, out: &mut Vec) -> Follow { + #[allow(clippy::too_many_arguments)] + fn pen_touch(&mut self, event: Event, id: TouchId, phase: TouchPhase, pos: Pos2, time: f64, out: &mut Vec) -> Follow { match phase { TouchPhase::Start => { // Every finger on the glass is a palm now. egui is told each has gone, so its @@ -397,12 +436,18 @@ impl Router { *finger = Finger::Palm; } self.fingers = fingers; - self.pen = Some(Pen { id, made: None }); + self.pen = Some(Pen { id, made: None, start: pos, since: time, still: self.pen_menu }); out.push(event); Follow::PenStart(pos) } TouchPhase::Move | TouchPhase::End | TouchPhase::Cancel => { let made = self.pen.is_some_and(|p| p.id == id && p.made == Some(true)); + if let Some(pen) = self.pen.as_mut().filter(|p| p.id == id) { + pen.still &= pen.start.distance(pos) <= SLOP; + if phase == TouchPhase::End && pen.still && time - pen.since >= LONG_PRESS { + self.menu_at = Some(pen.start); + } + } if matches!(phase, TouchPhase::End | TouchPhase::Cancel) && self.pen.is_some_and(|p| p.id == id) { self.pen = None; } @@ -423,6 +468,14 @@ impl Router { } } +/// A right click at `at`, which opens the page's menu there. +fn events_push_menu(out: &mut Vec, at: Pos2) { + out.push(Event::PointerMoved(at)); + for pressed in [true, false] { + out.push(Event::PointerButton { pos: at, button: PointerButton::Secondary, pressed, modifiers: Modifiers::NONE }); + } +} + fn press_event(pos: Pos2, pressed: bool, modifiers: Modifiers) -> Event { Event::PointerButton { pos, button: PointerButton::Primary, pressed, modifiers } } @@ -669,4 +722,101 @@ mod tests { let landed = routed(&mut r, contact(9, TouchPhase::Start, AT, Some(0.4), false), 0.04, false); assert_eq!(presses(&landed), vec![(along, false), (AT, true)], "the finger's drag ends where it was, then the pen presses"); } + + fn secondary_clicks(events: &[Event]) -> Vec<(Pos2, bool)> { + events + .iter() + .filter_map(|e| match e { + Event::PointerButton { pos, pressed, button: PointerButton::Secondary, .. } => Some((*pos, *pressed)), + _ => None, + }) + .collect() + } + + /// Issue #714: a finger held still on the page and lifted where it came down opens the page's + /// menu there, as a right click does: after its own click, a secondary press and release. + #[test] + fn a_finger_long_press_lifted_in_place_opens_the_menu() { + let mut r = Router::default(); + routed(&mut r, finger(TouchPhase::Start, AT), 0.0, true); + routed(&mut r, Vec::new(), LONG_PRESS + 0.01, true); + let wobble = AT + Vec2::new(2.0, 1.0); + routed(&mut r, finger(TouchPhase::Move, wobble), LONG_PRESS + 0.05, true); + let up = routed(&mut r, finger(TouchPhase::End, wobble), LONG_PRESS + 0.1, true); + assert_eq!(secondary_clicks(&up), vec![(AT, true), (AT, false)], "a right click where the finger came down: {up:?}"); + let last_primary = up.iter().rposition(|e| matches!(e, Event::PointerButton { button: PointerButton::Primary, .. })); + let first_secondary = up.iter().position(|e| matches!(e, Event::PointerButton { button: PointerButton::Secondary, .. })); + assert!(last_primary < first_secondary, "the finger's own release comes first: {up:?}"); + } + + /// A long press that goes on to drag selects, as before, and opens no menu; a short tap and a + /// pan open none either. + #[test] + fn a_long_press_drag_a_tap_or_a_pan_opens_no_menu() { + let mut r = Router::default(); + let mut all = routed(&mut r, finger(TouchPhase::Start, AT), 0.0, true); + all.extend(routed(&mut r, Vec::new(), LONG_PRESS + 0.01, true)); + let to = AT + Vec2::new(60.0, 0.0); + all.extend(routed(&mut r, finger(TouchPhase::Move, to), LONG_PRESS + 0.1, true)); + all.extend(routed(&mut r, finger(TouchPhase::End, to), LONG_PRESS + 0.2, true)); + all.extend(routed(&mut r, finger(TouchPhase::Start, AT), 2.0, true)); + all.extend(routed(&mut r, finger(TouchPhase::End, AT), 2.1, true)); + all.extend(routed(&mut r, finger(TouchPhase::Start, AT), 3.0, true)); + all.extend(routed(&mut r, finger(TouchPhase::Move, AT + Vec2::new(0.0, 40.0)), 3.1, true)); + all.extend(routed(&mut r, finger(TouchPhase::End, AT + Vec2::new(0.0, 40.0)), 3.8, true)); + assert!(secondary_clicks(&all).is_empty(), "{all:?}"); + } + + /// Where a finger only pans (pen-only Draw), a long press still opens the menu, and nothing + /// is clicked or drawn: the hold never became a press. + #[test] + fn a_long_press_where_fingers_only_pan_opens_the_menu_and_draws_nothing() { + let mut r = Router::default(); + let target = |_| (Drag::Pan, false); + let mut all = finger(TouchPhase::Start, AT); + r.route(&mut all, 0.0, target); + let mut later = Vec::new(); + r.route(&mut later, LONG_PRESS + 0.01, target); + let mut up = finger(TouchPhase::End, AT); + r.route(&mut up, LONG_PRESS + 0.2, target); + all.extend(later); + let primaries = all.iter().chain(up.iter()).filter(|e| matches!(e, Event::PointerButton { button: PointerButton::Primary, .. })).count(); + assert_eq!(primaries, 0, "no click reaches the page: {all:?} {up:?}"); + assert_eq!(secondary_clicks(&up), vec![(AT, true), (AT, false)]); + } + + /// A finger that draws (an ink tool for fingers) opens no menu: its press is ink. + #[test] + fn a_finger_long_press_with_ink_in_hand_opens_no_menu() { + let mut r = Router::default(); + let target = |_| (Drag::Ink, false); + let mut events = finger(TouchPhase::Start, AT); + r.route(&mut events, 0.0, target); + let mut up = finger(TouchPhase::End, AT); + r.route(&mut up, LONG_PRESS + 0.2, target); + assert!(secondary_clicks(&up).is_empty(), "{up:?}"); + } + + /// Issue #714 for a stylus: held still while a pen does not draw on the page (Select, Type without + /// automatic switching), it opens the menu when lifted; while it draws, it writes, and opens none. + #[test] + fn a_stylus_long_press_opens_the_menu_only_where_it_does_not_draw() { + let pen = |phase, pos| contact(9, phase, pos, Some(0.5), true); + for (pen_menu, menu) in [(true, true), (false, false)] { + let mut r = Router { pen_menu, ..Router::default() }; + let target = |_| (Drag::Pan, false); + let mut down = pen(TouchPhase::Start, AT); + r.route(&mut down, 0.0, target); + let mut up = pen(TouchPhase::End, AT + Vec2::new(1.0, 1.0)); + r.route(&mut up, LONG_PRESS + 0.1, target); + assert_eq!(!secondary_clicks(&up).is_empty(), menu, "pen menu {pen_menu}: {up:?}"); + } + // A quick stylus tap opens none. + let mut r = Router { pen_menu: true, ..Router::default() }; + let mut down = pen(TouchPhase::Start, AT); + r.route(&mut down, 0.0, |_| (Drag::Pointer, false)); + let mut up = pen(TouchPhase::End, AT); + r.route(&mut up, 0.1, |_| (Drag::Pointer, false)); + assert!(secondary_clicks(&up).is_empty(), "{up:?}"); + } } diff --git a/crates/ui-egui/tests/ui.rs b/crates/ui-egui/tests/ui.rs index e8f1bb86..cc8b1da5 100644 --- a/crates/ui-egui/tests/ui.rs +++ b/crates/ui-egui/tests/ui.rs @@ -23734,3 +23734,44 @@ fn ink_picked_up_with_the_lasso_can_be_moved() { settle(&mut h); assert_eq!(ink(&mut h)["holding"].as_u64(), Some(0), "a loop elsewhere picks up what is inside it: nothing"); } + +/// Issue #714: a finger held still on the page and lifted where it came down opens the page's own +/// menu there, as a right click does, in Type and in pen-only Draw (Draw with Touch off) alike, +/// and draws nothing. Where a finger draws, its press is ink and opens no menu. +#[test] +fn a_finger_long_press_on_the_page_opens_its_menu() { + for draw in [false, true] { + let (_t, mut app, s) = sample_app(if draw { "ui-long-press-draw" } else { "ui-long-press-type" }); + app.open_path(&s.md_page.to_string_lossy()); + let mut h = sized_harness(app, LIGHT, egui::vec2(1600.0, 900.0)); + settle(&mut h); + if draw { + h.get_by_label("Draw").click(); + h.run(); + h.get_by_label("Pen black").click(); + settle(&mut h); + // Pen-only Draw: a finger moves the page and does not draw. The switch shows once a + // touch has been seen, so a finger draws once first. + let words = h.state().page_rect().expect("the page"); + finger_drag(&mut h, words.min + egui::vec2(200.0, 120.0), egui::vec2(80.0, 10.0)); + settle(&mut h); + h.get_by_label("Draw with Touch").click(); + settle(&mut h); + } + let strokes_before = ask(&mut h, "ui.inspect", json!({})).expect("inspect")["ink"]["strokes"].as_u64().unwrap_or_default(); + assert!(h.query_by_label("This page").is_none(), "no menu yet"); + let body = h.state().page_rect().expect("the page"); + let at = egui::pos2(body.min.x + 300.0, body.min.y + 320.0); + routed_frame(&mut h, contact_events(1, egui::TouchPhase::Start, at, None, true)); + // Held still for longer than a long press, with the finger trembling a little. + let started = h.ctx.input(|i| i.time); + while h.ctx.input(|i| i.time) - started < 0.7 { + routed_frame(&mut h, contact_events(1, egui::TouchPhase::Move, at + egui::vec2(1.0, 1.0), None, true)); + } + routed_frame(&mut h, contact_events(1, egui::TouchPhase::End, at + egui::vec2(1.0, 1.0), None, true)); + settle(&mut h); + assert!(h.query_by_label("This page").is_some(), "the page's menu opened (draw: {draw})"); + let strokes = ask(&mut h, "ui.inspect", json!({})).expect("inspect")["ink"]["strokes"].as_u64().unwrap_or_default(); + assert_eq!(strokes, strokes_before, "the long press drew nothing (draw: {draw})"); + } +} From 01c0cb7d9850c7cd9b37c51fdc0832b0a2c5172a Mon Sep 17 00:00:00 2001 From: DWAA1660 Date: Sat, 5 Dec 2026 18:40:46 -0700 Subject: [PATCH 3/3] style(ink): collapse the restyle condition for clippy (#664) Co-Authored-By: Claude Opus 5.5 --- crates/ui-egui/src/inkpad.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/crates/ui-egui/src/inkpad.rs b/crates/ui-egui/src/inkpad.rs index cb2cda5c..3f4b943a 100644 --- a/crates/ui-egui/src/inkpad.rs +++ b/crates/ui-egui/src/inkpad.rs @@ -614,11 +614,11 @@ impl Pad { let mut changed = false; let width = Stroke::new(tool, colour, width).width; for &i in &self.held { - if let Some(stroke) = self.drawing.strokes.get_mut(i) { - if stroke.tool != tool || stroke.colour != colour || (stroke.width - width).abs() > f32::EPSILON { - (stroke.tool, stroke.colour, stroke.width) = (tool, colour, width); - changed = true; - } + if let Some(stroke) = self.drawing.strokes.get_mut(i) + && (stroke.tool != tool || stroke.colour != colour || (stroke.width - width).abs() > f32::EPSILON) + { + (stroke.tool, stroke.colour, stroke.width) = (tool, colour, width); + changed = true; } } self.dirty |= changed;