From fa1687eedfb3f82d8a05648599b3218b8d294537 Mon Sep 17 00:00:00 2001 From: batistalucasv Date: Sat, 5 Dec 2026 05:22:52 -0300 Subject: [PATCH] Keep text cursor on Ctrl/Cmd+Shift inside a linked text box (#9146) --- crates/ui-egui/src/canvas/input.rs | 16 +++++--- crates/ui-egui/src/canvas/tests.rs | 66 ++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 5 deletions(-) diff --git a/crates/ui-egui/src/canvas/input.rs b/crates/ui-egui/src/canvas/input.rs index d64838c0d7..77804fbc67 100644 --- a/crates/ui-egui/src/canvas/input.rs +++ b/crates/ui-egui/src/canvas/input.rs @@ -158,16 +158,21 @@ impl<'a> PageTextTool<'a> { } } - /// The tip for the link under `pos`: where it goes and how to follow it. - fn link_tip(&self, pos: Pos2) -> Option { + /// The hyperlink under `pos`: an object or text-box link when `allow_object` is true, or a text link. + fn link_at(&self, pos: Pos2, allow_object: bool) -> Option { let app = &*self.app; let (page, x, y) = page_of(self.body.pages, pos)?; let layout = &*self.layout; // A linked picture or shape under the pointer comes first: it lies over the text (#5267). // The text is the story under the pointer, as for the tracked change tip that follows. let story = pointer_story(app, layout, page, x, y, app.session.sel.focus.story); - let link = crate::objects::link_at(app, layout, self.body, pos) - .or_else(|| layout.hit(page, x, y, story).and_then(|at| link_under(app, layout, page, x, y, &at)))?; + let object = allow_object.then(|| crate::objects::link_at(app, layout, self.body, pos)).flatten(); + object.or_else(|| layout.hit(page, x, y, story).and_then(|at| link_under(app, layout, page, x, y, &at))) + } + + /// The tip for the link under `pos`: where it goes and how to follow it. + fn link_tip(&self, pos: Pos2) -> Option { + let link = self.link_at(pos, true)?; let target = if is_generated_bookmark_link(&link) { tl!("Current Document").to_string() } else { link }; Some(format!("{target}\n{}", crate::i18n::fmt(tl!("{key}+Click to follow link"), &[("key", self.link_key)]))) } @@ -301,7 +306,8 @@ impl CanvasTool for PageTextTool<'_> { return Cursor::System(CursorIcon::Copy); } // Word: Ctrl/⌘ held over a link shows the hand, as a click there follows it. - if pointer.mods.cmd && self.link_tip(pointer.pos).is_some() { + // A text box's own link only follows when Shift is not held; inner text links follow either way. + if pointer.mods.cmd && self.link_at(pointer.pos, !pointer.mods.shift).is_some() { return Cursor::System(CursorIcon::PointingHand); } text() diff --git a/crates/ui-egui/src/canvas/tests.rs b/crates/ui-egui/src/canvas/tests.rs index f4a3e9922e..a0242bbb55 100644 --- a/crates/ui-egui/src/canvas/tests.rs +++ b/crates/ui-egui/src/canvas/tests.rs @@ -2519,6 +2519,72 @@ fn a_linked_text_box_shows_and_follows_its_link_from_inside() { assert!(s.sel.is_collapsed() && s.sel.focus == start, "the caret went to the bookmark: {:?}", s.sel); } +/// #9146: Ctrl/⌘+Shift inside a linked text box does not follow the box's link on click, so it +/// shows the text cursor over plain text inside the box while still showing the hand over inner +/// text links (which Ctrl/⌘+Shift+click follows) and on plain Ctrl/⌘. +#[test] +fn ctrl_shift_inside_linked_text_box_shows_text_cursor() { + use craft::behavior::cursor::{Cursor, CursorIcon, PaintedCursor}; + + let mut a = WordApp::new(wordcraft_engine::Session::new(wordcraft_doc::Document::new()), Default::default()); + a.run("insert.textBox", json!({"text": "Plain inner"})).unwrap(); + let bx = a.session.layout().find_object(0, |o| o.text_box.is_some()).expect("the box"); + let box_story = StoryRef::Part(bx.text_box.expect("text box id")); + let end = Pos { off: bx.off + wordcraft_doc::para::OBJ.len_utf8(), ..bx.pos() }; + a.run("select.range", json!({"anchor": bx.pos(), "focus": end})).unwrap(); + a.run("insert.link", json!({"url": "https://example.com/box"})).unwrap(); + a.run("select.range", json!({"anchor": Pos::new(box_story, wordcraft_doc::Path::top(0), "Plain ".len()), "focus": Pos::new(box_story, wordcraft_doc::Path::top(0), "Plain inner".len())})).unwrap(); + a.run("insert.link", json!({"url": "https://example.com/inner"})).unwrap(); + a.session.sel = wordcraft_engine::Selection::caret(Pos::body(0, 0)); + + let mut h = crate::test_harness::builder().with_size(egui::vec2(1440.0, 900.0)).build_ui_state( + |ui, app: &mut WordApp| { + let ctx = ui.ctx().clone(); + app.logic(&ctx); + app.ui(ui); + }, + a, + ); + steps(&mut h, 6); + + let plain_caret = h.state_mut().session.layout().caret(&Pos::new(box_story, wordcraft_doc::Path::top(0), 2)).expect("plain text caret"); + let over_plain = + page_to_screen(h.state_mut(), plain_caret.page, plain_caret.x, plain_caret.top + plain_caret.height / 2.0).expect("plain on screen"); + let inner_caret = + h.state_mut().session.layout().caret(&Pos::new(box_story, wordcraft_doc::Path::top(0), "Plain in".len())).expect("inner link caret"); + let over_inner = + page_to_screen(h.state_mut(), inner_caret.page, inner_caret.x, inner_caret.top + inner_caret.height / 2.0).expect("inner on screen"); + + let painted = { + let env = craft::ui::runtime::env::Env::of_root(&h.ctx); + let shell = env.view().shell(); + shell.os == craft::host::target::Os::Windows && shell.runtime == craft::host::target::Runtime::Native + }; + let text = if painted { Cursor::Painted(PaintedCursor::IBeam) } else { Cursor::System(CursorIcon::Text) }; + let cursor_at = |h: &mut H, pos: Pos2, mods: egui::Modifiers| { + h.event(egui::Event::ModifiersChanged(mods)); + h.event(egui::Event::PointerMoved(pos)); + h.app_frame(); + craft::ui::runtime::cursor::current(&h.ctx) + }; + + assert_eq!( + cursor_at(&mut h, over_plain, egui::Modifiers::COMMAND), + Some(Cursor::System(CursorIcon::PointingHand)), + "Ctrl/Cmd over plain text inside linked text box shows hand" + ); + assert_eq!( + cursor_at(&mut h, over_plain, egui::Modifiers::COMMAND | egui::Modifiers::SHIFT), + Some(text), + "Ctrl/Cmd+Shift over plain text inside linked text box shows text cursor" + ); + assert_eq!( + cursor_at(&mut h, over_inner, egui::Modifiers::COMMAND | egui::Modifiers::SHIFT), + Some(Cursor::System(CursorIcon::PointingHand)), + "Ctrl/Cmd+Shift over inner hyperlink still shows hand" + ); +} + /// Unit-level checks for #7742 (`hostile_numbers_are_ignored` logic): invalid `ScrollSpeed` /// codes, non-finite framework wheel/ease inputs, out-of-range `view.zoom` commands, and /// `sync_view_state` recovery from non-finite `session.view.zoom`.