euv-core 0.46.1

Core cross-platform runtime for euv featuring arena-based fine-grained reactivity, deterministic layout, flat scene graph, multi-mode rendering, and direct OS platform adapters.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
use super::*;

/// Canonical identifier and ABI tag helpers for `QuickAppVendor`.
impl QuickAppVendor {
    /// Returns the canonical lowercase QuickApp vendor identifier string.
    ///
    /// # Arguments
    ///
    /// - `&Self` - QuickApp vendor variant.
    ///
    /// # Returns
    ///
    /// - `&'static str` - Canonical vendor identifier.
    #[must_use]
    pub const fn as_str(&self) -> &'static str {
        match *self {
            Self::Alliance => QUICKAPP_VENDOR_ALLIANCE,
            Self::Huawei => QUICKAPP_VENDOR_HUAWEI,
        }
    }

    /// Returns a compact ABI tag byte stored in `RawWindowHandle::MiniAppCanvas`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - QuickApp vendor variant.
    ///
    /// # Returns
    ///
    /// - `u8` - Numeric vendor discriminant (`101` Alliance, `102` Huawei).
    #[must_use]
    pub const fn tag(&self) -> u8 {
        match *self {
            Self::Alliance => 101,
            Self::Huawei => 102,
        }
    }

    /// Returns both supported QuickApp runtime vendors.
    ///
    /// # Returns
    ///
    /// - `[Self; 2]` - `[Alliance, Huawei]`.
    #[must_use]
    pub const fn all_vendors() -> [Self; 2] {
        [Self::Alliance, Self::Huawei]
    }
}

/// Manifest feature name helpers for `QuickAppFeatureModule`.
impl QuickAppFeatureModule {
    /// Returns the canonical `manifest.json` `"features"` name string (e.g. `"system.router"`).
    ///
    /// # Arguments
    ///
    /// - `&Self` - QuickApp feature module variant.
    ///
    /// # Returns
    ///
    /// - `&'static str` - Manifest feature identifier.
    #[must_use]
    pub const fn manifest_name(&self) -> &'static str {
        match *self {
            Self::Router => QUICKAPP_FEATURE_ROUTER,
            Self::Storage => QUICKAPP_FEATURE_STORAGE,
            Self::Clipboard => QUICKAPP_FEATURE_CLIPBOARD,
            Self::Device => QUICKAPP_FEATURE_DEVICE,
            Self::Network => QUICKAPP_FEATURE_NETWORK,
            Self::Vibrator => QUICKAPP_FEATURE_VIBRATOR,
            Self::Prompt => QUICKAPP_FEATURE_PROMPT,
            Self::Share => QUICKAPP_FEATURE_SHARE,
        }
    }

    /// Returns the complete baseline set of QuickApp manifest features required by EUV hosts.
    ///
    /// # Returns
    ///
    /// - `[Self; 8]` - All 8 core QuickApp system features.
    #[must_use]
    pub const fn core_features() -> [Self; 8] {
        [
            Self::Router,
            Self::Storage,
            Self::Clipboard,
            Self::Device,
            Self::Network,
            Self::Vibrator,
            Self::Prompt,
            Self::Share,
        ]
    }
}

/// Default `QuickAppDeviceMetrics` modeling a 1080x2400@3x QuickApp Alliance device.
impl Default for QuickAppDeviceMetrics {
    /// Creates default QuickApp Alliance display metrics.
    ///
    /// # Returns
    ///
    /// - `Self` - Default `QuickAppDeviceMetrics`.
    fn default() -> Self {
        Self::for_vendor(QuickAppVendor::Alliance)
    }
}

/// Preset and logical-size helpers for `QuickAppDeviceMetrics`.
impl QuickAppDeviceMetrics {
    /// Creates canonical display metrics for `vendor`.
    ///
    /// # Arguments
    ///
    /// - `QuickAppVendor` - QuickApp runtime vendor.
    ///
    /// # Returns
    ///
    /// - `Self` - Preset device metrics.
    #[must_use]
    pub const fn for_vendor(vendor: QuickAppVendor) -> Self {
        Self {
            vendor,
            screen_width_px: QUICKAPP_DEFAULT_SCREEN_W_PX,
            screen_height_px: QUICKAPP_DEFAULT_SCREEN_H_PX,
            screen_density: QUICKAPP_DEFAULT_DENSITY,
            design_width: QUICKAPP_DESIGN_WIDTH_PX,
            status_bar_height_px: QUICKAPP_DEFAULT_STATUS_BAR_PX,
            nav_bar_height_px: QUICKAPP_DEFAULT_NAV_BAR_PX,
            theme: ThemeMode::Light,
        }
    }

