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    /// 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.
147    ///
148    /// # Arguments
149    ///
150    /// - `&Self` - Mutable platform backend.
151    /// - `WindowId` - Target window handle.
152    /// - `&GpuFrameSubmission` - Direct-GPU batches, blur passes, and incremental atlas uploads.
153    ///
154    /// # Returns
155    ///
156    /// - `Result<GpuPresentReceipt, PlatformError>` - Presentation receipt (`presented = false` when backend uses CPU framebuffer fallback).
157    fn present_gpu_frame(
158        &mut self,
159        _id: WindowId,
160        _submission: &GpuFrameSubmission,
161    ) -> Result<GpuPresentReceipt, PlatformError> {
162        Ok(GpuPresentReceipt::unsupported())
163    }
164
165    /// Reads the current system clipboard text if available.
166    ///
167    /// # Arguments
168    ///
169    /// - `&Self` - Backend reference.
170    ///
171    /// # Returns
172    ///
173    /// - `Option<String>` - Clipboard UTF-8 contents.
174    fn read_clipboard(&self) -> Option<String>;
175
176    /// Writes `text` into the system clipboard.
177    ///
178    /// # Arguments
179    ///
180    /// - `&Self` - Mutable platform backend.
181    /// - `&str` - UTF-8 text to store.
182    ///
183    /// # Returns
184    ///
185    /// - `Result<(), PlatformError>` - Clipboard write status.
186    fn write_clipboard(&mut self, text: &str) -> Result<(), PlatformError>;
187
188    /// Queries the current operating system theme mode (`Light` / `Dark`).
189    ///
190    /// # Arguments
191    ///
192    /// - `&Self` - Backend reference.
193    ///
194    /// # Returns
195    ///
196    /// - `ThemeMode` - Detected OS appearance mode.
197    fn system_theme(&self) -> ThemeMode;
198
199    /// Queries the active display's native hardware refresh rate in Hertz (`60.0` fallback).
200    ///
201    /// # Arguments
202    ///
203    /// - `&Self` - Backend reference.
204    ///
205    /// # Returns
206    ///
207    /// - `f32` - Native display refresh rate in Hertz (e.g. `120.0`, `144.0`, `180.0`).
208    fn display_refresh_rate_hz(&self) -> f32 {
209        60.0
210    }
211}
212
213/// Unified key-value local storage contract shared across Web, Mobile, HarmonyOS, Wearables, Mini-Programs, and QuickApps.
214pub trait PlatformStorage {
215    /// Reads a UTF-8 value associated with `key` from sandboxed local storage.
216    ///
217    /// # Arguments
218    ///
219    /// - `&Self` - Storage adapter reference.
220    /// - `&str` - Storage key.
221    ///
222    /// # Returns
223    ///
224    /// - `Option<String>` - Stored value, if present.
225    fn storage_get(&self, key: &str) -> Option<String>;
226
227    /// Stores a UTF-8 `value` under `key` in sandboxed local storage.
228    ///
229    /// # Arguments
230    ///
231    /// - `&Self` - Mutable storage adapter.
232    /// - `&str` - Storage key.
233    /// - `&str` - UTF-8 value to persist.
234    ///
235    /// # Returns
236    ///
237    /// - `Result<(), PlatformError>` - Persistence status.
238    fn storage_set(&mut self, key: &str, value: &str) -> Result<(), PlatformError>;
239
240    /// Removes `key` from sandboxed local storage.
241    ///
242    /// # Arguments
243    ///
244    /// - `&Self` - Mutable storage adapter.
245    /// - `&str` - Storage key to remove.
246    ///
247    /// # Returns
248    ///
249    /// - `Result<(), PlatformError>` - Removal status.
250    fn storage_remove(&mut self, key: &str) -> Result<(), PlatformError>;
251
252    /// Clears all keys from sandboxed local storage.
253    ///
254    /// # Arguments
255    ///
256    /// - `&Self` - Mutable storage adapter.
257    ///
258    /// # Returns
259    ///
260    /// - `Result<(), PlatformError>` - Clear status.
261    fn storage_clear(&mut self) -> Result<(), PlatformError>;
262}
263
264/// Unified host-level navigation bridge abstracting OS routers, MiniApp page stacks, and QuickApp `@system.router`.
265pub trait PlatformNavigation {
266    /// Pushes or navigates to `route` on the host navigation stack.
267    ///
268    /// # Arguments
269    ///
270    /// - `&Self` - Mutable navigation adapter.
271    /// - `&str` - Target logical route path.
272    ///
273    /// # Returns
274    ///
275    /// - `Result<(), PlatformError>` - Navigation dispatch status.
276    fn navigate_to(&mut self, route: &str) -> Result<(), PlatformError>;
277
278    /// Pops `delta` entries from the host navigation stack.
279    ///
280    /// # Arguments
281    ///
282    /// - `&Self` - Mutable navigation adapter.
283    /// - `u32` - Number of pages to navigate back (`>= 1`).
284    ///
285    /// # Returns
286    ///
287    /// - `Result<(), PlatformError>` - Back navigation status.
288    fn navigate_back(&mut self, delta: u32) -> Result<(), PlatformError>;
289
290    /// Returns the currently active logical route path.
291    ///
292    /// # Arguments
293    ///
294    /// - `&Self` - Navigation adapter reference.
295    ///
296    /// # Returns
297    ///
298    /// - `&str` - Active route path string.
299    fn current_route(&self) -> &str;
300}
301
302/// Unified Freeze / Resume / Destroy surface lifecycle contract smoothing platform differences across all targets.
303pub trait PlatformLifecycleBridge {
304    /// Suspends frame pumping, cancels active pointers, and emits canonical freeze/background events.
305    ///
306    /// # Arguments
307    ///
308    /// - `&Self` - Mutable lifecycle adapter.
309    ///
310    /// # Returns
311    ///
312    /// - `Vec<WindowEvent>` - Normalized freeze events to dispatch to `App`.
313    fn freeze_surface(&mut self) -> Vec<WindowEvent>;
314
315    /// Resumes frame pumping and emits canonical appear/foreground events.
316    ///
317    /// # Arguments
318    ///
319    /// - `&Self` - Mutable lifecycle adapter.
320    ///
321    /// # Returns
322    ///
323    /// - `Vec<WindowEvent>` - Normalized resume events to dispatch to `App`.
324    fn resume_surface(&mut self) -> Vec<WindowEvent>;
325
326    /// Tears down surface attachments, cancels pointers, and emits canonical terminate events.
327    ///
328    /// # Arguments
329    ///
330    /// - `&Self` - Mutable lifecycle adapter.
331    ///
332    /// # Returns
333    ///
334    /// - `Vec<WindowEvent>` - Normalized teardown events to dispatch to `App`.
335    fn destroy_surface(&mut self) -> Vec<WindowEvent>;
336}
337
338/// Supertrait implemented by full cross-platform host adapters exposing uniform backend, storage, navigation, and lifecycle semantics.
339pub trait PlatformHostAdapter:
340    PlatformBackend + PlatformStorage + PlatformNavigation + PlatformLifecycleBridge
341{
342    /// Returns the concrete `DeviceFormFactor` of this running host instance.
343    ///
344    /// # Arguments
345    ///
346    /// - `&Self` - Host adapter reference.
347    ///
348    /// # Returns
349    ///
350    /// - `DeviceFormFactor` - Active physical or virtual form factor.
351    fn device_form_factor(&self) -> DeviceFormFactor;
352
353    /// Returns the canonical lowercase vendor or runtime name for diagnostics (never required by app UI code).
354    ///
355    /// # Arguments
356    ///
357    /// - `&Self` - Host adapter reference.
358    ///
359    /// # Returns
360    ///
361    /// - `&'static str` - Vendor or runtime identifier string.
362    fn host_vendor_name(&self) -> &'static str;
363}