Skip to content

Commit

Permalink
Add option to always open hyperlink in a new browser tab (#3242)
Browse files Browse the repository at this point in the history
* add option to always open hyperlink in a new browser tab

* Fix logic error
  • Loading branch information
FreddyFunk authored Aug 12, 2023
1 parent 1036cb1 commit 1023f93
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions crates/egui/src/widgets/hyperlink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ impl Widget for Link {
pub struct Hyperlink {
url: String,
text: WidgetText,
new_tab: bool,
}

impl Hyperlink {
Expand All @@ -92,6 +93,7 @@ impl Hyperlink {
Self {
url: url.clone(),
text: url.into(),
new_tab: false,
}
}

Expand All @@ -100,21 +102,28 @@ 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() {
let modifiers = ui.ctx().input(|i| i.modifiers);
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(),
});
});
}
Expand Down

0 comments on commit 1023f93

Please sign in to comment.