winit: replace special private function to spawn event loop with public API in the winit backend builder

This means the event loop spawning goes through the backend, which will permit for state from the Backend struct to be available when spinning the event loop even when spawning it merely.
This commit is contained in:
Simon Hausmann 2025-04-08 10:19:40 +02:00 committed by Simon Hausmann
parent 33561b0457
commit ac256d61d8
5 changed files with 48 additions and 23 deletions

View file

@ -131,6 +131,7 @@ send_wrapper = { workspace = true }
serde-wasm-bindgen = "0.6.0"
wasm-bindgen = "0.2.80"
wasm-bindgen-futures = "0.4.30"
i-slint-backend-winit = { workspace = true }
[target.'cfg(target_vendor = "apple")'.dependencies]
i-slint-backend-winit = { workspace = true, optional = true }

View file

@ -39,12 +39,26 @@ struct WasmCallbacks {
thread_local! {static WASM_CALLBACKS: RefCell<Option<WasmCallbacks>> = Default::default();}
#[wasm_bindgen(start)]
pub fn init_backend() -> Result<(), JsValue> {
console_error_panic_hook::set_once();
// Initialize the winit backend when we're used in the browser's main thread.
if web_sys::window().is_some() {
let backend =
i_slint_backend_winit::Backend::builder().with_spawn_event_loop(true).build().unwrap();
i_slint_core::platform::set_platform(Box::new(backend))
.map_err(|e| -> JsValue { format!("{e}").into() })?;
}
Ok(())
}
/// Register DOM event handlers on all instance and set up the event loop for that.
/// You can call this function only once. It will throw an exception but that is safe
/// to ignore.
/// You can call this function only once.
#[wasm_bindgen]
pub fn run_event_loop() -> Result<(), JsValue> {
slint_interpreter::spawn_event_loop().map_err(|e| -> JsValue { format!("{e}").into() })
slint_interpreter::run_event_loop().map_err(|e| -> JsValue { format!("{e}").into() })
}
#[wasm_bindgen]