    /// Computes logical viewport width in dp (`screen_width_px / screen_density`).
    ///
    /// # Arguments
    ///
    /// - `&Self` - Device metrics reference.
    ///
    /// # Returns
    ///
    /// - `u32` - Logical viewport width in dp (`>= 1`).
    #[must_use]
    pub fn logical_width(&self) -> u32 {
        let d: f32 = self.get_screen_density().max(0.75);
        ((self.get_screen_width_px().max(1) as f32) / d)
            .round()
            .max(1.0) as u32
    }

    /// Computes logical viewport height in dp (`screen_height_px / screen_density`).
    ///
    /// # Arguments
    ///
    /// - `&Self` - Device metrics reference.
    ///
    /// # Returns
    ///
    /// - `u32` - Logical viewport height in dp (`>= 1`).
    #[must_use]
    pub fn logical_height(&self) -> u32 {
        let d: f32 = self.get_screen_density().max(0.75);
        ((self.get_screen_height_px().max(1) as f32) / d)
            .round()
            .max(1.0) as u32
    }
}

/// Default `QuickAppPlatform` configured for QuickApp Alliance (`QuickAppVendor::Alliance`).
impl Default for QuickAppPlatform {
    /// Creates a default QuickApp Alliance host adapter.
    ///
    /// # Returns
    ///
    /// - `Self` - Default `QuickAppPlatform`.
    fn default() -> Self {
        Self::for_vendor(QuickAppVendor::Alliance)
    }
}

/// Vendor constructors, touch/IME/lifecycle handlers, and `@system.*` command queue helpers for `QuickAppPlatform`.
impl QuickAppPlatform {
    /// Creates a `QuickAppPlatform` configured for `vendor`.
    ///
    /// # Arguments
    ///
    /// - `QuickAppVendor` - QuickApp runtime vendor (`Alliance` or `Huawei`).
    ///
    /// # Returns
    ///
    /// - `Self` - Configured QuickApp backend.
    #[must_use]
    pub fn for_vendor(vendor: QuickAppVendor) -> Self {
        Self::from_metrics(QuickAppDeviceMetrics::for_vendor(vendor))
    }

    /// Creates a `QuickAppPlatform` from explicit `@system.device` screen metrics.
    ///
    /// # Arguments
    ///
    /// - `QuickAppDeviceMetrics` - Reported device screen metrics.
    ///
    /// # Returns
    ///
    /// - `Self` - Configured QuickApp backend.
    #[must_use]
    pub fn from_metrics(device_metrics: QuickAppDeviceMetrics) -> Self {
        let safe_area: SafeAreaInsets = resolve_quickapp_safe_area(&device_metrics);
        Self {
            device_metrics,
            safe_area,
            next_id: 1,
            primary_window: None,
            windows: HashMap::new(),
            touch_tracker: TouchPointerTracker::default(),
            pending_events: Vec::new(),
            outbound_commands: Vec::new(),
            storage: HashMap::new(),
            route_stack: vec![QUICKAPP_DEFAULT_ROUTE.to_string()],
            clipboard: None,
            pump_paused: false,
        }
    }

    /// Returns the primary QuickApp canvas `WindowId`, if created.
    ///
    /// # Arguments
    ///
    /// - `&Self` - QuickApp platform reference.
    ///
    /// # Returns
    ///
    /// - `Option<WindowId>` - Primary window handle.
    #[must_use]
    pub fn primary_window_id(&self) -> Option<WindowId> {
        let Self { primary_window, .. } = self;
        *primary_window
    }

