Skip to content

Commit cc46410

Browse files
committed
egui-web: Update web_location_hash when hash in URL changes
Fixes #1139
1 parent e4aa1e6 commit cc46410

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

egui_web/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ All notable changes to the `egui_web` integration will be noted in this file.
77
* The default painter is now glow instead of WebGL ([#1020](https://github.com/emilk/egui/pull/1020)).
88
* Made the WebGL painter opt-in ([#1020](https://github.com/emilk/egui/pull/1020)).
99
* Fix glow failure Chrome ((#1092)[https://github.com/emilk/egui/pull/1092]).
10+
* Update `epi::IntegrationInfo::web_location_hash` on `hashchange` event ([#1140](https://github.com/emilk/egui/pull/1140)).
1011

1112

1213
## 0.16.0 - 2021-12-29

egui_web/src/backend.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ impl epi::backend::RepaintSignal for NeedRepaint {
8282
// ----------------------------------------------------------------------------
8383

8484
pub struct AppRunner {
85-
frame: epi::Frame,
85+
pub(crate) frame: epi::Frame,
8686
egui_ctx: egui::Context,
8787
painter: Box<dyn Painter>,
8888
pub(crate) input: WebInput,

egui_web/src/lib.rs

+16
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,22 @@ fn install_document_events(runner_ref: &AppRunnerRef) -> Result<(), JsValue> {
668668
closure.forget();
669669
}
670670

671+
{
672+
// hashchange
673+
let runner_ref = runner_ref.clone();
674+
let closure = Closure::wrap(Box::new(move || {
675+
let runner_lock = runner_ref.0.lock();
676+
let mut frame_lock = runner_lock.frame.lock();
677+
678+
// `epi::Frame::info(&self)` clones `epi::IntegrationInfo`, but we need to modify the original here
679+
if let Some(web_info) = &mut frame_lock.info.web_info {
680+
web_info.web_location_hash = location_hash().unwrap_or_default();
681+
}
682+
}) as Box<dyn FnMut()>);
683+
window.add_event_listener_with_callback("hashchange", closure.as_ref().unchecked_ref())?;
684+
closure.forget();
685+
}
686+
671687
Ok(())
672688
}
673689

0 commit comments

Comments
 (0)