Skip to main content

cosmic_applet_avestruz/views/components/
menu.rs

1// SPDX-License-Identifier: MIT
2
3//! A small menu that opens under what was pressed: what can be done to
4//! pass a note on, what the cog above the feed offers, how fast a video
5//! plays.
6
7use 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
15/// How wide a menu is, the room around what it offers, and the gap between
16/// what was pressed and the menu.
17pub const MENU_WIDTH: f32 = 160.0;
18const MENU_PADDING: u16 = 4;
19pub const MENU_GAP: f32 = 2.0;
20
21/// How tall one thing a menu offers is, at most.
22const MENU_ITEM_HEIGHT: f32 = 36.0;
23
24/// How tall a menu that offers `items` things is, at most: the room it
25/// needs under what was pressed to show all of them.
26pub fn menu_height(items: usize) -> f32 {
27    f32::from(MENU_PADDING) * 2.0 + MENU_ITEM_HEIGHT * items as f32
28}
29
30/// A menu of `choices`, one under the other, `width` wide: each what it
31/// is called, whether it is the one chosen now, which is ticked, and what
32/// choosing it does.
33pub 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
46/// A menu of `items`, one under the other: each a picture, what it is
47/// called, and what choosing it does. One that does nothing shows, and
48/// can't be chosen.
49pub fn menu<'a>(items: impl IntoIterator<Item = (icon::Handle, String, Option<Message>)>) -> Element<'a, Message> {
50    menu_at(items, None)
51}
52
53/// How an item of a menu looks while the keyboard is at it: as one the
54/// pointer is over, wherever the pointer is.
55fn 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
65/// As [`menu`], with the keyboard at the item that is `at` in `items`, if
66/// it is at one: that item is shown as one the pointer is over.
67pub 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}