StratoSDK is a framework with a declarative approach similar to Flutter/React, written and designed entirely for Rust.
| 1 | //! Mobile device detection utilities. |
| 2 | |
| 3 | use std::sync::OnceLock; |
| 4 | |
| 5 | mod user_agent; |
| 6 | |
| 7 | pub use user_agent::is_mobile_user_agent; |
| 8 | |
| 9 | /// Cached result of mobile device detection. |
| 10 | static IS_MOBILE: OnceLock<bool> = OnceLock::new(); |
| 11 | |
| 12 | /// Returns `true` if the current device appears to be a mobile device that would |
| 13 | /// benefit from soft keyboard support. |
| 14 | /// |
| 15 | /// This function caches its result since the device type won't change during a session. |
| 16 | pub fn is_mobile_device() -> bool { |
| 17 | *IS_MOBILE.get_or_init(detect_mobile_device) |
| 18 | } |
| 19 | |
| 20 | /// Performs the actual mobile device detection by checking the user agent and touch capabilities. |
| 21 | fn detect_mobile_device() -> bool { |
| 22 | let navigator = gloo::utils::window().navigator(); |
| 23 | let has_touch = navigator.max_touch_points() > 0; |
| 24 | |
| 25 | if !has_touch { |
| 26 | return false; |
| 27 | } |
| 28 | |
| 29 | let ua = navigator.user_agent().ok().unwrap_or_default(); |
| 30 | // Standard mobile OS (iPhone, Android, etc.) or iPad (reports as "Macintosh" with touch) |
| 31 | is_mobile_user_agent(&ua) || ua.to_lowercase().contains("macintosh") |
| 32 | } |
| 33 |