    /// Drains all queued outbound `@system.*` host commands for execution by the UX shell.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable QuickApp platform.
    ///
    /// # Returns
    ///
    /// - `Vec<QuickAppHostCommand>` - Ordered host commands.
    pub fn drain_host_commands(&mut self) -> Vec<QuickAppHostCommand> {
        take(self.get_mut_outbound_commands())
    }

    /// Translates QuickApp `<canvas>` touch coordinates (in `750px` design space) into EUV logical dp pointer events.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable QuickApp platform.
    /// - `i32` - Normalized touch action (`ANDROID_ACTION_DOWN`..`ANDROID_ACTION_CANCEL`).
    /// - `&[(i32, f32, f32)]` - Touch tuples `(pointer_id, design_x, design_y)`.
    ///
    /// # Returns
    ///
    /// - `Vec<WindowEvent>` - Normalized pointer events also enqueued for `poll_events()`.
    pub fn handle_design_touch(
        &mut self,
        action: i32,
        design_touches: &[(i32, f32, f32)],
    ) -> Vec<WindowEvent> {
        let wid: WindowId = self.primary_window_id().unwrap_or(WindowId::new(1));
        let metrics: QuickAppDeviceMetrics = self.get_device_metrics();
        let mut logical: Vec<(i32, f32, f32)> = Vec::with_capacity(design_touches.len());
        for &(pid, dx, dy) in design_touches.iter() {
            logical.push((
                pid,
                quickapp_design_px_to_dp(dx, &metrics),
                quickapp_design_px_to_dp(dy, &metrics),
            ));
        }
        let events: Vec<WindowEvent> = self
            .get_mut_touch_tracker()
            .handle_android_motion(wid, action, 0, &logical);
        self.get_mut_pending_events().extend(events.clone());
        events
    }

    /// Dispatches UTF-8 text received from a QuickApp `<input>` bridge into EUV `TextInput` / `Ime` events.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable QuickApp platform.
    /// - `String` - Committed UTF-8 text.
    pub fn handle_ime_commit(&mut self, text: String) {
        if text.is_empty() {
            return;
        }
        let wid: WindowId = self.primary_window_id().unwrap_or(WindowId::new(1));
        self.get_mut_pending_events()
            .push(WindowEvent::Ime(wid, ImeEvent::Commit(text.clone())));
        self.get_mut_pending_events()
            .push(WindowEvent::TextInput(wid, text));
    }

