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::Headless => PLATFORM_NAME_HEADLESS,
25 }
26 }
27
28 /// Returns the default `DeviceFormFactor` associated with this platform.
29 ///
30 /// # Arguments
31 ///
32 /// - `&Self` - Platform kind.
33 ///
34 /// # Returns
35 ///
36 /// - `DeviceFormFactor` - Device form factor.
37 #[must_use]
38 pub const fn form_factor(&self) -> DeviceFormFactor {
39 match *self {
40 Self::MacOs | Self::Windows | Self::Linux => DeviceFormFactor::Desktop,
41 Self::IpadOs => DeviceFormFactor::Tablet,
42 Self::Ios | Self::Android => DeviceFormFactor::Phone,
43 Self::Web => DeviceFormFactor::Browser,
44 Self::Headless => DeviceFormFactor::Headless,
45 }
46 }
47
48 /// Returns true for mobile or tablet touch-first platforms (`Android`, `Ios`, `IpadOs`).
49 ///
50 /// # Arguments
51 ///
52 /// - `&Self` - Platform kind.
53 ///
54 /// # Returns
55 ///
56 /// - `bool` - True for mobile/tablet targets.
57 #[must_use]
58 pub const fn is_mobile_or_tablet(&self) -> bool {
59 matches!(*self, Self::Android | Self::Ios | Self::IpadOs)
60 }
61
62 /// Returns the list of all 7 production platforms supported by euv.
63 ///
64 /// # Returns
65 ///
66 /// - `[Self; 7]` - Array containing Web, Android, iOS, iPadOS, macOS, Windows, and Linux.
67 #[must_use]
68 pub const fn all_targets() -> [Self; 7] {
69 [
70 Self::Web,
71 Self::Android,
72 Self::Ios,
73 Self::IpadOs,
74 Self::MacOs,
75 Self::Windows,
76 Self::Linux,
77 ]
78 }
79}
80
81/// Capability matrix factory for `PlatformCapabilities`.
82impl PlatformCapabilities {
83 /// Returns the canonical capability matrix for `platform`.
84 ///
85 /// # Arguments
86 ///
87 /// - `PlatformKind` - Target platform.
88 ///
89 /// # Returns
90 ///
91 /// - `Self` - Normalized platform capabilities.
92 #[must_use]
93 pub fn for_platform(platform: PlatformKind) -> Self {
94 match platform {
95 PlatformKind::MacOs => Self {
96 multi_window: true,
97 hover_pointer: true,
98 touch: false,
99 stylus: false,
100 hardware_keyboard: true,
101 safe_area_insets: false,
102 hardware_gpu: true,
103 software_blit: true,
104 clipboard: true,
105 ime: true,
106 dynamic_island: false,
107 live_activities: false,
108 system_tray: true,
109 biometrics: true,
110 haptics: true,
111 protocol_handler: true,
112 accessibility_bridge: true,
113 },
114 PlatformKind::Windows => Self {
115 multi_window: true,
116 hover_pointer: true,
117 touch: false,
118 stylus: false,
119 hardware_keyboard: true,
120 safe_area_insets: false,
121 hardware_gpu: true,
122 software_blit: true,
123 clipboard: true,
124 ime: true,
125 dynamic_island: false,
126 live_activities: false,
127 system_tray: true,
128 biometrics: true,
129 haptics: false,
130 protocol_handler: true,
131 accessibility_bridge: true,
132 },
133 PlatformKind::Linux => Self {
134 multi_window: true,
135 hover_pointer: true,
136 touch: false,
137 stylus: false,
138 hardware_keyboard: true,
139 safe_area_insets: false,
140 hardware_gpu: true,
141 software_blit: true,
142 clipboard: true,
143 ime: true,
144 dynamic_island: false,
145 live_activities: false,
146 system_tray: true,
147 biometrics: false,
148 haptics: false,
149 protocol_handler: true,
150 accessibility_bridge: true,
151 },
152 PlatformKind::IpadOs => Self {
153 multi_window: true,
154 hover_pointer: true,
155 touch: true,
156 stylus: true,
157 hardware_keyboard: true,
158 safe_area_insets: true,
159 hardware_gpu: true,
160 software_blit: true,
161 clipboard: true,
162 ime: true,
163 dynamic_island: false,
164 live_activities: true,
165 system_tray: false,
166 biometrics: true,
167 haptics: true,
168 protocol_handler: true,
169 accessibility_bridge: true,
170 },
171 PlatformKind::Ios => Self {
172 multi_window: false,
173 hover_pointer: false,
174 touch: true,
175 stylus: true,
176 hardware_keyboard: false,
177 safe_area_insets: true,
178 hardware_gpu: true,
179 software_blit: true,
180 clipboard: true,
181 ime: true,
182 dynamic_island: true,
183 live_activities: true,
184 system_tray: false,
185 biometrics: true,
186 haptics: true,
187 protocol_handler: true,
188 accessibility_bridge: true,
189 },
190 PlatformKind::Android => Self {
191 multi_window: false,
192 hover_pointer: false,
193 touch: true,
194 stylus: true,
195 hardware_keyboard: false,
196 safe_area_insets: true,
197 hardware_gpu: true,
198 software_blit: true,
199 clipboard: true,
200 ime: true,
201 dynamic_island: false,
202 live_activities: true,
203 system_tray: false,
204 biometrics: true,
205 haptics: true,
206 protocol_handler: true,
207 accessibility_bridge: true,
208 },
209 PlatformKind::Web => Self {
210 multi_window: false,
211 hover_pointer: true,
212 touch: true,
213 stylus: true,
214 hardware_keyboard: true,
215 safe_area_insets: true,
216 hardware_gpu: true,
217 software_blit: true,
218 clipboard: true,
219 ime: true,
220 dynamic_island: false,
221 live_activities: false,
222 system_tray: false,
223 biometrics: true,
224 haptics: true,
225 protocol_handler: true,
226 accessibility_bridge: true,
227 },
228 PlatformKind::Headless => Self {
229 multi_window: true,
230 hover_pointer: true,
231 touch: true,
232 stylus: false,
233 hardware_keyboard: true,
234 safe_area_insets: true,
235 hardware_gpu: false,
236 software_blit: true,
237 clipboard: true,
238 ime: true,
239 dynamic_island: false,
240 live_activities: false,
241 system_tray: true,
242 biometrics: false,
243 haptics: false,
244 protocol_handler: true,
245 accessibility_bridge: true,
246 },
247 }
248 }
249}
250
251/// Public accessors for `DisplayModeInfo`.
252impl DisplayModeInfo {
253 /// Returns the OS mode identifier.
254 ///
255 /// # Arguments
256 ///
257 /// - `&Self` - Display mode info.
258 ///
259 /// # Returns
260 ///
261 /// - `i32` - Mode identifier.
262 #[must_use]
263 pub fn mode_id(&self) -> i32 {
264 self.get_mode_id()
265 }
266
267 /// Returns the physical resolution `(width, height)` in pixels.
268 ///
269 /// # Arguments
270 ///
271 /// - `&Self` - Display mode info.
272 ///
273 /// # Returns
274 ///
275 /// - `(u32, u32)` - Physical width and height.
276 #[must_use]
277 pub fn physical_size(&self) -> (u32, u32) {
278 (self.get_physical_width(), self.get_physical_height())
279 }
280
281 /// Returns the hardware refresh rate in Hertz.
282 ///
283 /// # Arguments
284 ///
285 /// - `&Self` - Display mode info.
286 ///
287 /// # Returns
288 ///
289 /// - `f32` - Refresh rate in Hertz.
290 #[must_use]
291 pub fn refresh_rate_hz(&self) -> f32 {
292 self.get_refresh_rate_hz()
293 }
294}
295
296/// Public accessors for `FramePacingConfig`.
297impl FramePacingConfig {
298 /// Returns the minimum adaptive refresh rate in Hertz.
299 ///
300 /// # Arguments
301 ///
302 /// - `&Self` - Frame pacing config.
303 ///
304 /// # Returns
305 ///
306 /// - `f32` - Minimum refresh rate in Hertz.
307 #[must_use]
308 pub fn min_hz(&self) -> f32 {
309 self.get_min_hz()
310 }
311
312 /// Returns the maximum adaptive refresh rate in Hertz.
313 ///
314 /// # Arguments
315 ///
316 /// - `&Self` - Frame pacing config.
317 ///
318 /// # Returns
319 ///
320 /// - `f32` - Maximum refresh rate in Hertz.
321 #[must_use]
322 pub fn max_hz(&self) -> f32 {
323 self.get_max_hz()
324 }
325
326 /// Returns the preferred target refresh rate in Hertz.
327 ///
328 /// # Arguments
329 ///
330 /// - `&Self` - Frame pacing config.
331 ///
332 /// # Returns
333 ///
334 /// - `f32` - Preferred refresh rate in Hertz.
335 #[must_use]
336 pub fn preferred_hz(&self) -> f32 {
337 self.get_preferred_hz()
338 }
339
340 /// Returns the target frame interval in seconds.
341 ///
342 /// # Arguments
343 ///
344 /// - `&Self` - Frame pacing config.
345 ///
346 /// # Returns
347 ///
348 /// - `f32` - Frame duration in seconds.
349 #[must_use]
350 pub fn frame_interval_secs(&self) -> f32 {
351 self.get_frame_interval_secs()
352 }
353
354 /// Returns the target frame interval in nanoseconds.
355 ///
356 /// # Arguments
357 ///
358 /// - `&Self` - Frame pacing config.
359 ///
360 /// # Returns
361 ///
362 /// - `u64` - Frame duration in nanoseconds.
363 #[must_use]
364 pub fn frame_interval_nanos(&self) -> u64 {
365 self.get_frame_interval_nanos()
366 }
367}
368
369/// Multi-touch pointer lifecycle tracking and cancellation helpers for `TouchPointerTracker`.
370impl TouchPointerTracker {
371 /// Returns the currently active mapped pointer identifiers.
372 ///
373 /// # Arguments
374 ///
375 /// - `&Self` - Tracker reference.
376 ///
377 /// # Returns
378 ///
379 /// - `&[u64]` - Slice of active pointer IDs.
380 #[must_use]
381 pub fn active_ids(&self) -> &[u64] {
382 self.get_active_pointers().as_slice()
383 }
384
385 /// Returns the count of currently active touch pointers.
386 ///
387 /// # Arguments
388 ///
389 /// - `&Self` - Tracker reference.
390 ///
391 /// # Returns
392 ///
393 /// - `usize` - Active pointer count.
394 #[must_use]
395 pub fn active_count(&self) -> usize {
396 self.get_active_pointers().len()
397 }
398
399 /// Maps an Android `MotionEvent` raw pointer ID (`>= 0`) to a non-zero `euv` pointer identifier (`>= 1`).
400 ///
401 /// # Arguments
402 ///
403 /// - `i32` - Android pointer identifier.
404 ///
405 /// # Returns
406 ///
407 /// - `u64` - Non-zero mapped pointer identifier.
408 #[must_use]
409 pub fn map_android_pointer_id(raw_id: i32) -> u64 {
410 (raw_id.max(0) as u64).saturating_add(1)
411 }
412
413 /// Inserts `pointer_id` into the active contact set if not already present.
414 ///
415 /// # Arguments
416 ///
417 /// - `&Self` - Mutable tracker.
418 /// - `u64` - Mapped pointer identifier.
419 fn track_pointer(&mut self, pointer_id: u64) {
420 if pointer_id != 0 && !self.get_active_pointers().contains(&pointer_id) {
421 self.get_mut_active_pointers().push(pointer_id);
422 }
423 }
424
425 /// Removes `pointer_id` from the active contact set.
426 ///
427 /// # Arguments
428 ///
429 /// - `&Self` - Mutable tracker.
430 /// - `u64` - Mapped pointer identifier.
431 fn untrack_pointer(&mut self, pointer_id: u64) {
432 self.get_mut_active_pointers()
433 .retain(|id: &u64| *id != pointer_id);
434 }
435
436 /// Translates an Android `MotionEvent` (`action_masked`, `action_index`, `pointers`) into normalized `WindowEvent`s.
437 ///
438 /// # Arguments
439 ///
440 /// - `&Self` - Mutable tracker.
441 /// - `WindowId` - Target window handle.
442 /// - `i32` - Masked Android motion action (`ACTION_DOWN`, `ACTION_POINTER_DOWN`, `ACTION_MOVE`, `ACTION_POINTER_UP`, `ACTION_UP`, `ACTION_CANCEL`).
443 /// - `usize` - Pointer index reported by `MotionEvent.getActionIndex()`.
444 /// - `&[(i32, f32, f32)]` - Active pointer tuples `(raw_pointer_id, logical_x, logical_y)` in index order.
445 ///
446 /// # Returns
447 ///
448 /// - `Vec<WindowEvent>` - Ordered events to dispatch to `App`.
449 pub fn handle_android_motion(
450 &mut self,
451 wid: WindowId,
452 action_masked: i32,
453 action_index: usize,
454 pointers: &[(i32, f32, f32)],
455 ) -> Vec<WindowEvent> {
456 let mut events: Vec<WindowEvent> = Vec::new();
457 match action_masked {
458 ANDROID_ACTION_DOWN => {
459 if !self.get_active_pointers().is_empty() {
460 let stale: Vec<u64> = take(self.get_mut_active_pointers());
461 for id in stale.into_iter() {
462 events.push(WindowEvent::PointerCancel(wid, id));
463 }
464 events.push(WindowEvent::PointerCancel(wid, 0));
465 }
466 if let Some(&(raw_id, x, y)) = pointers.get(action_index) {
467 let pid: u64 = Self::map_android_pointer_id(raw_id);
468 self.track_pointer(pid);
469 events.push(WindowEvent::PointerDown(
470 wid,
471 PointerEvent::touch(pid, x, y),
472 ));
473 }
474 }
475 ANDROID_ACTION_POINTER_DOWN => {
476 if let Some(&(raw_id, x, y)) = pointers.get(action_index) {
477 let pid: u64 = Self::map_android_pointer_id(raw_id);
478 self.track_pointer(pid);
479 events.push(WindowEvent::PointerDown(
480 wid,
481 PointerEvent::touch(pid, x, y),
482 ));
483 }
484 }
485 ANDROID_ACTION_MOVE => {
486 for &(raw_id, x, y) in pointers.iter() {
487 let pid: u64 = Self::map_android_pointer_id(raw_id);
488 self.track_pointer(pid);
489 events.push(WindowEvent::PointerMove(
490 wid,
491 PointerEvent::touch(pid, x, y),
492 ));
493 }
494 }
495 ANDROID_ACTION_POINTER_UP => {
496 if let Some(&(raw_id, x, y)) = pointers.get(action_index) {
497 let pid: u64 = Self::map_android_pointer_id(raw_id);
498 self.untrack_pointer(pid);
499 events.push(WindowEvent::PointerUp(wid, PointerEvent::touch(pid, x, y)));
500 }
501 }
502 ANDROID_ACTION_UP => {
503 if let Some(&(raw_id, x, y)) = pointers.get(action_index) {
504 let pid: u64 = Self::map_android_pointer_id(raw_id);
505 self.untrack_pointer(pid);
506 events.push(WindowEvent::PointerUp(wid, PointerEvent::touch(pid, x, y)));
507 }
508 self.get_mut_active_pointers().clear();
509 }
510 ANDROID_ACTION_CANCEL => {
511 let stale: Vec<u64> = take(self.get_mut_active_pointers());
512 for id in stale.into_iter() {
513 events.push(WindowEvent::PointerCancel(wid, id));
514 }
515 events.push(WindowEvent::PointerCancel(wid, 0));
516 }
517 _ => {}
518 }
519 events
520 }
521
522 /// Records iOS `touchesBegan` contacts and produces corresponding `PointerDown` events.
523 ///
524 /// # Arguments
525 ///
526 /// - `&Self` - Mutable tracker.
527 /// - `WindowId` - Target window handle.
528 /// - `&[(u64, f32, f32)]` - Touch tuples `(pointer_id, logical_x, logical_y)`.
529 ///
530 /// # Returns
531 ///
532 /// - `Vec<WindowEvent>` - `PointerDown` events.
533 pub fn handle_ios_touches_began(
534 &mut self,
535 wid: WindowId,
536 touches: &[(u64, f32, f32)],
537 ) -> Vec<WindowEvent> {
538 let mut events: Vec<WindowEvent> = Vec::with_capacity(touches.len());
539 for &(raw_id, x, y) in touches.iter() {
540 let pid: u64 = raw_id.max(1);
541 self.track_pointer(pid);
542 events.push(WindowEvent::PointerDown(
543 wid,
544 PointerEvent::touch(pid, x, y),
545 ));
546 }
547 events
548 }
549
550 /// Handles iOS `touchesCancelled`, emitting `PointerCancel` for every cancelled and stale contact.
551 ///
552 /// # Arguments
553 ///
554 /// - `&Self` - Mutable tracker.
555 /// - `WindowId` - Target window handle.
556 /// - `&[u64]` - Cancelled touch pointer identifiers.
557 /// - `&[u64]` - Remaining active touch pointer identifiers from `UIEvent.allTouches`.
558 ///
559 /// # Returns
560 ///
561 /// - `Vec<WindowEvent>` - Ordered `PointerCancel` events.
562 pub fn handle_ios_touches_cancelled(
563 &mut self,
564 wid: WindowId,
565 cancelled_ids: &[u64],
566 remaining_live_ids: &[u64],
567 ) -> Vec<WindowEvent> {
568 let mut events: Vec<WindowEvent> = Vec::new();
569 for &raw_id in cancelled_ids.iter() {
570 let pid: u64 = raw_id.max(1);
571 self.untrack_pointer(pid);
572 events.push(WindowEvent::PointerCancel(wid, pid));
573 }
574 let stale: Vec<u64> = self
575 .get_active_pointers()
576 .iter()
577 .copied()
578 .filter(|id: &u64| !remaining_live_ids.contains(id))
579 .collect();
580 for id in stale.into_iter() {
581 self.untrack_pointer(id);
582 events.push(WindowEvent::PointerCancel(wid, id));
583 }
584 if self.get_active_pointers().is_empty() {
585 events.push(WindowEvent::PointerCancel(wid, 0));
586 }
587 events
588 }
589
590 /// Cancels all active touches on focus loss, pause, or window detachment so input never wedges.
591 ///
592 /// # Arguments
593 ///
594 /// - `&Self` - Mutable tracker.
595 /// - `WindowId` - Target window handle.
596 ///
597 /// # Returns
598 ///
599 /// - `Vec<WindowEvent>` - Per-pointer and global `PointerCancel` events (empty if no contacts were active).
600 pub fn cancel_all_on_focus_loss(&mut self, wid: WindowId) -> Vec<WindowEvent> {
601 let active: Vec<u64> = take(self.get_mut_active_pointers());
602 if active.is_empty() {
603 return Vec::new();
604 }
605 let mut events: Vec<WindowEvent> = Vec::with_capacity(active.len().saturating_add(1));
606 for id in active.into_iter() {
607 events.push(WindowEvent::PointerCancel(wid, id));
608 }
609 events.push(WindowEvent::PointerCancel(wid, 0));
610 events
611 }
612}
613
614/// Generation-stamped same-process session lifecycle helpers for `MobileSessionLifecycle`.
615impl MobileSessionLifecycle {
616 /// Registers a new host activity or scene session, resetting transient flags and returning its monotonic generation.
617 ///
618 /// # Arguments
619 ///
620 /// - `&Self` - Mutable lifecycle coordinator.
621 /// - `u64` - Unique identifier of the newly created host activity or scene.
622 ///
623 /// # Returns
624 ///
625 /// - `u64` - Monotonic session generation assigned to `owner_id`.
626 pub fn begin_session(&mut self, owner_id: u64) -> u64 {
627 let next_gen: u64 = self.get_session_generation().wrapping_add(1).max(1);
628 self.set_session_generation(next_gen);
629 self.set_active_owner_id(owner_id);
630 self.set_surface_attached(false);
631 self.set_loop_running(false);
632 self.set_app_running(true);
633 self.set_background_frozen(false);
634 self.set_terminated(false);
635 self.get_mut_touch_tracker()
636 .get_mut_active_pointers()
637 .clear();
638 next_gen
639 }
640
641 /// Re-claims bridge ownership when `owner_id` resumes and is either the latest session or the previous owner cleared.
642 ///
643 /// # Arguments
644 ///
645 /// - `&Self` - Mutable lifecycle coordinator.
646 /// - `u64` - Resuming host activity or scene identifier.
647 /// - `u64` - Generation originally assigned to `owner_id`.
648 ///
649 /// # Returns
650 ///
651 /// - `bool` - `true` when `owner_id` holds active bridge ownership after the call.
652 pub fn activate_session(&mut self, owner_id: u64, session_gen: u64) -> bool {
653 let cur_gen: u64 = self.get_session_generation();
654 if session_gen >= cur_gen || self.get_active_owner_id() == 0 {
655 if session_gen > cur_gen {
656 self.set_session_generation(session_gen);
657 }
658 self.set_active_owner_id(owner_id);
659 return true;
660 }
661 self.get_active_owner_id() == owner_id && cur_gen == session_gen
662 }
663
664 /// Ends `owner_id`'s session only when `(owner_id, session_gen)` still owns the bridge, preventing delayed teardown races.
665 ///
666 /// # Arguments
667 ///
668 /// - `&Self` - Mutable lifecycle coordinator.
669 /// - `u64` - Destroying host activity or scene identifier.
670 /// - `u64` - Generation originally assigned to `owner_id`.
671 ///
672 /// # Returns
673 ///
674 /// - `bool` - `true` when ownership belonged to `owner_id` and was cleared; `false` when a newer session already replaced it.
675 pub fn end_session(&mut self, owner_id: u64, session_gen: u64) -> bool {
676 if self.get_active_owner_id() != owner_id || self.get_session_generation() != session_gen {
677 return false;
678 }
679 self.set_active_owner_id(0);
680 self.set_surface_attached(false);
681 self.set_loop_running(false);
682 self.set_app_running(false);
683 self.set_terminated(true);
684 self.get_mut_touch_tracker()
685 .get_mut_active_pointers()
686 .clear();
687 true
688 }
689
690 /// Returns `true` when `(owner_id, session_gen)` is currently the authoritative foreground bridge owner.
691 ///
692 /// # Arguments
693 ///
694 /// - `&Self` - Lifecycle coordinator reference.
695 /// - `u64` - Host activity or scene identifier.
696 /// - `u64` - Generation assigned to `owner_id`.
697 ///
698 /// # Returns
699 ///
700 /// - `bool` - `true` when `owner_id` matches the active generation and owner.
701 #[must_use]
702 pub fn is_active_owner(&self, owner_id: u64, session_gen: u64) -> bool {
703 owner_id != 0
704 && self.get_active_owner_id() == owner_id
705 && self.get_session_generation() == session_gen
706 }
707
708 /// Returns the current monotonic session generation.
709 ///
710 /// # Arguments
711 ///
712 /// - `&Self` - Lifecycle coordinator reference.
713 ///
714 /// # Returns
715 ///
716 /// - `u64` - Current session generation.
717 #[must_use]
718 pub fn active_session_generation(&self) -> u64 {
719 self.get_session_generation()
720 }
721
722 /// Returns the currently active bridge owner identifier (`0` when no activity or scene is active).
723 ///
724 /// # Arguments
725 ///
726 /// - `&Self` - Lifecycle coordinator reference.
727 ///
728 /// # Returns
729 ///
730 /// - `u64` - Active host owner identifier.
731 #[must_use]
732 pub fn active_owner_id(&self) -> u64 {
733 self.get_active_owner_id()
734 }
735
736 /// Returns the current VSync loop generation counter.
737 ///
738 /// # Arguments
739 ///
740 /// - `&Self` - Lifecycle coordinator reference.
741 ///
742 /// # Returns
743 ///
744 /// - `u64` - Active VSync callback generation.
745 #[must_use]
746 pub fn loop_generation(&self) -> u64 {
747 self.get_loop_generation()
748 }
749
750 /// Returns whether the VSync render loop is currently active.
751 ///
752 /// # Arguments
753 ///
754 /// - `&Self` - Lifecycle coordinator reference.
755 ///
756 /// # Returns
757 ///
758 /// - `bool` - `true` when the VSync frame pump is running.
759 #[must_use]
760 pub fn is_loop_running(&self) -> bool {
761 self.get_loop_running()
762 }
763
764 /// Returns whether a native rendering surface is currently attached.
765 ///
766 /// # Arguments
767 ///
768 /// - `&Self` - Lifecycle coordinator reference.
769 ///
770 /// # Returns
771 ///
772 /// - `bool` - `true` when the native surface is valid and bound.
773 #[must_use]
774 pub fn is_surface_attached(&self) -> bool {
775 self.get_surface_attached()
776 }
777
778 /// Returns `true` when an existing activity resumed after `Terminate` or exit request and must recreate its native `App` handle.
779 ///
780 /// # Arguments
781 ///
782 /// - `&Self` - Lifecycle coordinator reference.
783 ///
784 /// # Returns
785 ///
786 /// - `bool` - `true` when the native handle is no longer running or was terminated.
787 #[must_use]
788 pub fn should_recreate_handle(&self) -> bool {
789 self.get_terminated() || !self.get_app_running()
790 }
791
792 /// Marks the active session's surface attached and recreates the live running state if needed.
793 ///
794 /// # Arguments
795 ///
796 /// - `&Self` - Mutable lifecycle coordinator.
797 pub fn on_surface_created(&mut self) {
798 self.set_surface_attached(true);
799 if self.should_recreate_handle() {
800 self.set_app_running(true);
801 self.set_terminated(false);
802 self.set_background_frozen(false);
803 }
804 }
805
806 /// Marks the active surface detached and emits `PointerCancel` events for any active contacts.
807 ///
808 /// # Arguments
809 ///
810 /// - `&Self` - Mutable lifecycle coordinator.
811 /// - `WindowId` - Window whose surface detached.
812 ///
813 /// # Returns
814 ///
815 /// - `Vec<WindowEvent>` - Cancellation events for any dangling touch contacts.
816 pub fn on_surface_destroyed(&mut self, wid: WindowId) -> Vec<WindowEvent> {
817 self.set_surface_attached(false);
818 self.get_mut_touch_tracker().cancel_all_on_focus_loss(wid)
819 }
820
821 /// Transitions the active session to resumed foreground state and returns the unique VSync loop generation.
822 ///
823 /// # Arguments
824 ///
825 /// - `&Self` - Mutable lifecycle coordinator.
826 ///
827 /// # Returns
828 ///
829 /// - `u64` - Unique VSync loop generation preventing duplicate frame callbacks.
830 pub fn on_resume(&mut self) -> u64 {
831 self.set_background_frozen(false);
832 if self.should_recreate_handle() {
833 self.set_app_running(true);
834 self.set_terminated(false);
835 }
836 let next_loop: u64 = self.get_loop_generation().wrapping_add(1).max(1);
837 self.set_loop_generation(next_loop);
838 self.set_loop_running(true);
839 next_loop
840 }
841
842 /// Pauses the active VSync loop, advances the loop generation to invalidate in-flight callbacks, and cancels active touches.
843 ///
844 /// # Arguments
845 ///
846 /// - `&Self` - Mutable lifecycle coordinator.
847 /// - `WindowId` - Target window handle.
848 ///
849 /// # Returns
850 ///
851 /// - `Vec<WindowEvent>` - Cancellation events for any active touch contacts.
852 pub fn on_pause(&mut self, wid: WindowId) -> Vec<WindowEvent> {
853 self.set_loop_running(false);
854 let next_loop: u64 = self.get_loop_generation().wrapping_add(1).max(1);
855 self.set_loop_generation(next_loop);
856 self.get_mut_touch_tracker().cancel_all_on_focus_loss(wid)
857 }
858
859 /// Marks the active session background-frozen with the VSync loop halted.
860 ///
861 /// # Arguments
862 ///
863 /// - `&Self` - Mutable lifecycle coordinator.
864 pub fn on_background(&mut self) {
865 self.set_background_frozen(true);
866 self.set_loop_running(false);
867 }
868
869 /// Records a double-back exit request or OS termination so subsequent re-entry recreates a fresh native handle.
870 ///
871 /// # Arguments
872 ///
873 /// - `&Self` - Mutable lifecycle coordinator.
874 pub fn on_terminate(&mut self) {
875 self.set_app_running(false);
876 self.set_terminated(true);
877 self.set_loop_running(false);
878 }
879
880 /// Returns an immutable reference to the session's `TouchPointerTracker`.
881 ///
882 /// # Arguments
883 ///
884 /// - `&Self` - Lifecycle coordinator reference.
885 ///
886 /// # Returns
887 ///
888 /// - `&TouchPointerTracker` - Active touch tracker.
889 #[must_use]
890 pub fn touch_tracker(&self) -> &TouchPointerTracker {
891 self.get_touch_tracker()
892 }
893
894 /// Returns a mutable reference to the session's `TouchPointerTracker`.
895 ///
896 /// # Arguments
897 ///
898 /// - `&Self` - Mutable lifecycle coordinator.
899 ///
900 /// # Returns
901 ///
902 /// - `&mut TouchPointerTracker` - Mutable touch tracker.
903 pub fn touch_tracker_mut(&mut self) -> &mut TouchPointerTracker {
904 self.get_mut_touch_tracker()
905 }
906}