Skip to content

Commit

Permalink
fix: always open links in a new tab
Browse files Browse the repository at this point in the history
This is a workaround for the app freezing because of
bfcache (see bevyengine/bevy#10328).
  • Loading branch information
carrascomj committed Oct 31, 2023
1 parent be4d3af commit 45c78f8
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
34 changes: 34 additions & 0 deletions src/extra_egui.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use bevy_egui::egui::{Link, Widget, WidgetText};

/// Clickable hyperlink, same as [`bevy_egui::egui::Hyperlink`] but it always
/// opens the url in a new tab.
#[must_use = "You should put this widget in an ui with `ui.add(widget);`"]
pub struct NewTabHyperlink {
url: &'static str,
text: WidgetText,
}

impl NewTabHyperlink {
pub fn from_label_and_url(text: impl Into<WidgetText>, url: &'static str) -> Self {
Self {
url,
text: text.into(),
}
}
}
impl Widget for NewTabHyperlink {
fn ui(self, ui: &mut bevy_egui::egui::Ui) -> bevy_egui::egui::Response {
let Self { url, text } = self;

let response = ui.add(Link::new(text));
if response.clicked() | response.middle_clicked() {
ui.ctx().output_mut(|o| {
o.open_url = Some(bevy_egui::egui::output::OpenUrl {
url: url.to_string(),
new_tab: true,
});
});
}
response.on_hover_text(url)
}
}
3 changes: 2 additions & 1 deletion src/gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use crate::data::{Data, ReactionState};
use crate::escher::{ArrowTag, EscherMap, Hover, MapState, NodeToText, ARROW_COLOR};
use crate::extra_egui::NewTabHyperlink;
use crate::geom::{AnyTag, Drag, HistTag, VisCondition, Xaxis};
use crate::info::Info;
use crate::screenshot::ScreenshotEvent;
Expand Down Expand Up @@ -317,7 +318,7 @@ pub fn ui_settings(
}
});

ui.add(egui::Hyperlink::from_label_and_url(
ui.add(NewTabHyperlink::from_label_and_url(
"How to use?",
"https://biosustain.github.io/shu/docs/plotting.html",
));
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use bevy_prototype_lyon::prelude::*;
mod aesthetics;
mod data;
mod escher;
mod extra_egui;
mod funcplot;
mod geom;
mod gui;
Expand Down

0 comments on commit 45c78f8

Please sign in to comment.