cosmic/widget/
responsive_menu_bar.rs

1use std::collections::HashMap;
2
3use apply::Apply;
4
5use crate::{
6    Core, Element,
7    widget::{button, icon, responsive_container},
8};
9
10use super::menu::{self, ItemHeight, ItemWidth};
11
12#[must_use]
13pub fn responsive_menu_bar() -> ResponsiveMenuBar {
14    ResponsiveMenuBar::default()
15}
16
17pub struct ResponsiveMenuBar {
18    collapsed_item_width: ItemWidth,
19    item_width: ItemWidth,
20    item_height: ItemHeight,
21    spacing: f32,
22}
23
24impl Default for ResponsiveMenuBar {
25    fn default() -> ResponsiveMenuBar {
26        ResponsiveMenuBar {
27            collapsed_item_width: {
28                #[cfg(all(feature = "winit", feature = "wayland", target_os = "linux"))]
29                if matches!(
30                    crate::app::cosmic::WINDOWING_SYSTEM.get(),
31                    Some(crate::app::cosmic::WindowingSystem::Wayland)
32                ) {
33                    ItemWidth::Static(150)
34                } else {
35                    ItemWidth::Static(84)
36                }
37                #[cfg(not(all(feature = "winit", feature = "wayland", target_os = "linux")))]
38                {
39                    ItemWidth::Static(84)
40                }
41            },
42            item_width: ItemWidth::Uniform(150),
43            item_height: ItemHeight::Uniform(30),
44            spacing: 0.,
45        }
46    }
47}
48
49impl ResponsiveMenuBar {
50    /// Set the item width
51    #[must_use]
52    pub fn item_width(mut self, item_width: ItemWidth) -> Self {
53        self.item_width = item_width;
54        self
55    }
56
57    /// Set the item height
58    #[must_use]
59    pub fn item_height(mut self, item_height: ItemHeight) -> Self {
60        self.item_height = item_height;
61        self
62    }
63
64    /// Set the spacing
65    #[must_use]
66    pub fn spacing(mut self, spacing: f32) -> Self {
67        self.spacing = spacing;
68        self
69    }
70
71    /// # Panics
72    ///
73    /// Will panic if the menu bar collapses without tracking the size
74    pub fn into_element<
75        'a,
76        Message: Clone + 'static,
77        A: menu::Action<Message = Message> + Clone,
78        S: Into<std::borrow::Cow<'static, str>> + 'static,
79    >(
80        self,
81        core: &Core,
82        key_binds: &HashMap<menu::KeyBind, A>,
83        id: crate::widget::Id,
84        action_message: impl Fn(crate::surface::Action) -> Message + Send + Sync + Clone + 'static,
85        trees: Vec<(S, Vec<menu::Item<A, S>>)>,
86    ) -> Element<'a, Message> {
87        use crate::widget::id_container;
88
89        let menu_bar_size = core.menu_bars.get(&id);
90
91        #[allow(clippy::if_not_else)]
92        if !menu_bar_size.is_some_and(|(limits, size)| {
93            let max_size = limits.max();
94            max_size.width < size.width
95        }) {
96            responsive_container::responsive_container(
97                id_container(
98                    menu::bar(
99                        trees
100                            .into_iter()
101                            .map(|mt: (S, Vec<menu::Item<A, S>>)| {
102                                menu::Tree::<_>::with_children(
103                                    crate::widget::RcElementWrapper::new(Element::from(
104                                        menu::root(mt.0),
105                                    )),
106                                    menu::items(key_binds, mt.1),
107                                )
108                            })
109                            .collect(),
110                    )
111                    .item_width(self.item_width)
112                    .item_height(self.item_height)
113                    .spacing(self.spacing)
114                    .on_surface_action(action_message.clone())
115                    .window_id_maybe(core.main_window_id()),
116                    crate::widget::Id::new(format!("menu_bar_expanded_{id}")),
117                ),
118                id,
119                action_message,
120            )
121            .apply(Element::from)
122        } else {
123            responsive_container::responsive_container(
124                id_container(
125                    menu::bar(vec![menu::Tree::<_>::with_children(
126                        Element::from(
127                            button::icon(icon::from_name("open-menu-symbolic"))
128                                .padding([4, 12])
129                                .class(crate::theme::Button::MenuRoot),
130                        ),
131                        menu::items(
132                            key_binds,
133                            trees
134                                .into_iter()
135                                .map(|mt| menu::Item::Folder(mt.0, mt.1))
136                                .collect(),
137                        )
138                        .into_iter()
139                        .map(|t| {
140                            t.width(match self.item_width {
141                                ItemWidth::Uniform(w) | ItemWidth::Static(w) => w,
142                            })
143                        })
144                        .collect(),
145                    )])
146                    .item_height(self.item_height)
147                    .item_width(self.collapsed_item_width)
148                    .spacing(self.spacing)
149                    .on_surface_action(action_message.clone())
150                    .window_id_maybe(core.main_window_id()),
151                    crate::widget::Id::new(format!("menu_bar_collapsed_{id}")),
152                ),
153                id,
154                action_message,
155            )
156            .size(menu_bar_size.unwrap().1)
157            .apply(Element::from)
158        }
159    }
160}