mod app;
mod layout;
mod view;
use app::{MenuOutcome, TrayMenuApp};
use hyprforge_popup::geometry::{Point, Size};
use hyprforge_tray::menu::Menu;
use layout::MenuLayout;
const POPUP_WIDTH: f64 = 220.0;
const LOCK_NAME: &str = "hyprforge-traymenu.lock";
fn parse_anchor(mut args: impl Iterator<Item = String>) -> Option<Point> {
let mut x = None;
let mut y = None;
while let Some(arg) = args.next() {
match arg.as_str() {
"--x" => x = args.next().and_then(|v| v.parse().ok()),
"--y" => y = args.next().and_then(|v| v.parse().ok()),
_ => {}
}
}
Some(Point { x: x?, y: y? })
}
fn main() -> std::process::ExitCode {
let lock_path = hyprforge_popup::singleton::lock_path(LOCK_NAME);
let _lock = match hyprforge_popup::singleton::acquire(&lock_path) {
Ok(Some(lock)) => Some(lock),
Ok(None) => return std::process::ExitCode::SUCCESS,
Err(e) => {
eprintln!("couldn't set up the single-instance lock ({e}) — continuing anyway");
None
}
};
let Some(anchor) = parse_anchor(std::env::args().skip(1)) else {
eprintln!("usage: hyprforge-traymenu --x <n> --y <n> (menu JSON on stdin)");
return std::process::ExitCode::FAILURE;
};
let mut input = String::new();
{
use std::io::Read;
if let Err(e) = std::io::stdin().read_to_string(&mut input) {
eprintln!("couldn't read the menu from stdin: {e}");
return std::process::ExitCode::FAILURE;
}
}
let menu: Menu = match serde_json::from_str(&input) {
Ok(menu) => menu,
Err(e) => {
eprintln!("couldn't parse the menu handed to this popup: {e}");
return std::process::ExitCode::FAILURE;
}
};
let rows: Vec<_> = menu.flatten().into_iter().cloned().collect();
let mut theme = hyprforge_appearance::look::resolve();
theme.font_size = theme.drawable_font_size();
let row_layout = MenuLayout::for_font_size(theme.font_size);
let popup_height = row_layout.popup_height(&rows);
let monitors = hyprforge_popup::monitors();
if monitors.is_empty() {
eprintln!("couldn't read any monitors from hyprctl — is Hyprland running?");
return std::process::ExitCode::FAILURE;
}
let popup_size = Size { width: POPUP_WIDTH, height: popup_height };
let prefs = match hyprforge_tray::prefs::load() {
Ok(prefs) => prefs,
Err(e) => {
eprintln!("couldn't read tray.toml ({e}) — using the defaults for this menu");
hyprforge_tray::prefs::Prefs::default()
}
};
let menu_y_offset = prefs.menu_y_offset;
let dismissal = if prefs.menu_closes_on_click_outside {
hyprforge_popup::Dismissal::CloseOnFocusLoss
} else {
hyprforge_popup::Dismissal::HoldKeyboard
};
let Some(placement) = hyprforge_popup::place_below_bar(&monitors, anchor, menu_y_offset, popup_size) else {
eprintln!("couldn't work out where to place the menu");
return std::process::ExitCode::FAILURE;
};
let connection = match hyprforge_popup::Connection::connect_to_env() {
Ok(connection) => connection,
Err(e) => {
eprintln!("couldn't connect to the compositor: {e}");
return std::process::ExitCode::FAILURE;
}
};
let app = TrayMenuApp::new(rows.clone(), row_layout, dismissal);
match hyprforge_popup::Popup::run(connection, placement, app, theme) {
Ok(hyprforge_popup::Outcome::App(MenuOutcome::Chosen(index))) => {
if let Some(action) = rows.get(index).and_then(|row| row.action.as_deref()) {
println!("{action}");
}
std::process::ExitCode::SUCCESS
}
Ok(hyprforge_popup::Outcome::App(MenuOutcome::Cancelled)) => std::process::ExitCode::SUCCESS,
Ok(hyprforge_popup::Outcome::Closed | hyprforge_popup::Outcome::Disconnected) => {
std::process::ExitCode::SUCCESS
}
Err(e) => {
eprintln!("{e}");
std::process::ExitCode::FAILURE
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn args(s: &str) -> impl Iterator<Item = String> + '_ {
s.split_whitespace().map(str::to_string)
}
#[test]
fn both_flags_are_required_in_either_order() {
assert_eq!(parse_anchor(args("--x 10 --y 20")), Some(Point { x: 10.0, y: 20.0 }));
assert_eq!(parse_anchor(args("--y 20 --x 10")), Some(Point { x: 10.0, y: 20.0 }));
assert_eq!(parse_anchor(args("--x 10")), None, "missing --y");
assert_eq!(parse_anchor(args("--y 20")), None, "missing --x");
assert_eq!(parse_anchor(args("")), None);
}
#[test]
fn an_unparsable_number_is_treated_as_missing_rather_than_panicking() {
assert_eq!(parse_anchor(args("--x not-a-number --y 20")), None);
}
#[test]
fn a_non_finite_font_size_falls_back_rather_than_panicking_the_renderer() {
let theme = hyprforge_look::Theme { font_size: f32::NAN, ..hyprforge_look::Theme::default() };
assert_eq!(theme.drawable_font_size(), 15.0);
let theme = hyprforge_look::Theme { font_size: 0.0, ..hyprforge_look::Theme::default() };
assert!(theme.drawable_font_size() >= 6.0);
}
}