Skip to content

Commit

Permalink
On Web, implement navigating back/forward with mouse buttons (#5792)
Browse files Browse the repository at this point in the history
### What
By popular request

### Checklist
* [x] I have read and agree to [Contributor
Guide](https://github.com/rerun-io/rerun/blob/main/CONTRIBUTING.md) and
the [Code of
Conduct](https://github.com/rerun-io/rerun/blob/main/CODE_OF_CONDUCT.md)
* [x] I've included a screenshot or gif (if applicable)
* [x] I have tested the web demo (if applicable):
* Using newly built examples:
[rerun.io/viewer](https://rerun.io/viewer/pr/5792)
* Using examples from latest `main` build:
[rerun.io/viewer](https://rerun.io/viewer/pr/5792?manifest_url=https://app.rerun.io/version/main/examples_manifest.json)
* Using full set of examples from `nightly` build:
[rerun.io/viewer](https://rerun.io/viewer/pr/5792?manifest_url=https://app.rerun.io/version/nightly/examples_manifest.json)
* [x] The PR title and labels are set such as to maximize their
usefulness for the next release's CHANGELOG
* [x] If applicable, add a new check to the [release
checklist](https://github.com/rerun-io/rerun/blob/main/tests/python/release_checklist)!

- [PR Build Summary](https://build.rerun.io/pr/5792)
- [Docs
preview](https://rerun.io/preview/fb21b7fbbebe58182dfe33276e81c59540393683/docs)
<!--DOCS-PREVIEW-->
- [Examples
preview](https://rerun.io/preview/fb21b7fbbebe58182dfe33276e81c59540393683/examples)
<!--EXAMPLES-PREVIEW-->
- [Recent benchmark results](https://build.rerun.io/graphs/crates.html)
- [Wasm size tracking](https://build.rerun.io/graphs/sizes.html)
  • Loading branch information
emilk committed Apr 4, 2024
1 parent 464f4fc commit 3d68b52
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
16 changes: 16 additions & 0 deletions crates/re_viewer/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1320,6 +1320,22 @@ impl eframe::App for App {
.add(egui_ctx.input(|i| i.time), seconds);
}

#[cfg(target_arch = "wasm32")]
{
// Handle pressing the back/forward mouse buttons explicitly, since eframe catches those.
let back_pressed =
egui_ctx.input(|i| i.pointer.button_pressed(egui::PointerButton::Extra1));
let fwd_pressed =
egui_ctx.input(|i| i.pointer.button_pressed(egui::PointerButton::Extra2));

if back_pressed {
crate::web_tools::go_back();
}
if fwd_pressed {
crate::web_tools::go_forward();
}
}

// Temporarily take the `StoreHub` out of the Viewer so it doesn't interfere with mutability
let mut store_hub = self.store_hub.take().unwrap();

Expand Down
22 changes: 22 additions & 0 deletions crates/re_viewer/src/web_tools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,28 @@ pub fn percent_encode(s: &str) -> String {
format!("{}", js_sys::encode_uri_component(s))
}

pub fn go_back() -> Option<()> {
let history = web_sys::window()?
.history()
.map_err(|err| format!("Failed to get History API: {}", string_from_js_value(err)))
.ok_or_log_error()?;
history
.back()
.map_err(|err| format!("Failed to go back: {}", string_from_js_value(err)))
.ok_or_log_error()
}

pub fn go_forward() -> Option<()> {
let history = web_sys::window()?
.history()
.map_err(|err| format!("Failed to get History API: {}", string_from_js_value(err)))
.ok_or_log_error()?;
history
.forward()
.map_err(|err| format!("Failed to go forward: {}", string_from_js_value(err)))
.ok_or_log_error()
}

/// The current percent-encoded URL suffix, e.g. "?foo=bar#baz".
pub fn current_url_suffix() -> Option<String> {
let location = web_sys::window()?.location();
Expand Down

0 comments on commit 3d68b52

Please sign in to comment.