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
This commit is contained in:
Frederic L 2023-08-12 14:05:49 +02:00 committed by GitHub
parent 1036cb1f7d
commit 1023f937a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 11 additions and 2 deletions

View File

@ -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(),
});
});
}