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 frames;
9mod gpu;
10mod images;
11mod ime;
12mod input;
13mod keys;
14mod os;
15mod pacing;
16mod platform;
17mod pointer;
18mod surface;
19mod text_input;
20mod view;
21mod window;
22mod windows;
23
24use inset_embedder::{EmbedderClient, PlatformRef};
25
26pub use images::{DecodeExecution, create_image_loader};
27pub use platform::WinitPlatform;
28
29/// Configuration for the implicit view created before application startup.
30#[derive(Clone, Debug, PartialEq)]
31pub struct ImplicitViewConfig {
32    pub title: String,
33    pub logical_size: [f64; 2],
34}
35
36impl Default for ImplicitViewConfig {
37    fn default() -> ImplicitViewConfig {
38        ImplicitViewConfig {
39            title: "Inset".to_owned(),
40            logical_size: [900.0, 600.0],
41        }
42    }
43}
44
45/// Runs the winit event loop until quit.
46pub struct WinitEmbedder {
47    /// `None` starts without an implicit native window.
48    pub implicit_view: Option<ImplicitViewConfig>,
49}
50
51impl Default for WinitEmbedder {
52    fn default() -> WinitEmbedder {
53        WinitEmbedder {
54            implicit_view: Some(ImplicitViewConfig::default()),
55        }
56    }
57}
58
59impl WinitEmbedder {
60    /// Starts the native loop. `start` runs on the first `resumed`, after the
61    /// configured implicit view is created, and returns the client.
62    pub fn run<C: EmbedderClient + 'static>(self, start: impl FnOnce(PlatformRef) -> C + 'static) {
63        window::run(self, start, None);
64    }
65
66    /// Starts the native loop with an image loader of the application's choosing.
67    ///
68    /// The factory receives the renderer's device resources before `start` runs. Use
69    /// `create_image_loader(images, DecodeExecution::Local)` to decode without a worker
70    /// thread, or build a `valo_codec::ImageLoader` with decoders and an order of your own.
71    pub fn run_with_image_loader<C: EmbedderClient + 'static>(
72        self,
73        make_loader: impl FnOnce(valo::ImageContext) -> valo_codec::ImageLoader + 'static,
74        start: impl FnOnce(PlatformRef) -> C + 'static,
75    ) {
76        window::run(self, start, Some(Box::new(make_loader)));
77    }
78}