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}
193
194/// Unified key-value local storage contract shared across Web, Mobile, HarmonyOS, Wearables, Mini-Programs, and QuickApps.
195pub trait PlatformStorage {
196    /// Reads a UTF-8 value associated with `key` from sandboxed local storage.
197    ///
198    /// # Arguments
199    ///
200    /// - `&Self` - Storage adapter reference.
201    /// - `&str` - Storage key.
202    ///
203    /// # Returns
204    ///
205    /// - `Option<String>` - Stored value, if present.
206    fn storage_get(&self, key: &str) -> Option<String>;
207
208    /// Stores a UTF-8 `value` under `key` in sandboxed local storage.
209    ///
210    /// # Arguments
211    ///
212    /// - `&Self` - Mutable storage adapter.
213    /// - `&str` - Storage key.
214    /// - `&str` - UTF-8 value to persist.
215    ///
216    /// # Returns
217    ///
218    /// - `Result<(), PlatformError>` - Persistence status.
219    fn storage_set(&mut self, key: &str, value: &str) -> Result<(), PlatformError>;
220
221    /// Removes `key` from sandboxed local storage.
222    ///
223    /// # Arguments
224    ///
225    /// - `&Self` - Mutable storage adapter.
226    /// - `&str` - Storage key to remove.
227    ///
228    /// # Returns
229    ///
230    /// - `Result<(), PlatformError>` - Removal status.
231    fn storage_remove(&mut self, key: &str) -> Result<(), PlatformError>;
232
233    /// Clears all keys from sandboxed local storage.
234    ///
235    /// # Arguments
236    ///
237    /// - `&Self` - Mutable storage adapter.
238    ///
239    /// # Returns
240    ///
241    /// - `Result<(), PlatformError>` - Clear status.
242    fn storage_clear(&mut self) -> Result<(), PlatformError>;
243}
244
245/// Unified host-level navigation bridge abstracting OS routers, MiniApp page stacks, and QuickApp `@system.router`.
246pub trait PlatformNavigation {
247    /// Pushes or navigates to `route` on the host navigation stack.
248    ///
249    /// # Arguments
250    ///
251    /// - `&Self` - Mutable navigation adapter.
252    /// - `&str` - Target logical route path.
253    ///
254    /// # Returns
255    ///
256    /// - `Result<(), PlatformError>` - Navigation dispatch status.
257    fn navigate_to(&mut self, route: &str) -> Result<(), PlatformError>;
258
259    /// Pops `delta` entries from the host navigation stack.
260    ///
261    /// # Arguments
262    ///
263    /// - `&Self` - Mutable navigation adapter.
264    /// - `u32` - Number of pages to navigate back (`>= 1`).
265    ///
266    /// # Returns
267    ///
268    /// - `Result<(), PlatformError>` - Back navigation status.
269    fn navigate_back(&mut self, delta: u32) -> Result<(), PlatformError>;
270
271    /// Returns the currently active logical route path.
272    ///
273    /// # Arguments
274    ///
275    /// - `&Self` - Navigation adapter reference.
276    ///
277    /// # Returns
278    ///
279    /// - `&str` - Active route path string.
280    fn current_route(&self) -> &str;
281}
282
283/// Unified Freeze / Resume / Destroy surface lifecycle contract smoothing platform differences across all targets.
284pub trait PlatformLifecycleBridge {
285    /// Suspends frame pumping, cancels active pointers, and emits canonical freeze/background events.
286    ///
287    /// # Arguments
288    ///
289    /// - `&Self` - Mutable lifecycle adapter.
290    ///
291    /// # Returns
292    ///
293    /// - `Vec<WindowEvent>` - Normalized freeze events to dispatch to `App`.
294    fn freeze_surface(&mut self) -> Vec<WindowEvent>;
295
296    /// Resumes frame pumping and emits canonical appear/foreground events.
297    ///
298    /// # Arguments
299    ///
300    /// - `&Self` - Mutable lifecycle adapter.
301    ///
302    /// # Returns
303    ///
304    /// - `Vec<WindowEvent>` - Normalized resume events to dispatch to `App`.
305    fn resume_surface(&mut self) -> Vec<WindowEvent>;
306
307    /// Tears down surface attachments, cancels pointers, and emits canonical terminate events.
308    ///
309    /// # Arguments
310    ///
311    /// - `&Self` - Mutable lifecycle adapter.
312    ///
313    /// # Returns
314    ///
315    /// - `Vec<WindowEvent>` - Normalized teardown events to dispatch to `App`.
316    fn destroy_surface(&mut self) -> Vec<WindowEvent>;
317}
318
319/// Supertrait implemented by full cross-platform host adapters exposing uniform backend, storage, navigation, and lifecycle semantics.
320pub trait PlatformHostAdapter:
321    PlatformBackend + PlatformStorage + PlatformNavigation + PlatformLifecycleBridge
322{
323    /// Returns the concrete `DeviceFormFactor` of this running host instance.
324    ///
325    /// # Arguments
326    ///
327    /// - `&Self` - Host adapter reference.
328    ///
329    /// # Returns
330    ///
331    /// - `DeviceFormFactor` - Active physical or virtual form factor.
332    fn device_form_factor(&self) -> DeviceFormFactor;
333
334    /// Returns the canonical lowercase vendor or runtime name for diagnostics (never required by app UI code).
335    ///
336    /// # Arguments
337    ///
338    /// - `&Self` - Host adapter reference.
339    ///
340    /// # Returns
341    ///
342    /// - `&'static str` - Vendor or runtime identifier string.
343    fn host_vendor_name(&self) -> &'static str;
344}