refactor: menu bar buttons

This commit is contained in:
Ashley Wulber 2025-02-18 01:20:30 -05:00
parent ad65f45f5b
commit 5ef18de61f
No known key found for this signature in database
GPG key ID: 5216D4F46A90A820
4 changed files with 174 additions and 36 deletions

View file

@ -388,32 +388,65 @@ impl Core {
id
}
pub fn responsive_menu_bar<'a, Message: Clone + From<SurfaceMessage> + 'static>(
#[cfg(feature = "wayland")]
/// # Panics
///
/// Will panic if the menu bar collapses without tracking the size
pub fn responsive_menu_bar<
'a,
Message: Clone
+ From<crate::surface_message::SurfaceMessage>
+ crate::surface_message::SurfaceMessageHandler
+ 'static,
>(
&self,
id: crate::widget::Id,
roots: Vec<Tree<'a, Message>>,
trees: Vec<(
crate::widget::Button<'a, Message>,
Vec<impl Into<Tree<'a, Message>>>,
)>,
) -> crate::Element<'a, Message> {
use iced::Length;
use crate::widget::id_container;
let menu_bar_size = self.menu_bars.get(&id);
#[allow(clippy::if_not_else)]
if !menu_bar_size.is_some_and(|(limits, size)| {
let max_size = limits.max();
max_size.width < size.width
}) {
crate::Element::from(responsive_container::responsive_container(
menu::bar(roots),
id_container(
menu::bar(
trees
.into_iter()
.map(|mt| menu::Tree::<_>::with_children(mt.0, mt.1))
.collect(),
),
crate::widget::Id::new(format!("menu_bar_expanded_{id}")),
),
id,
))
} else {
crate::Element::from(
responsive_container::responsive_container(
menu::bar(vec![menu::Tree::<_>::with_children(
Element::from(
button::icon(icon::from_name("open-menu-symbolic"))
.padding([4, 12])
.class(crate::theme::Button::MenuRoot),
),
roots,
)]),
id_container(
menu::bar(vec![menu::Tree::<_>::with_children(
Element::from(
button::icon(icon::from_name("open-menu-symbolic"))
.padding([4, 12])
.class(crate::theme::Button::MenuRoot),
),
trees
.into_iter()
.map(|mt| {
menu::Tree::<_>::with_children(mt.0.width(Length::Fill), mt.1)
})
.collect(),
)]),
crate::widget::Id::new(format!("menu_bar_collapsed_{id}")),
),
id,
)
.size(menu_bar_size.unwrap().1),

View file

@ -9,9 +9,9 @@ use std::rc::Rc;
use iced_widget::core::{renderer, Element};
use crate::iced_core::{Alignment, Length};
use crate::widget::icon;
use crate::widget::menu::action::MenuAction;
use crate::widget::menu::key_bind::KeyBind;
use crate::widget::{icon, Button};
use crate::{theme, widget};
/// Nested menu is essentially a tree of items, a menu is a collection of items
@ -192,14 +192,13 @@ pub enum MenuItem<A: MenuAction, L: Into<Cow<'static, str>>> {
/// - A button for the root menu item.
pub fn menu_root<'a, Message, Renderer: renderer::Renderer>(
label: impl Into<Cow<'a, str>> + 'a,
) -> iced::Element<'a, Message, crate::Theme, Renderer>
) -> Button<'a, Message>
where
Element<'a, Message, crate::Theme, Renderer>: From<widget::Button<'a, Message>>,
{
widget::button::custom(widget::text(label))
.padding([4, 12])
.class(theme::Button::MenuRoot)
.into()
}
/// Create a list of menu items from a vector of `MenuItem`.