Add `Response::clicked_with_open_in_background` (#7093)

Useful for buttons that should act as Hyperlinks
This commit is contained in:
Emil Ernerfeldt 2025-05-26 19:44:55 +02:00 committed by GitHub
parent a085d0b3a5
commit da67465a6c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 14 additions and 8 deletions

View File

@ -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,

View File

@ -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 {