    /// Translates a QuickApp `app.ux` / `page.ux` lifecycle callback into canonical EUV `SystemEvent`s.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable QuickApp platform.
    /// - `QuickAppLifecycleHook` - QuickApp lifecycle hook payload.
    ///
    /// # Returns
    ///
    /// - `Vec<WindowEvent>` - Normalized events also enqueued for `poll_events()`.
    pub fn handle_lifecycle_hook(&mut self, hook: QuickAppLifecycleHook) -> Vec<WindowEvent> {
        let wid: WindowId = self.primary_window_id().unwrap_or(WindowId::new(1));
        let mut emitted: Vec<WindowEvent> = Vec::new();
        match hook {
            QuickAppLifecycleHook::OnCreate { deep_link } => {
                self.set_pump_paused(false);
                if let Some(uri) = deep_link
                    && !uri.is_empty()
                {
                    emitted.push(WindowEvent::System(SystemEvent::DeepLink(uri)));
                }
            }
            QuickAppLifecycleHook::OnShow => {
                self.set_pump_paused(false);
                emitted.push(WindowEvent::System(SystemEvent::Appear));
                emitted.push(WindowEvent::System(SystemEvent::Foreground));
            }
            QuickAppLifecycleHook::OnHide => {
                self.set_pump_paused(true);
                let cancels: Vec<WindowEvent> =
                    self.get_mut_touch_tracker().cancel_all_on_focus_loss(wid);
                emitted.extend(cancels);
                emitted.push(WindowEvent::System(SystemEvent::Disappear));
                emitted.push(WindowEvent::System(SystemEvent::Background));
            }
            QuickAppLifecycleHook::OnDestroy => {
                self.set_pump_paused(true);
                let cancels: Vec<WindowEvent> =
                    self.get_mut_touch_tracker().cancel_all_on_focus_loss(wid);
                emitted.extend(cancels);
                emitted.push(WindowEvent::System(SystemEvent::Terminate));
            }
            QuickAppLifecycleHook::OnBackPress => {
                emitted.push(WindowEvent::KeyDown(
                    wid,
                    KeyEvent::new(KeyCode::Back, Modifiers::default(), false),
                ));
                emitted.push(WindowEvent::System(SystemEvent::BackPressed));
            }
            QuickAppLifecycleHook::OnConfigurationChanged { theme, locale } => {
                self.get_mut_device_metrics().set_theme(theme);
                emitted.push(WindowEvent::ThemeChanged(wid, theme));
                emitted.push(WindowEvent::System(SystemEvent::ThemeChanged(theme)));
                if !locale.trim().is_empty() {
                    emitted.push(WindowEvent::System(SystemEvent::LocaleChanged(locale)));
                }
            }
            QuickAppLifecycleHook::OnMemoryWarning(level) => {
                let pressure: MemoryPressure = if level >= 2 {
                    MemoryPressure::Critical
                } else if level > 0 {
                    MemoryPressure::Warning
                } else {
                    MemoryPressure::Normal
                };
                emitted.push(WindowEvent::System(SystemEvent::MemoryWarning(pressure)));
            }
            QuickAppLifecycleHook::OnKeyboardChanged { visible, height_px } => {
                let density: f32 = self.get_device_metrics().get_screen_density().max(0.75);
                let height_dp: f32 = if visible {
                    (height_px as f32) / density
                } else {
                    0.0
                };
                emitted.push(WindowEvent::System(
                    SystemEvent::KeyboardVisibilityChanged {
                        visible,
                        height: height_dp,
                    },
                ));
            }
        }
        self.get_mut_pending_events().extend(emitted.clone());
        emitted
    }
}

/// `WindowController` implementation for `QuickAppPlatform`.
impl WindowController for QuickAppPlatform {
    /// Applies a `WindowCommand` against the QuickApp `<canvas>` window record.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable QuickApp platform.
    /// - `WindowCommand` - Command to apply.
    ///
    /// # Returns
    ///
    /// - `Result<(), PlatformError>` - Execution status.
    fn apply_command(&mut self, command: WindowCommand) -> Result<(), PlatformError> {
        match command {
            WindowCommand::SetTitle(id, title) => {
                let rec: &mut QuickAppWindowRecord = self
                    .get_mut_windows()
                    .get_mut(&id)
                    .ok_or(PlatformError::WindowNotFound(id.get_value()))?;
                rec.get_mut_config().set_title(title.clone());
                self.get_mut_outbound_commands()
                    .push(QuickAppHostCommand::SetTitleBar(title));
                Ok(())
            }
            WindowCommand::RequestRedraw(id) => {
                if !self.get_windows().contains_key(&id) {
                    return Err(PlatformError::WindowNotFound(id.get_value()));
                }
                self.get_mut_pending_events()
                    .push(WindowEvent::RedrawRequested(id));
                Ok(())
            }
            WindowCommand::Close(id) => {
                if self.get_mut_windows().remove(&id).is_none() {
                    return Err(PlatformError::WindowNotFound(id.get_value()));
                }
                Ok(())
            }
            WindowCommand::SetSize(id, _, _)
            | WindowCommand::SetPosition(id, _, _)
            | WindowCommand::SetResizable(id, _)
            | WindowCommand::SetDecorations(id, _)
            | WindowCommand::SetAlwaysOnTop(id, _)
            | WindowCommand::SetFullscreen(id, _)
            | WindowCommand::SetVisible(id, _)
            | WindowCommand::SetCursor(id, _)
            | WindowCommand::SetIme(id, _, _)
            | WindowCommand::SetStatusBarStyle(id, _)
            | WindowCommand::SetRenderMode(id, _) => {
                if !self.get_windows().contains_key(&id) {
                    return Err(PlatformError::WindowNotFound(id.get_value()));
                }
                Ok(())
            }
        }
    }
}

