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    /// Blocks with zero CPU usage until at least one OS event arrives or `timeout_ms` elapses, then drains all pending events.
88    ///
89    /// # Arguments
90    ///
91    /// - `&Self` - Mutable platform backend.
92    /// - `u32` - Maximum wait duration in milliseconds before returning.
93    ///
94    /// # Returns
95    ///
96    /// - `Vec<WindowEvent>` - Ordered events received upon wake (empty when timed out).
97    fn wait_for_events_timeout(&mut self, _timeout_ms: u32) -> Vec<WindowEvent> {
98        self.wait_for_events()
99    }
100
101    /// Pauses or resumes the native host frame pump when entering or leaving background-frozen state.
102    ///
103    /// # Arguments
104    ///
105    /// - `&Self` - Mutable platform backend.
106    /// - `bool` - `true` to pause frame pumping for background freeze; `false` to resume.
107    fn set_frame_pump_paused(&mut self, _paused: bool) {}
108
109    /// Returns whether the platform backend's frame pump is currently paused for background freeze.
110    ///
111    /// # Arguments
112    ///
113    /// - `&Self` - Backend reference.
114    ///
115    /// # Returns
116    ///
117    /// - `bool` - `true` when frame pumping is paused.
118    fn is_frame_pump_paused(&self) -> bool {
119        false
120    }
121
122    /// Presents a CPU-rendered RGBA8 `Framebuffer` directly onto the native window surface.
123    ///
124    /// # Arguments
125    ///
126    /// - `&Self` - Mutable platform backend.
127    /// - `WindowId` - Target window handle.
128    /// - `&Framebuffer` - Rendered RGBA8 framebuffer.
129    ///
130    /// # Returns
131    ///
132    /// - `Result<(), PlatformError>` - Presentation status.
133    fn present_framebuffer(
134        &mut self,
135        id: WindowId,
136        framebuffer: &Framebuffer,
137    ) -> Result<(), PlatformError>;
138
139    /// Presents a damaged sub-region of a CPU-rendered RGBA8 `Framebuffer` when supported by the backend.
140    ///
141    /// # Arguments
142    ///
143    /// - `&Self` - Mutable platform backend.
144    /// - `WindowId` - Target window handle.
145    /// - `&Framebuffer` - Rendered RGBA8 framebuffer.
146    /// - `DamageRegion` - Damaged pixel region updated this frame.
147    ///
148    /// # Returns
149    ///
150    /// - `Result<(), PlatformError>` - Presentation status.
151    fn present_framebuffer_region(
152        &mut self,
153        id: WindowId,
154        framebuffer: &Framebuffer,
155        _damage: DamageRegion,
156    ) -> Result<(), PlatformError> {
157        self.present_framebuffer(id, framebuffer)
158    }
159
160    /// Presents compiled 64-byte GPU primitive batches directly to the native GPU swapchain (`CAMetalLayer` / WebGL2 / WebGPU / Vulkan / DX12) with zero steady-state CPU RGBA readback.
161    ///
162    /// # Arguments
163    ///
164    /// - `&Self` - Mutable platform backend.
165    /// - `WindowId` - Target window handle.
166    /// - `&GpuFrameSubmission` - Direct-GPU batches, blur passes, and incremental atlas uploads.
167    ///
168    /// # Returns
169    ///
170    /// - `Result<GpuPresentReceipt, PlatformError>` - Presentation receipt (`presented = false` when backend uses CPU framebuffer fallback).
171    fn present_gpu_frame(
172        &mut self,
173        _id: WindowId,
174        _submission: &GpuFrameSubmission,
175    ) -> Result<GpuPresentReceipt, PlatformError> {
176        Ok(GpuPresentReceipt::unsupported())
177    }
178
179    /// Reads the current system clipboard text if available.
180    ///
181    /// # Arguments
182    ///
183    /// - `&Self` - Backend reference.
184    ///
185    /// # Returns
186    ///
187    /// - `Option<String>` - Clipboard UTF-8 contents.
188    fn read_clipboard(&self) -> Option<String>;
189
190    /// Writes `text` into the system clipboard.
191    ///
192    /// # Arguments
193    ///
194    /// - `&Self` - Mutable platform backend.
195    /// - `&str` - UTF-8 text to store.
196    ///
197    /// # Returns
198    ///
199    /// - `Result<(), PlatformError>` - Clipboard write status.
200    fn write_clipboard(&mut self, text: &str) -> Result<(), PlatformError>;
201
202    /// Queries the current operating system theme mode (`Light` / `Dark`).
203    ///
204    /// # Arguments
205    ///
206    /// - `&Self` - Backend reference.
207    ///
208    /// # Returns
209    ///
210    /// - `ThemeMode` - Detected OS appearance mode.
211    fn system_theme(&self) -> ThemeMode;
212
213    /// Queries the active display's native hardware refresh rate in Hertz (`60.0` fallback).
214    ///
215    /// # Arguments
216    ///
217    /// - `&Self` - Backend reference.
218    ///
219    /// # Returns
220    ///
221    /// - `f32` - Native display refresh rate in Hertz (e.g. `120.0`, `144.0`, `180.0`).
222    fn display_refresh_rate_hz(&self) -> f32 {
223        60.0
224    }
225}
226
227/// Unified key-value local storage contract shared across Web, Mobile, HarmonyOS, Wearables, Mini-Programs, and QuickApps.
228pub trait PlatformStorage {
229    /// Reads a UTF-8 value associated with `key` from sandboxed local storage.
230    ///
231    /// # Arguments
232    ///
233    /// - `&Self` - Storage adapter reference.
234    /// - `&str` - Storage key.
235    ///
236    /// # Returns
237    ///
238    /// - `Option<String>` - Stored value, if present.
239    fn storage_get(&self, key: &str) -> Option<String>;
240
241    /// Stores a UTF-8 `value` under `key` in sandboxed local storage.
242    ///
243    /// # Arguments
244    ///
245    /// - `&Self` - Mutable storage adapter.
246    /// - `&str` - Storage key.
247    /// - `&str` - UTF-8 value to persist.
248    ///
249    /// # Returns
250    ///
251    /// - `Result<(), PlatformError>` - Persistence status.
252    fn storage_set(&mut self, key: &str, value: &str) -> Result<(), PlatformError>;
253
254    /// Removes `key` from sandboxed local storage.
255    ///
256    /// # Arguments
257    ///
258    /// - `&Self` - Mutable storage adapter.
259    /// - `&str` - Storage key to remove.
260    ///
261    /// # Returns
262    ///
263    /// - `Result<(), PlatformError>` - Removal status.
264    fn storage_remove(&mut self, key: &str) -> Result<(), PlatformError>;
265
266    /// Clears all keys from sandboxed local storage.
267    ///
268    /// # Arguments
269    ///
270    /// - `&Self` - Mutable storage adapter.
271    ///
272    /// # Returns
273    ///
274    /// - `Result<(), PlatformError>` - Clear status.
275    fn storage_clear(&mut self) -> Result<(), PlatformError>;
276}
277
278/// Unified host-level navigation bridge abstracting OS routers, MiniApp page stacks, and QuickApp `@system.router`.
279pub trait PlatformNavigation {
280    /// Pushes or navigates to `route` on the host navigation stack.
281    ///
282    /// # Arguments
283    ///
284    /// - `&Self` - Mutable navigation adapter.
285    /// - `&str` - Target logical route path.
286    ///
287    /// # Returns
288    ///
289    /// - `Result<(), PlatformError>` - Navigation dispatch status.
290    fn navigate_to(&mut self, route: &str) -> Result<(), PlatformError>;
291
292    /// Pops `delta` entries from the host navigation stack.
293    ///
294    /// # Arguments
295    ///
296    /// - `&Self` - Mutable navigation adapter.
297    /// - `u32` - Number of pages to navigate back (`>= 1`).
298    ///
299    /// # Returns
300    ///
301    /// - `Result<(), PlatformError>` - Back navigation status.
302    fn navigate_back(&mut self, delta: u32) -> Result<(), PlatformError>;
303
304    /// Returns the currently active logical route path.
305    ///
306    /// # Arguments
307    ///
308    /// - `&Self` - Navigation adapter reference.
309    ///
310    /// # Returns
311    ///
312    /// - `&str` - Active route path string.
313    fn current_route(&self) -> &str;
314}
315
316/// Unified Freeze / Resume / Destroy surface lifecycle contract smoothing platform differences across all targets.
317pub trait PlatformLifecycleBridge {
318    /// Suspends frame pumping, cancels active pointers, and emits canonical freeze/background events.
319    ///
320    /// # Arguments
321    ///
322    /// - `&Self` - Mutable lifecycle adapter.
323    ///
324    /// # Returns
325    ///
326    /// - `Vec<WindowEvent>` - Normalized freeze events to dispatch to `App`.
327    fn freeze_surface(&mut self) -> Vec<WindowEvent>;
328
329    /// Resumes frame pumping and emits canonical appear/foreground events.
330    ///
331    /// # Arguments
332    ///
333    /// - `&Self` - Mutable lifecycle adapter.
334    ///
335    /// # Returns
336    ///
337    /// - `Vec<WindowEvent>` - Normalized resume events to dispatch to `App`.
338    fn resume_surface(&mut self) -> Vec<WindowEvent>;
339
340    /// Tears down surface attachments, cancels pointers, and emits canonical terminate events.
341    ///
342    /// # Arguments
343    ///
344    /// - `&Self` - Mutable lifecycle adapter.
345    ///
346    /// # Returns
347    ///
348    /// - `Vec<WindowEvent>` - Normalized teardown events to dispatch to `App`.
349    fn destroy_surface(&mut self) -> Vec<WindowEvent>;
350}
351
352/// Supertrait implemented by full cross-platform host adapters exposing uniform backend, storage, navigation, and lifecycle semantics.
353pub trait PlatformHostAdapter:
354    PlatformBackend + PlatformStorage + PlatformNavigation + PlatformLifecycleBridge
355{
356    /// Returns the concrete `DeviceFormFactor` of this running host instance.
357    ///
358    /// # Arguments
359    ///
360    /// - `&Self` - Host adapter reference.
361    ///
362    /// # Returns
363    ///
364    /// - `DeviceFormFactor` - Active physical or virtual form factor.
365    fn device_form_factor(&self) -> DeviceFormFactor;
366
367    /// Returns the canonical lowercase vendor or runtime name for diagnostics (never required by app UI code).
368    ///
369    /// # Arguments
370    ///
371    /// - `&Self` - Host adapter reference.
372    ///
373    /// # Returns
374    ///
375    /// - `&'static str` - Vendor or runtime identifier string.
376    fn host_vendor_name(&self) -> &'static str;
377}