Skip to main content

inset_embedder_winit/
lib.rs

1//! Desktop host: winit event loop and native windows. Frame pacing is
2//! `Window::request_redraw`. Presents through valo.
3
4// wgpu's handle registry nests auto-trait obligations past the default depth.
5#![recursion_limit = "256"]
6
7mod gpu;
8mod images;
9mod ime;
10mod keys;
11mod text_input;
12mod window;
13
14use inset_embedder::{EmbedderClient, PlatformRef};
15
16pub use images::{DecodeExecution, create_image_loader};
17
18/// Configuration for the implicit view created before application startup.
19#[derive(Clone, Debug, PartialEq)]
20pub struct ImplicitViewConfig {
21    pub title: String,
22    pub logical_size: [f64; 2],
23}
24
25impl Default for ImplicitViewConfig {
26    fn default() -> ImplicitViewConfig {
27        ImplicitViewConfig {
28            title: "Inset".to_owned(),
29            logical_size: [900.0, 600.0],
30        }
31    }
32}
33
34/// Runs the winit event loop until quit.
35pub struct WinitEmbedder {
36    /// `None` starts without an implicit native window.
37    pub implicit_view: Option<ImplicitViewConfig>,
38}
39
40impl Default for WinitEmbedder {
41    fn default() -> WinitEmbedder {
42        WinitEmbedder {
43            implicit_view: Some(ImplicitViewConfig::default()),
44        }
45    }
46}
47
48impl WinitEmbedder {
49    /// Starts the native loop. `start` runs on the first `resumed`, after the
50    /// configured implicit view is created, and returns the client.
51    pub fn run<C: EmbedderClient + 'static>(self, start: impl FnOnce(PlatformRef) -> C + 'static) {
52        window::run(self, start, None);
53    }
54
55    /// Starts the native loop with an image loader of the application's choosing.
56    ///
57    /// The factory receives the renderer's device resources before `start` runs. Use
58    /// `create_image_loader(images, DecodeExecution::Local)` to decode without a worker
59    /// thread, or build a `valo_codec::ImageLoader` with decoders and an order of your own.
60    pub fn run_with_image_loader<C: EmbedderClient + 'static>(
61        self,
62        make_loader: impl FnOnce(valo::ImageContext) -> valo_codec::ImageLoader + 'static,
63        start: impl FnOnce(PlatformRef) -> C + 'static,
64    ) {
65        window::run(self, start, Some(Box::new(make_loader)));
66    }
67}