From 73d1e35c3d512da043e088e8d18fc0d74e60dbf2 Mon Sep 17 00:00:00 2001 From: aiongg Date: Thu, 12 Nov 2026 01:03:15 +0000 Subject: [PATCH] Link: a click with a modifier held opens the URL in a new tab, as a middle click does (ctl:32) Fixtures record the URLs the frames ask to open, for the trial. --- .../ui-contract/src/runtime/egui/fixture.rs | 9 +++++++ crates/ui-contract/src/testing/fixture.rs | 5 ++++ crates/ui-contract/src/testing/frames.rs | 8 ++++++ crates/ui-widgets/src/buttons/link.rs | 8 +++--- crates/ui-widgets/tests/it/link.rs | 26 +++++++++++++++++++ crates/ui-widgets/tests/it/main.rs | 1 + 6 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 crates/ui-widgets/tests/it/link.rs diff --git a/crates/ui-contract/src/runtime/egui/fixture.rs b/crates/ui-contract/src/runtime/egui/fixture.rs index 65b49cc29..6b540802e 100644 --- a/crates/ui-contract/src/runtime/egui/fixture.rs +++ b/crates/ui-contract/src/runtime/egui/fixture.rs @@ -46,6 +46,8 @@ pub(crate) struct FrameResult { pub ime: Option, /// The texts the frame put on the clipboard. pub copied: Vec, + /// The URLs the frame asked the platform to open, each with whether it asked for a new tab. + pub opened: Vec<(String, bool)>, /// The text runs the frame painted. pub texts: Vec, /// The visual bounds of every shape the frame painted, within its clip rect. A shape with no extent (egui reports an empty galley's @@ -182,9 +184,16 @@ pub(crate) fn run_frame( .iter() .filter_map(|command| if let egui::OutputCommand::CopyText(text) = command { Some(text.clone()) } else { None }) .collect(); + let opened = output + .platform_output + .commands + .iter() + .filter_map(|command| if let egui::OutputCommand::OpenUrl(open) = command { Some((open.url.clone(), open.new_tab)) } else { None }) + .collect(); FrameResult { closed, copied, + opened, texts, bounds, overshoot, diff --git a/crates/ui-contract/src/testing/fixture.rs b/crates/ui-contract/src/testing/fixture.rs index 197e2521e..cadbb79c7 100644 --- a/crates/ui-contract/src/testing/fixture.rs +++ b/crates/ui-contract/src/testing/fixture.rs @@ -350,6 +350,11 @@ impl Fixture { self.frames.copied() } + /// Every URL the fixture's frames asked to open, in order, each with whether it asked for a new tab. + pub fn opened(&self) -> &[(String, bool)] { + self.frames.opened() + } + /// The visual bounds of the shapes the latest frame painted. pub fn shape_bounds(&self) -> &[Rect] { self.frames.shape_bounds() diff --git a/crates/ui-contract/src/testing/frames.rs b/crates/ui-contract/src/testing/frames.rs index d88e2e33a..d9e00e6a4 100644 --- a/crates/ui-contract/src/testing/frames.rs +++ b/crates/ui-contract/src/testing/frames.rs @@ -26,6 +26,7 @@ pub struct Frames { tree: Option, ime: Option, copied: Vec, + opened: Vec<(String, bool)>, texts: Vec, bounds: Vec, overshoot: Vec, @@ -94,6 +95,7 @@ impl Frames { tree: None, ime: None, copied: Vec::new(), + opened: Vec::new(), texts: Vec::new(), bounds: Vec::new(), overshoot: Vec::new(), @@ -231,6 +233,7 @@ impl Frames { self.tree = result.tree; self.ime = result.ime; self.copied = result.copied; + self.opened.extend(result.opened); self.texts = result.texts; self.bounds = result.bounds; self.overshoot = result.overshoot; @@ -247,6 +250,11 @@ impl Frames { &self.copied } + /// Every URL the frames so far asked to open, in order, each with whether it asked for a new tab. + pub fn opened(&self) -> &[(String, bool)] { + &self.opened + } + /// The text runs the last frame painted. pub fn texts(&self) -> &[fixture::PaintedText] { &self.texts diff --git a/crates/ui-widgets/src/buttons/link.rs b/crates/ui-widgets/src/buttons/link.rs index 24229d0d5..79c307392 100644 --- a/crates/ui-widgets/src/buttons/link.rs +++ b/crates/ui-widgets/src/buttons/link.rs @@ -22,7 +22,7 @@ pub struct LinkStyle { } /// A link: drawn in the accent colour in a type role (body by default) and underlined, so colour is not its only cue. -/// A click, Enter or Space opens the URL in the same tab and a middle click opens it in a new tab; the caller can also act on `clicked()`. AccessKit reports the role `Link`, with the URL when +/// A click, Enter or Space opens the URL in the same tab; a middle click, or a click with a modifier held, opens it in a new tab. The caller can also act on `clicked()`. AccessKit reports the role `Link`, with the URL when /// it has one. #[derive(crate::contract::Component, crate::contract::Props)] pub struct Link { @@ -89,10 +89,12 @@ impl Component for Link { PressPhase::Release { start, click: true, .. } => start.button == PointerButton::Middle, _ => false, }; + let activation = cx.activation(); if let Some(url) = &self.url - && (cx.activation().is_some() || middle_click) + && (activation.is_some() || middle_click) { - cx.open_url(url, middle_click); + let modified = activation.is_some_and(|activation| !activation.mods.is_none()); + cx.open_url(url, middle_click || modified); } } diff --git a/crates/ui-widgets/tests/it/link.rs b/crates/ui-widgets/tests/it/link.rs new file mode 100644 index 000000000..c7ad34545 --- /dev/null +++ b/crates/ui-widgets/tests/it/link.rs @@ -0,0 +1,26 @@ +//! A link opens its URL in the same tab on a plain click and in a new tab on a click with a modifier held, as a +//! middle click does. + +use craft_ui::buttons::Link; +use craft_ui::contract::Show as _; +use craft_ui::testing::layout::in_region; +use craft_ui::testing::{Frames, pointer, trial}; +use egui::{Modifiers, Rect, pos2, vec2}; +use std::cell::Cell; + +#[trial] +fn a_modified_click_opens_the_link_in_a_new_tab() { + let mut frames = Frames::new(); + let shown = Cell::new(Rect::NOTHING); + let mut show = |ui: &mut egui::Ui| { + let region = Rect::from_min_size(pos2(0.0, 0.0), vec2(200.0, 40.0)); + shown.set(in_region(ui, region, |ui| Link::new("Shortcuts").url("https://example.com").show(ui).rect)); + }; + frames.frame(Vec::new(), &mut show); + let at = shown.get().center(); + pointer::click(&mut frames, at, &mut show); + frames.hold(Modifiers::COMMAND); + pointer::click(&mut frames, at, &mut show); + let tabs: Vec = frames.opened().iter().map(|(_, new_tab)| *new_tab).collect(); + assert_eq!(tabs, [false, true], "a plain click opens in the same tab, a Cmd/Ctrl click in a new one"); +} diff --git a/crates/ui-widgets/tests/it/main.rs b/crates/ui-widgets/tests/it/main.rs index 24ba60bcb..88c02840c 100644 --- a/crates/ui-widgets/tests/it/main.rs +++ b/crates/ui-widgets/tests/it/main.rs @@ -13,6 +13,7 @@ mod keys; mod label; mod layer; mod layout; +mod link; mod list; mod menu; mod overlay;