Skip to main content

inset_embedder_winit/
lib.rs

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