pushrod/backend/mod.rs
1// Backend
2// Pushrod
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16//! # Backends
17//!
18//! A backend is everything between Pushrod and a screen: the window, input, per-widget textures,
19//! the clock, the clipboard, and opening URLs. The core never names a windowing library; it
20//! talks to a [`Backend`], and a backend hands it a [`Painter`] to draw
21//! each `Widget`.
22//!
23//! * [`headless`] - [`HeadlessBackend`](headless::HeadlessBackend) and
24//! [`RecordingPainter`](headless::RecordingPainter), in this crate: no window, records paint
25//! calls, for tests.
26//! * `pushrod-sdl2` - the `SDL2` backend, in its own crate.
27//!
28//! An application picks a backend by starting the loop with it, for example
29//! `pushrod_sdl2::run(engine, WindowSettings::new("App", size(400, 300)), state)`.
30
31pub mod headless;
32
33use crate::color::Color;
34use crate::event::{EngineEvent, EventMask};
35use crate::geometry::{Rect, Size};
36use crate::input::InputEvent;
37use crate::paint::{CachePolicy, Painter};
38use crate::widget::WidgetId;
39use std::sync::Arc;
40use std::time::{Duration, Instant};
41
42/// A function another thread calls to wake a waiting backend; see [`Backend::waker`].
43pub type Waker = Arc<dyn Fn() + Send + Sync>;
44
45/// The services a windowing system provides to the engine.
46///
47/// A backend owns the window and one texture per `Widget`. The engine asks it to paint a
48/// `Widget` only when that `Widget` is invalidated, then composes each frame by drawing the
49/// cached textures. Window creation happens when the backend is built (each backend has its own
50/// settings); [`Backend::resize_window`] changes it afterward.
51pub trait Backend {
52 /// Returns how this backend caches painted `Widget`s.
53 ///
54 /// # Returns
55 ///
56 /// The backend's [`CachePolicy`].
57 fn cache_policy(&self) -> CachePolicy;
58
59 /// Returns the size of the window.
60 ///
61 /// # Returns
62 ///
63 /// The window's size in pixels.
64 fn window_size(&self) -> Size;
65
66 /// Resizes the window.
67 ///
68 /// # Arguments
69 ///
70 /// * `size` - The new size in pixels.
71 fn resize_window(&mut self, size: Size);
72
73 /// Tells the backend which input classes the application wants, so it can stop delivering the
74 /// rest. A backend may ignore this; the engine filters as well.
75 ///
76 /// # Arguments
77 ///
78 /// * `mask` - The run loop's event mask.
79 fn set_event_mask(&mut self, mask: EventMask);
80
81 /// Starts or stops text input to match the focused `Widget`.
82 ///
83 /// # Arguments
84 ///
85 /// * `active` - `true` while a text editor has focus.
86 fn set_text_input(&mut self, active: bool);
87
88 /// Collects waiting input.
89 ///
90 /// # Arguments
91 ///
92 /// * `wait` - How long to block for the first event; `None` returns at once.
93 ///
94 /// # Returns
95 ///
96 /// The events that arrived, in order; empty if none did.
97 fn poll_events(&mut self, wait: Option<Duration>) -> Vec<InputEvent>;
98
99 /// Returns a function other threads call to end a [`Backend::poll_events`] wait early; the
100 /// next poll then yields [`InputEvent::Wake`].
101 ///
102 /// # Returns
103 ///
104 /// The waker, or `None` if the backend cannot be woken (the loop then notices posted events
105 /// within a frame).
106 fn waker(&self) -> Option<Waker>;
107
108 /// Returns the current time.
109 ///
110 /// # Returns
111 ///
112 /// The backend's clock reading.
113 fn now(&self) -> Instant {
114 Instant::now()
115 }
116
117 /// Paints one `Widget` into its own texture, creating or resizing the texture as needed.
118 ///
119 /// # Arguments
120 ///
121 /// * `id` - The `Widget` the texture belongs to.
122 /// * `size` - The `Widget`'s size in pixels; never zero in either dimension.
123 /// * `paint` - Draws the `Widget`, given the backend's [`Painter`].
124 fn paint_widget(&mut self, id: WidgetId, size: Size, paint: &mut dyn FnMut(&mut dyn Painter));
125
126 /// Starts a frame by clearing the window.
127 ///
128 /// # Arguments
129 ///
130 /// * `clear` - The color to clear to.
131 fn begin_frame(&mut self, clear: Color);
132
133 /// Copies a `Widget`'s cached texture into the frame; nothing happens if it has none.
134 ///
135 /// # Arguments
136 ///
137 /// * `id` - The `Widget` to draw.
138 /// * `bounds` - Where to draw it, in window coordinates.
139 fn draw_widget(&mut self, id: WidgetId, bounds: Rect);
140
141 /// Finishes the frame and shows it.
142 fn present(&mut self);
143
144 /// Returns the engine events the backend produced since the last call (fonts loaded, glyph
145 /// atlas pages added).
146 ///
147 /// # Returns
148 ///
149 /// The events, in order.
150 fn take_notices(&mut self) -> Vec<EngineEvent>;
151
152 /// Reads the clipboard.
153 ///
154 /// # Returns
155 ///
156 /// The clipboard text, or `None` if it is empty or unavailable.
157 fn clipboard_text(&mut self) -> Option<String>;
158
159 /// Writes the clipboard.
160 ///
161 /// # Arguments
162 ///
163 /// * `text` - The text to store.
164 fn set_clipboard_text(&mut self, text: &str);
165
166 /// Opens a URL in the system's default handler.
167 ///
168 /// # Arguments
169 ///
170 /// * `url` - The URL to open.
171 ///
172 /// # Returns
173 ///
174 /// `Ok` if the request was made.
175 ///
176 /// # Errors
177 ///
178 /// Returns the reason the request failed, such as no handler for the URL.
179 fn open_url(&mut self, url: &str) -> Result<(), String>;
180}