Skip to main content

uzor_framework/
platform.rs

1//! Platform facts the event loop cannot see, pushed by the thin native wrapper
2//! (Swift / Kotlin, through `uzor-window-mobile`) from any thread: the safe area
3//! where winit does not report it (Android `WindowInsets`) and the soft keyboard
4//! height (iOS `keyboardWillChangeFrame`, Android `WindowInsets.ime`), both in
5//! logical px. The native host picks them up on its next loop turn and echoes
6//! `WindowInput::Insets` to every window.
7
8use std::sync::Mutex;
9
10use crate::types::window::Insets;
11
12#[derive(Default)]
13struct State {
14    safe: Option<Insets>,
15    keyboard: f64,
16    dirty: bool,
17    wake: Option<std::sync::Arc<dyn Fn() + Send + Sync>>,
18}
19
20static STATE: Mutex<Option<State>> = Mutex::new(None);
21
22fn with<R>(f: impl FnOnce(&mut State) -> R) -> R {
23    let mut g = STATE.lock().unwrap_or_else(|e| e.into_inner());
24    f(g.get_or_insert_with(State::default))
25}
26
27fn changed(s: &mut State) -> Option<std::sync::Arc<dyn Fn() + Send + Sync>> {
28    s.dirty = true;
29    s.wake.clone()
30}
31
32/// The platform's safe area (overrides what the host derives itself; on iOS winit
33/// already reports it, on Android only the wrapper knows it).
34pub fn set_safe_area(safe: Insets) {
35    let wake = with(|s| {
36        if s.safe == Some(safe) {
37            return None;
38        }
39        s.safe = Some(safe);
40        changed(s)
41    });
42    if let Some(w) = wake {
43        w();
44    }
45}
46
47/// The soft keyboard's height over the bottom of the window (0 when hidden).
48pub fn set_keyboard(height: f64) {
49    let height = height.max(0.0);
50    let wake = with(|s| {
51        if s.keyboard == height {
52            return None;
53        }
54        s.keyboard = height;
55        changed(s)
56    });
57    if let Some(w) = wake {
58        w();
59    }
60}
61
62/// The last values the wrapper pushed: (safe area if it set one, keyboard height).
63pub fn current() -> (Option<Insets>, f64) {
64    with(|s| (s.safe, s.keyboard))
65}
66
67/// New values since the last call (the host's loop turn).
68#[allow(dead_code)]
69pub(crate) fn take_changed() -> Option<(Option<Insets>, f64)> {
70    with(|s| std::mem::take(&mut s.dirty).then_some((s.safe, s.keyboard)))
71}
72
73/// Wake the host's loop (from any thread): something it polls changed (a tray
74/// menu pick, a tray widget update).
75#[allow(dead_code)]
76pub(crate) fn wake() {
77    if let Some(w) = with(|s| s.wake.clone()) {
78        w();
79    }
80}
81
82/// The host's loop wake, called after every change.
83#[allow(dead_code)]
84pub(crate) fn set_wake(f: impl Fn() + Send + Sync + 'static) {
85    with(|s| s.wake = Some(std::sync::Arc::new(f)));
86}
87
88#[cfg(test)]
89mod tests {
90    use super::*;
91
92    #[test]
93    fn changes_are_taken_once_and_wake_the_loop() {
94        use std::sync::atomic::{AtomicUsize, Ordering};
95        static WOKE: AtomicUsize = AtomicUsize::new(0);
96        set_wake(|| {
97            WOKE.fetch_add(1, Ordering::Relaxed);
98        });
99        let _ = take_changed();
100        set_keyboard(300.0);
101        set_keyboard(300.0);
102        assert_eq!(WOKE.load(Ordering::Relaxed), 1, "same value does not wake again");
103        assert_eq!(take_changed().map(|c| c.1), Some(300.0));
104        assert_eq!(take_changed(), None);
105        set_safe_area(Insets::new(24.0, 0.0, 48.0, 0.0));
106        assert_eq!(take_changed(), Some((Some(Insets::new(24.0, 0.0, 48.0, 0.0)), 300.0)));
107        set_keyboard(-5.0);
108        assert_eq!(current().1, 0.0);
109    }
110}