/// `PlatformBackend` implementation for `QuickAppPlatform`.
impl PlatformBackend for QuickAppPlatform {
    /// Returns `PlatformKind::QuickApp`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - QuickApp platform reference.
    ///
    /// # Returns
    ///
    /// - `PlatformKind` - `PlatformKind::QuickApp`.
    fn kind(&self) -> PlatformKind {
        PlatformKind::QuickApp
    }

    /// Returns QuickApp capability flags.
    ///
    /// # Arguments
    ///
    /// - `&Self` - QuickApp platform reference.
    ///
    /// # Returns
    ///
    /// - `PlatformCapabilities` - QuickApp capability matrix.
    fn capabilities(&self) -> PlatformCapabilities {
        PlatformCapabilities::for_platform(PlatformKind::QuickApp)
    }

    /// Creates a QuickApp `<canvas>` window sized to the normalized logical screen dimensions.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable QuickApp platform.
    /// - `&WindowConfig` - Declarative window config.
    ///
    /// # Returns
    ///
    /// - `Result<WindowState, PlatformError>` - Created QuickApp window state.
    fn create_window(&mut self, config: &WindowConfig) -> Result<WindowState, PlatformError> {
        let id: WindowId = WindowId::new(self.get_next_id());
        self.set_next_id(self.get_next_id().wrapping_add(1));
        let metrics: QuickAppDeviceMetrics = self.get_device_metrics();
        let mut qa_cfg: WindowConfig = config.clone();
        qa_cfg.set_size(metrics.logical_width(), metrics.logical_height());
        let mut state: WindowState =
            WindowState::from_config(id, &qa_cfg, metrics.get_screen_density());
        state.set_safe_area(self.get_safe_area());
        state.set_system_theme(metrics.get_theme());
        let record: QuickAppWindowRecord = QuickAppWindowRecord {
            config: qa_cfg,
            state,
            canvas_id: id.get_value() as u32,
            last_frame: Framebuffer::allocate(
                state.get_physical_width(),
                state.get_physical_height(),
            ),
            present_count: 0,
        };
        self.get_mut_windows().insert(id, record);
        if self.primary_window_id().is_none() {
            self.set_primary_window(Some(id));
        }
        Ok(state)
    }

    /// Queries live `WindowState` for `id`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - QuickApp platform reference.
    /// - `WindowId` - Window identifier.
    ///
    /// # Returns
    ///
    /// - `Option<WindowState>` - Window state if alive.
    fn window_state(&self, id: WindowId) -> Option<WindowState> {
        self.get_windows()
            .get(&id)
            .map(QuickAppWindowRecord::get_state)
    }

    /// Returns `RawWindowHandle::MiniAppCanvas` tagged with the active `QuickAppVendor`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - QuickApp platform reference.
    /// - `WindowId` - Window identifier.
    ///
    /// # Returns
    ///
    /// - `RawWindowHandle` - QuickApp canvas handle.
    fn raw_window_handle(&self, id: WindowId) -> RawWindowHandle {
        match self.get_windows().get(&id) {
            Some(rec) => RawWindowHandle::MiniAppCanvas {
                canvas_id: rec.get_canvas_id(),
                vendor_tag: self.get_device_metrics().get_vendor().tag(),
            },
            None => RawWindowHandle::Headless,
        }
    }

    /// Drains queued QuickApp touch, IME, back-press, and lifecycle events.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable QuickApp platform.
    ///
    /// # Returns
    ///
    /// - `Vec<WindowEvent>` - Pending events.
    fn poll_events(&mut self) -> Vec<WindowEvent> {
        take(self.get_mut_pending_events())
    }

    /// Pauses or resumes the QuickApp canvas frame pump flag.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable QuickApp platform.
    /// - `bool` - `true` to pause for background freeze.
    fn set_frame_pump_paused(&mut self, paused: bool) {
        self.set_pump_paused(paused);
    }

    /// Returns whether the QuickApp frame pump is paused.
    ///
    /// # Arguments
    ///
    /// - `&Self` - QuickApp platform reference.
    ///
    /// # Returns
    ///
    /// - `bool` - Pause state.
    fn is_frame_pump_paused(&self) -> bool {
        self.get_pump_paused()
    }

