From 1023f937a67f3771e25c45cc49067acb71740e97 Mon Sep 17 00:00:00 2001 From: Frederic L <27208977+FreddyFunk@users.noreply.github.com> Date: Sat, 12 Aug 2023 14:05:49 +0200 Subject: [PATCH] Add option to always open hyperlink in a new browser tab (#3242) * add option to always open hyperlink in a new browser tab * Fix logic error --- crates/egui/src/widgets/hyperlink.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/crates/egui/src/widgets/hyperlink.rs b/crates/egui/src/widgets/hyperlink.rs index 361b21bb..0ef0ee48 100644 --- a/crates/egui/src/widgets/hyperlink.rs +++ b/crates/egui/src/widgets/hyperlink.rs @@ -83,6 +83,7 @@ impl Widget for Link { pub struct Hyperlink { url: String, text: WidgetText, + new_tab: bool, } impl Hyperlink { @@ -92,6 +93,7 @@ impl Hyperlink { Self { url: url.clone(), text: url.into(), + new_tab: false, } } @@ -100,13 +102,20 @@ impl Hyperlink { Self { url: url.to_string(), text: text.into(), + new_tab: false, } } + + /// Always open this hyperlink in a new browser tab. + pub fn open_in_new_tab(mut self, new_tab: bool) -> Self { + self.new_tab = new_tab; + self + } } impl Widget for Hyperlink { fn ui(self, ui: &mut Ui) -> Response { - let Self { url, text } = self; + let Self { url, text, new_tab } = self; let response = ui.add(Link::new(text)); if response.clicked() { @@ -114,7 +123,7 @@ impl Widget for Hyperlink { ui.ctx().output_mut(|o| { o.open_url = Some(crate::output::OpenUrl { url: url.clone(), - new_tab: modifiers.any(), + new_tab: new_tab || modifiers.any(), }); }); }