Skip to content

Commit

Permalink
web_demo: make hash anchor case insensitive (#5446)
Browse files Browse the repository at this point in the history
<!--
Please read the "Making a PR" section of
[`CONTRIBUTING.md`](https://github.com/emilk/egui/blob/master/CONTRIBUTING.md)
before opening a Pull Request!

* Keep your PR:s small and focused.
* The PR title is what ends up in the changelog, so make it descriptive!
* If applicable, add a screenshot or gif.
* If it is a non-trivial addition, consider adding a demo for it to
`egui_demo_lib`, or a new example.
* Do NOT open PR:s from your `master` branch, as that makes it hard for
maintainers to test and add commits to your PR.
* Remember to run `cargo fmt` and `cargo clippy`.
* Open the PR as a draft until you have self-reviewed it and run
`./scripts/check.sh`.
* When you have addressed a PR comment, mark it as resolved.

Please be patient! I will review your PR, but my time is limited!
-->

* [X] I have followed the instructions in the PR template
  • Loading branch information
tguichaoua authored Dec 11, 2024
1 parent f28080c commit 4362a24
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions crates/egui_demo_app/src/wrap_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,19 @@ impl Anchor {
Self::Rendering,
]
}

#[cfg(target_arch = "wasm32")]
fn from_str_case_insensitive(anchor: &str) -> Option<Self> {
let anchor = anchor.to_lowercase();
Self::all().into_iter().find(|x| x.to_string() == anchor)
}
}

impl std::fmt::Display for Anchor {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{self:?}")
let mut name = format!("{self:?}");
name.make_ascii_lowercase();
f.write_str(&name)
}
}

Expand Down Expand Up @@ -263,11 +271,15 @@ impl eframe::App for WrapApp {

fn update(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) {
#[cfg(target_arch = "wasm32")]
if let Some(anchor) = frame.info().web_info.location.hash.strip_prefix('#') {
let anchor = Anchor::all().into_iter().find(|x| x.to_string() == anchor);
if let Some(v) = anchor {
self.state.selected_anchor = v;
}
if let Some(anchor) = frame
.info()
.web_info
.location
.hash
.strip_prefix('#')
.and_then(Anchor::from_str_case_insensitive)
{
self.state.selected_anchor = anchor;
}

#[cfg(not(target_arch = "wasm32"))]
Expand Down

0 comments on commit 4362a24

Please sign in to comment.