wip: menus

This commit is contained in:
Ashley Wulber 2025-01-22 16:31:51 -05:00
parent a406859e65
commit 93e3d4fd42
No known key found for this signature in database
GPG key ID: 5216D4F46A90A820
25 changed files with 1358 additions and 243 deletions

View file

@ -84,7 +84,7 @@ where
.map(|root| {
let mut tree = Tree::empty();
let flat = root
.flattern()
.flatten()
.iter()
.map(|mt| Tree::new(mt.item.as_widget()))
.collect();
@ -110,7 +110,7 @@ pub(crate) fn menu_roots_diff<'a, Message, Renderer>(
.zip(menu_roots.iter())
.for_each(|(t, root)| {
let mut flat = root
.flattern()
.flatten()
.iter()
.map(|mt| {
let widget = mt.item.as_widget();
@ -129,7 +129,7 @@ pub(crate) fn menu_roots_diff<'a, Message, Renderer>(
let extended = menu_roots[tree.children.len()..].iter().map(|root| {
let mut tree = Tree::empty();
let flat = root
.flattern()
.flatten()
.iter()
.map(|mt| Tree::new(mt.item.as_widget()))
.collect();
@ -366,6 +366,8 @@ where
if state.menu_states.is_empty() && view_cursor.is_over(layout.bounds()) {
state.view_cursor = view_cursor;
state.open = true;
// #[cfg(feature = "wayland")]
// TODO emit Message to open menu
}
}
_ => (),
@ -437,6 +439,9 @@ where
_renderer: &Renderer,
translation: Vector,
) -> Option<overlay::Element<'b, Message, crate::Theme, Renderer>> {
// #[cfg(feature = "wayland")]
// return None;
let state = tree.state.downcast_ref::<MenuBarState>();
if !state.open {
return None;

View file

@ -1,8 +1,13 @@
// From iced_aw, license MIT
//! Menu tree overlay
use std::{
borrow::Cow,
sync::{Arc, Mutex},
};
use super::{menu_bar::MenuBarState, menu_tree::MenuTree};
use crate::style::menu_bar::StyleSheet;
use crate::{style::menu_bar::StyleSheet, widget::dropdown};
use iced_core::{Border, Shadow};
use iced_widget::core::{
@ -240,7 +245,7 @@ struct MenuBounds {
impl MenuBounds {
#[allow(clippy::too_many_arguments)]
fn new<Message, Renderer>(
menu_tree: &MenuTree<'_, Message, Renderer>,
menu_tree: &MenuTree<Message>,
renderer: &Renderer,
item_width: ItemWidth,
item_height: ItemHeight,
@ -300,7 +305,7 @@ impl MenuState {
overlay_offset: Vector,
slice: MenuSlice,
renderer: &Renderer,
menu_tree: &MenuTree<'_, Message, Renderer>,
menu_tree: &MenuTree<Message>,
tree: &mut [Tree],
) -> Node
where
@ -354,7 +359,7 @@ impl MenuState {
overlay_offset: Vector,
index: usize,
renderer: &Renderer,
menu_tree: &MenuTree<'_, Message, Renderer>,
menu_tree: &MenuTree<Message>,
tree: &mut Tree,
) -> Node
where
@ -427,12 +432,9 @@ impl MenuState {
}
}
pub(crate) struct Menu<'a, 'b, Message, Renderer>
where
Renderer: renderer::Renderer,
{
pub(crate) tree: &'b mut Tree,
pub(crate) menu_roots: &'b mut Vec<MenuTree<'a, Message, Renderer>>,
pub(crate) struct Menu<'b, Message: std::clone::Clone> {
pub(crate) tree: dropdown::menu::State,
pub(crate) menu_roots: Cow<'b, Vec<MenuTree<Message>>>,
pub(crate) bounds_expand: u16,
/// Allows menu overlay items to overlap the parent
pub(crate) menu_overlays_parent: bool,
@ -444,10 +446,10 @@ where
pub(crate) cross_offset: i32,
pub(crate) root_bounds_list: Vec<Rectangle>,
pub(crate) path_highlight: Option<PathHighlight>,
pub(crate) style: &'b <crate::Theme as StyleSheet>::Style,
pub(crate) style: Cow<'b, <crate::Theme as StyleSheet>::Style>,
pub(crate) position: Point,
}
impl<'a, 'b, Message, Renderer> Menu<'a, 'b, Message, Renderer>
impl<'b, Message: Clone + 'static, Renderer> Menu<'b, Message>
where
Renderer: renderer::Renderer,
{
@ -751,7 +753,7 @@ fn pad_rectangle(rect: Rectangle, padding: Padding) -> Rectangle {
}
pub(super) fn init_root_menu<Message, Renderer>(
menu: &mut Menu<'_, '_, Message, Renderer>,
menu: &mut Menu<'_, Message>,
renderer: &Renderer,
shell: &mut Shell<'_, Message>,
overlay_cursor: Point,
@ -829,7 +831,7 @@ pub(super) fn init_root_menu<Message, Renderer>(
#[allow(clippy::too_many_arguments)]
fn process_menu_events<'b, Message, Renderer>(
tree: &'b mut Tree,
menu_roots: &'b mut [MenuTree<'_, Message, Renderer>],
menu_roots: &'b mut [MenuTree<Message>],
event: event::Event,
view_cursor: Cursor,
renderer: &Renderer,
@ -887,7 +889,7 @@ where
#[allow(unused_results)]
fn process_overlay_events<Message, Renderer>(
menu: &mut Menu<'_, '_, Message, Renderer>,
menu: &mut Menu<'_, Message>,
renderer: &Renderer,
viewport_size: Size,
overlay_offset: Vector,
@ -897,6 +899,7 @@ fn process_overlay_events<Message, Renderer>(
) -> event::Status
where
Renderer: renderer::Renderer,
Message: std::clone::Clone,
{
use event::Status::{Captured, Ignored};
/*
@ -1077,16 +1080,13 @@ where
Captured
}
fn process_scroll_events<Message, Renderer>(
menu: &mut Menu<'_, '_, Message, Renderer>,
fn process_scroll_events<Message: Clone>(
menu: &mut Menu<'_, Message>,
delta: mouse::ScrollDelta,
overlay_cursor: Point,
viewport_size: Size,
overlay_offset: Vector,
) -> event::Status
where
Renderer: renderer::Renderer,
{
) -> event::Status {
use event::Status::{Captured, Ignored};
use mouse::ScrollDelta;
@ -1155,16 +1155,13 @@ where
#[allow(clippy::pedantic)]
/// Returns (children_size, child_positions, child_sizes)
fn get_children_layout<Message, Renderer>(
menu_tree: &MenuTree<'_, Message, Renderer>,
renderer: &Renderer,
fn get_children_layout<Message>(
menu_tree: &MenuTree<Message>,
renderer: &crate::Renderer,
item_width: ItemWidth,
item_height: ItemHeight,
tree: &mut [Tree],
) -> (Size, Vec<f32>, Vec<Size>)
where
Renderer: renderer::Renderer,
{
) -> (Size, Vec<f32>, Vec<Size>) {
let width = match item_width {
ItemWidth::Uniform(u) => f32::from(u),
ItemWidth::Static(s) => f32::from(menu_tree.width.unwrap_or(s)),
@ -1184,7 +1181,8 @@ where
.children
.iter()
.map(|mt| {
let w = mt.item.as_widget();
let guard = mt.item.0.lock().unwrap();
let w = guard.as_widget();
match w.size().height {
Length::Fixed(f) => Size::new(width, f),
Length::Shrink => {

View file

@ -5,13 +5,14 @@
use std::borrow::Cow;
use std::collections::HashMap;
use std::rc::Rc;
use std::sync::{Arc, Mutex};
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, ArcElementWrapper};
use crate::{theme, widget};
/// Nested menu is essentially a tree of items, a menu is a collection of items
@ -23,27 +24,27 @@ 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,
// TODO needs to be wrapped in Arc<Mutex kinda like in cosmic-files. Check the widget there for details
/// The item of the menu tree
pub(crate) item: Element<'a, Message, crate::Theme, Renderer>,
pub(crate) item: ArcElementWrapper<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<ArcElementWrapper<Message>>) -> Self {
Self {
index: 0,
item: item.into(),
@ -55,8 +56,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<ArcElementWrapper<Message>>,
children: Vec<impl Into<MenuTree<Message>>>,
) -> Self {
Self {
index: 0,
@ -92,7 +93,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,36 +110,32 @@ where
}
/// Flatten the menu tree
pub(crate) fn flattern(&'a self) -> Vec<&Self> {
pub(crate) fn flatten(&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<Message: Clone + 'static>(
mt: &MenuTree<Message>,
flat: &mut Vec<MenuTree<Message>>,
) {
mt.children.iter().for_each(|c| {
mt.children.clone().into_iter().for_each(|c| {
flat.push(c);
});
mt.children.iter().for_each(|c| {
rec(c, flat);
rec(&c, flat);
});
}
let mut flat = Vec::new();
flat.push(self);
flat.push(self.clone());
rec(self, &mut flat);
flat
}
}
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(ArcElementWrapper(Arc::new(Mutex::new(value))))
}
}
@ -157,6 +154,7 @@ pub fn menu_button<'a, Message: 'a>(
.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.
@ -213,18 +211,13 @@ where
/// # Returns
/// - A vector of `MenuTree`.
pub fn menu_items<
'a,
A: MenuAction<Message = Message>,
L: Into<Cow<'static, str>> + 'static,
Message: 'a,
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>>,
{
) -> 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 {
@ -259,7 +252,7 @@ where
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 key = find_key(&action, key_binds);
@ -277,7 +270,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);
@ -307,36 +300,40 @@ 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
},
),
trees.push(MenuTree::<Message>::with_children(
ArcElementWrapper(Arc::new(Mutex::new(crate::Element::from(
menu_button::<'static, _>(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
},
),
)))),
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(),
));
)));
}
}
}