a lightweight, interval-based utility to combat digital strain through "Ma" (intentional pauses) for the eyes and body.
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat: add X11/Wayland/Windows detection with tests

+76
+1
src/overlay/mod.rs
··· 1 1 pub mod monitors; 2 2 pub mod password; 3 + pub mod session; 3 4 4 5 use std::time::Duration; 5 6
+75
src/overlay/session.rs
··· 1 + #[derive(Debug, Clone, Copy, PartialEq)] 2 + pub enum SessionType { 3 + /// X11 (Linux) — set_position works; one Slint window per monitor. 4 + X11, 5 + /// Wayland — set_position is ignored; requires wlr-layer-shell for multi-monitor. 6 + Wayland, 7 + /// Windows — set_position works; one Slint window per monitor. 8 + Windows, 9 + } 10 + 11 + /// Detect the current display session type. 12 + pub fn detect() -> SessionType { 13 + detect_from_env(|k| std::env::var(k)) 14 + } 15 + 16 + fn detect_from_env<F>(var: F) -> SessionType 17 + where 18 + F: for<'a> Fn(&'a str) -> Result<String, std::env::VarError>, 19 + { 20 + #[cfg(target_os = "windows")] 21 + { 22 + let _ = var; 23 + return SessionType::Windows; 24 + } 25 + 26 + #[cfg(not(target_os = "windows"))] 27 + { 28 + // WAYLAND_DISPLAY set → native Wayland session. 29 + if var("WAYLAND_DISPLAY").map(|v| !v.is_empty()).unwrap_or(false) { 30 + return SessionType::Wayland; 31 + } 32 + SessionType::X11 33 + } 34 + } 35 + 36 + #[cfg(test)] 37 + mod tests { 38 + use super::*; 39 + 40 + fn mock_env<'a>(wayland: Option<&'a str>, _display: Option<&'a str>) -> impl for<'k> Fn(&'k str) -> Result<String, std::env::VarError> + 'a { 41 + move |key| match key { 42 + "WAYLAND_DISPLAY" => wayland 43 + .map(|v| Ok(v.to_string())) 44 + .unwrap_or(Err(std::env::VarError::NotPresent)), 45 + "DISPLAY" => _display 46 + .map(|v| Ok(v.to_string())) 47 + .unwrap_or(Err(std::env::VarError::NotPresent)), 48 + _ => Err(std::env::VarError::NotPresent), 49 + } 50 + } 51 + 52 + #[test] 53 + fn wayland_display_set_returns_wayland() { 54 + let session = detect_from_env(mock_env(Some("wayland-0"), Some(":0"))); 55 + assert_eq!(session, SessionType::Wayland); 56 + } 57 + 58 + #[test] 59 + fn no_wayland_returns_x11() { 60 + let session = detect_from_env(mock_env(None, Some(":0"))); 61 + assert_eq!(session, SessionType::X11); 62 + } 63 + 64 + #[test] 65 + fn empty_wayland_display_returns_x11() { 66 + let session = detect_from_env(mock_env(Some(""), Some(":0"))); 67 + assert_eq!(session, SessionType::X11); 68 + } 69 + 70 + #[test] 71 + fn no_env_at_all_returns_x11() { 72 + let session = detect_from_env(mock_env(None, None)); 73 + assert_eq!(session, SessionType::X11); 74 + } 75 + }