repame_shell/quit.rs
1//! Quitting: the one thing a game cannot do the same way on every platform.
2//!
3//! `std::process::exit` is `unreachable` on `wasm32-unknown-unknown`: it traps
4//! the instance and strands every winit `RefCell` guard, so the next event
5//! panics with "RefCell already borrowed". A browser has no window to close
6//! and no exit to call, so a quit there latches instead: the shell keeps
7//! servicing frames while the app stops stepping and paints [`Quit::parked`],
8//! leaving the tab to be closed. A parked root still says so on screen, and
9//! the game should cut its audio (`repame_audio::Audio::silence`) so a
10//! looping track does not play on over that message.
11
12use repose_core::View;
13use repose_core::prelude::Modifier;
14use repose_ui::{Center, Text, ViewExt};
15
16/// A quit request, latched so a browser shell can park instead of exiting.
17#[derive(Default)]
18pub struct Quit {
19 latched: bool,
20 hooks: Vec<Box<dyn FnOnce()>>,
21}
22
23impl Quit {
24 /// Registers a one-shot shutdown hook, run once when [`Self::request`]
25 /// is called. Games use this to flush pipeline caches before the device
26 /// is dropped.
27 pub fn on_shutdown(&mut self, hook: impl FnOnce() + 'static) {
28 self.hooks.push(Box::new(hook));
29 }
30
31 /// Ask to quit. Does not return off the web, where the process exits
32 /// here.
33 pub fn request(&mut self) {
34 if self.latched {
35 return;
36 }
37 self.latched = true;
38 for hook in self.hooks.drain(..) {
39 hook();
40 }
41 #[cfg(not(target_arch = "wasm32"))]
42 std::process::exit(0);
43 }
44
45 /// Whether the quit is latched, and the app is parked.
46 pub fn latched(&self) -> bool {
47 self.latched
48 }
49
50 /// The root a parked shell paints: one centered line saying the game
51 /// closed, so a blank tab is not mistaken for a hung frame.
52 pub fn parked() -> View {
53 Center(Modifier::new().fill_max_size()).child(Text("The game has been closed."))
54 }
55}