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 /// Window title. Owned so it can be computed at runtime (a version
20 /// string, a loaded file name, ...) rather than only ever a `&'static`
21 /// literal.
22 pub title: String,
23 /// Initial window width, in physical pixels.
24 pub width: u32,
25 /// Initial window height, in physical pixels.
26 pub height: u32,
27}
28
29/// Abstracts over a platform-specific window implementation.
30///
31/// Implement this trait to plug in any windowing library (e.g. winit). The
32/// associated types expose the window handle used for surface creation and an
33/// "exposed" value (e.g. a shareable Arc) that systems can inspect.
34pub trait WindowProvider: 'static {
35 /// The concrete window handle type used to create a GPU surface.
36 type Handle: GPUSurfaceHandle;
37 /// An additional value derived from the window that can be cloned and
38 /// shared with other parts of the app (e.g. an `Arc<Window>`).
39 type Exposed: Clone + Send + Sync + 'static;
40
41 /// Create a window using the provided configuration.
42 fn create(config: &WindowConfig) -> Self;
43 /// Return the current inner size of `handle` in physical pixels.
44 fn size(handle: &Self::Handle) -> (u32, u32);
45 /// Return the exposed value for this window.
46 fn exposed(&self) -> Self::Exposed;
47 /// Return a reference to the raw window handle.
48 fn handle(&self) -> &Self::Handle;
49}
50
51/// Marker trait for window providers whose handle can be used as a GPU surface.
52pub trait PresentableWindow: WindowProvider
53where
54 Self::Handle: GPUSurfaceHandle,
55{
56}
57
58/// A [`WindowProvider`] that can drive the application's main loop.
59///
60/// The `run` method blocks (or hands off control to the OS event loop) and
61/// calls `on_frame` once per frame.
62pub trait WindowRunner: WindowProvider {
63 /// Start the event loop, calling `on_frame` each time a new frame should
64 /// be rendered.
65 fn run(self, on_frame: impl FnMut() + 'static);
66}
67
68/// Resource inserted by [`WindowPlugin`](crate::rendering::window_plugin::WindowPlugin)
69/// that gives systems access to the window handle and the exposed value.
70pub struct WindowResource<W: WindowProvider> {
71 /// The raw window handle, used for surface creation and size queries.
72 pub handle: W::Handle,
73 /// The platform-specific exposed value (e.g. `Arc<winit::window::Window>`).
74 pub exposed: W::Exposed,
75}