diff --git a/crates/egui/src/response.rs b/crates/egui/src/response.rs index 28f4bde7..0fdadfcc 100644 --- a/crates/egui/src/response.rs +++ b/crates/egui/src/response.rs @@ -218,6 +218,14 @@ impl Response { && self.ctx.input(|i| i.pointer.button_triple_clicked(button)) } + /// Was this widget middle-clicked or clicked while holding down a modifier key? + /// + /// This is used by [`crate::Hyperlink`] to check if a URL should be opened + /// in a new tab, using [`crate::OpenUrl::new_tab`]. + pub fn clicked_with_open_in_background(&self) -> bool { + self.middle_clicked() || self.clicked() && self.ctx.input(|i| i.modifiers.any()) + } + /// `true` if there was a click *outside* the rect of this widget. /// /// Clicks on widgets contained in this one counts as clicks inside this widget, diff --git a/crates/egui/src/widgets/hyperlink.rs b/crates/egui/src/widgets/hyperlink.rs index 4896be41..3e5ff88d 100644 --- a/crates/egui/src/widgets/hyperlink.rs +++ b/crates/egui/src/widgets/hyperlink.rs @@ -129,18 +129,16 @@ impl Widget for Hyperlink { let response = ui.add(Link::new(text)); - if response.clicked() { - let modifiers = ui.ctx().input(|i| i.modifiers); - ui.ctx().open_url(crate::OpenUrl { - url: url.clone(), - new_tab: new_tab || modifiers.any(), - }); - } - if response.middle_clicked() { + if response.clicked_with_open_in_background() { ui.ctx().open_url(crate::OpenUrl { url: url.clone(), new_tab: true, }); + } else if response.clicked() { + ui.ctx().open_url(crate::OpenUrl { + url: url.clone(), + new_tab, + }); } if ui.style().url_in_tooltip {