From 45c78f8573ba75f275f489e711149ff3d18f8110 Mon Sep 17 00:00:00 2001 From: carrascomj Date: Tue, 31 Oct 2023 15:05:58 +0100 Subject: [PATCH] fix: always open links in a new tab This is a workaround for the app freezing because of bfcache (see https://github.com/bevyengine/bevy/issues/10328). --- src/extra_egui.rs | 34 ++++++++++++++++++++++++++++++++++ src/gui.rs | 3 ++- src/main.rs | 1 + 3 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 src/extra_egui.rs diff --git a/src/extra_egui.rs b/src/extra_egui.rs new file mode 100644 index 0000000..9f34f65 --- /dev/null +++ b/src/extra_egui.rs @@ -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, 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) + } +} diff --git a/src/gui.rs b/src/gui.rs index bb6bc5e..12ee045 100644 --- a/src/gui.rs +++ b/src/gui.rs @@ -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; @@ -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", )); diff --git a/src/main.rs b/src/main.rs index 14fcfb9..d3b0d97 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,6 +9,7 @@ use bevy_prototype_lyon::prelude::*; mod aesthetics; mod data; mod escher; +mod extra_egui; mod funcplot; mod geom; mod gui;