use hyprforge_tray::menu::{ItemKind, MenuItem};
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct MenuLayout {
pub padding: f64,
pub row_height: f64,
pub separator_height: f64,
}
impl MenuLayout {
pub const PADDING: f64 = 8.0;
pub const ROW_PADDING: f64 = 7.0;
pub const SEPARATOR_HEIGHT: f64 = 9.0;
pub const ROW_HPADDING: f64 = 10.0;
pub const CHECK_WIDTH: f64 = 18.0;
pub fn for_font_size(font_size: f32) -> MenuLayout {
let font_size = font_size as f64;
let line = font_size * 1.2;
MenuLayout {
padding: Self::PADDING,
row_height: line + Self::ROW_PADDING * 2.0,
separator_height: Self::SEPARATOR_HEIGHT,
}
}
fn height_of(&self, item: &MenuItem) -> f64 {
if item.kind == ItemKind::Separator {
self.separator_height
} else {
self.row_height
}
}
#[cfg(test)]
pub fn row_top(&self, rows: &[MenuItem], index: usize) -> f64 {
rows[..index].iter().map(|r| self.height_of(r)).sum()
}
pub fn popup_height(&self, rows: &[MenuItem]) -> f64 {
let content: f64 = rows.iter().map(|r| self.height_of(r)).sum();
(content + self.padding * 2.0).max(self.padding * 2.0)
}
pub fn is_clickable(item: &MenuItem) -> bool {
item.kind != ItemKind::Separator && item.enabled && item.action.is_some()
}
pub fn row_at(&self, rows: &[MenuItem], local_y: f64) -> Option<usize> {
let mut y = local_y - self.padding;
if y < 0.0 {
return None;
}
for (index, item) in rows.iter().enumerate() {
let height = self.height_of(item);
if y < height {
return Self::is_clickable(item).then_some(index);
}
y -= height;
}
None
}
pub fn label_width(&self, popup_width: f64) -> f64 {
(popup_width - self.padding * 2.0 - Self::ROW_HPADDING * 2.0 - Self::CHECK_WIDTH).max(0.0)
}
}
#[cfg(test)]
mod tests {
use super::*;
use hyprforge_tray::menu::Menu;
fn rows() -> Vec<MenuItem> {
Menu::new(vec![
MenuItem::checkmark("Wi-Fi", true, "wifi:radio:off"),
MenuItem::separator(),
MenuItem::standard("home", "wifi:connect:home"),
MenuItem::disabled("guest (enterprise)"),
MenuItem::separator(),
MenuItem::standard("Wi-Fi settings…", "wifi:settings"),
])
.items
}
#[test]
fn a_point_above_the_first_row_hits_nothing() {
let layout = MenuLayout::for_font_size(15.0);
assert_eq!(layout.row_at(&rows(), 0.0), None);
assert_eq!(layout.row_at(&rows(), layout.padding - 0.01), None);
}
#[test]
fn the_first_row_is_hit_right_up_to_its_own_bottom_edge() {
let layout = MenuLayout::for_font_size(15.0);
assert_eq!(layout.row_at(&rows(), layout.padding), Some(0));
assert_eq!(layout.row_at(&rows(), layout.padding + layout.row_height - 0.01), Some(0));
}
#[test]
fn a_separator_is_never_a_hit() {
let layout = MenuLayout::for_font_size(15.0);
let top = layout.padding + layout.row_top(&rows(), 1);
assert_eq!(layout.row_at(&rows(), top + layout.separator_height / 2.0), None);
}
#[test]
fn a_disabled_row_is_never_a_hit_even_though_it_has_its_own_height() {
let layout = MenuLayout::for_font_size(15.0);
let top = layout.padding + layout.row_top(&rows(), 3);
assert_eq!(layout.row_at(&rows(), top + layout.row_height / 2.0), None);
}
#[test]
fn an_ordinary_row_past_a_separator_still_resolves_to_its_own_index() {
let layout = MenuLayout::for_font_size(15.0);
let rows = rows();
let top = layout.padding + layout.row_top(&rows, 2);
assert_eq!(layout.row_at(&rows, top + layout.row_height / 2.0), Some(2));
let settings_top = layout.padding + layout.row_top(&rows, 5);
assert_eq!(layout.row_at(&rows, settings_top + layout.row_height / 2.0), Some(5));
}
#[test]
fn a_point_past_the_last_row_hits_nothing() {
let layout = MenuLayout::for_font_size(15.0);
let rows = rows();
let end = layout.padding + layout.row_top(&rows, rows.len());
assert_eq!(layout.row_at(&rows, end + 1.0), None);
}
#[test]
fn popup_height_is_every_row_stacked_plus_padding_on_both_sides() {
let layout = MenuLayout::for_font_size(15.0);
let rows = rows();
let expected = layout.row_top(&rows, rows.len()) + layout.padding * 2.0;
assert_eq!(layout.popup_height(&rows), expected);
}
#[test]
fn an_empty_menu_is_just_the_padding_not_a_negative_or_zero_height() {
let layout = MenuLayout::for_font_size(15.0);
assert_eq!(layout.popup_height(&[]), layout.padding * 2.0);
}
#[test]
fn a_bigger_font_makes_a_taller_row_but_not_a_taller_separator() {
let small = MenuLayout::for_font_size(12.0);
let large = MenuLayout::for_font_size(40.0);
assert!(large.row_height > small.row_height);
assert_eq!(large.separator_height, small.separator_height);
}
#[test]
fn label_width_leaves_room_for_the_checkmark_column_and_both_paddings() {
let layout = MenuLayout::for_font_size(15.0);
let popup_width = 220.0;
let width = layout.label_width(popup_width);
let expected = popup_width - layout.padding * 2.0 - MenuLayout::ROW_HPADDING * 2.0 - MenuLayout::CHECK_WIDTH;
assert_eq!(width, expected);
assert!(width > 0.0 && width < popup_width);
}
#[test]
fn label_width_floors_at_zero_for_a_popup_narrower_than_its_own_padding() {
let layout = MenuLayout::for_font_size(15.0);
assert_eq!(layout.label_width(0.0), 0.0);
assert_eq!(layout.label_width(-100.0), 0.0);
}
#[test]
fn only_an_enabled_row_with_an_action_is_clickable() {
assert!(MenuLayout::is_clickable(&MenuItem::standard("home", "wifi:connect:home")));
assert!(MenuLayout::is_clickable(&MenuItem::checkmark("Wi-Fi", true, "wifi:radio:off")));
assert!(!MenuLayout::is_clickable(&MenuItem::separator()));
assert!(!MenuLayout::is_clickable(&MenuItem::disabled("guest (enterprise)")));
assert!(!MenuLayout::is_clickable(&MenuItem::disabled_checkmark("Wi-Fi (blocked)", false)));
}
}