pebble/rendering/window.rs
1use raw_window_handle::{HasDisplayHandle, HasWindowHandle};
2
3/// A window handle that can be used to create a GPU surface.
4///
5/// Automatically implemented for any type that implements
6/// [`HasWindowHandle`], [`HasDisplayHandle`], `Send`, `Sync`, and `Clone`.
7pub trait GPUSurfaceHandle:
8 HasWindowHandle + HasDisplayHandle + Send + Sync + Clone + 'static
9{
10}
11
12impl<T> GPUSurfaceHandle for T where
13 T: HasWindowHandle + HasDisplayHandle + Send + Sync + Clone + 'static
14{
15}
16
17/// Configuration passed to a [`WindowProvider`] when creating a window.
18pub struct WindowConfig {
19 pub title: &'static str,
20 pub width: u32,
21 pub height: u32,
22}
23
24/// Abstracts over a platform-specific window implementation.
25///
26/// Implement this trait to plug in any windowing library (e.g. winit). The
27/// associated types expose the window handle used for surface creation and an
28/// "exposed" value (e.g. a shareable Arc) that systems can inspect.
29pub trait WindowProvider: 'static {
30 /// The concrete window handle type used to create a GPU surface.
31 type Handle: GPUSurfaceHandle;
32 /// An additional value derived from the window that can be cloned and
33 /// shared with other parts of the app (e.g. an `Arc<Window>`).
34 type Exposed: Clone + Send + Sync + 'static;
35
36 /// Create a window using the provided configuration.
37 fn create(config: &WindowConfig) -> Self;
38 /// Return the current inner size of `handle` in physical pixels.
39 fn size(handle: &Self::Handle) -> (u32, u32);
40 /// Return the exposed value for this window.
41 fn exposed(&self) -> Self::Exposed;
42 /// Return a reference to the raw window handle.
43 fn handle(&self) -> &Self::Handle;
44}
45
46/// Marker trait for window providers whose handle can be used as a GPU surface.
47pub trait PresentableWindow: WindowProvider
48where
49 Self::Handle: GPUSurfaceHandle,
50{
51}
52
53/// A [`WindowProvider`] that can drive the application's main loop.
54///
55/// The `run` method blocks (or hands off control to the OS event loop) and
56/// calls `on_frame` once per frame.
57pub trait WindowRunner: WindowProvider {
58 /// Start the event loop, calling `on_frame` each time a new frame should
59 /// be rendered.
60 fn run(self, on_frame: impl FnMut() + 'static);
61}
62
63/// Resource inserted by [`WindowPlugin`](crate::rendering::window_plugin::WindowPlugin)
64/// that gives systems access to the window handle and the exposed value.
65pub struct WindowResource<W: WindowProvider> {
66 /// The raw window handle, used for surface creation and size queries.
67 pub handle: W::Handle,
68 /// The platform-specific exposed value (e.g. `Arc<winit::window::Window>`).
69 pub exposed: W::Exposed,
70}