Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reintroduce the loading spinner #107

Merged
merged 5 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ env_logger = "0.10"
[target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen-futures = "0.4"

# to access the DOM (to hide the loading text)
[target.'cfg(target_arch = "wasm32")'.dependencies.web-sys]
version = "0.3.4"

[profile.release]
opt-level = 2 # fast and small wasm
Expand Down
9 changes: 8 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@
transform: rotate(360deg);
}
}

</style>
</head>

Expand All @@ -123,6 +122,14 @@
<!-- the id is hardcoded in main.rs . so, make sure both match. -->
<canvas id="the_canvas_id"></canvas>

<!-- the loading spinner will be removed in main.rs -->
<div class="centered" id="loading_text">
<p style="font-size:16px">
Loading…
</p>
<div class="lds-dual-ring"></div>
</div>

<!--Register Service Worker. this will cache the wasm / js scripts for offline use (for PWA functionality). -->
<!-- Force refresh (Ctrl + F5) to load the latest files instead of cached files -->
<script>
Expand Down
21 changes: 18 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,28 @@ fn main() {
let web_options = eframe::WebOptions::default();

wasm_bindgen_futures::spawn_local(async {
eframe::WebRunner::new()
let start_result = eframe::WebRunner::new()
.start(
"the_canvas_id", // hardcode it
web_options,
Box::new(|cc| Box::new(eframe_template::TemplateApp::new(cc))),
)
.await
.expect("failed to start eframe");
.await;
let loading_text = web_sys::window()
.and_then(|w| w.document())
.and_then(|d| d.get_element_by_id("loading_text"));
match start_result {
Ok(_) => {
loading_text.map(|e| e.remove());
}
Err(e) => {
loading_text.map(|e| {
e.set_inner_html(
"<p> The app has crashed. See the developer console for details. </p>",
)
});
panic!("failed to start eframe: {e:?}");
}
}
});
}