Skip to main content

euv_core/platform/info/
impl.rs

1use super::*;
2
3/// Metadata queries for `PlatformKind`.
4impl PlatformKind {
5    /// Returns the canonical lowercase name of this platform.
6    ///
7    /// # Arguments
8    ///
9    /// - `&Self` - Platform kind.
10    ///
11    /// # Returns
12    ///
13    /// - `&'static str` - Canonical platform name string.
14    #[must_use]
15    pub const fn as_str(&self) -> &'static str {
16        match *self {
17            Self::Web => PLATFORM_NAME_WEB,
18            Self::Android => PLATFORM_NAME_ANDROID,
19            Self::Ios => PLATFORM_NAME_IOS,
20            Self::IpadOs => PLATFORM_NAME_IPADOS,
21            Self::MacOs => PLATFORM_NAME_MACOS,
22            Self::Windows => PLATFORM_NAME_WINDOWS,
23            Self::Linux => PLATFORM_NAME_LINUX,
24            Self::HarmonyOs => PLATFORM_NAME_HARMONY_OS,
25            Self::Wearable => PLATFORM_NAME_WEARABLE,
26            Self::MiniApp => PLATFORM_NAME_MINIAPP,
27            Self::QuickApp => PLATFORM_NAME_QUICKAPP,
28            Self::Terminal => PLATFORM_NAME_TERMINAL,
29            Self::TerminalCli => PLATFORM_NAME_TERMINAL_CLI,
30            Self::Headless => PLATFORM_NAME_HEADLESS,
31        }
32    }
33
34    /// Parses a canonical platform identifier string into a `PlatformKind`.
35    ///
36    /// # Arguments
37    ///
38    /// - `&str` - Platform identifier string (case-insensitive).
39    ///
40    /// # Returns
41    ///
42    /// - `Option<Self>` - Matching `PlatformKind`, if recognized.
43    #[must_use]
44    pub fn from_str_id(name: &str) -> Option<Self> {
45        let lower: String = name.trim().to_ascii_lowercase();
46        match lower.as_str() {
47            PLATFORM_NAME_WEB => Some(Self::Web),
48            PLATFORM_NAME_ANDROID => Some(Self::Android),
49            PLATFORM_NAME_IOS => Some(Self::Ios),
50            PLATFORM_NAME_IPADOS => Some(Self::IpadOs),
51            PLATFORM_NAME_MACOS => Some(Self::MacOs),
52            PLATFORM_NAME_WINDOWS => Some(Self::Windows),
53            PLATFORM_NAME_LINUX => Some(Self::Linux),
54            PLATFORM_NAME_HARMONY_OS | PLATFORM_ALIAS_OHOS | PLATFORM_ALIAS_OPENHARMONY => {
55                Some(Self::HarmonyOs)
56            }
57            PLATFORM_NAME_WEARABLE | PLATFORM_ALIAS_WATCH => Some(Self::Wearable),
58            PLATFORM_NAME_MINIAPP => Some(Self::MiniApp),
59            PLATFORM_NAME_QUICKAPP => Some(Self::QuickApp),
60            PLATFORM_NAME_TERMINAL | PLATFORM_ALIAS_TUI => Some(Self::Terminal),
61            PLATFORM_NAME_TERMINAL_CLI | PLATFORM_ALIAS_CLI => Some(Self::TerminalCli),
62            PLATFORM_NAME_HEADLESS => Some(Self::Headless),
63            _ => None,
64        }
65    }
66
67    /// Returns the default `DeviceFormFactor` associated with this platform.
68    ///
69    /// # Arguments
70    ///
71    /// - `&Self` - Platform kind.
72    ///
73    /// # Returns
74    ///
75    /// - `DeviceFormFactor` - Device form factor.
76    #[must_use]
77    pub const fn form_factor(&self) -> DeviceFormFactor {
78        match *self {
79            Self::MacOs | Self::Windows | Self::Linux => DeviceFormFactor::Desktop,
80            Self::IpadOs => DeviceFormFactor::Tablet,
81            Self::Ios | Self::Android | Self::HarmonyOs => DeviceFormFactor::Phone,
82            Self::Wearable => DeviceFormFactor::Watch,
83            Self::MiniApp | Self::QuickApp => DeviceFormFactor::MiniProgram,
84            Self::Terminal | Self::TerminalCli => DeviceFormFactor::Terminal,
85            Self::Web => DeviceFormFactor::Browser,
86            Self::Headless => DeviceFormFactor::Headless,
87        }
88    }
89
90    /// Returns the default hardware or software `GraphicsBackend` associated with this platform.
91    ///
92    /// # Arguments
93    ///
94    /// - `&Self` - Platform kind.
95    ///
96    /// # Returns
97    ///
98    /// - `GraphicsBackend` - Preferred graphics backend.
99    #[must_use]
100    pub const fn default_graphics_backend(&self) -> GraphicsBackend {
101        match *self {
102            Self::MacOs | Self::Ios | Self::IpadOs => GraphicsBackend::Metal,
103            Self::Windows => GraphicsBackend::Direct3D12,
104            Self::Linux | Self::Android | Self::HarmonyOs | Self::Wearable => {
105                GraphicsBackend::Vulkan
106            }
107            Self::Web => GraphicsBackend::WebGpu,
108            Self::MiniApp | Self::QuickApp => GraphicsBackend::WebGl2,
109            Self::Terminal | Self::TerminalCli => GraphicsBackend::SoftwareCpu,
110            Self::Headless => GraphicsBackend::Headless,
111        }
112    }
113
114    /// Returns true for mobile or tablet touch-first platforms (`Android`, `Ios`, `IpadOs`, `HarmonyOs`).
115    ///
116    /// # Arguments
117    ///
118    /// - `&Self` - Platform kind.
119    ///
120    /// # Returns
121    ///
122    /// - `bool` - True for mobile/tablet targets.
123    #[must_use]
124    pub const fn is_mobile_or_tablet(&self) -> bool {
125        matches!(
126            *self,
127            Self::Android | Self::Ios | Self::IpadOs | Self::HarmonyOs
128        )
129    }
130
131    /// Returns true when this platform renders to an interactive terminal or CLI stream.
132    ///
133    /// # Arguments
134    ///
135    /// - `&Self` - Platform kind.
136    ///
137    /// # Returns
138    ///
139    /// - `bool` - True for `Terminal` or `TerminalCli`.
140    #[must_use]
141    pub const fn is_terminal(&self) -> bool {
142        matches!(*self, Self::Terminal | Self::TerminalCli)
143    }
144
145    /// Returns true when this platform targets smartwatches or wrist wearables.
146    ///
147    /// # Arguments
148    ///
149    /// - `&Self` - Platform kind.
150    ///
151    /// # Returns
152    ///
153    /// - `bool` - True for `Wearable`.
154    #[must_use]
155    pub const fn is_wearable(&self) -> bool {
156        matches!(*self, Self::Wearable)
157    }
158
159    /// Returns true when this platform runs inside a Mini-Program or QuickApp container.
160    ///
161    /// # Arguments
162    ///
163    /// - `&Self` - Platform kind.
164    ///
165    /// # Returns
166    ///
167    /// - `bool` - True for `MiniApp` or `QuickApp`.
168    #[must_use]
169    pub const fn is_mini_container(&self) -> bool {
170        matches!(*self, Self::MiniApp | Self::QuickApp)
171    }
172
173    /// Returns true when this platform targets HarmonyOS / OpenHarmony.
174    ///
175    /// # Arguments
176    ///
177    /// - `&Self` - Platform kind.
178    ///
179    /// # Returns
180    ///
181    /// - `bool` - True for `HarmonyOs`.
182    #[must_use]
183    pub const fn is_harmony(&self) -> bool {
184        matches!(*self, Self::HarmonyOs)
185    }
186
187    /// Returns the list of all 7 classic desktop, mobile, and web targets supported by euv.
188    ///
189    /// # Returns
190    ///
191    /// - `[Self; 7]` - Array containing Web, Android, iOS, iPadOS, macOS, Windows, and Linux.
192    #[must_use]
193    pub const fn all_targets() -> [Self; 7] {
194        [
195            Self::Web,
196            Self::Android,
197            Self::Ios,
198            Self::IpadOs,
199            Self::MacOs,
200            Self::Windows,
201            Self::Linux,
202        ]
203    }
204
205    /// Returns the complete list of all 13 production execution targets across Desktop, Mobile, HarmonyOS, Wearables, Mini-Programs, QuickApps, and Terminal.
206    ///
207    /// # Returns
208    ///
209    /// - `[Self; 13]` - Complete production platform target list.
210    #[must_use]
211    pub const fn all_supported_targets() -> [Self; 13] {
212        [
213            Self::Web,
214            Self::Android,
215            Self::Ios,
216            Self::IpadOs,
217            Self::MacOs,
218            Self::Windows,
219            Self::Linux,
220            Self::HarmonyOs,
221            Self::Wearable,
222            Self::MiniApp,
223            Self::QuickApp,
224            Self::Terminal,
225            Self::TerminalCli,
226        ]
227    }
228}
229
230/// Capability matrix factory for `PlatformCapabilities`.
231impl PlatformCapabilities {
232    /// Returns the canonical capability matrix for `platform`.
233    ///
234    /// # Arguments
235    ///
236    /// - `PlatformKind` - Target platform.
237    ///
238    /// # Returns
239    ///
240    /// - `Self` - Normalized platform capabilities.
241    #[must_use]
242    pub fn for_platform(platform: PlatformKind) -> Self {
243        match platform {
244            PlatformKind::MacOs => Self {
245                multi_window: true,
246                hover_pointer: true,
247                touch: false,
248                stylus: false,
249                hardware_keyboard: true,
250                safe_area_insets: false,
251                hardware_gpu: true,
252                software_blit: true,
253                clipboard: true,
254                ime: true,
255                dynamic_island: false,
256                live_activities: false,
257                system_tray: true,
258                biometrics: true,
259                haptics: true,
260                protocol_handler: true,
261                accessibility_bridge: true,
262                rotary_input: false,
263                always_on_display: false,
264                capsule_chrome: false,
265                terminal_cells: false,
266                distributed_continuation: false,
267                local_storage: true,
268            },
269            PlatformKind::Windows => Self {
270                multi_window: true,
271                hover_pointer: true,
272                touch: false,
273                stylus: false,
274                hardware_keyboard: true,
275                safe_area_insets: false,
276                hardware_gpu: true,
277                software_blit: true,
278                clipboard: true,
279                ime: true,
280                dynamic_island: false,
281                live_activities: false,
282                system_tray: true,
283                biometrics: true,
284                haptics: false,
285                protocol_handler: true,
286                accessibility_bridge: true,
287                rotary_input: false,
288                always_on_display: false,
289                capsule_chrome: false,
290                terminal_cells: false,
291                distributed_continuation: false,
292                local_storage: true,
293            },
294            PlatformKind::Linux => Self {
295                multi_window: true,
296                hover_pointer: true,
297                touch: false,
298                stylus: false,
299                hardware_keyboard: true,
300                safe_area_insets: false,
301                hardware_gpu: true,
302                software_blit: true,
303                clipboard: true,
304                ime: true,
305                dynamic_island: false,
306                live_activities: false,
307                system_tray: true,
308                biometrics: false,
309                haptics: false,
310                protocol_handler: true,
311                accessibility_bridge: true,
312                rotary_input: false,
313                always_on_display: false,
314                capsule_chrome: false,
315                terminal_cells: false,
316                distributed_continuation: false,
317                local_storage: true,
318            },
319            PlatformKind::IpadOs => Self {
320                multi_window: true,
321                hover_pointer: true,
322                touch: true,
323                stylus: true,
324                hardware_keyboard: true,
325                safe_area_insets: true,
326                hardware_gpu: true,
327                software_blit: true,
328                clipboard: true,
329                ime: true,
330                dynamic_island: false,
331                live_activities: true,
332                system_tray: false,
333                biometrics: true,
334                haptics: true,
335                protocol_handler: true,
336                accessibility_bridge: true,
337                rotary_input: false,
338                always_on_display: false,
339                capsule_chrome: false,
340                terminal_cells: false,
341                distributed_continuation: false,
342                local_storage: true,
343            },
344            PlatformKind::Ios => Self {
345                multi_window: false,
346                hover_pointer: false,
347                touch: true,
348                stylus: true,
349                hardware_keyboard: false,
350                safe_area_insets: true,
351                hardware_gpu: true,
352                software_blit: true,
353                clipboard: true,
354                ime: true,
355                dynamic_island: true,
356                live_activities: true,
357                system_tray: false,
358                biometrics: true,
359                haptics: true,
360                protocol_handler: true,
361                accessibility_bridge: true,
362                rotary_input: false,
363                always_on_display: true,
364                capsule_chrome: false,
365                terminal_cells: false,
366                distributed_continuation: false,
367                local_storage: true,
368            },
369            PlatformKind::Android => Self {
370                multi_window: false,
371                hover_pointer: false,
372                touch: true,
373                stylus: true,
374                hardware_keyboard: false,
375                safe_area_insets: true,
376                hardware_gpu: true,
377                software_blit: true,
378                clipboard: true,
379                ime: true,
380                dynamic_island: false,
381                live_activities: true,
382                system_tray: false,
383                biometrics: true,
384                haptics: true,
385                protocol_handler: true,
386                accessibility_bridge: true,
387                rotary_input: false,
388                always_on_display: true,
389                capsule_chrome: false,
390                terminal_cells: false,
391                distributed_continuation: false,
392                local_storage: true,
393            },
394            PlatformKind::HarmonyOs => Self {
395                multi_window: true,
396                hover_pointer: true,
397                touch: true,
398                stylus: true,
399                hardware_keyboard: true,
400                safe_area_insets: true,
401                hardware_gpu: true,
402                software_blit: true,
403                clipboard: true,
404                ime: true,
405                dynamic_island: false,
406                live_activities: true,
407                system_tray: false,
408                biometrics: true,
409                haptics: true,
410                protocol_handler: true,
411                accessibility_bridge: true,
412                rotary_input: true,
413                always_on_display: true,
414                capsule_chrome: false,
415                terminal_cells: false,
416                distributed_continuation: true,
417                local_storage: true,
418            },
419            PlatformKind::Wearable => Self {
420                multi_window: false,
421                hover_pointer: false,
422                touch: true,
423                stylus: false,
424                hardware_keyboard: false,
425                safe_area_insets: true,
426                hardware_gpu: true,
427                software_blit: true,
428                clipboard: false,
429                ime: true,
430                dynamic_island: false,
431                live_activities: true,
432                system_tray: false,
433                biometrics: false,
434                haptics: true,
435                protocol_handler: true,
436                accessibility_bridge: true,
437                rotary_input: true,
438                always_on_display: true,
439                capsule_chrome: false,
440                terminal_cells: false,
441                distributed_continuation: false,
442                local_storage: true,
443            },
444            PlatformKind::MiniApp => Self {
445                multi_window: false,
446                hover_pointer: false,
447                touch: true,
448                stylus: false,
449                hardware_keyboard: false,
450                safe_area_insets: true,
451                hardware_gpu: true,
452                software_blit: true,
453                clipboard: true,
454                ime: true,
455                dynamic_island: false,
456                live_activities: false,
457                system_tray: false,
458                biometrics: false,
459                haptics: true,
460                protocol_handler: true,
461                accessibility_bridge: true,
462                rotary_input: false,
463                always_on_display: false,
464                capsule_chrome: true,
465                terminal_cells: false,
466                distributed_continuation: true,
467                local_storage: true,
468            },
469            PlatformKind::QuickApp => Self {
470                multi_window: false,
471                hover_pointer: false,
472                touch: true,
473                stylus: false,
474                hardware_keyboard: false,
475                safe_area_insets: true,
476                hardware_gpu: true,
477                software_blit: true,
478                clipboard: true,
479                ime: true,
480                dynamic_island: false,
481                live_activities: false,
482                system_tray: false,
483                biometrics: false,
484                haptics: true,
485                protocol_handler: true,
486                accessibility_bridge: true,
487                rotary_input: false,
488                always_on_display: false,
489                capsule_chrome: true,
490                terminal_cells: false,
491                distributed_continuation: false,
492                local_storage: true,
493            },
494            PlatformKind::Web => Self {
495                multi_window: false,
496                hover_pointer: true,
497                touch: true,
498                stylus: true,
499                hardware_keyboard: true,
500                safe_area_insets: true,
501                hardware_gpu: true,
502                software_blit: true,
503                clipboard: true,
504                ime: true,
505                dynamic_island: false,
506                live_activities: false,
507                system_tray: false,
508                biometrics: true,
509                haptics: true,
510                protocol_handler: true,
511                accessibility_bridge: true,
512                rotary_input: false,
513                always_on_display: false,
514                capsule_chrome: false,
515                terminal_cells: false,
516                distributed_continuation: false,
517                local_storage: true,
518            },
519            PlatformKind::Terminal => Self {
520                multi_window: false,
521                hover_pointer: true,
522                touch: false,
523                stylus: false,
524                hardware_keyboard: true,
525                safe_area_insets: false,
526                hardware_gpu: false,
527                software_blit: false,
528                clipboard: true,
529                ime: true,
530                dynamic_island: false,
531                live_activities: false,
532                system_tray: false,
533                biometrics: false,
534                haptics: false,
535                protocol_handler: false,
536                accessibility_bridge: true,
537                rotary_input: false,
538                always_on_display: false,
539                capsule_chrome: false,
540                terminal_cells: true,
541                distributed_continuation: false,
542                local_storage: false,
543            },
544            PlatformKind::TerminalCli => Self {
545                multi_window: false,
546                hover_pointer: false,
547                touch: false,
548                stylus: false,
549                hardware_keyboard: false,
550                safe_area_insets: false,
551                hardware_gpu: false,
552                software_blit: false,
553                clipboard: false,
554                ime: false,
555                dynamic_island: false,
556                live_activities: false,
557                system_tray: false,
558                biometrics: false,
559                haptics: false,
560                protocol_handler: false,
561                accessibility_bridge: true,
562                rotary_input: false,
563                always_on_display: false,
564                capsule_chrome: false,
565                terminal_cells: true,
566                distributed_continuation: false,
567                local_storage: false,
568            },
569            PlatformKind::Headless => Self {
570                multi_window: true,
571                hover_pointer: true,
572                touch: true,
573                stylus: false,
574                hardware_keyboard: true,
575                safe_area_insets: true,
576                hardware_gpu: false,
577                software_blit: true,
578                clipboard: true,
579                ime: true,
580                dynamic_island: false,
581                live_activities: false,
582                system_tray: true,
583                biometrics: false,
584                haptics: false,
585                protocol_handler: true,
586                accessibility_bridge: true,
587                rotary_input: false,
588                always_on_display: false,
589                capsule_chrome: false,
590                terminal_cells: false,
591                distributed_continuation: false,
592                local_storage: true,
593            },
594        }
595    }
596}
597
598/// Public accessors for `DisplayModeInfo`.
599impl DisplayModeInfo {
600    /// Returns the OS mode identifier.
601    ///
602    /// # Arguments
603    ///
604    /// - `&Self` - Display mode info.
605    ///
606    /// # Returns
607    ///
608    /// - `i32` - Mode identifier.
609    #[must_use]
610    pub fn mode_id(&self) -> i32 {
611        self.get_mode_id()
612    }
613
614    /// Returns the physical resolution `(width, height)` in pixels.
615    ///
616    /// # Arguments
617    ///
618    /// - `&Self` - Display mode info.
619    ///
620    /// # Returns
621    ///
622    /// - `(u32, u32)` - Physical width and height.
623    #[must_use]
624    pub fn physical_size(&self) -> (u32, u32) {
625        (self.get_physical_width(), self.get_physical_height())
626    }
627
628    /// Returns the hardware refresh rate in Hertz.
629    ///
630    /// # Arguments
631    ///
632    /// - `&Self` - Display mode info.
633    ///
634    /// # Returns
635    ///
636    /// - `f32` - Refresh rate in Hertz.
637    #[must_use]
638    pub fn refresh_rate_hz(&self) -> f32 {
639        self.get_refresh_rate_hz()
640    }
641}
642
643/// Public accessors for `FramePacingConfig`.
644impl FramePacingConfig {
645    /// Returns the minimum adaptive refresh rate in Hertz.
646    ///
647    /// # Arguments
648    ///
649    /// - `&Self` - Frame pacing config.
650    ///
651    /// # Returns
652    ///
653    /// - `f32` - Minimum refresh rate in Hertz.
654    #[must_use]
655    pub fn min_hz(&self) -> f32 {
656        self.get_min_hz()
657    }
658
659    /// Returns the maximum adaptive refresh rate in Hertz.
660    ///
661    /// # Arguments
662    ///
663    /// - `&Self` - Frame pacing config.
664    ///
665    /// # Returns
666    ///
667    /// - `f32` - Maximum refresh rate in Hertz.
668    #[must_use]
669    pub fn max_hz(&self) -> f32 {
670        self.get_max_hz()
671    }
672
673    /// Returns the preferred target refresh rate in Hertz.
674    ///
675    /// # Arguments
676    ///
677    /// - `&Self` - Frame pacing config.
678    ///
679    /// # Returns
680    ///
681    /// - `f32` - Preferred refresh rate in Hertz.
682    #[must_use]
683    pub fn preferred_hz(&self) -> f32 {
684        self.get_preferred_hz()
685    }
686
687    /// Returns the target frame interval in seconds.
688    ///
689    /// # Arguments
690    ///
691    /// - `&Self` - Frame pacing config.
692    ///
693    /// # Returns
694    ///
695    /// - `f32` - Frame duration in seconds.
696    #[must_use]
697    pub fn frame_interval_secs(&self) -> f32 {
698        self.get_frame_interval_secs()
699    }
700
701    /// Returns the target frame interval in nanoseconds.
702    ///
703    /// # Arguments
704    ///
705    /// - `&Self` - Frame pacing config.
706    ///
707    /// # Returns
708    ///
709    /// - `u64` - Frame duration in nanoseconds.
710    #[must_use]
711    pub fn frame_interval_nanos(&self) -> u64 {
712        self.get_frame_interval_nanos()
713    }
714}
715
716/// Multi-touch pointer lifecycle tracking and cancellation helpers for `TouchPointerTracker`.
717impl TouchPointerTracker {
718    /// Returns the currently active mapped pointer identifiers.
719    ///
720    /// # Arguments
721    ///
722    /// - `&Self` - Tracker reference.
723    ///
724    /// # Returns
725    ///
726    /// - `&[u64]` - Slice of active pointer IDs.
727    #[must_use]
728    pub fn active_ids(&self) -> &[u64] {
729        self.get_active_pointers().as_slice()
730    }
731
732    /// Returns the count of currently active touch pointers.
733    ///
734    /// # Arguments
735    ///
736    /// - `&Self` - Tracker reference.
737    ///
738    /// # Returns
739    ///
740    /// - `usize` - Active pointer count.
741    #[must_use]
742    pub fn active_count(&self) -> usize {
743        self.get_active_pointers().len()
744    }
745
746    /// Maps an Android `MotionEvent` raw pointer ID (`>= 0`) to a non-zero `euv` pointer identifier (`>= 1`).
747    ///
748    /// # Arguments
749    ///
750    /// - `i32` - Android pointer identifier.
751    ///
752    /// # Returns
753    ///
754    /// - `u64` - Non-zero mapped pointer identifier.
755    #[must_use]
756    pub fn map_android_pointer_id(raw_id: i32) -> u64 {
757        (raw_id.max(0) as u64).saturating_add(1)
758    }
759
760    /// Inserts `pointer_id` into the active contact set if not already present.
761    ///
762    /// # Arguments
763    ///
764    /// - `&Self` - Mutable tracker.
765    /// - `u64` - Mapped pointer identifier.
766    fn track_pointer(&mut self, pointer_id: u64) {
767        if pointer_id != 0 && !self.get_active_pointers().contains(&pointer_id) {
768            self.get_mut_active_pointers().push(pointer_id);
769        }
770    }
771
772    /// Removes `pointer_id` from the active contact set.
773    ///
774    /// # Arguments
775    ///
776    /// - `&Self` - Mutable tracker.
777    /// - `u64` - Mapped pointer identifier.
778    fn untrack_pointer(&mut self, pointer_id: u64) {
779        self.get_mut_active_pointers()
780            .retain(|id: &u64| *id != pointer_id);
781    }
782
783    /// Translates an Android `MotionEvent` (`action_masked`, `action_index`, `pointers`) into normalized `WindowEvent`s.
784    ///
785    /// # Arguments
786    ///
787    /// - `&Self` - Mutable tracker.
788    /// - `WindowId` - Target window handle.
789    /// - `i32` - Masked Android motion action (`ACTION_DOWN`, `ACTION_POINTER_DOWN`, `ACTION_MOVE`, `ACTION_POINTER_UP`, `ACTION_UP`, `ACTION_CANCEL`).
790    /// - `usize` - Pointer index reported by `MotionEvent.getActionIndex()`.
791    /// - `&[(i32, f32, f32)]` - Active pointer tuples `(raw_pointer_id, logical_x, logical_y)` in index order.
792    ///
793    /// # Returns
794    ///
795    /// - `Vec<WindowEvent>` - Ordered events to dispatch to `App`.
796    pub fn handle_android_motion(
797        &mut self,
798        wid: WindowId,
799        action_masked: i32,
800        action_index: usize,
801        pointers: &[(i32, f32, f32)],
802    ) -> Vec<WindowEvent> {
803        let mut events: Vec<WindowEvent> = Vec::new();
804        match action_masked {
805            ANDROID_ACTION_DOWN => {
806                if !self.get_active_pointers().is_empty() {
807                    let stale: Vec<u64> = take(self.get_mut_active_pointers());
808                    for id in stale.into_iter() {
809                        events.push(WindowEvent::PointerCancel(wid, id));
810                    }
811                    events.push(WindowEvent::PointerCancel(wid, 0));
812                }
813                if let Some(&(raw_id, x, y)) = pointers.get(action_index) {
814                    let pid: u64 = Self::map_android_pointer_id(raw_id);
815                    self.track_pointer(pid);
816                    events.push(WindowEvent::PointerDown(
817                        wid,
818                        PointerEvent::touch(pid, x, y),
819                    ));
820                }
821            }
822            ANDROID_ACTION_POINTER_DOWN => {
823                if let Some(&(raw_id, x, y)) = pointers.get(action_index) {
824                    let pid: u64 = Self::map_android_pointer_id(raw_id);
825                    self.track_pointer(pid);
826                    events.push(WindowEvent::PointerDown(
827                        wid,
828                        PointerEvent::touch(pid, x, y),
829                    ));
830                }
831            }
832            ANDROID_ACTION_MOVE => {
833                for &(raw_id, x, y) in pointers.iter() {
834                    let pid: u64 = Self::map_android_pointer_id(raw_id);
835                    self.track_pointer(pid);
836                    events.push(WindowEvent::PointerMove(
837                        wid,
838                        PointerEvent::touch(pid, x, y),
839                    ));
840                }
841            }
842            ANDROID_ACTION_POINTER_UP => {
843                if let Some(&(raw_id, x, y)) = pointers.get(action_index) {
844                    let pid: u64 = Self::map_android_pointer_id(raw_id);
845                    self.untrack_pointer(pid);
846                    events.push(WindowEvent::PointerUp(wid, PointerEvent::touch(pid, x, y)));
847                }
848            }
849            ANDROID_ACTION_UP => {
850                if let Some(&(raw_id, x, y)) = pointers.get(action_index) {
851                    let pid: u64 = Self::map_android_pointer_id(raw_id);
852                    self.untrack_pointer(pid);
853                    events.push(WindowEvent::PointerUp(wid, PointerEvent::touch(pid, x, y)));
854                }
855                self.get_mut_active_pointers().clear();
856            }
857            ANDROID_ACTION_CANCEL => {
858                let stale: Vec<u64> = take(self.get_mut_active_pointers());
859                for id in stale.into_iter() {
860                    events.push(WindowEvent::PointerCancel(wid, id));
861                }
862                events.push(WindowEvent::PointerCancel(wid, 0));
863            }
864            _ => {}
865        }
866        events
867    }
868
869    /// Records iOS `touchesBegan` contacts and produces corresponding `PointerDown` events.
870    ///
871    /// # Arguments
872    ///
873    /// - `&Self` - Mutable tracker.
874    /// - `WindowId` - Target window handle.
875    /// - `&[(u64, f32, f32)]` - Touch tuples `(pointer_id, logical_x, logical_y)`.
876    ///
877    /// # Returns
878    ///
879    /// - `Vec<WindowEvent>` - `PointerDown` events.
880    pub fn handle_ios_touches_began(
881        &mut self,
882        wid: WindowId,
883        touches: &[(u64, f32, f32)],
884    ) -> Vec<WindowEvent> {
885        let mut events: Vec<WindowEvent> = Vec::with_capacity(touches.len());
886        for &(raw_id, x, y) in touches.iter() {
887            let pid: u64 = raw_id.max(1);
888            self.track_pointer(pid);
889            events.push(WindowEvent::PointerDown(
890                wid,
891                PointerEvent::touch(pid, x, y),
892            ));
893        }
894        events
895    }
896
897    /// Handles iOS `touchesCancelled`, emitting `PointerCancel` for every cancelled and stale contact.
898    ///
899    /// # Arguments
900    ///
901    /// - `&Self` - Mutable tracker.
902    /// - `WindowId` - Target window handle.
903    /// - `&[u64]` - Cancelled touch pointer identifiers.
904    /// - `&[u64]` - Remaining active touch pointer identifiers from `UIEvent.allTouches`.
905    ///
906    /// # Returns
907    ///
908    /// - `Vec<WindowEvent>` - Ordered `PointerCancel` events.
909    pub fn handle_ios_touches_cancelled(
910        &mut self,
911        wid: WindowId,
912        cancelled_ids: &[u64],
913        remaining_live_ids: &[u64],
914    ) -> Vec<WindowEvent> {
915        let mut events: Vec<WindowEvent> = Vec::new();
916        for &raw_id in cancelled_ids.iter() {
917            let pid: u64 = raw_id.max(1);
918            self.untrack_pointer(pid);
919            events.push(WindowEvent::PointerCancel(wid, pid));
920        }
921        let stale: Vec<u64> = self
922            .get_active_pointers()
923            .iter()
924            .copied()
925            .filter(|id: &u64| !remaining_live_ids.contains(id))
926            .collect();
927        for id in stale.into_iter() {
928            self.untrack_pointer(id);
929            events.push(WindowEvent::PointerCancel(wid, id));
930        }
931        if self.get_active_pointers().is_empty() {
932            events.push(WindowEvent::PointerCancel(wid, 0));
933        }
934        events
935    }
936
937    /// Cancels all active touches on focus loss, pause, or window detachment so input never wedges.
938    ///
939    /// # Arguments
940    ///
941    /// - `&Self` - Mutable tracker.
942    /// - `WindowId` - Target window handle.
943    ///
944    /// # Returns
945    ///
946    /// - `Vec<WindowEvent>` - Per-pointer and global `PointerCancel` events (empty if no contacts were active).
947    pub fn cancel_all_on_focus_loss(&mut self, wid: WindowId) -> Vec<WindowEvent> {
948        let active: Vec<u64> = take(self.get_mut_active_pointers());
949        if active.is_empty() {
950            return Vec::new();
951        }
952        let mut events: Vec<WindowEvent> = Vec::with_capacity(active.len().saturating_add(1));
953        for id in active.into_iter() {
954            events.push(WindowEvent::PointerCancel(wid, id));
955        }
956        events.push(WindowEvent::PointerCancel(wid, 0));
957        events
958    }
959}
960
961/// Generation-stamped same-process session lifecycle helpers for `MobileSessionLifecycle`.
962impl MobileSessionLifecycle {
963    /// Registers a new host activity or scene session, resetting transient flags and returning its monotonic generation.
964    ///
965    /// # Arguments
966    ///
967    /// - `&Self` - Mutable lifecycle coordinator.
968    /// - `u64` - Unique identifier of the newly created host activity or scene.
969    ///
970    /// # Returns
971    ///
972    /// - `u64` - Monotonic session generation assigned to `owner_id`.
973    pub fn begin_session(&mut self, owner_id: u64) -> u64 {
974        let next_gen: u64 = self.get_session_generation().wrapping_add(1).max(1);
975        self.set_session_generation(next_gen);
976        self.set_active_owner_id(owner_id);
977        self.set_surface_attached(false);
978        self.set_loop_running(false);
979        self.set_app_running(true);
980        self.set_background_frozen(false);
981        self.set_terminated(false);
982        self.get_mut_touch_tracker()
983            .get_mut_active_pointers()
984            .clear();
985        next_gen
986    }
987
988    /// Re-claims bridge ownership when `owner_id` resumes and is either the latest session or the previous owner cleared.
989    ///
990    /// # Arguments
991    ///
992    /// - `&Self` - Mutable lifecycle coordinator.
993    /// - `u64` - Resuming host activity or scene identifier.
994    /// - `u64` - Generation originally assigned to `owner_id`.
995    ///
996    /// # Returns
997    ///
998    /// - `bool` - `true` when `owner_id` holds active bridge ownership after the call.
999    pub fn activate_session(&mut self, owner_id: u64, session_gen: u64) -> bool {
1000        let cur_gen: u64 = self.get_session_generation();
1001        if session_gen >= cur_gen || self.get_active_owner_id() == 0 {
1002            if session_gen > cur_gen {
1003                self.set_session_generation(session_gen);
1004            }
1005            self.set_active_owner_id(owner_id);
1006            return true;
1007        }
1008        self.get_active_owner_id() == owner_id && cur_gen == session_gen
1009    }
1010
1011    /// Ends `owner_id`'s session only when `(owner_id, session_gen)` still owns the bridge, preventing delayed teardown races.
1012    ///
1013    /// # Arguments
1014    ///
1015    /// - `&Self` - Mutable lifecycle coordinator.
1016    /// - `u64` - Destroying host activity or scene identifier.
1017    /// - `u64` - Generation originally assigned to `owner_id`.
1018    ///
1019    /// # Returns
1020    ///
1021    /// - `bool` - `true` when ownership belonged to `owner_id` and was cleared; `false` when a newer session already replaced it.
1022    pub fn end_session(&mut self, owner_id: u64, session_gen: u64) -> bool {
1023        if self.get_active_owner_id() != owner_id || self.get_session_generation() != session_gen {
1024            return false;
1025        }
1026        self.set_active_owner_id(0);
1027        self.set_surface_attached(false);
1028        self.set_loop_running(false);
1029        self.set_app_running(false);
1030        self.set_terminated(true);
1031        self.get_mut_touch_tracker()
1032            .get_mut_active_pointers()
1033            .clear();
1034        true
1035    }
1036
1037    /// Returns `true` when `(owner_id, session_gen)` is currently the authoritative foreground bridge owner.
1038    ///
1039    /// # Arguments
1040    ///
1041    /// - `&Self` - Lifecycle coordinator reference.
1042    /// - `u64` - Host activity or scene identifier.
1043    /// - `u64` - Generation assigned to `owner_id`.
1044    ///
1045    /// # Returns
1046    ///
1047    /// - `bool` - `true` when `owner_id` matches the active generation and owner.
1048    #[must_use]
1049    pub fn is_active_owner(&self, owner_id: u64, session_gen: u64) -> bool {
1050        owner_id != 0
1051            && self.get_active_owner_id() == owner_id
1052            && self.get_session_generation() == session_gen
1053    }
1054
1055    /// Returns the current monotonic session generation.
1056    ///
1057    /// # Arguments
1058    ///
1059    /// - `&Self` - Lifecycle coordinator reference.
1060    ///
1061    /// # Returns
1062    ///
1063    /// - `u64` - Current session generation.
1064    #[must_use]
1065    pub fn active_session_generation(&self) -> u64 {
1066        self.get_session_generation()
1067    }
1068
1069    /// Returns the currently active bridge owner identifier (`0` when no activity or scene is active).
1070    ///
1071    /// # Arguments
1072    ///
1073    /// - `&Self` - Lifecycle coordinator reference.
1074    ///
1075    /// # Returns
1076    ///
1077    /// - `u64` - Active host owner identifier.
1078    #[must_use]
1079    pub fn active_owner_id(&self) -> u64 {
1080        self.get_active_owner_id()
1081    }
1082
1083    /// Returns the current VSync loop generation counter.
1084    ///
1085    /// # Arguments
1086    ///
1087    /// - `&Self` - Lifecycle coordinator reference.
1088    ///
1089    /// # Returns
1090    ///
1091    /// - `u64` - Active VSync callback generation.
1092    #[must_use]
1093    pub fn loop_generation(&self) -> u64 {
1094        self.get_loop_generation()
1095    }
1096
1097    /// Returns whether the VSync render loop is currently active.
1098    ///
1099    /// # Arguments
1100    ///
1101    /// - `&Self` - Lifecycle coordinator reference.
1102    ///
1103    /// # Returns
1104    ///
1105    /// - `bool` - `true` when the VSync frame pump is running.
1106    #[must_use]
1107    pub fn is_loop_running(&self) -> bool {
1108        self.get_loop_running()
1109    }
1110
1111    /// Returns whether a native rendering surface is currently attached.
1112    ///
1113    /// # Arguments
1114    ///
1115    /// - `&Self` - Lifecycle coordinator reference.
1116    ///
1117    /// # Returns
1118    ///
1119    /// - `bool` - `true` when the native surface is valid and bound.
1120    #[must_use]
1121    pub fn is_surface_attached(&self) -> bool {
1122        self.get_surface_attached()
1123    }
1124
1125    /// Returns `true` when an existing activity resumed after `Terminate` or exit request and must recreate its native `App` handle.
1126    ///
1127    /// # Arguments
1128    ///
1129    /// - `&Self` - Lifecycle coordinator reference.
1130    ///
1131    /// # Returns
1132    ///
1133    /// - `bool` - `true` when the native handle is no longer running or was terminated.
1134    #[must_use]
1135    pub fn should_recreate_handle(&self) -> bool {
1136        self.get_terminated() || !self.get_app_running()
1137    }
1138
1139    /// Marks the active session's surface attached and recreates the live running state if needed.
1140    ///
1141    /// # Arguments
1142    ///
1143    /// - `&Self` - Mutable lifecycle coordinator.
1144    pub fn on_surface_created(&mut self) {
1145        self.set_surface_attached(true);
1146        if self.should_recreate_handle() {
1147            self.set_app_running(true);
1148            self.set_terminated(false);
1149            self.set_background_frozen(false);
1150        }
1151    }
1152
1153    /// Marks the active surface detached and emits `PointerCancel` events for any active contacts.
1154    ///
1155    /// # Arguments
1156    ///
1157    /// - `&Self` - Mutable lifecycle coordinator.
1158    /// - `WindowId` - Window whose surface detached.
1159    ///
1160    /// # Returns
1161    ///
1162    /// - `Vec<WindowEvent>` - Cancellation events for any dangling touch contacts.
1163    pub fn on_surface_destroyed(&mut self, wid: WindowId) -> Vec<WindowEvent> {
1164        self.set_surface_attached(false);
1165        self.get_mut_touch_tracker().cancel_all_on_focus_loss(wid)
1166    }
1167
1168    /// Transitions the active session to resumed foreground state and returns the unique VSync loop generation.
1169    ///
1170    /// # Arguments
1171    ///
1172    /// - `&Self` - Mutable lifecycle coordinator.
1173    ///
1174    /// # Returns
1175    ///
1176    /// - `u64` - Unique VSync loop generation preventing duplicate frame callbacks.
1177    pub fn on_resume(&mut self) -> u64 {
1178        self.set_background_frozen(false);
1179        if self.should_recreate_handle() {
1180            self.set_app_running(true);
1181            self.set_terminated(false);
1182        }
1183        let next_loop: u64 = self.get_loop_generation().wrapping_add(1).max(1);
1184        self.set_loop_generation(next_loop);
1185        self.set_loop_running(true);
1186        next_loop
1187    }
1188
1189    /// Pauses the active VSync loop, advances the loop generation to invalidate in-flight callbacks, and cancels active touches.
1190    ///
1191    /// # Arguments
1192    ///
1193    /// - `&Self` - Mutable lifecycle coordinator.
1194    /// - `WindowId` - Target window handle.
1195    ///
1196    /// # Returns
1197    ///
1198    /// - `Vec<WindowEvent>` - Cancellation events for any active touch contacts.
1199    pub fn on_pause(&mut self, wid: WindowId) -> Vec<WindowEvent> {
1200        self.set_loop_running(false);
1201        let next_loop: u64 = self.get_loop_generation().wrapping_add(1).max(1);
1202        self.set_loop_generation(next_loop);
1203        self.get_mut_touch_tracker().cancel_all_on_focus_loss(wid)
1204    }
1205
1206    /// Marks the active session background-frozen with the VSync loop halted.
1207    ///
1208    /// # Arguments
1209    ///
1210    /// - `&Self` - Mutable lifecycle coordinator.
1211    pub fn on_background(&mut self) {
1212        self.set_background_frozen(true);
1213        self.set_loop_running(false);
1214    }
1215
1216    /// Records a double-back exit request or OS termination so subsequent re-entry recreates a fresh native handle.
1217    ///
1218    /// # Arguments
1219    ///
1220    /// - `&Self` - Mutable lifecycle coordinator.
1221    pub fn on_terminate(&mut self) {
1222        self.set_app_running(false);
1223        self.set_terminated(true);
1224        self.set_loop_running(false);
1225    }
1226
1227    /// Returns an immutable reference to the session's `TouchPointerTracker`.
1228    ///
1229    /// # Arguments
1230    ///
1231    /// - `&Self` - Lifecycle coordinator reference.
1232    ///
1233    /// # Returns
1234    ///
1235    /// - `&TouchPointerTracker` - Active touch tracker.
1236    #[must_use]
1237    pub fn touch_tracker(&self) -> &TouchPointerTracker {
1238        self.get_touch_tracker()
1239    }
1240
1241    /// Returns a mutable reference to the session's `TouchPointerTracker`.
1242    ///
1243    /// # Arguments
1244    ///
1245    /// - `&Self` - Mutable lifecycle coordinator.
1246    ///
1247    /// # Returns
1248    ///
1249    /// - `&mut TouchPointerTracker` - Mutable touch tracker.
1250    pub fn touch_tracker_mut(&mut self) -> &mut TouchPointerTracker {
1251        self.get_mut_touch_tracker()
1252    }
1253}