1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/// An `enum` of common operating systems.
#[allow(clippy::upper_case_acronyms)] // `Ios` looks too ugly
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum OperatingSystem {
    /// Unknown OS - could be wasm
    Unknown,

    /// Android OS.
    Android,

    /// Apple iPhone OS.
    IOS,

    /// Linux or Unix other than Android.
    Nix,

    /// MacOS.
    Mac,

    /// Windows.
    Windows,
}

impl Default for OperatingSystem {
    fn default() -> Self {
        Self::from_target_os()
    }
}

impl OperatingSystem {
    /// Uses the compile-time `target_arch` to identify the OS.
    pub const fn from_target_os() -> Self {
        if cfg!(target_arch = "wasm32") {
            Self::Unknown
        } else if cfg!(target_os = "android") {
            Self::Android
        } else if cfg!(target_os = "ios") {
            Self::IOS
        } else if cfg!(target_os = "macos") {
            Self::Mac
        } else if cfg!(target_os = "windows") {
            Self::Windows
        } else if cfg!(target_os = "linux")
            || cfg!(target_os = "dragonfly")
            || cfg!(target_os = "freebsd")
            || cfg!(target_os = "netbsd")
            || cfg!(target_os = "openbsd")
        {
            Self::Nix
        } else {
            Self::Unknown
        }
    }

    /// Helper: try to guess from the user-agent of a browser.
    pub fn from_user_agent(user_agent: &str) -> Self {
        if user_agent.contains("Android") {
            Self::Android
        } else if user_agent.contains("like Mac") {
            Self::IOS
        } else if user_agent.contains("Win") {
            Self::Windows
        } else if user_agent.contains("Mac") {
            Self::Mac
        } else if user_agent.contains("Linux")
            || user_agent.contains("X11")
            || user_agent.contains("Unix")
        {
            Self::Nix
        } else {
            #[cfg(feature = "log")]
            log::warn!(
                "egui: Failed to guess operating system from User-Agent {:?}. Please file an issue at https://github.com/emilk/egui/issues",
                user_agent);

            Self::Unknown
        }
    }
}