    /// Stores `framebuffer` and queues `QuickAppHostCommand::PresentCanvas`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable QuickApp platform.
    /// - `WindowId` - Window identifier.
    /// - `&Framebuffer` - Rendered RGBA8 framebuffer.
    ///
    /// # Returns
    ///
    /// - `Result<(), PlatformError>` - Presentation status.
    fn present_framebuffer(
        &mut self,
        id: WindowId,
        framebuffer: &Framebuffer,
    ) -> Result<(), PlatformError> {
        let rec: &mut QuickAppWindowRecord = self
            .get_mut_windows()
            .get_mut(&id)
            .ok_or(PlatformError::WindowNotFound(id.get_value()))?;
        rec.set_last_frame(framebuffer.clone());
        rec.set_present_count(rec.get_present_count().wrapping_add(1));
        self.get_mut_outbound_commands()
            .push(QuickAppHostCommand::PresentCanvas {
                width: framebuffer.get_width(),
                height: framebuffer.get_height(),
            });
        Ok(())
    }

    /// Reads mirrored `@system.clipboard` text.
    ///
    /// # Arguments
    ///
    /// - `&Self` - QuickApp platform reference.
    ///
    /// # Returns
    ///
    /// - `Option<String>` - Cached clipboard string.
    fn read_clipboard(&self) -> Option<String> {
        let Self { clipboard, .. } = self;
        clipboard.clone()
    }

    /// Writes `text` to clipboard cache and queues `QuickAppHostCommand::ClipboardSet`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable QuickApp platform.
    /// - `&str` - Clipboard text.
    ///
    /// # Returns
    ///
    /// - `Result<(), PlatformError>` - Write status.
    fn write_clipboard(&mut self, text: &str) -> Result<(), PlatformError> {
        self.set_clipboard(Some(text.to_string()));
        self.get_mut_outbound_commands()
            .push(QuickAppHostCommand::ClipboardSet(text.to_string()));
        Ok(())
    }

    /// Returns active QuickApp appearance mode.
    ///
    /// # Arguments
    ///
    /// - `&Self` - QuickApp platform reference.
    ///
    /// # Returns
    ///
    /// - `ThemeMode` - Active appearance mode.
    fn system_theme(&self) -> ThemeMode {
        self.get_device_metrics().get_theme()
    }
}

/// `PlatformStorage` implementation backed by `@system.storage` commands.
impl PlatformStorage for QuickAppPlatform {
    /// Reads `key` from mirrored `@system.storage`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - QuickApp platform reference.
    /// - `&str` - Storage key.
    ///
    /// # Returns
    ///
    /// - `Option<String>` - Stored value, if present.
    fn storage_get(&self, key: &str) -> Option<String> {
        self.get_storage().get(key).cloned()
    }

    /// Persists `(key, value)` and emits `QuickAppHostCommand::StorageSet`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable QuickApp platform.
    /// - `&str` - Storage key.
    /// - `&str` - Value to store.
    ///
    /// # Returns
    ///
    /// - `Result<(), PlatformError>` - Storage status.
    fn storage_set(&mut self, key: &str, value: &str) -> Result<(), PlatformError> {
        let _: Option<String> = self
            .get_mut_storage()
            .insert(key.to_string(), value.to_string());
        self.get_mut_outbound_commands()
            .push(QuickAppHostCommand::StorageSet {
                key: key.to_string(),
                value: value.to_string(),
            });
        Ok(())
    }

    /// Removes `key` and emits `QuickAppHostCommand::StorageDelete`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable QuickApp platform.
    /// - `&str` - Storage key.
    ///
    /// # Returns
    ///
    /// - `Result<(), PlatformError>` - Removal status.
    fn storage_remove(&mut self, key: &str) -> Result<(), PlatformError> {
        let _: Option<String> = self.get_mut_storage().remove(key);
        self.get_mut_outbound_commands()
            .push(QuickAppHostCommand::StorageDelete(key.to_string()));
        Ok(())
    }

