use hyprforge_bluetooth::backend::{for_display as bt_for_display, BluetoothBackend};
use hyprforge_bluetooth::{Address, AdapterState, BlueZBackend, Device};
use hyprforge_bluetooth::Status as BtStatus;
use futures_util::StreamExt;
use hyprforge_core::displayd_proxy::DisplaydProxy;
use hyprforge_ecosystem::sunset_control::{Hyprsunset, SunsetBackend, SunsetControlError};
use hyprforge_network::backend::{for_display as net_for_display, NetworkBackend, SavedNetwork};
use hyprforge_network::{
AccessPoint, NetworkError, NetworkManagerBackend, RadioState, Ssid, WiredState, WiredStatus,
};
use hyprforge_network::Status as NetStatus;
use hyprforge_power::{
BatteryBackend, BatteryInfo, BatteryState, DetachedBackend, InhibitBackend, InhibitorInfo,
PowerProfile, PowerProfilesBackend, PowerProfilesDaemonBackend, UPowerBackend, WhatSet,
};
use hyprforge_tray::menu::{radio_menu, Menu, MenuItem};
use hyprforge_tray::prefs::{self, MenuServing, Prefs};
use hyprforge_tray::{watcher_present, Category, TrayIcon, TrayItem};
use hyprforge_tray::Status as TrayStatus;
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::Mutex;
type IconSlot = Arc<Mutex<Option<Arc<TrayIcon>>>>;
#[derive(Clone)]
struct ManagedIcon {
slot: IconSlot,
index: u32,
}
impl ManagedIcon {
fn new(icon: Arc<TrayIcon>, index: u32) -> Self {
ManagedIcon { slot: Arc::new(Mutex::new(Some(icon))), index }
}
}
const POLL_INTERVAL: Duration = Duration::from_secs(10);
const RETRY_INTERVAL: Duration = Duration::from_secs(3);
const WATCHER_POLL_INTERVAL: Duration = Duration::from_secs(5);
const PREFS_WATCH_INTERVAL: Duration = Duration::from_millis(500);
#[tokio::main]
async fn main() -> anyhow::Result<()> {
if std::env::args().nth(1).as_deref() == Some(hyprforge_power::keep_awake::HOLDER_ARG) {
hyprforge_power::keep_awake::hold().await?;
return Ok(());
}
tracing_subscriber::fmt()
.with_env_filter(
tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("info")),
)
.init();
let probe = zbus::Connection::session().await?;
if !watcher_present(&probe).await {
tracing::info!("no tray host yet; waiting for one to start");
loop {
tokio::time::sleep(WATCHER_POLL_INTERVAL).await;
if watcher_present(&probe).await {
break;
}
}
}
tracing::info!("tray host found; registering icons");
let (clicks_tx, clicks_rx) = tokio::sync::mpsc::unbounded_channel();
let (menu_clicks_tx, menu_clicks_rx) = tokio::sync::mpsc::unbounded_channel();
let (refresh_tx, refresh_rx) = tokio::sync::mpsc::unbounded_channel();
let serving = prefs::load().unwrap_or_default().menu.serving_here();
let network_icon = register_with_retry(
network_item(None, None, None, true),
network_menu(None, None, true, &[], &[]),
0,
serving,
clicks_tx.clone(),
menu_clicks_tx.clone(),
)
.await;
let bluetooth_icon = register_with_retry(
bluetooth_item(None, None, true),
bluetooth_menu(None, true, &[]),
1,
serving,
clicks_tx.clone(),
menu_clicks_tx.clone(),
)
.await;
let keep_awake_icon = register_with_retry(
keep_awake_item(None, &[], true),
keep_awake_menu(None, &[], true),
2,
serving,
clicks_tx.clone(),
menu_clicks_tx.clone(),
)
.await;
let night_light_icon = register_with_retry(
night_light_item(&NightLightState::CouldNotCheck),
night_light_menu(&NightLightState::CouldNotCheck),
3,
serving,
clicks_tx.clone(),
menu_clicks_tx.clone(),
)
.await;
let power_icon = register_with_retry(
power_item(&BatteryReading::Unavailable, &ProfileReading::Unavailable),
power_menu(&BatteryReading::Unavailable, &ProfileReading::Unavailable),
4,
serving,
clicks_tx.clone(),
menu_clicks_tx.clone(),
)
.await;
let displays_icon = register_with_retry(
displays_item(&DisplaysReading::Unavailable, false),
displays_menu(&DisplaysReading::Unavailable, false),
5,
serving,
clicks_tx.clone(),
menu_clicks_tx.clone(),
)
.await;
let icons = vec![
ManagedIcon::new(network_icon, 0),
ManagedIcon::new(bluetooth_icon, 1),
ManagedIcon::new(keep_awake_icon, 2),
ManagedIcon::new(night_light_icon, 3),
ManagedIcon::new(power_icon, 4),
ManagedIcon::new(displays_icon, 5),
];
let night_light_belief: NightLightBelief = Arc::new(std::sync::Mutex::new(true));
let revert_deadline: RevertDeadline = Arc::new(std::sync::Mutex::new(None));
let poll_task = tokio::spawn(poll_loop(
icons.clone(),
night_light_belief.clone(),
revert_deadline.clone(),
clicks_tx.clone(),
menu_clicks_tx.clone(),
refresh_rx,
));
let click_task = tokio::spawn(handle_clicks(clicks_rx));
let menu_click_task = tokio::spawn(handle_menu_clicks(
menu_clicks_rx,
refresh_tx.clone(),
night_light_belief,
revert_deadline.clone(),
));
let revert_task = tokio::spawn(watch_display_reverts(revert_deadline, refresh_tx.clone()));
let prefs_watch_task = tokio::spawn(prefs_watch_loop(refresh_tx));
let reannounce_task = tokio::spawn(reannounce_loop(probe, icons));
tokio::select! {
r = poll_task => log_task_exit("poll", r),
r = click_task => log_task_exit("clicks", r),
r = menu_click_task => log_task_exit("menu-clicks", r),
r = reannounce_task => log_task_exit("reannounce", r),
r = prefs_watch_task => log_task_exit("prefs-watch", r),
r = revert_task => log_task_exit("display-reverts", r),
}
Ok(())
}
fn log_task_exit(name: &str, result: Result<(), tokio::task::JoinError>) {
match result {
Ok(()) => tracing::warn!(task = name, "background task exited unexpectedly"),
Err(e) => tracing::error!(task = name, error = %e, "background task panicked"),
}
}
async fn register_with_retry(
item: TrayItem,
menu: Menu,
index: u32,
serving: MenuServing,
clicks: tokio::sync::mpsc::UnboundedSender<String>,
menu_clicks: tokio::sync::mpsc::UnboundedSender<String>,
) -> Arc<TrayIcon> {
loop {
match TrayIcon::register_serving(
item.clone(),
menu.clone(),
serving,
index,
clicks.clone(),
menu_clicks.clone(),
)
.await
{
Ok(icon) => return Arc::new(icon),
Err(e) => {
tracing::warn!(error = %e, icon = %item.id, "failed to register tray icon; retrying");
tokio::time::sleep(RETRY_INTERVAL).await;
}
}
}
}
struct Reconnecting<B> {
backend: Option<Arc<B>>,
}
impl<B> Reconnecting<B> {
fn new() -> Self {
Reconnecting { backend: None }
}
async fn get_or_connect<F, Fut, E>(&mut self, connect: F) -> Result<Arc<B>, E>
where
F: FnOnce() -> Fut,
Fut: std::future::Future<Output = Result<B, E>>,
{
if let Some(backend) = &self.backend {
return Ok(backend.clone());
}
let backend = Arc::new(connect().await?);
self.backend = Some(backend.clone());
Ok(backend)
}
}
async fn network_backend(
state: &mut Reconnecting<NetworkManagerBackend>,
) -> Option<Arc<NetworkManagerBackend>> {
match state.get_or_connect(NetworkManagerBackend::connect).await {
Ok(backend) => Some(backend),
Err(e) => {
tracing::warn!(error = %e, "could not connect to NetworkManager; will retry");
None
}
}
}
async fn bluetooth_backend(state: &mut Reconnecting<BlueZBackend>) -> Option<Arc<BlueZBackend>> {
match state.get_or_connect(BlueZBackend::connect).await {
Ok(backend) => Some(backend),
Err(e) => {
tracing::warn!(error = %e, "could not connect to BlueZ; will retry");
None
}
}
}
async fn keep_awake_backend(state: &mut Reconnecting<DetachedBackend>) -> Option<Arc<DetachedBackend>> {
match state.get_or_connect(|| async { DetachedBackend::this_binary() }).await {
Ok(backend) => Some(backend),
Err(e) => {
tracing::warn!(error = %e, "could not find this program to run keep awake's holder; will retry");
None
}
}
}
async fn upower_backend(state: &mut Reconnecting<UPowerBackend>) -> Option<Arc<UPowerBackend>> {
match state.get_or_connect(UPowerBackend::connect).await {
Ok(backend) => Some(backend),
Err(e) => {
tracing::warn!(error = %e, "could not connect to UPower; will retry");
None
}
}
}
async fn profiles_backend(
state: &mut Reconnecting<PowerProfilesDaemonBackend>,
) -> Option<Arc<PowerProfilesDaemonBackend>> {
match state.get_or_connect(PowerProfilesDaemonBackend::connect).await {
Ok(backend) => Some(backend),
Err(e) => {
tracing::warn!(error = %e, "could not connect to power-profiles-daemon; will retry");
None
}
}
}
type NightLightBelief = Arc<std::sync::Mutex<bool>>;
async fn sample_network(state: &mut Reconnecting<NetworkManagerBackend>) -> (TrayItem, Menu) {
let Some(backend) = network_backend(state).await else {
return (network_item(None, None, None, true), network_menu(None, None, true, &[], &[]));
};
match backend.status().await {
Ok(status) => {
let (points, wifi_present) = match backend.access_points().await {
Ok(points) => (points, true),
Err(NetworkError::NoWifiDevice) => (Vec::new(), false),
Err(_) => (Vec::new(), true),
};
let wired = backend.wired().await.unwrap_or_default();
let strength = status
.connected_to
.as_ref()
.and_then(|ssid| points.iter().find(|ap| &ap.ssid == ssid).map(|ap| ap.strength));
let saved = backend.saved_networks().await.unwrap_or_default();
let item = with_wired(
network_item(Some(&status), status.connected_to.as_ref(), strength, false),
status.connected_to.as_ref(),
&wired,
wifi_present,
);
let menu = with_wired_menu(
network_menu(Some(&status), status.connected_to.as_ref(), false, &points, &saved),
&wired,
wifi_present,
false,
);
(item, menu)
}
Err(e) => {
tracing::warn!(error = %e, "NetworkManager status call failed");
(network_item(None, None, None, true), network_menu(None, None, true, &[], &[]))
}
}
}
async fn sample_bluetooth(state: &mut Reconnecting<BlueZBackend>) -> (TrayItem, Menu) {
let Some(backend) = bluetooth_backend(state).await else {
return (bluetooth_item(None, None, true), bluetooth_menu(None, true, &[]));
};
match backend.status().await {
Ok(status) => {
let devices = backend.devices().await.unwrap_or_default();
let connected = devices.iter().find(|d| d.connected);
let item = bluetooth_item(Some(&status), connected, false);
let menu = bluetooth_menu(Some(&status), false, &devices);
(item, menu)
}
Err(e) => {
tracing::warn!(error = %e, "BlueZ status call failed");
(bluetooth_item(None, None, true), bluetooth_menu(None, true, &[]))
}
}
}
async fn sample_keep_awake(state: &mut Reconnecting<DetachedBackend>) -> (TrayItem, Menu) {
let Some(backend) = keep_awake_backend(state).await else {
return (keep_awake_item(None, &[], true), keep_awake_menu(None, &[], true));
};
match backend.held().await {
Ok(held) => {
let others = backend.list_inhibitors().await.unwrap_or_default();
let others = other_inhibitors(others);
(
keep_awake_item(Some(held.is_some()), &others, false),
keep_awake_menu(Some(held.is_some()), &others, false),
)
}
Err(e) => {
tracing::warn!(error = %e, "logind held() call failed");
(keep_awake_item(None, &[], true), keep_awake_menu(None, &[], true))
}
}
}
async fn sample_power(
upower: &mut Reconnecting<UPowerBackend>,
profiles: &mut Reconnecting<PowerProfilesDaemonBackend>,
) -> (TrayItem, Menu) {
let battery = match upower_backend(upower).await {
None => BatteryReading::Unavailable,
Some(backend) => match backend.battery().await {
Ok(Some(info)) => BatteryReading::Present(info),
Ok(None) => BatteryReading::Absent,
Err(e) => {
tracing::warn!(error = %e, "UPower battery call failed");
BatteryReading::Unavailable
}
},
};
let profile = match profiles_backend(profiles).await {
None => ProfileReading::Unavailable,
Some(backend) => match backend.active_profile().await {
Ok(active) => {
let offered = backend.profiles().await.unwrap_or_else(|_| vec![active]);
ProfileReading::Known { active, offered }
}
Err(e) => {
tracing::warn!(error = %e, "power-profiles-daemon call failed");
ProfileReading::Unavailable
}
},
};
(power_item(&battery, &profile), power_menu(&battery, &profile))
}
const DISPLAYD_CALL_TIMEOUT: Duration = hyprforge_core::command::TIMEOUT;
const DISPLAYD_APPLY_TIMEOUT: Duration = Duration::from_secs(25);
async fn displayd_bounded<T>(
what: &str,
within: Duration,
call: impl std::future::Future<Output = zbus::Result<T>>,
) -> anyhow::Result<T> {
match tokio::time::timeout(within, call).await {
Ok(result) => Ok(result?),
Err(_) => anyhow::bail!("{what} timed out after {}s", within.as_secs()),
}
}
async fn session_bus(state: &mut Reconnecting<zbus::Connection>) -> Option<Arc<zbus::Connection>> {
let connect = || async {
match tokio::time::timeout(DISPLAYD_CALL_TIMEOUT, zbus::Connection::session()).await {
Ok(result) => result.map_err(anyhow::Error::from),
Err(_) => Err(anyhow::anyhow!("the session bus isn't answering")),
}
};
match state.get_or_connect(connect).await {
Ok(conn) => Some(conn),
Err(e) => {
tracing::warn!(error = %e, "could not connect to the session bus; will retry");
None
}
}
}
async fn read_displays(conn: &zbus::Connection) -> anyhow::Result<DisplaysReading> {
let proxy =
displayd_bounded("connecting to displayd", DISPLAYD_CALL_TIMEOUT, DisplaydProxy::new(conn))
.await?;
let profiles = displayd_bounded("listing layouts", DISPLAYD_CALL_TIMEOUT, proxy.list_profiles())
.await?
.into_iter()
.map(|(id, name, _heads, last_used)| DisplayProfile { id, name, last_used })
.collect();
let current =
displayd_bounded("reading the current layout", DISPLAYD_CALL_TIMEOUT, proxy.get_current_profile())
.await?;
Ok(DisplaysReading::Known {
profiles,
current: Some(current).filter(|id| !id.is_empty()),
})
}
async fn sample_displays(
bus: &mut Reconnecting<zbus::Connection>,
deadline: &RevertDeadline,
) -> (TrayItem, Menu) {
let pending = revert_pending(
*deadline.lock().unwrap_or_else(|e| e.into_inner()),
tokio::time::Instant::now(),
);
let reading = match session_bus(bus).await {
None => DisplaysReading::Unavailable,
Some(conn) => read_displays(&conn).await.unwrap_or_else(|e| {
tracing::debug!(error = %e, "displayd didn't answer");
DisplaysReading::Unavailable
}),
};
(displays_item(&reading, pending), displays_menu(&reading, pending))
}
fn set_revert_deadline(
deadline: &RevertDeadline,
seconds: u32,
refresh: &tokio::sync::mpsc::UnboundedSender<()>,
) {
let wait = Duration::from_secs(u64::from(seconds));
*deadline.lock().unwrap_or_else(|e| e.into_inner()) = Some(tokio::time::Instant::now() + wait);
let _ = refresh.send(());
let refresh = refresh.clone();
tokio::spawn(async move {
tokio::time::sleep(wait + Duration::from_secs(1)).await;
let _ = refresh.send(());
});
}
fn clear_revert_deadline(deadline: &RevertDeadline, refresh: &tokio::sync::mpsc::UnboundedSender<()>) {
*deadline.lock().unwrap_or_else(|e| e.into_inner()) = None;
let _ = refresh.send(());
}
async fn watch_display_reverts(deadline: RevertDeadline, refresh: tokio::sync::mpsc::UnboundedSender<()>) {
loop {
if let Err(e) = follow_display_reverts(&deadline, &refresh).await {
tracing::debug!(error = %e, "not following displayd's revert signals; will retry");
}
tokio::time::sleep(RETRY_INTERVAL).await;
}
}
async fn follow_display_reverts(
deadline: &RevertDeadline,
refresh: &tokio::sync::mpsc::UnboundedSender<()>,
) -> anyhow::Result<()> {
let conn = match tokio::time::timeout(DISPLAYD_CALL_TIMEOUT, zbus::Connection::session()).await {
Ok(result) => result?,
Err(_) => anyhow::bail!("the session bus isn't answering"),
};
let proxy =
displayd_bounded("connecting to displayd", DISPLAYD_CALL_TIMEOUT, DisplaydProxy::new(&conn))
.await?;
let mut pending =
displayd_bounded("subscribing to RevertPending", DISPLAYD_CALL_TIMEOUT, proxy.receive_revert_pending())
.await?;
let mut resolved = displayd_bounded(
"subscribing to RevertResolved",
DISPLAYD_CALL_TIMEOUT,
proxy.receive_revert_resolved(),
)
.await?;
loop {
tokio::select! {
Some(signal) = pending.next() => {
set_revert_deadline(deadline, signal.args()?.seconds, refresh);
}
Some(_) = resolved.next() => clear_revert_deadline(deadline, refresh),
else => anyhow::bail!("displayd's signal streams ended"),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
enum NightLightState {
NotRunning,
CouldNotCheck,
Known { temperature: i64, on: bool },
}
async fn sample_night_light(believed_on: &NightLightBelief) -> (TrayItem, Menu) {
let believed_on = *believed_on.lock().unwrap_or_else(|e| e.into_inner());
let state = tokio::task::spawn_blocking(move || match Hyprsunset.current_temperature() {
Ok(temperature) => NightLightState::Known { temperature, on: believed_on },
Err(SunsetControlError::NotRunning) => NightLightState::NotRunning,
Err(e) => {
tracing::warn!(error = %e, "hyprsunset temperature check failed");
NightLightState::CouldNotCheck
}
})
.await
.unwrap_or_else(|e| {
tracing::warn!(error = %e, "night light check panicked");
NightLightState::CouldNotCheck
});
(night_light_item(&state), night_light_menu(&state))
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum IconAction {
Keep,
Register,
Drop,
Reregister,
}
fn icon_action(wanted: bool, served: Option<MenuServing>, serving: MenuServing) -> IconAction {
match (wanted, served) {
(true, None) => IconAction::Register,
(false, Some(_)) => IconAction::Drop,
(true, Some(served)) if served != serving => IconAction::Reregister,
(true, Some(_)) | (false, None) => IconAction::Keep,
}
}
fn needs_sampling(wanted: bool, currently_up: bool) -> bool {
!matches!((wanted, currently_up), (false, false))
}
fn refresh_prefs(current: &mut Prefs, warned: &mut bool) {
match prefs::load() {
Ok(loaded) => {
*current = loaded;
*warned = false;
}
Err(e) => {
if !*warned {
tracing::warn!(
error = %e,
"could not read tray.toml; leaving tray icons as they are"
);
*warned = true;
}
}
}
}
#[allow(clippy::too_many_arguments)]
async fn sync_icon(
slot: &IconSlot,
wanted: bool,
item: TrayItem,
menu: Menu,
index: u32,
serving: MenuServing,
clicks: &tokio::sync::mpsc::UnboundedSender<String>,
menu_clicks: &tokio::sync::mpsc::UnboundedSender<String>,
) {
let id = item.id.clone();
let mut guard = slot.lock().await;
match icon_action(wanted, guard.as_ref().map(|icon| icon.serving()), serving) {
IconAction::Keep => {
if let Some(icon) = guard.as_ref() {
if let Err(e) = icon.update(item).await {
tracing::warn!(error = %e, icon = %id, "failed to update tray icon");
}
if let Err(e) = icon.update_menu(menu).await {
tracing::warn!(error = %e, icon = %id, "failed to update tray menu");
}
}
}
IconAction::Drop => {
tracing::info!(icon = %id, "tray icon switched off; releasing its bus name");
*guard = None;
}
IconAction::Reregister => {
tracing::info!(icon = %id, ?serving, "tray menu mode changed; registering the icon again");
if let Some(old) = guard.take() {
old.release().await;
}
register_into(&mut guard, item, menu, index, serving, clicks, menu_clicks).await;
}
IconAction::Register => {
register_into(&mut guard, item, menu, index, serving, clicks, menu_clicks).await;
}
}
}
async fn register_into(
guard: &mut Option<Arc<TrayIcon>>,
item: TrayItem,
menu: Menu,
index: u32,
serving: MenuServing,
clicks: &tokio::sync::mpsc::UnboundedSender<String>,
menu_clicks: &tokio::sync::mpsc::UnboundedSender<String>,
) {
let id = item.id.clone();
match TrayIcon::register_serving(item, menu, serving, index, clicks.clone(), menu_clicks.clone()).await {
Ok(icon) => *guard = Some(Arc::new(icon)),
Err(e) => {
tracing::warn!(error = %e, icon = %id, "failed to register tray icon; will retry next tick");
}
}
}
fn prefs_stamp(path: &std::path::Path) -> Option<(std::time::SystemTime, u64)> {
let meta = std::fs::metadata(path).ok()?;
Some((meta.modified().ok()?, meta.len()))
}
async fn prefs_watch_loop(refresh: tokio::sync::mpsc::UnboundedSender<()>) {
let path = hyprforge_tray::prefs::path();
let mut seen = prefs_stamp(&path);
let mut ticker = tokio::time::interval(PREFS_WATCH_INTERVAL);
loop {
ticker.tick().await;
let now = prefs_stamp(&path);
if now == seen {
continue;
}
seen = now;
if refresh.send(()).is_err() {
return;
}
}
}
async fn poll_loop(
icons: Vec<ManagedIcon>,
night_light_belief: NightLightBelief,
revert_deadline: RevertDeadline,
clicks: tokio::sync::mpsc::UnboundedSender<String>,
menu_clicks: tokio::sync::mpsc::UnboundedSender<String>,
mut refresh_rx: tokio::sync::mpsc::UnboundedReceiver<()>,
) {
let mut net_state = Reconnecting::new();
let mut bt_state = Reconnecting::new();
let mut power_state = Reconnecting::new();
let mut upower_state = Reconnecting::new();
let mut profiles_state = Reconnecting::new();
let mut bus_state = Reconnecting::new();
let mut prefs = Prefs::default();
let mut prefs_load_failed = false;
let mut ticker = tokio::time::interval(POLL_INTERVAL);
loop {
tokio::select! {
_ = ticker.tick() => {}
_ = refresh_rx.recv() => {}
}
refresh_prefs(&mut prefs, &mut prefs_load_failed);
let serving = prefs.menu.serving_here();
let wanted = [
prefs.network,
prefs.bluetooth,
prefs.keep_awake,
prefs.night_light,
prefs.power,
prefs.displays,
];
let mut up = [false; 6];
for (slot, is_up) in icons.iter().zip(up.iter_mut()) {
*is_up = slot.slot.lock().await.is_some();
}
let net = needs_sampling(wanted[0], up[0]).then_some(());
let net = match net {
Some(()) => Some(sample_network(&mut net_state).await),
None => None,
};
let bt = match needs_sampling(wanted[1], up[1]) {
true => Some(sample_bluetooth(&mut bt_state).await),
false => None,
};
let ka = match needs_sampling(wanted[2], up[2]) {
true => Some(sample_keep_awake(&mut power_state).await),
false => None,
};
let nl = match needs_sampling(wanted[3], up[3]) {
true => Some(sample_night_light(&night_light_belief).await),
false => None,
};
let pw = match needs_sampling(wanted[4], up[4]) {
true => Some(sample_power(&mut upower_state, &mut profiles_state).await),
false => None,
};
let dp = match needs_sampling(wanted[5], up[5]) {
true => Some(sample_displays(&mut bus_state, &revert_deadline).await),
false => None,
};
let ticks: [Option<(TrayItem, Menu)>; 6] = [net, bt, ka, nl, pw, dp];
for ((icon, sampled), want) in icons.iter().zip(ticks).zip(wanted) {
let Some((item, menu)) = sampled else { continue };
sync_icon(&icon.slot, want, item, menu, icon.index, serving, &clicks, &menu_clicks).await;
}
}
}
async fn handle_clicks(mut clicks: tokio::sync::mpsc::UnboundedReceiver<String>) {
while let Some(screen) = clicks.recv().await {
spawn_settings(&screen);
}
}
fn spawn_settings(screen: &str) {
match tokio::process::Command::new("hyprforge-settings")
.arg("--screen")
.arg(screen)
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.spawn()
{
Ok(_) => {}
Err(e) => {
tracing::warn!(error = %e, screen = %screen, "failed to launch hyprforge-settings")
}
}
}
async fn reannounce_loop(probe: zbus::Connection, icons: Vec<ManagedIcon>) {
let mut present = true;
let mut ticker = tokio::time::interval(WATCHER_POLL_INTERVAL);
loop {
ticker.tick().await;
let now_present = watcher_present(&probe).await;
if now_present && !present {
tracing::info!("tray host reappeared; re-announcing icons");
for icon in &icons {
if let Some(tray_icon) = icon.slot.lock().await.as_ref() {
if let Err(e) = tray_icon.announce().await {
tracing::warn!(error = %e, "failed to re-announce a tray icon");
}
}
}
}
present = now_present;
}
}
fn wifi_signal_icon(strength: u8) -> &'static str {
match strength {
80..=u8::MAX => "network-wireless-signal-excellent-symbolic",
60..=79 => "network-wireless-signal-good-symbolic",
40..=59 => "network-wireless-signal-ok-symbolic",
20..=39 => "network-wireless-signal-weak-symbolic",
_ => "network-wireless-signal-none-symbolic",
}
}
fn network_item(
status: Option<&NetStatus>,
connected_ssid: Option<&Ssid>,
strength: Option<u8>,
unavailable: bool,
) -> TrayItem {
let id = NETWORK_ITEM_ID.to_string();
let title = "Network".to_string();
let Some(status) = status.filter(|_| !unavailable) else {
return TrayItem {
id,
category: Category::Hardware,
status: TrayStatus::Active,
title,
icon_name: "dialog-warning".to_string(),
tooltip_title: "Network unavailable".to_string(),
tooltip_body: "NetworkManager isn't running.".to_string(),
};
};
match status.radio {
RadioState::Off | RadioState::HardwareOff => TrayItem {
id,
category: Category::Hardware,
status: TrayStatus::Active,
title,
icon_name: "network-wireless-disconnected-symbolic".to_string(),
tooltip_title: "Wi-Fi is off".to_string(),
tooltip_body: "Click for the menu to turn it back on".to_string(),
},
RadioState::On => match connected_ssid {
Some(ssid) => TrayItem {
id,
category: Category::Hardware,
status: TrayStatus::Active,
title,
icon_name: wifi_signal_icon(strength.unwrap_or(0)).to_string(),
tooltip_title: ssid.to_display_string(),
tooltip_body: match strength {
Some(s) => format!("Signal {s}%"),
None => "Connected".to_string(),
},
},
None => TrayItem {
id,
category: Category::Hardware,
status: TrayStatus::Active,
title,
icon_name: "network-wireless-signal-none-symbolic".to_string(),
tooltip_title: "Not connected".to_string(),
tooltip_body: "Wi-Fi is on".to_string(),
},
},
}
}
fn bluetooth_item(
status: Option<&BtStatus>,
connected_device: Option<&Device>,
unavailable: bool,
) -> TrayItem {
let id = "hyprforge-bluetooth".to_string();
let title = "Bluetooth".to_string();
let Some(status) = status.filter(|_| !unavailable) else {
return TrayItem {
id,
category: Category::Hardware,
status: TrayStatus::Active,
title,
icon_name: "dialog-warning".to_string(),
tooltip_title: "Bluetooth unavailable".to_string(),
tooltip_body: "bluetoothd isn't running.".to_string(),
};
};
match status.state {
AdapterState::Off | AdapterState::HardwareBlocked => TrayItem {
id,
category: Category::Hardware,
status: TrayStatus::Active,
title,
icon_name: "network-bluetooth-inactive-symbolic".to_string(),
tooltip_title: "Bluetooth is off".to_string(),
tooltip_body: "Click for the menu to turn it back on".to_string(),
},
AdapterState::On | AdapterState::Changing => match connected_device {
Some(device) => TrayItem {
id,
category: Category::Hardware,
status: TrayStatus::Active,
title,
icon_name: "network-bluetooth-activated-symbolic".to_string(),
tooltip_title: device.alias.clone(),
tooltip_body: device.kind.label().to_string(),
},
None => TrayItem {
id,
category: Category::Hardware,
status: TrayStatus::Active,
title,
icon_name: "network-bluetooth-symbolic".to_string(),
tooltip_title: "Not connected".to_string(),
tooltip_body: "Bluetooth is on".to_string(),
},
},
}
}
const MAX_NETWORKS_SHOWN: usize = 8;
fn hex_encode(bytes: &[u8]) -> String {
use std::fmt::Write;
let mut out = String::with_capacity(bytes.len() * 2);
for b in bytes {
let _ = write!(out, "{b:02x}");
}
out
}
fn hex_decode(s: &str) -> Option<Vec<u8>> {
if !s.len().is_multiple_of(2) {
return None;
}
let bytes = s.as_bytes();
let mut out = Vec::with_capacity(bytes.len() / 2);
for pair in bytes.chunks_exact(2) {
let hi = (pair[0] as char).to_digit(16)?;
let lo = (pair[1] as char).to_digit(16)?;
out.push(((hi << 4) | lo) as u8);
}
Some(out)
}
const ENTERPRISE_ROW_REASON: &str = "enterprise sign-in not supported";
fn network_menu(
status: Option<&NetStatus>,
connected_ssid: Option<&Ssid>,
unavailable: bool,
access_points: &[AccessPoint],
saved: &[SavedNetwork],
) -> Menu {
let settings_row = MenuItem::standard("Network settings…", "wifi:settings");
let Some(status) = status.filter(|_| !unavailable) else {
return radio_menu(vec![MenuItem::disabled("Network unavailable")], Vec::new(), settings_row);
};
let top = match status.radio {
RadioState::Off => {
return radio_menu(
vec![MenuItem::checkmark("Wi-Fi", false, "wifi:radio:on")],
Vec::new(),
settings_row,
);
}
RadioState::HardwareOff => {
return radio_menu(
vec![MenuItem::disabled_checkmark("Wi-Fi (blocked by hardware switch)", false)],
Vec::new(),
settings_row,
);
}
RadioState::On => vec![MenuItem::checkmark("Wi-Fi", true, "wifi:radio:off")],
};
let mut content = Vec::new();
let displayed = net_for_display(access_points.to_vec());
let total = displayed.len();
for ap in displayed.into_iter().take(MAX_NETWORKS_SHOWN) {
let is_connected = connected_ssid == Some(&ap.ssid);
let label = ap.ssid.to_display_string();
if ap.security.unsupported_reason().is_some() {
content.push(MenuItem::disabled(format!("{label} ({ENTERPRISE_ROW_REASON})")));
continue;
}
let has_saved = saved.iter().any(|s| s.ssid == ap.ssid);
let action = if ap.security.needs_passphrase() && !has_saved {
"wifi:settings".to_string()
} else {
format!("wifi:connect:{}", hex_encode(ap.ssid.as_bytes()))
};
if is_connected {
content.push(MenuItem::checkmark(label, true, action));
} else {
content.push(MenuItem::standard(label, action));
}
}
if total > MAX_NETWORKS_SHOWN {
content.push(MenuItem::disabled(format!(
"+{} more — see Settings",
total - MAX_NETWORKS_SHOWN
)));
}
radio_menu(top, content, settings_row)
}
fn wired_label(port: &WiredStatus, several: bool) -> String {
if several {
format!("Wired ({})", port.interface)
} else {
"Wired".to_string()
}
}
fn with_wired(
wifi: TrayItem,
connected_ssid: Option<&Ssid>,
wired: &[WiredStatus],
wifi_present: bool,
) -> TrayItem {
let several = wired.len() > 1;
let wired_item = |icon: &str, tooltip_title: String, tooltip_body: String| TrayItem {
icon_name: icon.to_string(),
tooltip_title,
tooltip_body,
..wifi.clone()
};
if let Some(port) = wired.iter().find(|p| p.state == WiredState::Connected) {
let mut body: Vec<String> = Vec::new();
if several {
body.push(port.interface.clone());
}
if let Some(speed) = port.speed_mbps {
body.push(format!("{speed} Mb/s"));
}
if let Some(ssid) = connected_ssid {
body.push(format!("also on Wi-Fi ({})", ssid.to_display_string()));
}
return wired_item(
"network-wired-activated-symbolic",
match &port.connection {
Some(name) => format!("Wired — {name}"),
None => "Wired".to_string(),
},
body.join(" · "),
);
}
if connected_ssid.is_none() {
if let Some(port) = wired.iter().find(|p| p.state == WiredState::Connecting) {
return wired_item(
"network-wired-symbolic",
format!("{} — connecting…", wired_label(port, several)),
String::new(),
);
}
}
if wifi_present {
return wifi;
}
match wired.iter().find(|p| p.state == WiredState::Disconnected) {
Some(port) => wired_item(
"network-wired-disconnected-symbolic",
"Not connected".to_string(),
format!("{} has a cable, but no connection is active", wired_label(port, several)),
),
None if wired.is_empty() => wired_item(
"network-wired-unavailable-symbolic",
"No network devices".to_string(),
"NetworkManager sees no Wi-Fi or Ethernet here.".to_string(),
),
None => wired_item(
"network-wired-unavailable-symbolic",
"Cable unplugged".to_string(),
String::new(),
),
}
}
fn with_wired_menu(wifi: Menu, wired: &[WiredStatus], wifi_present: bool, unavailable: bool) -> Menu {
if unavailable {
return wifi;
}
let several = wired.len() > 1;
let rows: Vec<MenuItem> = wired
.iter()
.map(|port| {
let label = wired_label(port, several);
match port.state {
WiredState::Connected => MenuItem::checkmark(
match &port.connection {
Some(name) => format!("{label} — {name}"),
None => label,
},
true,
format!("wired:disconnect:{}", port.interface),
),
WiredState::Connecting => MenuItem::disabled(format!("{label} — connecting…")),
WiredState::Disconnected => {
MenuItem::standard(label, format!("wired:connect:{}", port.interface))
}
WiredState::CableUnplugged => MenuItem::disabled(format!("{label} — cable unplugged")),
}
})
.collect();
if !wifi_present {
let settings_row = MenuItem::standard("Network settings…", "wifi:settings");
let top = if rows.is_empty() {
vec![MenuItem::disabled("No network devices")]
} else {
rows
};
return radio_menu(top, Vec::new(), settings_row);
}
if rows.is_empty() {
return wifi;
}
let mut items = rows;
items.push(MenuItem::separator());
items.extend(wifi.items);
Menu::new(items)
}
fn bluetooth_menu(status: Option<&BtStatus>, unavailable: bool, devices: &[Device]) -> Menu {
let settings_row = MenuItem::standard("Bluetooth settings…", "bt:settings");
let Some(status) = status.filter(|_| !unavailable) else {
return radio_menu(vec![MenuItem::disabled("Bluetooth unavailable")], Vec::new(), settings_row);
};
let top = match status.state {
AdapterState::Off => {
return radio_menu(
vec![MenuItem::checkmark("Bluetooth", false, "bt:radio:on")],
Vec::new(),
settings_row,
);
}
AdapterState::HardwareBlocked => {
return radio_menu(
vec![MenuItem::disabled_checkmark("Bluetooth (blocked by hardware switch)", false)],
Vec::new(),
settings_row,
);
}
AdapterState::On | AdapterState::Changing => {
vec![MenuItem::checkmark("Bluetooth", true, "bt:radio:off")]
}
};
let mut content = Vec::new();
for device in bt_for_display(devices.to_vec()) {
if !device.paired {
content.push(MenuItem::standard(
format!("{} — pair in Settings", device.alias),
"bt:settings",
));
continue;
}
let action = if device.connected {
format!("bt:disconnect:{}", device.address)
} else {
format!("bt:connect:{}", device.address)
};
if device.connected {
content.push(MenuItem::checkmark(device.alias.clone(), true, action));
} else {
content.push(MenuItem::standard(device.alias.clone(), action));
}
}
radio_menu(top, content, settings_row)
}
const KEEP_AWAKE_WHO: &str = "hyprforge-trayd";
const KEEP_AWAKE_WHY: &str = "user requested via the tray";
const KEEP_AWAKE_ITEM_ID: &str = "hyprforge-keep-awake";
const NIGHT_LIGHT_ITEM_ID: &str = "hyprforge-night-light";
const MAX_OTHER_INHIBITORS_SHOWN: usize = 5;
fn other_inhibitors(all: Vec<InhibitorInfo>) -> Vec<InhibitorInfo> {
all.into_iter()
.filter(|i| i.who != KEEP_AWAKE_WHO && i.who != hyprforge_power::keep_awake::HOLDER_WHO)
.collect()
}
fn keep_awake_item(held: Option<bool>, others: &[InhibitorInfo], unavailable: bool) -> TrayItem {
let id = KEEP_AWAKE_ITEM_ID.to_string();
let title = "Keep awake".to_string();
let Some(held) = held.filter(|_| !unavailable) else {
return TrayItem {
id,
category: Category::SystemServices,
status: TrayStatus::Active,
title,
icon_name: "dialog-warning".to_string(),
tooltip_title: "Keep awake unavailable".to_string(),
tooltip_body: "systemd-logind isn't answering.".to_string(),
};
};
let plural = if others.len() == 1 { "" } else { "es" };
if held {
TrayItem {
id,
category: Category::SystemServices,
status: TrayStatus::Active,
title,
icon_name: "changes-prevent-symbolic".to_string(),
tooltip_title: "Keeping this machine awake".to_string(),
tooltip_body: if others.is_empty() {
String::new()
} else {
format!("Also held by {} other process{plural}", others.len())
},
}
} else {
TrayItem {
id,
category: Category::SystemServices,
status: TrayStatus::Active,
title,
icon_name: "changes-allow-symbolic".to_string(),
tooltip_title: "Not keeping this machine awake".to_string(),
tooltip_body: if others.is_empty() {
"Nothing is preventing sleep".to_string()
} else {
format!("{} other process{plural} preventing sleep", others.len())
},
}
}
}
fn keep_awake_menu(held: Option<bool>, others: &[InhibitorInfo], unavailable: bool) -> Menu {
let settings_row = MenuItem::standard("Keep awake settings…", "keepawake:settings");
let Some(held) = held.filter(|_| !unavailable) else {
return radio_menu(vec![MenuItem::disabled("Keep awake unavailable")], Vec::new(), settings_row);
};
let toggle = MenuItem::checkmark(
"Keep awake",
held,
if held { "keepawake:off" } else { "keepawake:on" },
);
let mut content = Vec::new();
if !others.is_empty() {
content.push(MenuItem::disabled("Also preventing sleep:"));
for other in others.iter().take(MAX_OTHER_INHIBITORS_SHOWN) {
content.push(MenuItem::disabled(format!("{} — {}", other.who, other.why)));
}
if others.len() > MAX_OTHER_INHIBITORS_SHOWN {
content.push(MenuItem::disabled(format!(
"+{} more",
others.len() - MAX_OTHER_INHIBITORS_SHOWN
)));
}
}
radio_menu(vec![toggle], content, settings_row)
}
const NIGHT_LIGHT_PRESETS: &[i64] = &[4500, 3500, 2700];
fn night_light_item(state: &NightLightState) -> TrayItem {
let id = NIGHT_LIGHT_ITEM_ID.to_string();
let title = "Night light".to_string();
match *state {
NightLightState::NotRunning => TrayItem {
id,
category: Category::SystemServices,
status: TrayStatus::Active,
title,
icon_name: "dialog-warning".to_string(),
tooltip_title: "Night light unavailable".to_string(),
tooltip_body: "hyprsunset isn't running.".to_string(),
},
NightLightState::CouldNotCheck => TrayItem {
id,
category: Category::SystemServices,
status: TrayStatus::Active,
title,
icon_name: "dialog-information".to_string(),
tooltip_title: "Night light status unknown".to_string(),
tooltip_body: "Couldn't tell whether hyprsunset is running.".to_string(),
},
NightLightState::Known { temperature, on: true } => TrayItem {
id,
category: Category::SystemServices,
status: TrayStatus::Active,
title,
icon_name: "redshift-status-on-symbolic".to_string(),
tooltip_title: "Night light is on".to_string(),
tooltip_body: format!("{temperature}K"),
},
NightLightState::Known { on: false, .. } => TrayItem {
id,
category: Category::SystemServices,
status: TrayStatus::Active,
title,
icon_name: "redshift-status-off-symbolic".to_string(),
tooltip_title: "Night light is off".to_string(),
tooltip_body: String::new(),
},
}
}
fn night_light_menu(state: &NightLightState) -> Menu {
let settings_row = MenuItem::standard("Night light settings…", "nightlight:settings");
let (temperature, on) = match *state {
NightLightState::NotRunning => {
return radio_menu(
vec![MenuItem::disabled("Night light unavailable — hyprsunset isn't running")],
Vec::new(),
settings_row,
);
}
NightLightState::CouldNotCheck => {
return radio_menu(
vec![MenuItem::disabled("Night light status unknown")],
Vec::new(),
settings_row,
);
}
NightLightState::Known { temperature, on } => (temperature, on),
};
let toggle_action = if on {
"nightlight:off".to_string()
} else {
format!("nightlight:set:{}", NIGHT_LIGHT_PRESETS[0])
};
let toggle = MenuItem::checkmark("Night light", on, toggle_action);
let mut content = Vec::new();
for &kelvin in NIGHT_LIGHT_PRESETS {
let is_current = on && temperature == kelvin;
let action = format!("nightlight:set:{kelvin}");
if is_current {
content.push(MenuItem::checkmark(format!("{kelvin}K"), true, action));
} else {
content.push(MenuItem::standard(format!("{kelvin}K"), action));
}
}
radio_menu(vec![toggle], content, settings_row)
}
const POWER_ITEM_ID: &str = "hyprforge-power";
#[derive(Debug, Clone, Copy, PartialEq)]
enum BatteryReading {
Present(BatteryInfo),
Absent,
Unavailable,
}
#[derive(Debug, Clone, PartialEq)]
enum ProfileReading {
Known {
active: PowerProfile,
offered: Vec<PowerProfile>,
},
Unavailable,
}
fn profile_label(profile: PowerProfile) -> &'static str {
match profile {
PowerProfile::PowerSaver => "Power saver",
PowerProfile::Balanced => "Balanced",
PowerProfile::Performance => "Performance",
}
}
fn profile_icon(profile: PowerProfile) -> &'static str {
match profile {
PowerProfile::PowerSaver => "battery-profile-powersave-symbolic",
PowerProfile::Balanced => "battery-profile-balanced-symbolic",
PowerProfile::Performance => "battery-profile-performance-symbolic",
}
}
fn is_charging(state: BatteryState) -> bool {
matches!(state, BatteryState::Charging | BatteryState::PendingCharge)
}
fn battery_icon(info: &BatteryInfo) -> String {
if info.state == BatteryState::FullyCharged {
return "battery-full-charged-symbolic".to_string();
}
let level = ((u16::from(info.percentage) + 5) / 10 * 10).min(100);
let charging = if is_charging(info.state) { "-charging" } else { "" };
format!("battery-{level:03}{charging}-symbolic")
}
fn battery_words(info: &BatteryInfo) -> String {
let pct = info.percentage;
match info.state {
BatteryState::FullyCharged => "Fully charged".to_string(),
BatteryState::Charging => match info.time_remaining_words() {
Some(t) => format!("{pct}% — charging, full in {t}"),
None => format!("{pct}% — charging"),
},
BatteryState::Discharging => match info.time_remaining_words() {
Some(t) => format!("{pct}% — {t} left"),
None => format!("{pct}% — on battery"),
},
BatteryState::PendingCharge => format!("{pct}% — plugged in, not charging"),
BatteryState::PendingDischarge | BatteryState::Empty | BatteryState::Unknown => {
format!("{pct}%")
}
}
}
fn power_item(battery: &BatteryReading, profile: &ProfileReading) -> TrayItem {
let item = |icon_name: String, tooltip_title: String, tooltip_body: String| TrayItem {
id: POWER_ITEM_ID.to_string(),
category: Category::Hardware,
status: TrayStatus::Active,
title: "Power".to_string(),
icon_name,
tooltip_title,
tooltip_body,
};
let profile_words = match profile {
ProfileReading::Known { active, .. } => Some(format!("{} profile", profile_label(*active))),
ProfileReading::Unavailable => None,
};
match (battery, profile) {
(BatteryReading::Present(info), _) => item(
battery_icon(info),
battery_words(info),
profile_words.unwrap_or_default(),
),
(BatteryReading::Absent, ProfileReading::Known { active, .. }) => item(
profile_icon(*active).to_string(),
profile_words.unwrap_or_default(),
"No battery".to_string(),
),
(BatteryReading::Unavailable, ProfileReading::Known { active, .. }) => item(
profile_icon(*active).to_string(),
profile_words.unwrap_or_default(),
"Battery status unavailable — UPower isn't answering.".to_string(),
),
(BatteryReading::Absent, ProfileReading::Unavailable) => item(
"dialog-warning".to_string(),
"Power profiles unavailable".to_string(),
"power-profiles-daemon isn't running.".to_string(),
),
(BatteryReading::Unavailable, ProfileReading::Unavailable) => item(
"dialog-warning".to_string(),
"Power status unavailable".to_string(),
"Neither UPower nor power-profiles-daemon is answering.".to_string(),
),
}
}
fn power_menu(battery: &BatteryReading, profile: &ProfileReading) -> Menu {
let settings_row = MenuItem::standard("Power settings…", "power:settings");
let top = match battery {
BatteryReading::Present(info) => MenuItem::disabled(format!("Battery {}", battery_words(info))),
BatteryReading::Absent => MenuItem::disabled("Power profile"),
BatteryReading::Unavailable => MenuItem::disabled("Battery status unavailable"),
};
let content = match profile {
ProfileReading::Known { active, offered } => offered
.iter()
.map(|&p| {
let action = format!("power:profile:{}", p.as_str());
if p == *active {
MenuItem::checkmark(profile_label(p), true, action)
} else {
MenuItem::standard(profile_label(p), action)
}
})
.collect(),
ProfileReading::Unavailable => vec![MenuItem::disabled(
"Power profiles unavailable — power-profiles-daemon isn't running",
)],
};
radio_menu(vec![top], content, settings_row)
}
const DISPLAYS_ITEM_ID: &str = "hyprforge-displays";
const MAX_DISPLAY_PROFILES_SHOWN: usize = 8;
#[derive(Debug, Clone, PartialEq)]
struct DisplayProfile {
id: String,
name: String,
last_used: String,
}
#[derive(Debug, Clone, PartialEq)]
enum DisplaysReading {
Known {
profiles: Vec<DisplayProfile>,
current: Option<String>,
},
Unavailable,
}
type RevertDeadline = Arc<std::sync::Mutex<Option<tokio::time::Instant>>>;
fn revert_pending(deadline: Option<tokio::time::Instant>, now: tokio::time::Instant) -> bool {
deadline.is_some_and(|d| d > now)
}
fn displays_item(reading: &DisplaysReading, pending: bool) -> TrayItem {
let item = |status, icon: &str, tooltip_title: String, tooltip_body: String| TrayItem {
id: DISPLAYS_ITEM_ID.to_string(),
category: Category::Hardware,
status,
title: "Displays".to_string(),
icon_name: icon.to_string(),
tooltip_title,
tooltip_body,
};
if pending {
return item(
TrayStatus::NeedsAttention,
"preferences-desktop-display-randr-symbolic",
"Keep this display layout?".to_string(),
"It reverts on its own unless you keep it.".to_string(),
);
}
match reading {
DisplaysReading::Unavailable => item(
TrayStatus::Active,
"dialog-warning",
"Displays unavailable".to_string(),
"hyprforge-displayd isn't running.".to_string(),
),
DisplaysReading::Known { profiles, current } => {
let saved = match profiles.len() {
1 => "1 saved layout".to_string(),
n => format!("{n} saved layouts"),
};
let current_name = current
.as_ref()
.and_then(|id| profiles.iter().find(|p| &p.id == id))
.map(|p| p.name.clone());
item(
TrayStatus::Active,
"monitor-symbolic",
current_name.unwrap_or_else(|| "No saved layout matches".to_string()),
saved,
)
}
}
}
fn displays_menu(reading: &DisplaysReading, pending: bool) -> Menu {
let settings_row = MenuItem::standard("Display settings…", "display:settings");
if pending {
return radio_menu(
vec![MenuItem::disabled("Keep this layout? It reverts on its own.")],
vec![
MenuItem::standard("Keep this layout", "display:keep"),
MenuItem::standard("Revert now", "display:revert"),
],
settings_row,
);
}
let (profiles, current) = match reading {
DisplaysReading::Unavailable => {
return radio_menu(
vec![MenuItem::disabled("Displays unavailable — hyprforge-displayd isn't running")],
Vec::new(),
settings_row,
);
}
DisplaysReading::Known { profiles, current } => (profiles, current),
};
if profiles.is_empty() {
return radio_menu(vec![MenuItem::disabled("No saved layouts yet")], Vec::new(), settings_row);
}
let mut ordered: Vec<&DisplayProfile> = profiles.iter().collect();
ordered.sort_by(|a, b| b.last_used.cmp(&a.last_used));
let mut content: Vec<MenuItem> = ordered
.iter()
.take(MAX_DISPLAY_PROFILES_SHOWN)
.map(|p| {
let action = format!("display:apply:{}", p.id);
if current.as_deref() == Some(p.id.as_str()) {
MenuItem::checkmark(p.name.clone(), true, action)
} else {
MenuItem::standard(p.name.clone(), action)
}
})
.collect();
if ordered.len() > MAX_DISPLAY_PROFILES_SHOWN {
content.push(MenuItem::disabled(format!(
"+{} more — see Settings",
ordered.len() - MAX_DISPLAY_PROFILES_SHOWN
)));
}
radio_menu(vec![MenuItem::disabled("Display layouts")], content, settings_row)
}
#[derive(Debug, Clone, PartialEq, Eq)]
enum MenuAction {
OpenSettings(&'static str),
WifiRadio(bool),
WifiConnect(Vec<u8>),
BtRadio(bool),
BtConnect(Address),
BtDisconnect(Address),
KeepAwake(bool),
NightLightOff,
NightLightSet(i64),
PowerProfile(PowerProfile),
WiredConnect(String),
WiredDisconnect(String),
DisplayApply(String),
DisplayKeep,
DisplayRevert,
}
fn interface_name(name: &str) -> Option<String> {
let valid = !name.is_empty()
&& name.len() <= 15
&& !name.bytes().any(|b| b == b'/' || b.is_ascii_whitespace());
valid.then(|| name.to_string())
}
fn parse_menu_action(action: &str) -> Option<MenuAction> {
match action {
"wifi:settings" => Some(MenuAction::OpenSettings("network")),
"wifi:radio:on" => Some(MenuAction::WifiRadio(true)),
"wifi:radio:off" => Some(MenuAction::WifiRadio(false)),
"bt:settings" => Some(MenuAction::OpenSettings("bluetooth")),
"bt:radio:on" => Some(MenuAction::BtRadio(true)),
"bt:radio:off" => Some(MenuAction::BtRadio(false)),
"keepawake:on" => Some(MenuAction::KeepAwake(true)),
"keepawake:off" => Some(MenuAction::KeepAwake(false)),
"keepawake:settings" => Some(MenuAction::OpenSettings("power")),
"nightlight:off" => Some(MenuAction::NightLightOff),
"nightlight:settings" => Some(MenuAction::OpenSettings("night-light")),
"power:settings" => Some(MenuAction::OpenSettings("power")),
"display:settings" => Some(MenuAction::OpenSettings("displays")),
"display:keep" => Some(MenuAction::DisplayKeep),
"display:revert" => Some(MenuAction::DisplayRevert),
_ => {
if let Some(hex) = action.strip_prefix("wifi:connect:") {
hex_decode(hex).map(MenuAction::WifiConnect)
} else if let Some(addr) = action.strip_prefix("bt:connect:") {
Some(MenuAction::BtConnect(Address::new(addr)))
} else if let Some(addr) = action.strip_prefix("bt:disconnect:") {
Some(MenuAction::BtDisconnect(Address::new(addr)))
} else if let Some(interface) = action.strip_prefix("wired:connect:") {
interface_name(interface).map(MenuAction::WiredConnect)
} else if let Some(interface) = action.strip_prefix("wired:disconnect:") {
interface_name(interface).map(MenuAction::WiredDisconnect)
} else if let Some(id) = action.strip_prefix("display:apply:") {
(!id.is_empty() && id.bytes().all(|b| b.is_ascii_alphanumeric()))
.then(|| MenuAction::DisplayApply(id.to_string()))
} else if let Some(name) = action.strip_prefix("power:profile:") {
PowerProfile::parse(name).map(MenuAction::PowerProfile)
} else {
action
.strip_prefix("nightlight:set:")
.and_then(|kelvin| kelvin.parse::<i64>().ok())
.map(MenuAction::NightLightSet)
}
}
}
}
async fn scan_then_refresh(
net_state: &mut Reconnecting<hyprforge_network::NetworkManagerBackend>,
refresh: tokio::sync::mpsc::UnboundedSender<()>,
) {
let Some(backend) = network_backend(net_state).await else {
return;
};
if let Err(e) = backend.request_scan().await {
tracing::debug!(error = %e, "scan on menu open was refused");
return;
}
tokio::spawn(async move {
tokio::time::sleep(SCAN_SETTLE).await;
let _ = refresh.send(());
});
}
const NETWORK_ITEM_ID: &str = "hyprforge-network";
const SCAN_SETTLE: std::time::Duration = std::time::Duration::from_secs(3);
struct MenuClickState {
net: Reconnecting<NetworkManagerBackend>,
bt: Reconnecting<BlueZBackend>,
logind: Reconnecting<DetachedBackend>,
profiles: Reconnecting<PowerProfilesDaemonBackend>,
bus: Reconnecting<zbus::Connection>,
}
async fn handle_menu_clicks(
mut actions: tokio::sync::mpsc::UnboundedReceiver<String>,
refresh: tokio::sync::mpsc::UnboundedSender<()>,
night_light_belief: NightLightBelief,
revert_deadline: RevertDeadline,
) {
let mut state = MenuClickState {
net: Reconnecting::new(),
bt: Reconnecting::new(),
logind: Reconnecting::new(),
profiles: Reconnecting::new(),
bus: Reconnecting::new(),
};
while let Some(action) = actions.recv().await {
if let Some(id) = action.strip_prefix(hyprforge_tray::OPENED_PREFIX) {
if id == NETWORK_ITEM_ID {
scan_then_refresh(&mut state.net, refresh.clone()).await;
}
continue;
}
let Some(parsed) = parse_menu_action(&action) else {
tracing::warn!(action = %action, "unknown tray menu action; ignoring");
continue;
};
let refresh_after = matches!(
parsed,
MenuAction::PowerProfile(_) | MenuAction::WiredConnect(_) | MenuAction::WiredDisconnect(_)
);
match perform_menu_action(parsed, &mut state, &night_light_belief, &revert_deadline, &refresh)
.await
{
Ok(()) if refresh_after => {
let _ = refresh.send(());
}
Ok(()) => {}
Err(e) => tracing::warn!(error = %e, "tray menu action failed"),
}
}
}
async fn perform_menu_action(
action: MenuAction,
state: &mut MenuClickState,
night_light_belief: &NightLightBelief,
revert_deadline: &RevertDeadline,
refresh: &tokio::sync::mpsc::UnboundedSender<()>,
) -> anyhow::Result<()> {
match action {
MenuAction::OpenSettings(screen) => {
spawn_settings(screen);
Ok(())
}
MenuAction::WifiRadio(on) => {
let backend = network_backend(&mut state.net)
.await
.ok_or_else(|| anyhow::anyhow!("NetworkManager unavailable"))?;
backend.set_radio(on).await?;
Ok(())
}
MenuAction::WifiConnect(ssid_bytes) => {
let backend = network_backend(&mut state.net)
.await
.ok_or_else(|| anyhow::anyhow!("NetworkManager unavailable"))?;
let ssid = Ssid::new(ssid_bytes);
let points = backend.access_points().await?;
let Some(ap) = points.into_iter().find(|ap| ap.ssid == ssid) else {
anyhow::bail!("network no longer in range");
};
if ap.security.needs_passphrase() {
let saved = backend.saved_networks().await.unwrap_or_default();
let Some(saved) = saved.iter().find(|s| s.ssid == ap.ssid) else {
spawn_settings("network");
return Ok(());
};
backend.connect_saved(&saved.id).await?;
return Ok(());
}
backend.connect(&ap, None).await?;
Ok(())
}
MenuAction::BtRadio(on) => {
let backend = bluetooth_backend(&mut state.bt)
.await
.ok_or_else(|| anyhow::anyhow!("BlueZ unavailable"))?;
backend.set_powered(on).await?;
Ok(())
}
MenuAction::BtConnect(address) => {
let backend = bluetooth_backend(&mut state.bt)
.await
.ok_or_else(|| anyhow::anyhow!("BlueZ unavailable"))?;
backend.connect(&address).await?;
Ok(())
}
MenuAction::BtDisconnect(address) => {
let backend = bluetooth_backend(&mut state.bt)
.await
.ok_or_else(|| anyhow::anyhow!("BlueZ unavailable"))?;
backend.disconnect(&address).await?;
Ok(())
}
MenuAction::KeepAwake(on) => {
let backend = keep_awake_backend(&mut state.logind)
.await
.ok_or_else(|| anyhow::anyhow!("systemd-logind unavailable"))?;
if on {
backend.take(WhatSet::keep_awake(), KEEP_AWAKE_WHO, KEEP_AWAKE_WHY).await?;
} else {
backend.release().await?;
}
Ok(())
}
MenuAction::NightLightOff => {
tokio::task::spawn_blocking(|| Hyprsunset.turn_off()).await??;
*night_light_belief.lock().unwrap_or_else(|e| e.into_inner()) = false;
Ok(())
}
MenuAction::NightLightSet(kelvin) => {
tokio::task::spawn_blocking(move || Hyprsunset.set_temperature(kelvin)).await??;
*night_light_belief.lock().unwrap_or_else(|e| e.into_inner()) = true;
Ok(())
}
MenuAction::PowerProfile(profile) => {
let backend = profiles_backend(&mut state.profiles)
.await
.ok_or_else(|| anyhow::anyhow!("power-profiles-daemon unavailable"))?;
backend.set_active_profile(profile).await?;
Ok(())
}
MenuAction::WiredConnect(interface) => {
let backend = network_backend(&mut state.net)
.await
.ok_or_else(|| anyhow::anyhow!("NetworkManager unavailable"))?;
backend.wired_connect(&interface).await?;
Ok(())
}
MenuAction::WiredDisconnect(interface) => {
let backend = network_backend(&mut state.net)
.await
.ok_or_else(|| anyhow::anyhow!("NetworkManager unavailable"))?;
backend.wired_disconnect(&interface).await?;
Ok(())
}
MenuAction::DisplayApply(id) => {
let conn = session_bus(&mut state.bus)
.await
.ok_or_else(|| anyhow::anyhow!("session bus unavailable"))?;
let proxy = displayd_bounded(
"connecting to displayd",
DISPLAYD_CALL_TIMEOUT,
DisplaydProxy::new(&conn),
)
.await?;
let seconds = displayd_bounded(
"applying the layout",
DISPLAYD_APPLY_TIMEOUT,
proxy.apply_profile_reversible(&id),
)
.await?;
set_revert_deadline(revert_deadline, seconds, refresh);
Ok(())
}
MenuAction::DisplayKeep => {
let conn = session_bus(&mut state.bus)
.await
.ok_or_else(|| anyhow::anyhow!("session bus unavailable"))?;
let proxy = displayd_bounded(
"connecting to displayd",
DISPLAYD_CALL_TIMEOUT,
DisplaydProxy::new(&conn),
)
.await?;
displayd_bounded("keeping the layout", DISPLAYD_CALL_TIMEOUT, proxy.confirm_layout())
.await?;
clear_revert_deadline(revert_deadline, refresh);
Ok(())
}
MenuAction::DisplayRevert => {
let conn = session_bus(&mut state.bus)
.await
.ok_or_else(|| anyhow::anyhow!("session bus unavailable"))?;
let proxy = displayd_bounded(
"connecting to displayd",
DISPLAYD_CALL_TIMEOUT,
DisplaydProxy::new(&conn),
)
.await?;
displayd_bounded("reverting the layout", DISPLAYD_APPLY_TIMEOUT, proxy.revert_layout())
.await?;
clear_revert_deadline(revert_deadline, refresh);
Ok(())
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use hyprforge_bluetooth::DeviceKind;
use hyprforge_tray::menu::ItemKind;
const NETWORK_ICON_NAMES: &[&str] = &[
"network-wireless-signal-excellent-symbolic",
"network-wireless-signal-good-symbolic",
"network-wireless-signal-ok-symbolic",
"network-wireless-signal-weak-symbolic",
"network-wireless-signal-none-symbolic",
"network-wireless-disconnected-symbolic",
"network-wired-activated-symbolic",
"network-wired-symbolic",
"network-wired-disconnected-symbolic",
"network-wired-unavailable-symbolic",
"dialog-warning",
];
const BLUETOOTH_ICON_NAMES: &[&str] = &[
"network-bluetooth-activated-symbolic",
"network-bluetooth-symbolic",
"network-bluetooth-inactive-symbolic",
"dialog-warning",
];
const KEEP_AWAKE_ICON_NAMES: &[&str] =
&["changes-prevent-symbolic", "changes-allow-symbolic", "dialog-warning"];
const NIGHT_LIGHT_ICON_NAMES: &[&str] = &[
"redshift-status-on-symbolic",
"redshift-status-off-symbolic",
"dialog-warning",
"dialog-information",
];
fn net_status(radio: RadioState, connected: Option<&str>) -> NetStatus {
NetStatus { radio, connected_to: connected.map(Ssid::new) }
}
fn bt_status(state: AdapterState) -> BtStatus {
BtStatus { state, discovering: false, alias: "mock-adapter".to_string() }
}
fn device(alias: &str, connected: bool) -> Device {
Device {
address: hyprforge_bluetooth::Address::new("AA:BB:CC:DD:EE:FF"),
alias: alias.to_string(),
name: Some(alias.to_string()),
kind: DeviceKind::Headset,
paired: true,
trusted: true,
connected,
rssi: Some(-50),
}
}
#[test]
fn a_switched_off_wifi_radio_still_shows_an_icon() {
let off = network_item(Some(&net_status(RadioState::Off, None)), None, None, false);
assert_eq!(off.status, TrayStatus::Active);
let hw_off =
network_item(Some(&net_status(RadioState::HardwareOff, None)), None, None, false);
assert_eq!(hw_off.status, TrayStatus::Active, "rfkill hides the icon no more than a software switch does");
let on_unconnected = network_item(Some(&net_status(RadioState::On, None)), None, None, false);
assert_eq!(on_unconnected.status, TrayStatus::Active);
}
#[test]
fn a_switched_off_bluetooth_adapter_still_shows_an_icon() {
let off = bluetooth_item(Some(&bt_status(AdapterState::Off)), None, false);
assert_eq!(off.status, TrayStatus::Active);
let blocked = bluetooth_item(Some(&bt_status(AdapterState::HardwareBlocked)), None, false);
assert_eq!(blocked.status, TrayStatus::Active);
let on_unconnected = bluetooth_item(Some(&bt_status(AdapterState::On)), None, false);
assert_eq!(on_unconnected.status, TrayStatus::Active);
}
#[test]
fn a_radio_that_is_off_does_not_look_like_one_that_is_on_and_idle() {
let wifi_off = network_item(Some(&net_status(RadioState::Off, None)), None, None, false);
let wifi_idle = network_item(Some(&net_status(RadioState::On, None)), None, None, false);
assert_ne!(wifi_off.icon_name, wifi_idle.icon_name, "off and searching must be distinguishable");
let bt_off = bluetooth_item(Some(&bt_status(AdapterState::Off)), None, false);
let bt_idle = bluetooth_item(Some(&bt_status(AdapterState::On)), None, false);
let bt_connected = bluetooth_item(Some(&bt_status(AdapterState::On)), Some(&device("Headset", true)), false);
assert_ne!(bt_off.icon_name, bt_idle.icon_name);
assert_ne!(bt_idle.icon_name, bt_connected.icon_name, "idle and connected are three states, not two");
}
#[test]
fn no_item_this_daemon_builds_ever_asks_to_be_hidden() {
let items = [
network_item(Some(&net_status(RadioState::Off, None)), None, None, false),
network_item(Some(&net_status(RadioState::HardwareOff, None)), None, None, false),
network_item(Some(&net_status(RadioState::On, None)), None, None, false),
network_item(None, None, None, false),
bluetooth_item(Some(&bt_status(AdapterState::Off)), None, false),
bluetooth_item(Some(&bt_status(AdapterState::HardwareBlocked)), None, false),
bluetooth_item(Some(&bt_status(AdapterState::On)), None, false),
bluetooth_item(None, None, false),
night_light_item(&NightLightState::Known { temperature: 3500, on: false }),
night_light_item(&NightLightState::Known { temperature: 3500, on: true }),
night_light_item(&NightLightState::NotRunning),
keep_awake_item(Some(false), &[], false),
keep_awake_item(Some(true), &[], false),
keep_awake_item(None, &[], true),
power_item(&BatteryReading::Present(battery(40, BatteryState::Discharging)), &balanced()),
power_item(&BatteryReading::Absent, &balanced()),
power_item(&BatteryReading::Absent, &ProfileReading::Unavailable),
power_item(&BatteryReading::Unavailable, &ProfileReading::Unavailable),
displays_item(&DisplaysReading::Unavailable, false),
displays_item(&desk_and_laptop(), false),
];
for item in items {
assert_eq!(item.status, TrayStatus::Active, "{} asked to be hidden", item.id);
}
}
#[test]
fn signal_strength_maps_to_the_right_icon_at_its_boundaries() {
let cases: &[(u8, &str)] = &[
(100, "network-wireless-signal-excellent-symbolic"),
(80, "network-wireless-signal-excellent-symbolic"),
(79, "network-wireless-signal-good-symbolic"),
(60, "network-wireless-signal-good-symbolic"),
(59, "network-wireless-signal-ok-symbolic"),
(40, "network-wireless-signal-ok-symbolic"),
(39, "network-wireless-signal-weak-symbolic"),
(20, "network-wireless-signal-weak-symbolic"),
(19, "network-wireless-signal-none-symbolic"),
(0, "network-wireless-signal-none-symbolic"),
];
for &(strength, expected) in cases {
assert_eq!(
wifi_signal_icon(strength),
expected,
"strength {strength} should map to {expected}"
);
}
}
#[test]
fn an_unavailable_network_daemon_produces_an_icon_and_tooltip_that_say_so() {
let item = network_item(None, None, None, true);
assert_eq!(item.status, TrayStatus::Active, "must stay visible to be seen at all");
assert_eq!(item.icon_name, "dialog-warning");
assert!(item.tooltip_title.to_lowercase().contains("unavailable"));
let nothing_connected = network_item(Some(&net_status(RadioState::On, None)), None, None, false);
assert_ne!(
item.tooltip_title, nothing_connected.tooltip_title,
"unavailable and merely unconnected must not read the same"
);
assert_ne!(item.icon_name, nothing_connected.icon_name);
}
#[test]
fn a_missing_status_reads_the_same_as_an_explicit_unavailable_flag() {
let item = network_item(None, None, None, false);
assert!(item.tooltip_title.to_lowercase().contains("unavailable"));
}
#[test]
fn an_unavailable_bluetooth_daemon_produces_an_icon_and_tooltip_that_say_so() {
let item = bluetooth_item(None, None, true);
assert_eq!(item.status, TrayStatus::Active);
assert_eq!(item.icon_name, "dialog-warning");
assert!(item.tooltip_title.to_lowercase().contains("unavailable"));
let nothing_connected = bluetooth_item(Some(&bt_status(AdapterState::On)), None, false);
assert_ne!(item.tooltip_title, nothing_connected.tooltip_title);
assert_ne!(item.icon_name, nothing_connected.icon_name);
}
#[test]
fn a_connected_networks_tooltip_names_it() {
let ssid = Ssid::new("Coffee Shop");
let item = network_item(
Some(&net_status(RadioState::On, Some("Coffee Shop"))),
Some(&ssid),
Some(72),
false,
);
assert_eq!(item.tooltip_title, "Coffee Shop");
assert!(item.tooltip_body.contains("72"));
assert_eq!(item.icon_name, "network-wireless-signal-good-symbolic");
}
#[test]
fn a_connected_bluetooth_devices_tooltip_names_it() {
let d = device("WH-1000XM4", true);
let item = bluetooth_item(Some(&bt_status(AdapterState::On)), Some(&d), false);
assert_eq!(item.tooltip_title, "WH-1000XM4");
assert_eq!(item.tooltip_body, "Headset");
}
#[test]
fn every_network_icon_the_function_can_return_is_a_documented_freedesktop_name() {
let ssid = Ssid::new("home");
let items = [
network_item(None, None, None, true),
network_item(Some(&net_status(RadioState::Off, None)), None, None, false),
network_item(Some(&net_status(RadioState::HardwareOff, None)), None, None, false),
network_item(Some(&net_status(RadioState::On, None)), None, None, false),
network_item(Some(&net_status(RadioState::On, Some("home"))), Some(&ssid), Some(0), false),
network_item(Some(&net_status(RadioState::On, Some("home"))), Some(&ssid), Some(19), false),
network_item(Some(&net_status(RadioState::On, Some("home"))), Some(&ssid), Some(20), false),
network_item(Some(&net_status(RadioState::On, Some("home"))), Some(&ssid), Some(39), false),
network_item(Some(&net_status(RadioState::On, Some("home"))), Some(&ssid), Some(40), false),
network_item(Some(&net_status(RadioState::On, Some("home"))), Some(&ssid), Some(59), false),
network_item(Some(&net_status(RadioState::On, Some("home"))), Some(&ssid), Some(60), false),
network_item(Some(&net_status(RadioState::On, Some("home"))), Some(&ssid), Some(79), false),
network_item(Some(&net_status(RadioState::On, Some("home"))), Some(&ssid), Some(80), false),
network_item(Some(&net_status(RadioState::On, Some("home"))), Some(&ssid), Some(100), false),
network_item(Some(&net_status(RadioState::On, Some("home"))), Some(&ssid), None, false),
];
for item in items {
assert!(
NETWORK_ICON_NAMES.contains(&item.icon_name.as_str()),
"invented icon name: {}",
item.icon_name
);
}
}
#[test]
fn every_bluetooth_icon_the_function_can_return_is_a_documented_freedesktop_name() {
let connected = device("Headset", true);
let items = [
bluetooth_item(None, None, true),
bluetooth_item(Some(&bt_status(AdapterState::Off)), None, false),
bluetooth_item(Some(&bt_status(AdapterState::HardwareBlocked)), None, false),
bluetooth_item(Some(&bt_status(AdapterState::On)), None, false),
bluetooth_item(Some(&bt_status(AdapterState::Changing)), None, false),
bluetooth_item(Some(&bt_status(AdapterState::On)), Some(&connected), false),
];
for item in items {
assert!(
BLUETOOTH_ICON_NAMES.contains(&item.icon_name.as_str()),
"invented icon name: {}",
item.icon_name
);
}
}
#[test]
fn a_click_on_either_icon_maps_to_its_own_settings_screen() {
let network = network_item(Some(&net_status(RadioState::On, None)), None, None, false);
assert_eq!(network.activate_screen(), Some("network"));
let bluetooth = bluetooth_item(Some(&bt_status(AdapterState::On)), None, false);
assert_eq!(bluetooth.activate_screen(), Some("bluetooth"));
}
#[tokio::test]
async fn a_failed_connect_is_retried_on_the_next_tick_rather_than_cached() {
use std::sync::atomic::{AtomicUsize, Ordering};
let attempts = AtomicUsize::new(0);
let mut state: Reconnecting<u32> = Reconnecting::new();
let first = state
.get_or_connect(|| async {
attempts.fetch_add(1, Ordering::SeqCst);
Err::<u32, &'static str>("not running")
})
.await;
assert!(first.is_err());
assert!(
state.backend.is_none(),
"a failed connect must leave nothing cached, or a later tick would never try again"
);
let second = state
.get_or_connect(|| async {
attempts.fetch_add(1, Ordering::SeqCst);
Ok::<u32, &'static str>(42)
})
.await;
assert_eq!(*second.unwrap(), 42);
assert_eq!(attempts.load(Ordering::SeqCst), 2, "both ticks must have called connect");
let calls_before_third = attempts.load(Ordering::SeqCst);
let third = state
.get_or_connect(|| async {
attempts.fetch_add(1, Ordering::SeqCst);
Ok::<u32, &'static str>(99)
})
.await;
assert_eq!(*third.unwrap(), 42, "a cached success is reused, not replaced");
assert_eq!(
attempts.load(Ordering::SeqCst),
calls_before_third,
"a cached success must not reconnect"
);
}
#[test]
fn a_wanted_icon_that_is_not_up_yet_should_be_registered() {
assert_eq!(icon_action(true, None, MenuServing::Popup), IconAction::Register);
}
#[test]
fn an_unwanted_icon_that_is_currently_up_should_be_dropped_not_left_registered() {
assert_eq!(icon_action(false, Some(MenuServing::Popup), MenuServing::Popup), IconAction::Drop);
}
#[test]
fn an_icon_already_matching_the_preference_is_left_alone_either_way() {
assert_eq!(icon_action(true, Some(MenuServing::Popup), MenuServing::Popup), IconAction::Keep);
assert_eq!(icon_action(false, None, MenuServing::Popup), IconAction::Keep);
}
#[test]
fn a_change_of_menu_mode_registers_a_shown_icon_again() {
assert_eq!(icon_action(true, Some(MenuServing::Popup), MenuServing::Dbusmenu), IconAction::Reregister);
assert_eq!(icon_action(true, Some(MenuServing::Dbusmenu), MenuServing::Popup), IconAction::Reregister);
assert_eq!(icon_action(true, Some(MenuServing::Dbusmenu), MenuServing::Dbusmenu), IconAction::Keep);
}
#[test]
fn a_change_of_menu_mode_never_brings_back_a_hidden_icon() {
assert_eq!(icon_action(false, None, MenuServing::Dbusmenu), IconAction::Keep);
assert_eq!(icon_action(false, Some(MenuServing::Popup), MenuServing::Dbusmenu), IconAction::Drop);
}
#[test]
fn an_icon_switched_off_and_not_showing_is_never_sampled() {
assert!(!needs_sampling(false, false));
}
#[test]
fn every_icon_that_is_shown_or_about_to_change_is_still_sampled() {
assert!(needs_sampling(true, false), "about to be registered");
assert!(needs_sampling(true, true), "showing, and still wanted");
assert!(needs_sampling(false, true), "showing, and about to be dropped");
}
static CONFIG_ENV_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
fn with_temp_config_home<T>(f: impl FnOnce(&std::path::Path) -> T) -> T {
let _lock = CONFIG_ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let dir = tempfile::tempdir().unwrap();
let previous = std::env::var_os("XDG_CONFIG_HOME");
unsafe {
std::env::set_var("XDG_CONFIG_HOME", dir.path());
}
let out = f(dir.path());
match previous {
Some(p) => unsafe { std::env::set_var("XDG_CONFIG_HOME", p) },
None => unsafe { std::env::remove_var("XDG_CONFIG_HOME") },
}
out
}
#[test]
fn a_missing_tray_toml_refreshes_to_the_defaults() {
with_temp_config_home(|_dir| {
let mut current = Prefs { network: false, bluetooth: false, keep_awake: false, night_light: false, power: false, displays: false, menu_y_offset: 32, menu_closes_on_click_outside: true, menu: prefs::MenuMode::Auto };
let mut warned = false;
refresh_prefs(&mut current, &mut warned);
assert!(current.network, "a missing file is first run: both icons shown");
assert!(current.bluetooth);
assert!(!warned);
});
}
#[test]
fn an_unreadable_tray_toml_leaves_the_current_preferences_untouched() {
with_temp_config_home(|dir| {
let tray_toml = dir.join("hyprforge").join("tray.toml");
std::fs::create_dir_all(tray_toml.parent().unwrap()).unwrap();
std::fs::write(&tray_toml, "network = yes please\n").unwrap();
let mut current = Prefs { network: true, bluetooth: false, keep_awake: false, night_light: false, power: false, displays: false, menu_y_offset: 32, menu_closes_on_click_outside: true, menu: prefs::MenuMode::Auto };
let mut warned = false;
refresh_prefs(&mut current, &mut warned);
assert_eq!(
current,
Prefs { network: true, bluetooth: false, keep_awake: false, night_light: false, power: false, displays: false, menu_y_offset: 32, menu_closes_on_click_outside: true, menu: prefs::MenuMode::Auto },
"a failed read must not change what is currently shown"
);
assert!(warned, "the failure is reported");
});
}
#[test]
fn a_persistent_parse_failure_warns_only_once() {
with_temp_config_home(|dir| {
let tray_toml = dir.join("hyprforge").join("tray.toml");
std::fs::create_dir_all(tray_toml.parent().unwrap()).unwrap();
std::fs::write(&tray_toml, "network = yes please\n").unwrap();
let mut current = Prefs::default();
let mut warned = false;
refresh_prefs(&mut current, &mut warned);
assert!(warned);
let warned_before = warned;
refresh_prefs(&mut current, &mut warned);
assert_eq!(warned, warned_before, "still warned, but not re-logged");
std::fs::write(&tray_toml, "network = false\n").unwrap();
refresh_prefs(&mut current, &mut warned);
assert!(!warned, "a successful read clears the suppression");
});
}
use hyprforge_network::Security;
fn access_point(ssid: &str, security: Security) -> AccessPoint {
AccessPoint {
ssid: Ssid::new(ssid),
bssid: "00:11:22:33:44:55".to_string(),
strength: 70,
frequency_mhz: 5180,
security,
}
}
fn saved(ssid: &str) -> SavedNetwork {
SavedNetwork { ssid: Ssid::new(ssid), id: format!("conn-{ssid}"), autoconnect: true }
}
fn bt_device(alias: &str, paired: bool, connected: bool) -> Device {
Device {
address: Address::new("AA:BB:CC:DD:EE:FF"),
alias: alias.to_string(),
name: Some(alias.to_string()),
kind: DeviceKind::Headset,
paired,
trusted: paired,
connected,
rssi: Some(-50),
}
}
#[test]
fn the_connected_network_appears_as_a_checked_row() {
let ssid = Ssid::new("home");
let menu = network_menu(
Some(&net_status(RadioState::On, Some("home"))),
Some(&ssid),
false,
&[access_point("home", Security::Wpa2Personal), access_point("cafe", Security::Open)],
&[saved("home")],
);
let row = menu.flatten().into_iter().find(|i| i.label == "home").unwrap();
assert_eq!(row.kind, ItemKind::Checkmark);
assert_eq!(row.toggle, Some(true));
let other = menu.flatten().into_iter().find(|i| i.label == "cafe").unwrap();
assert_ne!(other.toggle, Some(true));
}
#[test]
fn an_enterprise_network_is_listed_but_disabled_and_says_why() {
let menu = network_menu(
Some(&net_status(RadioState::On, None)),
None,
false,
&[access_point("corp-wifi", Security::Enterprise)],
&[],
);
let row = menu.flatten().into_iter().find(|i| i.label.contains("corp-wifi")).unwrap();
assert!(!row.enabled);
assert!(row.action.is_none());
assert!(
row.label.to_lowercase().contains("enterprise"),
"row must say why it can't be joined: {}",
row.label
);
}
#[test]
fn an_unpaired_device_is_listed_and_hands_pairing_to_the_screen_that_can_do_it() {
let menu = bluetooth_menu(
Some(&bt_status(AdapterState::On)),
false,
&[bt_device("New Headphones", false, false)],
);
let row = menu
.flatten()
.into_iter()
.find(|i| i.label.contains("New Headphones"))
.expect("an unpaired device is still listed");
assert!(row.enabled, "a row that can do something must be clickable");
assert_eq!(
row.action.as_deref(),
Some("bt:settings"),
"pairing is handed to the screen, never attempted from the menu"
);
assert!(
row.label.contains("Settings"),
"the row says where it goes rather than looking like a connect: {}",
row.label
);
}
#[test]
fn a_radio_that_is_off_still_produces_a_usable_wifi_menu_with_a_settings_entry() {
let menu = network_menu(Some(&net_status(RadioState::Off, None)), None, false, &[], &[]);
assert!(!menu.items.is_empty(), "an empty popup reads as broken");
assert!(menu.flatten().iter().any(|i| i.label.contains("Network settings")));
}
#[test]
fn a_radio_that_is_off_still_produces_a_usable_bluetooth_menu_with_a_settings_entry() {
let menu = bluetooth_menu(Some(&bt_status(AdapterState::Off)), false, &[]);
assert!(!menu.items.is_empty());
assert!(menu.flatten().iter().any(|i| i.label.contains("Bluetooth settings")));
}
#[test]
fn an_unavailable_network_daemon_produces_a_menu_that_says_so_rather_than_an_empty_one() {
let menu = network_menu(None, None, true, &[], &[]);
assert!(!menu.items.is_empty());
assert!(menu.flatten().iter().any(|i| i.label.to_lowercase().contains("unavailable")));
assert!(menu.flatten().iter().any(|i| i.label.contains("Network settings")));
}
#[test]
fn an_unavailable_bluetooth_daemon_produces_a_menu_that_says_so_rather_than_an_empty_one() {
let menu = bluetooth_menu(None, true, &[]);
assert!(!menu.items.is_empty());
assert!(menu.flatten().iter().any(|i| i.label.to_lowercase().contains("unavailable")));
assert!(menu.flatten().iter().any(|i| i.label.contains("Bluetooth settings")));
}
#[test]
fn a_passphrase_network_with_no_saved_connection_routes_to_settings_not_a_join_attempt() {
let menu = network_menu(
Some(&net_status(RadioState::On, None)),
None,
false,
&[access_point("neighbour", Security::Wpa2Personal)],
&[], );
let row = menu.flatten().into_iter().find(|i| i.label == "neighbour").unwrap();
assert_eq!(row.action.as_deref(), Some("wifi:settings"));
assert_eq!(parse_menu_action(row.action.as_deref().unwrap()), Some(MenuAction::OpenSettings("network")));
}
#[test]
fn a_passphrase_network_with_a_saved_connection_gets_a_connect_action() {
let menu = network_menu(
Some(&net_status(RadioState::On, None)),
None,
false,
&[access_point("home", Security::Wpa2Personal)],
&[saved("home")],
);
let row = menu.flatten().into_iter().find(|i| i.label == "home").unwrap();
let action = row.action.as_deref().unwrap();
assert!(action.starts_with("wifi:connect:"), "expected a connect action, got {action:?}");
}
#[test]
fn the_prefs_stamp_sees_a_file_appear_change_and_vanish() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("tray.toml");
assert_eq!(prefs_stamp(&path), None, "a file that is not there");
std::fs::write(&path, "network = true\n").unwrap();
let created = prefs_stamp(&path);
assert!(created.is_some(), "a file that appeared");
std::fs::write(&path, "network = false\nbluetooth = false\n").unwrap();
assert_ne!(prefs_stamp(&path), created, "a file whose contents changed");
std::fs::remove_file(&path).unwrap();
assert_eq!(prefs_stamp(&path), None, "a file that was deleted");
}
#[test]
fn a_menu_open_is_not_mistaken_for_a_click() {
let opened = format!(
"{}{}",
hyprforge_tray::OPENED_PREFIX,
NETWORK_ITEM_ID
);
assert!(
opened.strip_prefix(hyprforge_tray::OPENED_PREFIX).is_some(),
"the open event must be recognisable by its prefix"
);
assert!(
parse_menu_action(&opened).is_none(),
"an open is an event, not an action"
);
}
#[test]
fn the_network_items_id_is_the_one_the_open_event_carries() {
let (item, _menu) = {
let status = net_status(RadioState::On, None);
(network_item(Some(&status), None, None, false), ())
};
assert_eq!(item.id, NETWORK_ITEM_ID);
}
#[test]
fn an_unknown_action_string_is_ignored_rather_than_misrouted() {
assert_eq!(parse_menu_action(""), None);
assert_eq!(parse_menu_action("wifi:teleport"), None);
assert_eq!(parse_menu_action("bt:levitate:AA:BB"), None);
assert_eq!(parse_menu_action("settings"), None, "the vocabulary is domain-prefixed");
}
#[test]
fn every_parseable_action_round_trips_to_a_distinct_operation() {
assert_eq!(parse_menu_action("wifi:settings"), Some(MenuAction::OpenSettings("network")));
assert_eq!(parse_menu_action("bt:settings"), Some(MenuAction::OpenSettings("bluetooth")));
assert_eq!(parse_menu_action("wifi:radio:on"), Some(MenuAction::WifiRadio(true)));
assert_eq!(parse_menu_action("wifi:radio:off"), Some(MenuAction::WifiRadio(false)));
assert_eq!(parse_menu_action("bt:radio:on"), Some(MenuAction::BtRadio(true)));
assert_eq!(parse_menu_action("bt:radio:off"), Some(MenuAction::BtRadio(false)));
assert_eq!(
parse_menu_action("wifi:connect:686f6d65"),
Some(MenuAction::WifiConnect(b"home".to_vec()))
);
assert_eq!(
parse_menu_action("bt:connect:AA:BB:CC:DD:EE:FF"),
Some(MenuAction::BtConnect(Address::new("AA:BB:CC:DD:EE:FF")))
);
assert_eq!(
parse_menu_action("bt:disconnect:AA:BB:CC:DD:EE:FF"),
Some(MenuAction::BtDisconnect(Address::new("AA:BB:CC:DD:EE:FF")))
);
}
#[test]
fn a_malformed_hex_ssid_in_a_connect_action_is_ignored_rather_than_panicking() {
assert_eq!(parse_menu_action("wifi:connect:zz"), None);
assert_eq!(parse_menu_action("wifi:connect:abc"), None, "odd length is not valid hex");
}
#[test]
fn every_action_a_built_menu_can_contain_parses_back_to_the_operation_that_built_it() {
let net_menus = [
network_menu(None, None, true, &[], &[]),
network_menu(Some(&net_status(RadioState::Off, None)), None, false, &[], &[]),
network_menu(Some(&net_status(RadioState::HardwareOff, None)), None, false, &[], &[]),
network_menu(
Some(&net_status(RadioState::On, Some("home"))),
Some(&Ssid::new("home")),
false,
&[
access_point("home", Security::Wpa2Personal),
access_point("open-cafe", Security::Open),
access_point("corp", Security::Enterprise),
access_point("neighbour", Security::Wpa3Personal),
],
&[saved("home")],
),
];
let bt_menus = [
bluetooth_menu(None, true, &[]),
bluetooth_menu(Some(&bt_status(AdapterState::Off)), false, &[]),
bluetooth_menu(Some(&bt_status(AdapterState::HardwareBlocked)), false, &[]),
bluetooth_menu(
Some(&bt_status(AdapterState::On)),
false,
&[
bt_device("Connected Buds", true, true),
bt_device("Paired Keyboard", true, false),
bt_device("Stranger Phone", false, false),
],
),
];
for menu in net_menus.iter().chain(bt_menus.iter()) {
for item in menu.flatten() {
let Some(action) = item.action.as_deref() else { continue };
if action.is_empty() {
continue;
}
assert!(
parse_menu_action(action).is_some(),
"action {action:?} on row {:?} does not parse back to an operation",
item.label
);
}
}
}
#[test]
fn hex_round_trips_arbitrary_bytes_including_non_utf8_ones() {
let bytes = vec![0u8, 1, 254, 255, b'a'];
assert_eq!(hex_decode(&hex_encode(&bytes)), Some(bytes));
}
fn inhibitor(who: &str, why: &str) -> InhibitorInfo {
InhibitorInfo {
what: "sleep:idle".to_string(),
who: who.to_string(),
why: why.to_string(),
mode: "block".to_string(),
uid: 1000,
pid: 4242,
}
}
#[test]
fn every_keep_awake_icon_the_function_can_return_is_a_documented_freedesktop_name() {
let other = [inhibitor("systemd-inhibit", "working")];
let items = [
keep_awake_item(None, &[], true),
keep_awake_item(Some(false), &[], false),
keep_awake_item(Some(true), &[], false),
keep_awake_item(Some(true), &other, false),
keep_awake_item(Some(false), &other, false),
];
for item in items {
assert!(
KEEP_AWAKE_ICON_NAMES.contains(&item.icon_name.as_str()),
"invented icon name: {}",
item.icon_name
);
}
}
#[test]
fn keep_awake_stays_visible_whether_or_not_it_is_holding_the_machine_awake() {
let off = keep_awake_item(Some(false), &[], false);
assert_eq!(
off.status,
TrayStatus::Active,
"a switch the user cannot see is a switch they cannot flip"
);
assert_eq!(on_icon(&off), "changes-allow-symbolic");
let on = keep_awake_item(Some(true), &[], false);
assert_eq!(on.status, TrayStatus::Active);
assert_eq!(on_icon(&on), "changes-prevent-symbolic");
}
fn on_icon(item: &TrayItem) -> &str {
&item.icon_name
}
#[test]
fn an_unavailable_logind_produces_an_icon_and_tooltip_that_say_so() {
let item = keep_awake_item(None, &[], true);
assert_eq!(item.status, TrayStatus::Active, "must stay visible to be seen at all");
assert!(item.tooltip_title.to_lowercase().contains("unavailable"));
}
#[test]
fn our_own_inhibitor_is_excluded_from_the_list_of_other_holders() {
let all = vec![inhibitor(KEEP_AWAKE_WHO, KEEP_AWAKE_WHY), inhibitor("mpv", "playing a video")];
let others = other_inhibitors(all);
assert_eq!(others.len(), 1);
assert_eq!(others[0].who, "mpv");
}
#[test]
fn the_keep_awake_holder_is_not_listed_as_another_holder() {
let all = vec![
inhibitor(hyprforge_power::keep_awake::HOLDER_WHO, "Keep awake is on"),
inhibitor("mpv", "playing a video"),
];
let others = other_inhibitors(all);
assert_eq!(others.len(), 1);
assert_eq!(others[0].who, "mpv");
}
#[test]
fn another_processs_inhibitor_appears_in_the_keep_awake_menu_and_ours_does_not_appear_twice() {
let all = vec![inhibitor(KEEP_AWAKE_WHO, KEEP_AWAKE_WHY), inhibitor("mpv", "playing a video")];
let others = other_inhibitors(all);
let menu = keep_awake_menu(Some(true), &others, false);
let rows = menu.flatten();
let mpv_rows: Vec<_> = rows.iter().filter(|i| i.label.contains("mpv")).collect();
assert_eq!(mpv_rows.len(), 1, "the other holder should appear exactly once");
assert!(!mpv_rows[0].enabled, "another process's lock cannot be released from here");
let own_rows: Vec<_> = rows.iter().filter(|i| i.label.contains(KEEP_AWAKE_WHO)).collect();
assert!(own_rows.is_empty(), "our own inhibitor must not appear in the disabled list too");
}
#[test]
fn the_keep_awake_menus_checkmark_reflects_whether_we_are_holding_it() {
let held = keep_awake_menu(Some(true), &[], false);
let row = held.flatten().into_iter().find(|i| i.kind == ItemKind::Checkmark).unwrap();
assert_eq!(row.toggle, Some(true));
assert_eq!(row.action.as_deref(), Some("keepawake:off"));
let not_held = keep_awake_menu(Some(false), &[], false);
let row = not_held.flatten().into_iter().find(|i| i.kind == ItemKind::Checkmark).unwrap();
assert_eq!(row.toggle, Some(false));
assert_eq!(row.action.as_deref(), Some("keepawake:on"));
}
#[test]
fn every_night_light_icon_the_function_can_return_is_a_documented_freedesktop_name() {
let items = [
night_light_item(&NightLightState::NotRunning),
night_light_item(&NightLightState::CouldNotCheck),
night_light_item(&NightLightState::Known { temperature: 4500, on: true }),
night_light_item(&NightLightState::Known { temperature: 4500, on: false }),
];
for item in items {
assert!(
NIGHT_LIGHT_ICON_NAMES.contains(&item.icon_name.as_str()),
"invented icon name: {}",
item.icon_name
);
}
}
#[test]
fn hyprsunset_not_running_produces_a_menu_that_says_so_distinct_from_night_light_off() {
let not_running = night_light_item(&NightLightState::NotRunning);
let off = night_light_item(&NightLightState::Known { temperature: 4500, on: false });
assert_ne!(not_running.icon_name, off.icon_name);
assert_ne!(not_running.tooltip_title, off.tooltip_title);
assert!(not_running.tooltip_title.to_lowercase().contains("unavailable"));
assert!(off.tooltip_title.to_lowercase().contains("off"));
let not_running_menu = night_light_menu(&NightLightState::NotRunning);
let off_menu = night_light_menu(&NightLightState::Known { temperature: 4500, on: false });
assert_ne!(not_running_menu, off_menu);
assert!(not_running_menu
.flatten()
.iter()
.any(|i| i.label.to_lowercase().contains("running")));
assert!(off_menu.flatten().iter().any(|i| i.kind == ItemKind::Checkmark));
assert!(!not_running_menu.flatten().iter().any(|i| i.kind == ItemKind::Checkmark));
}
#[test]
fn a_failed_check_reads_differently_from_not_running_and_from_off() {
let could_not_check = night_light_item(&NightLightState::CouldNotCheck);
let not_running = night_light_item(&NightLightState::NotRunning);
let off = night_light_item(&NightLightState::Known { temperature: 4500, on: false });
assert_ne!(could_not_check.icon_name, not_running.icon_name);
assert_ne!(could_not_check.icon_name, off.icon_name);
assert_ne!(could_not_check.tooltip_title, not_running.tooltip_title);
assert_ne!(could_not_check.tooltip_title, off.tooltip_title);
}
#[test]
fn a_known_reading_shows_the_temperature_only_while_believed_on() {
let on = night_light_item(&NightLightState::Known { temperature: 3500, on: true });
assert!(on.tooltip_body.contains("3500"));
assert_eq!(on.status, TrayStatus::Active);
let off = night_light_item(&NightLightState::Known { temperature: 3500, on: false });
assert_eq!(off.status, TrayStatus::Active, "a toggle has to be visible in both positions");
assert!(!off.tooltip_body.contains("3500"), "off has nothing numeric to show");
}
#[test]
fn the_current_preset_is_shown_checked_and_others_are_not() {
let menu = night_light_menu(&NightLightState::Known { temperature: 3500, on: true });
let current = menu.flatten().into_iter().find(|i| i.label == "3500K").unwrap();
assert_eq!(current.toggle, Some(true));
let other = menu.flatten().into_iter().find(|i| i.label == "4500K").unwrap();
assert_ne!(other.toggle, Some(true));
}
#[test]
fn every_keep_awake_and_night_light_action_parses_back_to_an_operation() {
let other = [inhibitor("mpv", "playing a video")];
let keep_awake_menus = [
keep_awake_menu(None, &[], true),
keep_awake_menu(Some(false), &[], false),
keep_awake_menu(Some(true), &other, false),
];
let night_light_menus = [
night_light_menu(&NightLightState::NotRunning),
night_light_menu(&NightLightState::CouldNotCheck),
night_light_menu(&NightLightState::Known { temperature: 4500, on: true }),
night_light_menu(&NightLightState::Known { temperature: 2700, on: false }),
];
for menu in keep_awake_menus.iter().chain(night_light_menus.iter()) {
for item in menu.flatten() {
let Some(action) = item.action.as_deref() else { continue };
if action.is_empty() {
continue;
}
assert!(
parse_menu_action(action).is_some(),
"action {action:?} on row {:?} does not parse back to an operation",
item.label
);
}
}
}
#[test]
fn keep_awake_and_night_light_actions_round_trip_to_distinct_operations() {
assert_eq!(parse_menu_action("keepawake:on"), Some(MenuAction::KeepAwake(true)));
assert_eq!(parse_menu_action("keepawake:off"), Some(MenuAction::KeepAwake(false)));
assert_eq!(parse_menu_action("nightlight:off"), Some(MenuAction::NightLightOff));
assert_eq!(parse_menu_action("nightlight:set:2700"), Some(MenuAction::NightLightSet(2700)));
assert_eq!(parse_menu_action("nightlight:set:not-a-number"), None);
}
#[test]
fn every_menus_unavailable_state_still_offers_its_settings_row() {
let menus = [
network_menu(None, None, true, &[], &[]),
bluetooth_menu(None, true, &[]),
keep_awake_menu(None, &[], true),
night_light_menu(&NightLightState::NotRunning),
night_light_menu(&NightLightState::CouldNotCheck),
power_menu(&BatteryReading::Unavailable, &ProfileReading::Unavailable),
displays_menu(&DisplaysReading::Unavailable, false),
displays_menu(&DisplaysReading::Known { profiles: vec![], current: None }, false),
displays_menu(&DisplaysReading::Unavailable, true),
];
for menu in menus {
let last = menu.items.last().expect("a menu with no rows at all");
assert!(last.enabled, "the settings row must still be clickable: {}", last.label);
assert!(
last.label.to_lowercase().contains("settings"),
"the last row of an unavailable menu must be its settings row, got {:?}",
last.label
);
}
}
#[test]
fn every_menus_ordinary_state_ends_with_its_own_settings_row() {
let net = network_menu(Some(&net_status(RadioState::On, None)), None, false, &[], &[]);
assert_eq!(net.items.last().unwrap().label, "Network settings…");
let bt = bluetooth_menu(Some(&bt_status(AdapterState::On)), false, &[]);
assert_eq!(bt.items.last().unwrap().label, "Bluetooth settings…");
let ka = keep_awake_menu(Some(false), &[], false);
assert_eq!(ka.items.last().unwrap().label, "Keep awake settings…");
let nl = night_light_menu(&NightLightState::Known { temperature: 4500, on: true });
assert_eq!(nl.items.last().unwrap().label, "Night light settings…");
let pw = power_menu(&BatteryReading::Absent, &balanced());
assert_eq!(pw.items.last().unwrap().label, "Power settings…");
let dp = displays_menu(&desk_and_laptop(), false);
assert_eq!(dp.items.last().unwrap().label, "Display settings…");
}
#[test]
fn the_new_settings_rows_open_the_same_screens_a_left_click_on_that_icon_does() {
let ka = keep_awake_menu(Some(false), &[], false);
let ka_row = ka.items.last().unwrap();
assert_eq!(
parse_menu_action(ka_row.action.as_deref().unwrap()),
Some(MenuAction::OpenSettings("power"))
);
let nl = night_light_menu(&NightLightState::Known { temperature: 4500, on: true });
let nl_row = nl.items.last().unwrap();
assert_eq!(
parse_menu_action(nl_row.action.as_deref().unwrap()),
Some(MenuAction::OpenSettings("night-light"))
);
let pw = power_menu(&BatteryReading::Absent, &balanced());
assert_eq!(
parse_menu_action(pw.items.last().unwrap().action.as_deref().unwrap()),
Some(MenuAction::OpenSettings("power"))
);
let dp = displays_menu(&desk_and_laptop(), false);
assert_eq!(
parse_menu_action(dp.items.last().unwrap().action.as_deref().unwrap()),
Some(MenuAction::OpenSettings("displays"))
);
}
#[test]
fn the_keep_awake_toggles_label_is_a_noun_like_the_other_three() {
let menu = keep_awake_menu(Some(true), &[], false);
let toggle = menu.flatten().into_iter().find(|i| i.kind == ItemKind::Checkmark).unwrap();
assert_eq!(toggle.label, "Keep awake");
}
#[test]
fn the_keep_awake_toggles_id_does_not_move_when_other_inhibitors_appear() {
let alone = keep_awake_menu(Some(true), &[], false);
let others = [inhibitor("mpv", "playing a video")];
let with_others = keep_awake_menu(Some(true), &others, false);
let alone_toggle = alone.flatten().into_iter().find(|i| i.kind == ItemKind::Checkmark).unwrap();
let with_others_toggle =
with_others.flatten().into_iter().find(|i| i.kind == ItemKind::Checkmark).unwrap();
assert_eq!(alone_toggle.id, with_others_toggle.id);
}
fn battery(percentage: u8, state: BatteryState) -> BatteryInfo {
BatteryInfo { percentage, state, time_to_empty: None, time_to_full: None }
}
fn balanced() -> ProfileReading {
ProfileReading::Known {
active: PowerProfile::Balanced,
offered: vec![PowerProfile::PowerSaver, PowerProfile::Balanced, PowerProfile::Performance],
}
}
fn power_icon_names() -> Vec<String> {
let mut names = Vec::new();
for level in (0..=100).step_by(10) {
names.push(format!("battery-{level:03}-symbolic"));
names.push(format!("battery-{level:03}-charging-symbolic"));
}
names.extend(
[
"battery-full-charged-symbolic",
"battery-profile-powersave-symbolic",
"battery-profile-balanced-symbolic",
"battery-profile-performance-symbolic",
"dialog-warning",
]
.map(String::from),
);
names
}
#[test]
fn every_power_icon_the_function_can_return_is_a_name_breeze_ships() {
let known = power_icon_names();
let states = [
BatteryState::Charging,
BatteryState::Discharging,
BatteryState::Empty,
BatteryState::FullyCharged,
BatteryState::PendingCharge,
BatteryState::PendingDischarge,
BatteryState::Unknown,
];
let profiles = [
ProfileReading::Unavailable,
ProfileReading::Known { active: PowerProfile::PowerSaver, offered: vec![] },
ProfileReading::Known { active: PowerProfile::Balanced, offered: vec![] },
ProfileReading::Known { active: PowerProfile::Performance, offered: vec![] },
];
let mut readings = vec![BatteryReading::Absent, BatteryReading::Unavailable];
for pct in 0..=100 {
for state in states {
readings.push(BatteryReading::Present(battery(pct, state)));
}
}
for reading in &readings {
for profile in &profiles {
let icon = power_item(reading, profile).icon_name;
assert!(known.contains(&icon), "{icon:?} is not a name this daemon may emit");
}
}
}
#[test]
fn the_battery_level_rounds_to_the_nearest_icon_step() {
let icon = |pct| battery_icon(&battery(pct, BatteryState::Discharging));
assert_eq!(icon(0), "battery-000-symbolic");
assert_eq!(icon(4), "battery-000-symbolic");
assert_eq!(icon(5), "battery-010-symbolic");
assert_eq!(icon(9), "battery-010-symbolic");
assert_eq!(icon(94), "battery-090-symbolic");
assert_eq!(icon(95), "battery-100-symbolic");
assert_eq!(icon(100), "battery-100-symbolic");
}
#[test]
fn a_charging_battery_draws_the_plug_and_a_full_one_its_own_icon() {
assert_eq!(
battery_icon(&battery(62, BatteryState::Charging)),
"battery-060-charging-symbolic"
);
assert_eq!(
battery_icon(&battery(62, BatteryState::PendingCharge)),
"battery-060-charging-symbolic"
);
assert_eq!(
battery_icon(&battery(100, BatteryState::FullyCharged)),
"battery-full-charged-symbolic"
);
}
#[test]
fn a_machine_with_no_battery_shows_its_power_profile_not_a_warning() {
let item = power_item(&BatteryReading::Absent, &balanced());
assert_eq!(item.icon_name, "battery-profile-balanced-symbolic");
assert_eq!(item.tooltip_title, "Balanced profile");
assert_eq!(item.tooltip_body, "No battery");
}
#[test]
fn upower_being_down_keeps_the_profile_and_does_not_claim_there_is_no_battery() {
let item = power_item(&BatteryReading::Unavailable, &balanced());
assert_eq!(item.icon_name, "battery-profile-balanced-symbolic");
assert!(item.tooltip_body.contains("UPower"), "{}", item.tooltip_body);
assert!(!item.tooltip_body.contains("No battery"));
let menu = power_menu(&BatteryReading::Unavailable, &balanced());
assert_eq!(menu.items[0].label, "Battery status unavailable");
assert!(menu.items.iter().any(|i| i.label == "Balanced" && i.action.is_some()));
}
#[test]
fn the_profile_daemon_being_down_keeps_the_battery_and_says_why_profiles_are_missing() {
let reading = BatteryReading::Present(BatteryInfo {
time_to_empty: Some(Duration::from_secs(92 * 60)),
..battery(73, BatteryState::Discharging)
});
let item = power_item(&reading, &ProfileReading::Unavailable);
assert_eq!(item.icon_name, "battery-070-symbolic");
assert_eq!(item.tooltip_title, "73% — 1h 32m left");
assert_eq!(item.tooltip_body, "", "no profile claimed when it could not be read");
let menu = power_menu(&reading, &ProfileReading::Unavailable);
assert_eq!(menu.items[0].label, "Battery 73% — 1h 32m left");
assert!(!menu.items[0].enabled, "the battery row is information, not a control");
assert!(menu
.items
.iter()
.any(|i| i.label.contains("power-profiles-daemon isn't running") && !i.enabled));
}
#[test]
fn the_profile_rows_are_what_the_daemon_offers_with_the_active_one_checked() {
let profile = ProfileReading::Known {
active: PowerProfile::PowerSaver,
offered: vec![PowerProfile::PowerSaver, PowerProfile::Balanced],
};
let menu = power_menu(&BatteryReading::Absent, &profile);
let rows: Vec<_> = menu
.items
.iter()
.filter(|i| i.action.as_deref().is_some_and(|a| a.starts_with("power:profile:")))
.collect();
assert_eq!(rows.len(), 2, "Performance was not offered and must not be listed");
assert_eq!(rows[0].label, "Power saver");
assert_eq!((rows[0].kind, rows[0].toggle), (ItemKind::Checkmark, Some(true)));
assert_eq!(rows[1].label, "Balanced");
assert_eq!(
parse_menu_action(rows[1].action.as_deref().unwrap()),
Some(MenuAction::PowerProfile(PowerProfile::Balanced))
);
}
#[test]
fn a_profile_name_the_daemon_never_defined_is_ignored_rather_than_sent() {
assert_eq!(parse_menu_action("power:profile:turbo"), None);
assert_eq!(parse_menu_action("power:profile:"), None);
}
#[test]
fn a_battery_held_below_full_on_mains_says_so() {
assert_eq!(
battery_words(&battery(80, BatteryState::PendingCharge)),
"80% — plugged in, not charging"
);
}
fn profile(id: &str, name: &str, last_used: &str) -> DisplayProfile {
DisplayProfile { id: id.to_string(), name: name.to_string(), last_used: last_used.to_string() }
}
fn desk_and_laptop() -> DisplaysReading {
DisplaysReading::Known {
profiles: vec![
profile("aa11", "Laptop only", "2026-09-20T08:00:00Z"),
profile("bb22", "Desk", "2026-09-23T09:00:00Z"),
],
current: Some("bb22".to_string()),
}
}
const DISPLAYS_ICON_NAMES: &[&str] =
&["monitor-symbolic", "preferences-desktop-display-randr-symbolic", "dialog-warning"];
#[test]
fn every_displays_icon_the_function_can_return_is_in_the_allowed_set() {
let readings = [
DisplaysReading::Unavailable,
DisplaysReading::Known { profiles: vec![], current: None },
desk_and_laptop(),
];
for reading in &readings {
for pending in [false, true] {
let icon = displays_item(reading, pending).icon_name;
assert!(DISPLAYS_ICON_NAMES.contains(&icon.as_str()), "{icon:?}");
}
}
}
#[test]
fn a_layout_waiting_to_be_kept_asks_for_attention_with_an_icon_of_its_own() {
let waiting = displays_item(&desk_and_laptop(), true);
assert_eq!(waiting.status, TrayStatus::NeedsAttention);
assert_eq!(waiting.icon_name, "preferences-desktop-display-randr-symbolic");
let settled = displays_item(&desk_and_laptop(), false);
assert_eq!(settled.status, TrayStatus::Active);
assert_ne!(settled.icon_name, waiting.icon_name);
}
#[test]
fn a_pending_revert_offers_keep_and_revert_and_no_other_layouts() {
let menu = displays_menu(&desk_and_laptop(), true);
let actions: Vec<_> = menu.items.iter().filter_map(|i| i.action.as_deref()).collect();
assert!(actions.contains(&"display:keep"));
assert!(actions.contains(&"display:revert"));
assert!(!actions.iter().any(|a| a.starts_with("display:apply:")), "{actions:?}");
assert_eq!(parse_menu_action("display:keep"), Some(MenuAction::DisplayKeep));
assert_eq!(parse_menu_action("display:revert"), Some(MenuAction::DisplayRevert));
}
#[test]
fn saved_layouts_are_listed_newest_first_with_the_current_one_checked() {
let menu = displays_menu(&desk_and_laptop(), false);
let rows: Vec<_> = menu
.items
.iter()
.filter(|i| i.action.as_deref().is_some_and(|a| a.starts_with("display:apply:")))
.collect();
assert_eq!(rows.iter().map(|r| r.label.as_str()).collect::<Vec<_>>(), ["Desk", "Laptop only"]);
assert_eq!((rows[0].kind, rows[0].toggle), (ItemKind::Checkmark, Some(true)));
assert_eq!(rows[1].kind, ItemKind::Standard);
assert_eq!(
parse_menu_action(rows[1].action.as_deref().unwrap()),
Some(MenuAction::DisplayApply("aa11".to_string()))
);
}
#[test]
fn the_tooltip_names_the_layout_in_effect_or_says_none_matches() {
let item = displays_item(&desk_and_laptop(), false);
assert_eq!(item.tooltip_title, "Desk");
assert_eq!(item.tooltip_body, "2 saved layouts");
let unmatched = DisplaysReading::Known {
profiles: vec![profile("aa11", "Laptop only", "2026-09-20T08:00:00Z")],
current: None,
};
let item = displays_item(&unmatched, false);
assert_eq!(item.tooltip_title, "No saved layout matches");
assert_eq!(item.tooltip_body, "1 saved layout");
}
#[test]
fn displayd_not_running_reads_differently_from_having_no_saved_layouts() {
let down = displays_menu(&DisplaysReading::Unavailable, false);
let empty = displays_menu(&DisplaysReading::Known { profiles: vec![], current: None }, false);
assert!(down.items[0].label.contains("hyprforge-displayd isn't running"));
assert_eq!(empty.items[0].label, "No saved layouts yet");
}
#[test]
fn an_apply_action_that_did_not_come_from_a_built_menu_is_ignored() {
assert_eq!(parse_menu_action("display:apply:"), None);
assert_eq!(parse_menu_action("display:apply:../etc"), None);
assert_eq!(parse_menu_action("display:apply:ab cd"), None);
}
#[test]
fn a_revert_deadline_that_has_passed_is_not_pending() {
let now = tokio::time::Instant::now();
assert!(revert_pending(Some(now + Duration::from_secs(5)), now));
assert!(!revert_pending(Some(now), now));
assert!(!revert_pending(None, now));
}
#[test]
fn more_layouts_than_fit_are_counted_rather_than_dropped() {
let profiles = (0..10)
.map(|i| profile(&format!("id{i}"), &format!("Layout {i}"), &format!("2026-09-{:02}T00:00:00Z", i + 1)))
.collect();
let menu = displays_menu(&DisplaysReading::Known { profiles, current: None }, false);
assert!(menu.items.iter().any(|i| i.label == "+2 more — see Settings"));
}
fn wired(interface: &str, state: WiredState, connection: Option<&str>) -> WiredStatus {
WiredStatus {
interface: interface.to_string(),
state,
connection: connection.map(str::to_string),
speed_mbps: (state == WiredState::Connected).then_some(1000),
}
}
fn wifi_on(ssid: Option<&str>) -> (TrayItem, Option<Ssid>) {
let connected = ssid.map(Ssid::new);
let status = net_status(RadioState::On, ssid);
(network_item(Some(&status), connected.as_ref(), Some(80), false), connected)
}
#[test]
fn every_wired_icon_the_function_can_return_is_in_the_allowed_set() {
let states = [
WiredState::Connected,
WiredState::Connecting,
WiredState::Disconnected,
WiredState::CableUnplugged,
];
for ssid in [None, Some("home")] {
for wifi_present in [false, true] {
let mut cases: Vec<Vec<WiredStatus>> = vec![vec![]];
cases.extend(states.iter().map(|&s| vec![wired("enp3s0", s, Some("LAN"))]));
for ports in cases {
let (item, connected) = wifi_on(ssid);
let icon = with_wired(item, connected.as_ref(), &ports, wifi_present).icon_name;
assert!(NETWORK_ICON_NAMES.contains(&icon.as_str()), "{icon:?}");
}
}
}
}
#[test]
fn a_connected_cable_outranks_wifi_and_says_wifi_is_up_too() {
let (item, connected) = wifi_on(Some("home"));
let item = with_wired(
item,
connected.as_ref(),
&[wired("enp3s0", WiredState::Connected, Some("Home LAN"))],
true,
);
assert_eq!(item.icon_name, "network-wired-activated-symbolic");
assert_eq!(item.tooltip_title, "Wired — Home LAN");
assert_eq!(item.tooltip_body, "1000 Mb/s · also on Wi-Fi (home)");
assert_eq!(item.title, "Network");
}
#[test]
fn wifi_keeps_the_icon_while_a_cable_is_still_connecting() {
let (item, connected) = wifi_on(Some("home"));
let wifi_icon = item.icon_name.clone();
let item = with_wired(
item,
connected.as_ref(),
&[wired("enp3s0", WiredState::Connecting, None)],
true,
);
assert_eq!(item.icon_name, wifi_icon);
}
#[test]
fn with_no_wifi_hardware_the_icon_is_the_wires_not_a_missing_radios() {
let (item, _) = wifi_on(None);
let unplugged = with_wired(
item.clone(),
None,
&[wired("enp3s0", WiredState::CableUnplugged, None)],
false,
);
assert_eq!(unplugged.icon_name, "network-wired-unavailable-symbolic");
assert_eq!(unplugged.tooltip_title, "Cable unplugged");
let idle = with_wired(item, None, &[wired("enp3s0", WiredState::Disconnected, None)], false);
assert_eq!(idle.icon_name, "network-wired-disconnected-symbolic");
assert_ne!(idle.icon_name, unplugged.icon_name, "plugged-in-idle and unplugged look different");
}
#[test]
fn an_unplugged_cable_does_not_displace_wifi() {
let (item, connected) = wifi_on(Some("home"));
let before = item.clone();
let after = with_wired(
item,
connected.as_ref(),
&[wired("enp3s0", WiredState::CableUnplugged, None)],
true,
);
assert_eq!(after, before);
}
fn menu_with(ports: &[WiredStatus], wifi_present: bool) -> Menu {
let status = net_status(RadioState::On, None);
with_wired_menu(network_menu(Some(&status), None, false, &[], &[]), ports, wifi_present, false)
}
#[test]
fn wired_rows_come_first_in_a_section_of_their_own() {
let menu = menu_with(&[wired("enp3s0", WiredState::Connected, Some("Home LAN"))], true);
assert_eq!(menu.items[0].label, "Wired — Home LAN");
assert_eq!((menu.items[0].kind, menu.items[0].toggle), (ItemKind::Checkmark, Some(true)));
assert_eq!(menu.items[1].kind, ItemKind::Separator);
assert_eq!(menu.items[2].label, "Wi-Fi");
assert_eq!(
parse_menu_action(menu.items[0].action.as_deref().unwrap()),
Some(MenuAction::WiredDisconnect("enp3s0".to_string()))
);
}
#[test]
fn a_plugged_in_idle_port_offers_to_connect_and_an_unplugged_one_only_says_so() {
let menu = menu_with(
&[
wired("enp3s0", WiredState::Disconnected, None),
wired("enx0011", WiredState::CableUnplugged, None),
],
true,
);
assert_eq!(menu.items[0].label, "Wired (enp3s0)", "several ports are told apart by name");
assert_eq!(
parse_menu_action(menu.items[0].action.as_deref().unwrap()),
Some(MenuAction::WiredConnect("enp3s0".to_string()))
);
assert_eq!(menu.items[1].label, "Wired (enx0011) — cable unplugged");
assert!(!menu.items[1].enabled);
}
#[test]
fn with_no_wifi_hardware_the_menu_offers_no_wifi_toggle() {
let menu = menu_with(&[wired("enp3s0", WiredState::Connected, Some("LAN"))], false);
assert!(!menu.items.iter().any(|i| i.label == "Wi-Fi"), "{:?}", menu.items);
assert_eq!(menu.items.last().unwrap().label, "Network settings…");
let bare = menu_with(&[], false);
assert_eq!(bare.items[0].label, "No network devices");
}
#[test]
fn with_no_wired_ports_the_wifi_menu_is_unchanged() {
let status = net_status(RadioState::On, None);
let wifi = network_menu(Some(&status), None, false, &[], &[]);
assert_eq!(with_wired_menu(wifi.clone(), &[], true, false), wifi);
}
#[test]
fn an_interface_name_the_kernel_would_refuse_is_ignored() {
assert_eq!(parse_menu_action("wired:connect:"), None);
assert_eq!(parse_menu_action("wired:connect:en p3s0"), None);
assert_eq!(parse_menu_action("wired:connect:../eth0"), None);
assert_eq!(parse_menu_action("wired:connect:abcdefghijklmnop"), None, "sixteen bytes");
assert_eq!(
parse_menu_action("wired:connect:enp197s0f4u1"),
Some(MenuAction::WiredConnect("enp197s0f4u1".to_string()))
);
}
}