cosmic_applet_avestruz/views/components/
menu.rs1use cosmic::Element;
8use cosmic::iced::Length;
9use cosmic::widget::button::Catalog;
10use cosmic::widget::menu::menu_button;
11use cosmic::widget::{self, container, icon, text};
12
13use crate::messages::Message;
14
15pub const MENU_WIDTH: f32 = 160.0;
18const MENU_PADDING: u16 = 4;
19pub const MENU_GAP: f32 = 2.0;
20
21const MENU_ITEM_HEIGHT: f32 = 36.0;
23
24pub fn menu_height(items: usize) -> f32 {
27 f32::from(MENU_PADDING) * 2.0 + MENU_ITEM_HEIGHT * items as f32
28}
29
30pub fn choice_menu<'a>(choices: impl IntoIterator<Item = (String, bool, Message)>, width: f32) -> Element<'a, Message> {
34 let choices = choices.into_iter().fold(widget::Column::new(), |choices, (label, chosen, on_press)| {
35 let tick: Element<'a, Message> = if chosen {
36 icon::from_name("object-select-symbolic").size(16).into()
37 } else {
38 widget::Space::new().width(16).into()
39 };
40 let choice = menu_button(vec![tick, widget::Space::new().width(8).into(), text(label).size(14).into()]);
41 choices.push(choice.on_press(on_press))
42 });
43 container(choices).padding(MENU_PADDING).width(Length::Fixed(width)).class(cosmic::theme::Container::Dropdown).into()
44}
45
46pub fn menu<'a>(items: impl IntoIterator<Item = (icon::Handle, String, Option<Message>)>) -> Element<'a, Message> {
50 menu_at(items, None)
51}
52
53fn at_the_keys() -> cosmic::theme::Button {
56 use cosmic::theme::Button::MenuItem;
57 cosmic::theme::Button::Custom {
58 active: Box::new(|focused, theme| theme.hovered(focused, false, &MenuItem)),
59 hovered: Box::new(|focused, theme| theme.hovered(focused, false, &MenuItem)),
60 pressed: Box::new(|focused, theme| theme.pressed(focused, false, &MenuItem)),
61 disabled: Box::new(|theme| theme.disabled(&MenuItem)),
62 }
63}
64
65pub fn menu_at<'a>(
68 items: impl IntoIterator<Item = (icon::Handle, String, Option<Message>)>,
69 at: Option<usize>,
70) -> Element<'a, Message> {
71 let items = items.into_iter().enumerate().fold(widget::Column::new(), |items, (place, (picture, label, on_press))| {
72 let item = menu_button(vec![
73 icon::icon(picture).size(16).into(),
74 widget::Space::new().width(8).into(),
75 text(label).size(14).into(),
76 ]);
77 let item = if at == Some(place) { item.class(at_the_keys()) } else { item };
78 items.push(item.on_press_maybe(on_press))
79 });
80 container(items).padding(MENU_PADDING).width(Length::Fixed(MENU_WIDTH)).class(cosmic::theme::Container::Dropdown).into()
81}