    /// Clears storage and emits `QuickAppHostCommand::StorageClear`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable QuickApp platform.
    ///
    /// # Returns
    ///
    /// - `Result<(), PlatformError>` - Clear status.
    fn storage_clear(&mut self) -> Result<(), PlatformError> {
        self.get_mut_storage().clear();
        self.get_mut_outbound_commands()
            .push(QuickAppHostCommand::StorageClear);
        Ok(())
    }
}

/// `PlatformNavigation` implementation backed by `@system.router` commands.
impl PlatformNavigation for QuickAppPlatform {
    /// Pushes `route` and emits `QuickAppHostCommand::RouterPush`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable QuickApp platform.
    /// - `&str` - Target page route URI.
    ///
    /// # Returns
    ///
    /// - `Result<(), PlatformError>` - Navigation status.
    fn navigate_to(&mut self, route: &str) -> Result<(), PlatformError> {
        if !route.is_empty() {
            self.get_mut_route_stack().push(route.to_string());
            self.get_mut_outbound_commands()
                .push(QuickAppHostCommand::RouterPush(route.to_string()));
        }
        Ok(())
    }

    /// Pops `delta` entries and emits `QuickAppHostCommand::RouterBack`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable QuickApp platform.
    /// - `u32` - Pages to navigate back.
    ///
    /// # Returns
    ///
    /// - `Result<(), PlatformError>` - Navigation status.
    fn navigate_back(&mut self, delta: u32) -> Result<(), PlatformError> {
        let steps: u32 = delta.max(1);
        for _ in 0..steps {
            if self.get_route_stack().len() > 1 {
                let _: Option<String> = self.get_mut_route_stack().pop();
            }
        }
        self.get_mut_outbound_commands()
            .push(QuickAppHostCommand::RouterBack(steps));
        Ok(())
    }

    /// Returns the currently active QuickApp page route URI.
    ///
    /// # Arguments
    ///
    /// - `&Self` - QuickApp platform reference.
    ///
    /// # Returns
    ///
    /// - `&str` - Active page route.
    fn current_route(&self) -> &str {
        self.get_route_stack()
            .last()
            .map(String::as_str)
            .unwrap_or(QUICKAPP_DEFAULT_ROUTE)
    }
}

/// `PlatformLifecycleBridge` implementation for `QuickAppPlatform`.
impl PlatformLifecycleBridge for QuickAppPlatform {
    /// Maps QuickApp `onHide` into unified freeze semantics.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable QuickApp platform.
    ///
    /// # Returns
    ///
    /// - `Vec<WindowEvent>` - Normalized freeze events.
    fn freeze_surface(&mut self) -> Vec<WindowEvent> {
        self.handle_lifecycle_hook(QuickAppLifecycleHook::OnHide)
    }

    /// Maps QuickApp `onShow` into unified resume semantics.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable QuickApp platform.
    ///
    /// # Returns
    ///
    /// - `Vec<WindowEvent>` - Normalized resume events.
    fn resume_surface(&mut self) -> Vec<WindowEvent> {
        self.handle_lifecycle_hook(QuickAppLifecycleHook::OnShow)
    }

    /// Maps QuickApp `onDestroy` into unified destroy semantics.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable QuickApp platform.
    ///
    /// # Returns
    ///
    /// - `Vec<WindowEvent>` - Normalized destroy events.
    fn destroy_surface(&mut self) -> Vec<WindowEvent> {
        self.handle_lifecycle_hook(QuickAppLifecycleHook::OnDestroy)
    }
}

/// `PlatformHostAdapter` implementation for `QuickAppPlatform`.
impl PlatformHostAdapter for QuickAppPlatform {
    /// Returns `DeviceFormFactor::MiniProgram`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - QuickApp platform reference.
    ///
    /// # Returns
    ///
    /// - `DeviceFormFactor` - `DeviceFormFactor::MiniProgram`.
    fn device_form_factor(&self) -> DeviceFormFactor {
        DeviceFormFactor::MiniProgram
    }

    /// Returns `"quickapp-alliance"` or `"quickapp-huawei"`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - QuickApp platform reference.
    ///
    /// # Returns
    ///
    /// - `&'static str` - Canonical QuickApp vendor identifier.
    fn host_vendor_name(&self) -> &'static str {
        self.get_device_metrics().get_vendor().as_str()
    }
}