egui/
os.rs

1/// An `enum` of common operating systems.
2#[allow(clippy::upper_case_acronyms)] // `Ios` looks too ugly
3#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
4pub enum OperatingSystem {
5    /// Unknown OS - could be wasm
6    Unknown,
7
8    /// Android OS
9    Android,
10
11    /// Apple iPhone OS
12    IOS,
13
14    /// Linux or Unix other than Android
15    Nix,
16
17    /// macOS
18    Mac,
19
20    /// Windows
21    Windows,
22}
23
24impl Default for OperatingSystem {
25    fn default() -> Self {
26        Self::from_target_os()
27    }
28}
29
30impl OperatingSystem {
31    /// Uses the compile-time `target_arch` to identify the OS.
32    pub const fn from_target_os() -> Self {
33        if cfg!(target_arch = "wasm32") {
34            Self::Unknown
35        } else if cfg!(target_os = "android") {
36            Self::Android
37        } else if cfg!(target_os = "ios") {
38            Self::IOS
39        } else if cfg!(target_os = "macos") {
40            Self::Mac
41        } else if cfg!(target_os = "windows") {
42            Self::Windows
43        } else if cfg!(target_os = "linux")
44            || cfg!(target_os = "dragonfly")
45            || cfg!(target_os = "freebsd")
46            || cfg!(target_os = "netbsd")
47            || cfg!(target_os = "openbsd")
48        {
49            Self::Nix
50        } else {
51            Self::Unknown
52        }
53    }
54
55    /// Helper: try to guess from the user-agent of a browser.
56    pub fn from_user_agent(user_agent: &str) -> Self {
57        if user_agent.contains("Android") {
58            Self::Android
59        } else if user_agent.contains("like Mac") {
60            Self::IOS
61        } else if user_agent.contains("Win") {
62            Self::Windows
63        } else if user_agent.contains("Mac") {
64            Self::Mac
65        } else if user_agent.contains("Linux")
66            || user_agent.contains("X11")
67            || user_agent.contains("Unix")
68        {
69            Self::Nix
70        } else {
71            #[cfg(feature = "log")]
72            log::warn!(
73                "egui: Failed to guess operating system from User-Agent {:?}. Please file an issue at https://github.com/emilk/egui/issues",
74                user_agent);
75
76            Self::Unknown
77        }
78    }
79}