feat: menu bar popups

This commit is contained in:
Ashley Wulber 2025-06-10 12:22:07 -04:00 committed by GitHub
parent 5b77f37fde
commit 92ec78ba29
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
24 changed files with 1861 additions and 928 deletions

View file

@ -11,7 +11,7 @@ use iced_widget::core::{Element, renderer};
use crate::iced_core::{Alignment, Length};
use crate::widget::menu::action::MenuAction;
use crate::widget::menu::key_bind::KeyBind;
use crate::widget::{Button, icon};
use crate::widget::{Button, RcElementWrapper, icon};
use crate::{theme, widget};
/// Nested menu is essentially a tree of items, a menu is a collection of items
@ -23,27 +23,25 @@ use crate::{theme, widget};
/// but there's no need to explicitly distinguish them here, if a menu tree
/// has children, it's a menu, otherwise it's an item
#[allow(missing_debug_implementations)]
pub struct MenuTree<'a, Message, Renderer = crate::Renderer> {
#[derive(Clone)]
pub struct MenuTree<Message> {
/// The menu tree will be flatten into a vector to build a linear widget tree,
/// the `index` field is the index of the item in that vector
pub(crate) index: usize,
/// The item of the menu tree
pub(crate) item: Element<'a, Message, crate::Theme, Renderer>,
pub(crate) item: RcElementWrapper<Message>,
/// The children of the menu tree
pub(crate) children: Vec<MenuTree<'a, Message, Renderer>>,
pub(crate) children: Vec<MenuTree<Message>>,
/// The width of the menu tree
pub(crate) width: Option<u16>,
/// The height of the menu tree
pub(crate) height: Option<u16>,
}
impl<'a, Message, Renderer> MenuTree<'a, Message, Renderer>
where
Renderer: renderer::Renderer,
{
impl<Message: Clone + 'static> MenuTree<Message> {
/// Create a new menu tree from a widget
pub fn new(item: impl Into<Element<'a, Message, crate::Theme, Renderer>>) -> Self {
pub fn new(item: impl Into<RcElementWrapper<Message>>) -> Self {
Self {
index: 0,
item: item.into(),
@ -55,8 +53,8 @@ where
/// Create a menu tree from a widget and a vector of sub trees
pub fn with_children(
item: impl Into<Element<'a, Message, crate::Theme, Renderer>>,
children: Vec<impl Into<MenuTree<'a, Message, Renderer>>>,
item: impl Into<RcElementWrapper<Message>>,
children: Vec<impl Into<MenuTree<Message>>>,
) -> Self {
Self {
index: 0,
@ -92,7 +90,7 @@ where
/// Set the index of each item
pub(crate) fn set_index(&mut self) {
/// inner counting function.
fn rec<Message, Renderer>(mt: &mut MenuTree<'_, Message, Renderer>, count: &mut usize) {
fn rec<Message: Clone + 'static>(mt: &mut MenuTree<Message>, count: &mut usize) {
// keep items under the same menu line up
mt.children.iter_mut().for_each(|c| {
c.index = *count;
@ -109,18 +107,18 @@ where
}
/// Flatten the menu tree
pub(crate) fn flattern(&'a self) -> Vec<&Self> {
pub(crate) fn flattern(&self) -> Vec<&Self> {
/// Inner flattening function
fn rec<'a, Message, Renderer>(
mt: &'a MenuTree<'a, Message, Renderer>,
flat: &mut Vec<&MenuTree<'a, Message, Renderer>>,
fn rec<'a, Message: Clone + 'static>(
mt: &'a MenuTree<Message>,
flat: &mut Vec<&'a MenuTree<Message>>,
) {
mt.children.iter().for_each(|c| {
flat.push(c);
});
mt.children.iter().for_each(|c| {
rec(c, flat);
rec(&c, flat);
});
}
@ -132,13 +130,9 @@ where
}
}
impl<'a, Message, Renderer> From<Element<'a, Message, crate::Theme, Renderer>>
for MenuTree<'a, Message, Renderer>
where
Renderer: renderer::Renderer,
{
fn from(value: Element<'a, Message, crate::Theme, Renderer>) -> Self {
Self::new(value)
impl<Message: Clone + 'static> From<crate::Element<'static, Message>> for MenuTree<Message> {
fn from(value: crate::Element<'static, Message>) -> Self {
Self::new(RcElementWrapper::new(value))
}
}
@ -160,6 +154,7 @@ where
.class(theme::Button::MenuItem)
}
#[derive(Clone)]
/// Represents a menu item that performs an action when selected or a separator between menu items.
///
/// - `Action` - Represents a menu item that performs an action when selected.
@ -215,20 +210,15 @@ where
///
/// # Returns
/// - A vector of `MenuTree`.
#[must_use]
pub fn menu_items<
'a,
A: MenuAction<Message = Message>,
L: Into<Cow<'static, str>> + 'static,
Message,
Renderer: renderer::Renderer + 'a,
Message: 'static + std::clone::Clone,
>(
key_binds: &HashMap<KeyBind, A>,
children: Vec<MenuItem<A, L>>,
) -> Vec<MenuTree<'a, Message, Renderer>>
where
Element<'a, Message, crate::Theme, Renderer>: From<widget::button::Button<'a, Message>>,
Message: 'a + Clone,
{
) -> Vec<MenuTree<Message>> {
fn find_key<A: MenuAction>(action: &A, key_binds: &HashMap<KeyBind, A>) -> String {
for (key_bind, key_action) in key_binds {
if action == key_action {
@ -249,9 +239,10 @@ where
match item {
MenuItem::Button(label, icon, action) => {
let l: Cow<'static, str> = label.into();
let key = find_key(&action, key_binds);
let mut items = vec![
widget::text(label).into(),
widget::text(l.clone()).into(),
widget::horizontal_space().into(),
widget::text(key).into(),
];
@ -261,15 +252,18 @@ where
items.insert(1, widget::Space::with_width(spacing.space_xxs).into());
}
// dbg!("button with action...", action.message());
let menu_button = menu_button(items).on_press(action.message());
trees.push(MenuTree::<Message, Renderer>::new(menu_button));
trees.push(MenuTree::<Message>::from(Element::from(menu_button)));
}
MenuItem::ButtonDisabled(label, icon, action) => {
let l: Cow<'static, str> = label.into();
let key = find_key(&action, key_binds);
let mut items = vec![
widget::text(label).into(),
widget::text(l.clone()).into(),
widget::horizontal_space().into(),
widget::text(key).into(),
];
@ -281,7 +275,7 @@ where
let menu_button = menu_button(items);
trees.push(MenuTree::<Message, Renderer>::new(menu_button));
trees.push(MenuTree::<Message>::from(Element::from(menu_button)));
}
MenuItem::CheckBox(label, icon, value, action) => {
let key = find_key(&action, key_binds);
@ -311,36 +305,42 @@ where
items.insert(2, widget::icon::icon(icon).size(14).into());
}
trees.push(MenuTree::new(menu_button(items).on_press(action.message())));
trees.push(MenuTree::from(Element::from(
menu_button(items).on_press(action.message()),
)));
}
MenuItem::Folder(label, children) => {
trees.push(MenuTree::<Message, Renderer>::with_children(
menu_button(vec![
widget::text(label).into(),
widget::horizontal_space().into(),
widget::icon::from_name("pan-end-symbolic")
.size(16)
.icon()
.into(),
])
.class(
// Menu folders have no on_press so they take on the disabled style by default
if children.is_empty() {
// This will make the folder use the disabled style if it has no children
theme::Button::MenuItem
} else {
// This will make the folder use the enabled style if it has children
theme::Button::MenuFolder
},
),
let l: Cow<'static, str> = label.into();
trees.push(MenuTree::<Message>::with_children(
RcElementWrapper::new(crate::Element::from(
menu_button::<'static, _>(vec![
widget::text(l.clone()).into(),
widget::horizontal_space().into(),
widget::icon::from_name("pan-end-symbolic")
.size(16)
.icon()
.into(),
])
.class(
// Menu folders have no on_press so they take on the disabled style by default
if children.is_empty() {
// This will make the folder use the disabled style if it has no children
theme::Button::MenuItem
} else {
// This will make the folder use the enabled style if it has children
theme::Button::MenuFolder
},
),
)),
menu_items(key_binds, children),
));
}
MenuItem::Divider => {
if i != size - 1 {
trees.push(MenuTree::<Message, Renderer>::new(
trees.push(MenuTree::<Message>::from(Element::from(
widget::divider::horizontal::light(),
));
)));
}
}
}