use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct Prefs {
pub network: bool,
pub bluetooth: bool,
pub keep_awake: bool,
pub night_light: bool,
pub power: bool,
pub displays: bool,
pub menu_y_offset: i32,
pub menu_closes_on_click_outside: bool,
pub menu: MenuMode,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum MenuMode {
#[default]
Auto,
Popup,
Dbusmenu,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MenuServing {
Popup,
Dbusmenu,
}
impl MenuMode {
pub fn serving(self, on_hyprland: bool, traymenu_installed: bool) -> MenuServing {
match self {
MenuMode::Popup => MenuServing::Popup,
MenuMode::Dbusmenu => MenuServing::Dbusmenu,
MenuMode::Auto if on_hyprland && traymenu_installed => MenuServing::Popup,
MenuMode::Auto => MenuServing::Dbusmenu,
}
}
pub fn serving_here(self) -> MenuServing {
let on_hyprland = std::env::var_os("HYPRLAND_INSTANCE_SIGNATURE").is_some_and(|v| !v.is_empty());
self.serving(on_hyprland, on_path(crate::launch::TRAYMENU_BINARY))
}
}
fn on_path(name: &str) -> bool {
use std::os::unix::fs::PermissionsExt;
let Some(path) = std::env::var_os("PATH") else {
return false;
};
std::env::split_paths(&path).any(|dir| {
std::fs::metadata(dir.join(name))
.is_ok_and(|meta| meta.is_file() && meta.permissions().mode() & 0o111 != 0)
})
}
impl Default for Prefs {
fn default() -> Self {
Prefs {
network: true,
bluetooth: true,
keep_awake: false,
night_light: false,
power: false,
displays: false,
menu_y_offset: 32,
menu_closes_on_click_outside: true,
menu: MenuMode::Auto,
}
}
}
#[derive(Debug, thiserror::Error)]
pub enum PrefsError {
#[error("{path} could not be read as tray settings: {source}")]
Unreadable {
path: PathBuf,
#[source]
source: toml::de::Error,
},
#[error("{path} could not be opened: {source}")]
Io {
path: PathBuf,
#[source]
source: std::io::Error,
},
#[error("{path} could not be written: {source}")]
Write {
path: PathBuf,
#[source]
source: std::io::Error,
},
}
pub fn path() -> PathBuf {
hyprforge_paths::tray_toml_path()
}
pub fn load() -> Result<Prefs, PrefsError> {
load_from(&path())
}
pub fn load_from(path: &Path) -> Result<Prefs, PrefsError> {
let text = match std::fs::read_to_string(path) {
Ok(text) => text,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(Prefs::default()),
Err(source) => {
return Err(PrefsError::Io {
path: path.to_path_buf(),
source,
})
}
};
toml::from_str(&text).map_err(|source| PrefsError::Unreadable {
path: path.to_path_buf(),
source,
})
}
pub fn save(prefs: &Prefs) -> Result<(), PrefsError> {
save_to(&path(), prefs)
}
pub fn save_to(path: &Path, prefs: &Prefs) -> Result<(), PrefsError> {
let text = toml::to_string_pretty(prefs).expect("Prefs is plain fields and always serialises");
hyprforge_paths::write_atomic(path, &text).map_err(|source| PrefsError::Write {
path: path.to_path_buf(),
source,
})
}
pub fn update(f: impl FnOnce(&mut Prefs)) -> Result<Prefs, PrefsError> {
update_at(&path(), f)
}
pub fn update_at(path: &Path, f: impl FnOnce(&mut Prefs)) -> Result<Prefs, PrefsError> {
let mut prefs = load_from(path)?;
f(&mut prefs);
save_to(path, &prefs)?;
Ok(prefs)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn both_icons_are_shown_until_someone_says_otherwise() {
let prefs = Prefs::default();
assert!(prefs.network);
assert!(prefs.bluetooth);
}
#[test]
fn a_missing_file_is_first_run_not_a_failure() {
let dir = tempfile::tempdir().unwrap();
let missing = dir.path().join("tray.toml");
assert_eq!(load_from(&missing).unwrap(), Prefs::default());
}
#[test]
fn a_file_that_exists_and_will_not_parse_is_reported_rather_than_defaulted() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("tray.toml");
std::fs::write(&path, "network = yes please\n").unwrap();
let err = load_from(&path).expect_err("a malformed file is an error, not defaults");
assert!(matches!(err, PrefsError::Unreadable { .. }));
assert!(err.to_string().contains("tray.toml"));
}
#[test]
fn a_partial_file_leaves_the_icons_it_does_not_mention_alone() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("tray.toml");
std::fs::write(&path, "network = false\n").unwrap();
let prefs = load_from(&path).unwrap();
assert!(!prefs.network);
assert!(prefs.bluetooth, "an unmentioned icon keeps its default");
}
#[test]
fn what_is_saved_is_what_comes_back() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("tray.toml");
let prefs = Prefs {
network: false,
bluetooth: true,
keep_awake: true,
night_light: true,
power: true,
displays: true,
menu_y_offset: 50,
menu_closes_on_click_outside: false,
menu: MenuMode::Dbusmenu,
};
save_to(&path, &prefs).unwrap();
assert_eq!(load_from(&path).unwrap(), prefs);
}
#[test]
fn the_two_new_icons_default_to_off_unlike_the_original_two() {
let prefs = Prefs::default();
assert!(prefs.network);
assert!(prefs.bluetooth);
assert!(!prefs.keep_awake);
assert!(!prefs.night_light);
}
#[test]
fn a_tray_toml_naming_only_the_original_two_icons_leaves_the_new_ones_off() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("tray.toml");
std::fs::write(&path, "network = true\nbluetooth = false\n").unwrap();
let prefs = load_from(&path).unwrap();
assert!(prefs.network);
assert!(!prefs.bluetooth);
assert!(!prefs.keep_awake, "an icon added later than this file defaults off");
assert!(!prefs.night_light, "an icon added later than this file defaults off");
assert!(!prefs.power, "an icon added later than this file defaults off");
assert!(!prefs.displays, "an icon added later than this file defaults off");
}
#[test]
fn a_tray_toml_naming_no_offset_gets_the_default_that_clears_a_typical_bar() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("tray.toml");
std::fs::write(&path, "network = true\nbluetooth = true\n").unwrap();
let prefs = load_from(&path).unwrap();
assert_eq!(prefs.menu_y_offset, 32);
}
#[test]
fn a_tray_toml_written_before_this_setting_existed_closes_on_a_click_outside() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("tray.toml");
std::fs::write(&path, "network = true\nmenu_y_offset = 40\n").unwrap();
let prefs = load_from(&path).unwrap();
assert_eq!(prefs.menu_y_offset, 40);
assert!(prefs.menu_closes_on_click_outside, "a file that predates the field gets the default, not false");
}
#[test]
fn a_configured_offset_round_trips() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("tray.toml");
let prefs = Prefs { menu_y_offset: 48, ..Prefs::default() };
save_to(&path, &prefs).unwrap();
assert_eq!(load_from(&path).unwrap().menu_y_offset, 48);
}
#[test]
fn two_writers_changing_different_fields_both_survive() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("tray.toml");
let screen_a_initial = load_from(&path).unwrap();
let screen_b_initial = load_from(&path).unwrap();
assert_eq!(screen_a_initial, screen_b_initial, "both start from the same defaults");
update_at(&path, |p| p.network = false).unwrap();
update_at(&path, |p| p.menu_y_offset = 60).unwrap();
let on_disk = load_from(&path).unwrap();
assert!(!on_disk.network, "screen A's write must not be undone by screen B's");
assert_eq!(on_disk.menu_y_offset, 60, "screen B's own write must have landed");
}
#[test]
fn update_refuses_to_write_over_a_file_it_cannot_reload() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("tray.toml");
std::fs::write(&path, "network = yes please\n").unwrap();
let before = std::fs::read_to_string(&path).unwrap();
let err = update_at(&path, |p| p.network = false)
.expect_err("a file that won't parse must refuse the write, not overwrite it");
assert!(matches!(err, PrefsError::Unreadable { .. }));
let after = std::fs::read_to_string(&path).unwrap();
assert_eq!(before, after, "the mutation must never have been applied or saved");
}
#[test]
fn a_tray_toml_written_before_the_menu_mode_existed_reads_as_auto() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("tray.toml");
std::fs::write(&path, "network = true\nmenu_y_offset = 40\n").unwrap();
assert_eq!(load_from(&path).unwrap().menu, MenuMode::Auto);
}
#[test]
fn the_menu_mode_is_written_and_read_as_a_lowercase_word() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("tray.toml");
std::fs::write(&path, "menu = \"dbusmenu\"\n").unwrap();
assert_eq!(load_from(&path).unwrap().menu, MenuMode::Dbusmenu);
save_to(&path, &Prefs { menu: MenuMode::Popup, ..Prefs::default() }).unwrap();
assert!(std::fs::read_to_string(&path).unwrap().contains("menu = \"popup\""));
}
#[test]
fn an_unknown_menu_mode_is_reported_rather_than_defaulted() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("tray.toml");
std::fs::write(&path, "menu = \"gtk\"\n").unwrap();
assert!(matches!(load_from(&path), Err(PrefsError::Unreadable { .. })));
}
#[test]
fn auto_serves_the_popup_only_on_hyprland_with_traymenu_installed() {
assert_eq!(MenuMode::Auto.serving(true, true), MenuServing::Popup);
assert_eq!(MenuMode::Auto.serving(true, false), MenuServing::Dbusmenu);
assert_eq!(MenuMode::Auto.serving(false, true), MenuServing::Dbusmenu);
assert_eq!(MenuMode::Auto.serving(false, false), MenuServing::Dbusmenu);
}
#[test]
fn a_forced_mode_wins_over_what_the_machine_offers() {
for (hyprland, installed) in [(true, true), (true, false), (false, true), (false, false)] {
assert_eq!(MenuMode::Popup.serving(hyprland, installed), MenuServing::Popup);
assert_eq!(MenuMode::Dbusmenu.serving(hyprland, installed), MenuServing::Dbusmenu);
}
}
#[test]
fn a_binary_counts_as_installed_only_when_it_is_an_executable_file_on_path() {
assert!(on_path("sh"), "every machine this runs on has a shell on $PATH");
assert!(!on_path("hyprforge-no-such-binary-anywhere"));
}
#[test]
fn update_on_a_missing_file_starts_from_the_defaults() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("tray.toml");
let updated = update_at(&path, |p| p.keep_awake = true).unwrap();
assert!(updated.keep_awake);
assert!(updated.network, "everything else stays at its default");
assert_eq!(load_from(&path).unwrap(), updated);
}
}