feat: rewrite moonset in Rust (gtk4-rs + gtk4-layer-shell)
Feature-parity with Python v0.2.0. Same CSS, same UI, same actions. Single 3.1 MB binary with embedded resources (CSS, wallpaper, avatar). Modules: power.rs, i18n.rs, config.rs, users.rs, panel.rs, main.rs 45 unit tests passing. Python sources retained as reference.
This commit is contained in:
+141
@@ -0,0 +1,141 @@
|
||||
// ABOUTME: Current user detection and avatar loading for the power menu.
|
||||
// ABOUTME: Retrieves user info from the system (pwd, AccountsService, ~/.face).
|
||||
|
||||
use nix::unistd::{getuid, User as NixUser};
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
const DEFAULT_ACCOUNTSSERVICE_DIR: &str = "/var/lib/AccountsService/icons";
|
||||
const GRESOURCE_PREFIX: &str = "/dev/moonarch/moonset";
|
||||
|
||||
/// Represents the current user for the power menu.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct User {
|
||||
pub username: String,
|
||||
pub display_name: String,
|
||||
pub home: PathBuf,
|
||||
pub uid: u32,
|
||||
}
|
||||
|
||||
/// Get the currently logged-in user's info from the system.
|
||||
pub fn get_current_user() -> Option<User> {
|
||||
let uid = getuid();
|
||||
let nix_user = NixUser::from_uid(uid).ok()??;
|
||||
|
||||
let gecos = nix_user.gecos.to_str().unwrap_or("").to_string();
|
||||
// GECOS field may contain comma-separated values; first field is the full name
|
||||
let display_name = if !gecos.is_empty() {
|
||||
let first = gecos.split(',').next().unwrap_or("");
|
||||
if first.is_empty() {
|
||||
nix_user.name.clone()
|
||||
} else {
|
||||
first.to_string()
|
||||
}
|
||||
} else {
|
||||
nix_user.name.clone()
|
||||
};
|
||||
|
||||
Some(User {
|
||||
username: nix_user.name,
|
||||
display_name,
|
||||
home: nix_user.dir,
|
||||
uid: uid.as_raw(),
|
||||
})
|
||||
}
|
||||
|
||||
/// Find the user's avatar image, checking ~/.face then AccountsService.
|
||||
pub fn get_avatar_path(home: &Path, username: Option<&str>) -> Option<PathBuf> {
|
||||
get_avatar_path_with(home, username, Path::new(DEFAULT_ACCOUNTSSERVICE_DIR))
|
||||
}
|
||||
|
||||
/// Find avatar with configurable AccountsService dir (for testing).
|
||||
pub fn get_avatar_path_with(
|
||||
home: &Path,
|
||||
username: Option<&str>,
|
||||
accountsservice_dir: &Path,
|
||||
) -> Option<PathBuf> {
|
||||
// ~/.face takes priority
|
||||
let face = home.join(".face");
|
||||
if face.exists() {
|
||||
return Some(face);
|
||||
}
|
||||
|
||||
// AccountsService icon
|
||||
if let Some(name) = username {
|
||||
if accountsservice_dir.exists() {
|
||||
let icon = accountsservice_dir.join(name);
|
||||
if icon.exists() {
|
||||
return Some(icon);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
/// Return the GResource path to the default avatar SVG.
|
||||
pub fn get_default_avatar_path() -> String {
|
||||
format!("{GRESOURCE_PREFIX}/default-avatar.svg")
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::fs;
|
||||
|
||||
#[test]
|
||||
fn get_current_user_returns_some() {
|
||||
let user = get_current_user();
|
||||
assert!(user.is_some());
|
||||
let user = user.unwrap();
|
||||
assert!(!user.username.is_empty());
|
||||
assert!(!user.display_name.is_empty());
|
||||
assert!(user.home.exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn returns_face_file_if_exists() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let face = dir.path().join(".face");
|
||||
fs::write(&face, "fake image").unwrap();
|
||||
let path = get_avatar_path_with(dir.path(), None, Path::new("/nonexistent"));
|
||||
assert_eq!(path, Some(face));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn returns_accountsservice_icon_if_exists() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let icons_dir = dir.path().join("icons");
|
||||
fs::create_dir(&icons_dir).unwrap();
|
||||
let icon = icons_dir.join("testuser");
|
||||
fs::write(&icon, "fake image").unwrap();
|
||||
let path = get_avatar_path_with(dir.path(), Some("testuser"), &icons_dir);
|
||||
assert_eq!(path, Some(icon));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn face_file_takes_priority_over_accountsservice() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let face = dir.path().join(".face");
|
||||
fs::write(&face, "fake image").unwrap();
|
||||
let icons_dir = dir.path().join("icons");
|
||||
fs::create_dir(&icons_dir).unwrap();
|
||||
let icon = icons_dir.join("testuser");
|
||||
fs::write(&icon, "fake image").unwrap();
|
||||
let path = get_avatar_path_with(dir.path(), Some("testuser"), &icons_dir);
|
||||
assert_eq!(path, Some(face));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn returns_none_when_no_avatar() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let path = get_avatar_path_with(dir.path(), None, Path::new("/nonexistent"));
|
||||
assert!(path.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn default_avatar_path_is_gresource() {
|
||||
let path = get_default_avatar_path();
|
||||
assert!(path.contains("default-avatar.svg"));
|
||||
assert!(path.starts_with("/dev/moonarch/moonset"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user