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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
#![deny(missing_docs)]
//! Desktop environment detection
//!
//! This crate implements automatic detection for the current desktop environment.
//!
//! See [`DesktopEnvironment`] for supported desktop environments.
//!
//! The environment can be detected using [`DesktopEnvironment::detect`]:
//!
//! ```rust
//! use detect_desktop_environment::DesktopEnvironment;
//!
//! match DesktopEnvironment::detect() {
//! Some(de) => println!("detected desktop environment: {de:?}"),
//! None => println!("failed to detect desktop environment"),
//! }
//! ```
/// Desktop environments supported by `detect-desktop-environment`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[non_exhaustive]
pub enum DesktopEnvironment {
/// Cinnamon, the default desktop environment for Linux Mint.
///
/// - <https://en.wikipedia.org/wiki/Cinnamon_(desktop_environment)>
Cinnamon,
/// COSMIC, the default desktop environment for Linux Pop!_OS.
///
/// Note: This corresponds to the classic COSMIC based on GNOME, not the Rust
/// [COSMIC-epoch](https://github.com/pop-os/cosmic-epoch). Please send a PR if you can
/// test how to detect cosmic-epoch.
///
/// - <https://github.com/pop-os/cosmic>
Cosmic,
/// Enlightenment desktop environment.
///
/// - <https://en.wikipedia.org/wiki/Enlightenment_(software)>
Enlightenment,
/// Gnome, the default environment for many major Linux distributions.
///
/// - <https://en.wikipedia.org/wiki/GNOME>
Gnome,
/// KDE Plasma, the Kool Desktop Environment.
///
/// - <https://kde.org/plasma-desktop/>
Kde,
/// LXDE
///
/// - <https://www.lxde.org/>
Lxde,
/// LXQt
///
/// - <https://lxqt-project.org/>
Lxqt,
/// MacOs, the environment for Apple's OS
MacOs,
/// MATE
///
/// <https://mate-desktop.org/>
Mate,
/// Unity, the legace desktop environment for Ubuntu
///
/// <https://en.wikipedia.org/wiki/Unity_%28user_interface%29>
Unity,
/// Windows, the environments for Microsoft's OS
Windows,
/// Xfce
///
/// <https://xfce.org/>
Xfce,
}
impl DesktopEnvironment {
/// Detect the current desktop environment
///
/// If the current desktop environment can't be detected, `None` is returned.
pub fn detect() -> Option<Self> {
Self::detect_impl()
}
/// Test if the desktop environment is based on the GTK framework
///
/// See <https://en.wikipedia.org/wiki/Category:Desktop_environments_based_on_GTK>
///
/// ```
/// use detect_desktop_environment::DesktopEnvironment;
///
/// // All matching desktop environments:
/// assert!(DesktopEnvironment::Cinnamon.gtk());
/// assert!(DesktopEnvironment::Cosmic.gtk());
/// assert!(DesktopEnvironment::Gnome.gtk());
/// assert!(DesktopEnvironment::Lxde.gtk());
/// assert!(DesktopEnvironment::Mate.gtk());
/// assert!(DesktopEnvironment::Unity.gtk());
/// assert!(DesktopEnvironment::Xfce.gtk());
///
/// // Non-GTK examples
/// assert!(!DesktopEnvironment::Kde.gtk());
/// assert!(!DesktopEnvironment::Windows.gtk());
/// ```
pub const fn gtk(self) -> bool {
use DesktopEnvironment::*;
matches!(self, Cinnamon | Cosmic | Gnome | Lxde | Mate | Unity | Xfce)
}
/// Test if the desktop environment is based on the Qt framework
///
/// ```
/// use detect_desktop_environment::DesktopEnvironment;
///
/// // All matching desktop environments:
/// assert!(DesktopEnvironment::Kde.qt());
/// assert!(DesktopEnvironment::Lxqt.qt());
///
/// // Non-Qt examples
/// assert!(!DesktopEnvironment::Gnome.qt());
/// assert!(!DesktopEnvironment::Windows.qt());
/// ```
pub const fn qt(self) -> bool {
use DesktopEnvironment::*;
matches!(self, Kde | Lxqt)
}
#[cfg(target_os = "macos")]
fn detect_impl() -> Option<Self> {
Some(DesktopEnvironment::MacOs)
}
#[cfg(target_os = "windows")]
fn detect_impl() -> Option<Self> {
Some(DesktopEnvironment::Windows)
}
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
fn detect_impl() -> Option<Self> {
std::env::var("XDG_CURRENT_DESKTOP")
.ok()
.as_deref()
.and_then(|de| match de {
"Cinnamon" => Some(DesktopEnvironment::Cinnamon),
"ENLIGHTENMENT" => Some(DesktopEnvironment::Enlightenment),
"GNOME" => Some(DesktopEnvironment::Gnome),
"KDE" => Some(DesktopEnvironment::Kde),
"LXDE" => Some(DesktopEnvironment::Lxde),
"LXQt" => Some(DesktopEnvironment::Lxqt),
"MATE" => Some(DesktopEnvironment::Mate),
"pop:GNOME" => Some(DesktopEnvironment::Gnome),
"Unity" => Some(DesktopEnvironment::Unity),
"X-Cinnamon" => Some(DesktopEnvironment::Cinnamon),
"XFCE" => Some(DesktopEnvironment::Xfce),
_ => None,
})
}
}