web_demo: make hash anchor case insensitive (#5446)

<!--
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
This commit is contained in:
Tristan Guichaoua 2024-12-11 13:44:37 +01:00 committed by GitHub
parent f28080c675
commit 4362a242b0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 18 additions and 6 deletions

View File

@ -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)
}
}
@ -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"))]