Skip to main content

euv_core/platform/backend/
trait.rs

1use super::*;
2
3/// Unified system abstraction implemented directly over each OS native API (Web, Android, iOS, iPadOS, macOS, Windows, Linux, Headless).
4pub trait PlatformBackend: WindowController {
5    /// Returns the concrete `PlatformKind` served by this backend.
6    ///
7    /// # Arguments
8    ///
9    /// - `&Self` - Backend reference.
10    ///
11    /// # Returns
12    ///
13    /// - `PlatformKind` - Active platform kind.
14    fn kind(&self) -> PlatformKind;
15
16    /// Returns the normalized capability matrix for this backend.
17    ///
18    /// # Arguments
19    ///
20    /// - `&Self` - Backend reference.
21    ///
22    /// # Returns
23    ///
24    /// - `PlatformCapabilities` - Hardware and OS capability flags.
25    fn capabilities(&self) -> PlatformCapabilities;
26
27    /// Creates a native or web window configured by `config`.
28    ///
29    /// # Arguments
30    ///
31    /// - `&Self` - Mutable platform backend.
32    /// - `&WindowConfig` - Declarative window configuration.
33    ///
34    /// # Returns
35    ///
36    /// - `Result<WindowState, PlatformError>` - Initial live window state on success.
37    fn create_window(&mut self, config: &WindowConfig) -> Result<WindowState, PlatformError>;
38
39    /// Returns the latest `WindowState` for `id`, if alive.
40    ///
41    /// # Arguments
42    ///
43    /// - `&Self` - Backend reference.
44    /// - `WindowId` - Window identifier.
45    ///
46    /// # Returns
47    ///
48    /// - `Option<WindowState>` - Live window state.
49    fn window_state(&self, id: WindowId) -> Option<WindowState>;
50
51    /// Returns the ABI-stable `RawWindowHandle` for hardware GPU surface binding.
52    ///
53    /// # Arguments
54    ///
55    /// - `&Self` - Backend reference.
56    /// - `WindowId` - Window identifier.
57    ///
58    /// # Returns
59    ///
60    /// - `RawWindowHandle` - Native OS surface pointer bundle.
61    fn raw_window_handle(&self, id: WindowId) -> RawWindowHandle;
62
63    /// Drains all pending OS window, input, and lifecycle events since the last poll.
64    ///
65    /// # Arguments
66    ///
67    /// - `&Self` - Mutable platform backend.
68    ///
69    /// # Returns
70    ///
71    /// - `Vec<WindowEvent>` - Ordered pending events.
72    fn poll_events(&mut self) -> Vec<WindowEvent>;
73
74    /// Blocks with zero CPU usage until at least one OS or lifecycle event arrives, then drains all pending events.
75    ///
76    /// # Arguments
77    ///
78    /// - `&Self` - Mutable platform backend.
79    ///
80    /// # Returns
81    ///
82    /// - `Vec<WindowEvent>` - Ordered events received upon wake.
83    fn wait_for_events(&mut self) -> Vec<WindowEvent> {
84        self.poll_events()
85    }
86
87    /// Pauses or resumes the native host frame pump when entering or leaving background-frozen state.
88    ///
89    /// # Arguments
90    ///
91    /// - `&Self` - Mutable platform backend.
92    /// - `bool` - `true` to pause frame pumping for background freeze; `false` to resume.
93    fn set_frame_pump_paused(&mut self, _paused: bool) {}
94
95    /// Returns whether the platform backend's frame pump is currently paused for background freeze.
96    ///
97    /// # Arguments
98    ///
99    /// - `&Self` - Backend reference.
100    ///
101    /// # Returns
102    ///
103    /// - `bool` - `true` when frame pumping is paused.
104    fn is_frame_pump_paused(&self) -> bool {
105        false
106    }
107
108    /// Presents a CPU-rendered RGBA8 `Framebuffer` directly onto the native window surface.
109    ///
110    /// # Arguments
111    ///
112    /// - `&Self` - Mutable platform backend.
113    /// - `WindowId` - Target window handle.
114    /// - `&Framebuffer` - Rendered RGBA8 framebuffer.
115    ///
116    /// # Returns
117    ///
118    /// - `Result<(), PlatformError>` - Presentation status.
119    fn present_framebuffer(
120        &mut self,
121        id: WindowId,
122        framebuffer: &Framebuffer,
123    ) -> Result<(), PlatformError>;
124
125    /// Presents a damaged sub-region of a CPU-rendered RGBA8 `Framebuffer` when supported by the backend.
126    ///
127    /// # Arguments
128    ///
129    /// - `&Self` - Mutable platform backend.
130    /// - `WindowId` - Target window handle.
131    /// - `&Framebuffer` - Rendered RGBA8 framebuffer.
132    /// - `DamageRegion` - Damaged pixel region updated this frame.
133    ///
134    /// # Returns
135    ///
136    /// - `Result<(), PlatformError>` - Presentation status.
137    fn present_framebuffer_region(
138        &mut self,
139        id: WindowId,
140        framebuffer: &Framebuffer,
141        _damage: DamageRegion,
142    ) -> Result<(), PlatformError> {
143        self.present_framebuffer(id, framebuffer)
144    }
145
146    /// Reads the current system clipboard text if available.
147    ///
148    /// # Arguments
149    ///
150    /// - `&Self` - Backend reference.
151    ///
152    /// # Returns
153    ///
154    /// - `Option<String>` - Clipboard UTF-8 contents.
155    fn read_clipboard(&self) -> Option<String>;
156
157    /// Writes `text` into the system clipboard.
158    ///
159    /// # Arguments
160    ///
161    /// - `&Self` - Mutable platform backend.
162    /// - `&str` - UTF-8 text to store.
163    ///
164    /// # Returns
165    ///
166    /// - `Result<(), PlatformError>` - Clipboard write status.
167    fn write_clipboard(&mut self, text: &str) -> Result<(), PlatformError>;
168
169    /// Queries the current operating system theme mode (`Light` / `Dark`).
170    ///
171    /// # Arguments
172    ///
173    /// - `&Self` - Backend reference.
174    ///
175    /// # Returns
176    ///
177    /// - `ThemeMode` - Detected OS appearance mode.
178    fn system_theme(&self) -> ThemeMode;
179
180    /// Queries the active display's native hardware refresh rate in Hertz (`60.0` fallback).
181    ///
182    /// # Arguments
183    ///
184    /// - `&Self` - Backend reference.
185    ///
186    /// # Returns
187    ///
188    /// - `f32` - Native display refresh rate in Hertz (e.g. `120.0`, `144.0`, `180.0`).
189    fn display_refresh_rate_hz(&self) -> f32 {
190        60.0
191    }
192}