use crate::layout::MenuLayout;
use hyprforge_look::Theme;
use hyprforge_tray::menu::{ItemKind, MenuItem};
use iced_runtime::core::text::Wrapping;
use iced_runtime::core::{Element, Length, Padding};
use iced_widget::{container, row, text, Space};
pub fn to_iced(c: hyprforge_look::Color) -> iced_runtime::core::Color {
iced_runtime::core::Color::from_rgba8(c.r, c.g, c.b, c.a as f32 / 255.0)
}
pub fn rows<'a, Message: 'a>(
items: &'a [MenuItem],
layout: &MenuLayout,
hovered: Option<usize>,
theme: &'a Theme,
popup_width: f64,
) -> Vec<Element<'a, Message, iced_widget::Theme, iced_tiny_skia::Renderer>> {
items
.iter()
.enumerate()
.map(|(index, item)| row_element(item, layout, Some(index) == hovered, theme, popup_width))
.collect()
}
fn max_label_chars(font_size: f32, available_width: f64) -> usize {
let avg_char_width = (font_size as f64 * 0.6).max(1.0);
((available_width / avg_char_width).floor() as usize).max(1)
}
fn truncate_label(label: &str, max_chars: usize) -> std::borrow::Cow<'_, str> {
if label.chars().count() <= max_chars {
return std::borrow::Cow::Borrowed(label);
}
let cut: String = label.chars().take(max_chars.saturating_sub(1)).collect();
std::borrow::Cow::Owned(format!("{cut}\u{2026}"))
}
fn row_element<'a, Message: 'a>(
item: &'a MenuItem,
layout: &MenuLayout,
hovered: bool,
theme: &'a Theme,
popup_width: f64,
) -> Element<'a, Message, iced_widget::Theme, iced_tiny_skia::Renderer> {
if item.kind == ItemKind::Separator {
let line_color = to_iced(theme.surfaces.text_dim);
return container(
container(Space::new())
.width(Length::Fill)
.height(Length::Fixed(1.0))
.style(move |_: &iced_widget::Theme| container::Style {
background: Some(line_color.into()),
..Default::default()
}),
)
.width(Length::Fill)
.height(Length::Fixed(layout.separator_height as f32))
.align_y(iced_runtime::core::alignment::Vertical::Center)
.into();
}
let clickable = MenuLayout::is_clickable(item);
let text_color = if !clickable {
to_iced(theme.surfaces.text_dim)
} else {
to_iced(theme.surfaces.text)
};
let check: Element<'a, Message, iced_widget::Theme, iced_tiny_skia::Renderer> = match item.toggle {
Some(on) => {
let mark = if on { "\u{2713}" } else { "" };
text(mark).size(theme.font_size).color(text_color).into()
}
None => Space::new().width(Length::Fixed(0.0)).into(),
};
let check: Element<'a, Message, iced_widget::Theme, iced_tiny_skia::Renderer> = container(check)
.width(Length::Fixed(MenuLayout::CHECK_WIDTH as f32))
.align_y(iced_runtime::core::alignment::Vertical::Center)
.into();
let max_chars = max_label_chars(theme.font_size, layout.label_width(popup_width));
let label_text = truncate_label(&item.label, max_chars);
let label: Element<'a, Message, iced_widget::Theme, iced_tiny_skia::Renderer> =
text(label_text.into_owned()).size(theme.font_size).wrapping(Wrapping::None).color(text_color).into();
let content = row![check, label].align_y(iced_runtime::core::alignment::Vertical::Center);
let background = if hovered && clickable { Some(to_iced(theme.accent).scale_alpha(0.18)) } else { None };
container(content)
.width(Length::Fill)
.height(Length::Fixed(layout.row_height as f32))
.padding(Padding {
top: 0.0,
right: MenuLayout::ROW_HPADDING as f32,
bottom: 0.0,
left: MenuLayout::ROW_HPADDING as f32,
})
.align_y(iced_runtime::core::alignment::Vertical::Center)
.clip(true)
.style(move |_: &iced_widget::Theme| container::Style {
background: background.map(Into::into),
..Default::default()
})
.into()
}