Skip to main content

pebble/rendering/
window_plugin.rs

1use crate::prelude::{Plugin, WindowConfig, WindowResource, WindowRunner};
2
3/// Plugin that creates the platform window and installs it as a runner.
4///
5/// On build it:
6/// 1. Creates the window via [`WindowProvider::create`](crate::rendering::window::WindowProvider::create).
7/// 2. Inserts a [`WindowResource`] containing the handle and exposed value.
8/// 3. Sets the app runner to `W::run`, which drives the frame loop.
9pub struct WindowPlugin<W: WindowRunner> {
10    pub config: WindowConfig,
11    _marker: std::marker::PhantomData<W>,
12}
13
14impl<W> WindowPlugin<W>
15where
16    W: WindowRunner,
17{
18    pub fn new(config: WindowConfig) -> Self {
19        Self {
20            config,
21            _marker: std::marker::PhantomData,
22        }
23    }
24}
25
26impl<W> Plugin for WindowPlugin<W>
27where
28    W: WindowRunner,
29    W::Handle: 'static + Send + Sync + Clone,
30{
31    fn build(&self, app: &mut crate::prelude::App) {
32        let window_source = W::create(&self.config);
33        let window_handle = window_source.handle().clone();
34        let window_exposed = window_source.exposed().clone();
35
36        app.add_resource(WindowResource::<W> {
37            handle: window_handle,
38            exposed: window_exposed,
39        });
40        app.set_runner(move |mut app| {
41            window_source.run(move || app.update());
42        });
43    }
44}