Skip to main content

cranpose_services/
navigation.rs

1//! System "back" navigation requests.
2//!
3//! A platform back affordance — Android's back key / gesture, iOS's left-edge
4//! swipe — feeds [`push_back_request`]; the app drains it with
5//! [`take_back_requests`] and pops its own navigation. This gives one API
6//! across platforms for what is otherwise a per-OS gesture.
7//!
8//! Whether the platform *routes* its back control here is governed by
9//! [`set_back_interception`], the analogue of Compose's `BackHandler(enabled)`:
10//!
11//! - **Android**: while interception is enabled the back key/gesture is
12//!   consumed and lands in [`push_back_request`]; while disabled it stays with
13//!   the system, so the default behavior (leaving the activity) keeps working.
14//!   Apps enable it exactly while they have somewhere to navigate back to.
15//! - **iOS**: the left-edge swipe is a framework-drawn gesture with no system
16//!   fallback, so it always pushes a request regardless of interception.
17//! - **Desktop/web**: no OS back control; apps may map keys themselves and
18//!   call [`push_back_request`] directly.
19
20use std::sync::atomic::AtomicBool;
21use std::sync::atomic::AtomicUsize;
22use std::sync::atomic::Ordering;
23
24static BACK_REQUESTS: AtomicUsize = AtomicUsize::new(0);
25static BACK_INTERCEPTION: AtomicBool = AtomicBool::new(false);
26
27/// Record a system back request (called by the platform backend's gesture /
28/// button handler).
29pub fn push_back_request() {
30    BACK_REQUESTS.fetch_add(1, Ordering::SeqCst);
31}
32
33/// Take (and clear) the number of pending back requests. Polled by the app; a
34/// burst collapses into a count the app can coalesce.
35pub fn take_back_requests() -> usize {
36    BACK_REQUESTS.swap(0, Ordering::SeqCst)
37}
38
39/// Declare whether the app currently wants the platform's back control routed
40/// to [`push_back_request`] instead of the platform default. Set it `true`
41/// while there is in-app navigation to pop and `false` when leaving the app is
42/// the right response (mirrors Compose's `BackHandler(enabled)`).
43pub fn set_back_interception(enabled: bool) {
44    BACK_INTERCEPTION.store(enabled, Ordering::SeqCst);
45}
46
47/// Whether the app asked to intercept the platform back control. Read by the
48/// platform input path.
49pub fn back_interception_enabled() -> bool {
50    BACK_INTERCEPTION.load(Ordering::SeqCst)
51}
52
53#[cfg(test)]
54mod tests {
55    use super::*;
56
57    #[test]
58    fn requests_accumulate_and_drain() {
59        let _ = take_back_requests(); // clear any residue
60        push_back_request();
61        push_back_request();
62        assert_eq!(take_back_requests(), 2);
63        assert_eq!(take_back_requests(), 0);
64    }
65
66    #[test]
67    fn interception_defaults_off_and_toggles() {
68        set_back_interception(false);
69        assert!(!back_interception_enabled());
70        set_back_interception(true);
71        assert!(back_interception_enabled());
72        set_back_interception(false);
73    }
74}