This commit is contained in:
Ashley Wulber 2025-04-24 18:24:10 -04:00
parent 6fb4a4a43e
commit 4fcd09d690
No known key found for this signature in database
GPG key ID: 5216D4F46A90A820
10 changed files with 999 additions and 763 deletions

View file

@ -13,17 +13,19 @@ use iced_core::widget::{Tree, Widget, tree};
use iced_core::{Length, Point, Size, event, mouse, touch};
use std::collections::HashSet;
use super::dropdown::menu::State;
/// A context menu is a menu in a graphical user interface that appears upon user interaction, such as a right-click mouse operation.
pub fn context_menu<'a, Message: 'a>(
content: impl Into<crate::Element<'a, Message>> + 'a,
pub fn context_menu<Message: 'static + Clone>(
content: impl Into<crate::Element<'static, Message>> + 'static,
// on_context: Message,
context_menu: Option<Vec<menu::Tree<'a, Message>>>,
) -> ContextMenu<'a, Message> {
context_menu: Option<Vec<menu::Tree<Message>>>,
) -> ContextMenu<'static, Message> {
let mut this = ContextMenu {
content: content.into(),
context_menu: context_menu.map(|menus| {
vec![menu::Tree::with_children(
crate::widget::row::<'static, Message>(),
crate::Element::from(crate::widget::row::<'static, Message>()),
menus,
)]
}),
@ -43,10 +45,12 @@ pub struct ContextMenu<'a, Message> {
#[setters(skip)]
content: crate::Element<'a, Message>,
#[setters(skip)]
context_menu: Option<Vec<menu::Tree<'a, Message>>>,
context_menu: Option<Vec<menu::Tree<Message>>>,
}
impl<Message: Clone> Widget<Message, crate::Theme, crate::Renderer> for ContextMenu<'_, Message> {
impl<Message: 'static + Clone> Widget<Message, crate::Theme, crate::Renderer>
for ContextMenu<'_, Message>
{
fn tag(&self) -> tree::Tag {
tree::Tag::of::<LocalState>()
}
@ -56,6 +60,7 @@ impl<Message: Clone> Widget<Message, crate::Theme, crate::Renderer> for ContextM
tree::State::new(LocalState {
context_cursor: Point::default(),
fingers_pressed: Default::default(),
menu_state: Default::default(),
})
}
@ -75,7 +80,7 @@ impl<Message: Clone> Widget<Message, crate::Theme, crate::Renderer> for ContextM
let flat = root
.flattern()
.iter()
.map(|mt| Tree::new(mt.item.as_widget()))
.map(|mt| Tree::new(mt.item.clone()))
.collect();
tree.children = flat;
tree
@ -183,10 +188,12 @@ impl<Message: Clone> Widget<Message, crate::Theme, crate::Renderer> for ContextM
&& (right_button_released(&event) || (touch_lifted(&event) && fingers_pressed == 2))
{
state.context_cursor = cursor.position().unwrap_or_default();
let menu_state = tree.children[1].state.downcast_mut::<MenuBarState>();
menu_state.open = true;
menu_state.view_cursor = cursor;
menu_state.inner.with_data_mut(|state| {
state.open = true;
state.view_cursor = cursor;
});
return event::Status::Captured;
}
@ -212,23 +219,28 @@ impl<Message: Clone> Widget<Message, crate::Theme, crate::Renderer> for ContextM
translation: Vector,
) -> Option<iced_core::overlay::Element<'b, Message, crate::Theme, crate::Renderer>> {
let state = tree.state.downcast_ref::<LocalState>();
let menu_state = state.menu_state.clone();
let Some(context_menu) = self.context_menu.as_mut() else {
return None;
};
if !tree.children[1].state.downcast_ref::<MenuBarState>().open {
if !tree.children[1]
.state
.downcast_ref::<MenuBarState>()
.inner
.with_data(|state| state.open)
{
return None;
}
let mut bounds = layout.bounds();
bounds.x = state.context_cursor.x;
bounds.y = state.context_cursor.y;
Some(
crate::widget::menu::Menu {
tree: &mut tree.children[1],
menu_roots: context_menu,
tree: menu_state,
menu_roots: std::borrow::Cow::Borrowed(context_menu),
bounds_expand: 16,
menu_overlays_parent: true,
close_condition: CloseCondition {
@ -243,7 +255,7 @@ impl<Message: Clone> Widget<Message, crate::Theme, crate::Renderer> for ContextM
cross_offset: 0,
root_bounds_list: vec![bounds],
path_highlight: Some(PathHighlight::MenuActive),
style: &crate::theme::menu_bar::MenuBarStyle::Default,
style: std::borrow::Cow::Borrowed(&crate::theme::menu_bar::MenuBarStyle::Default),
position: Point::new(translation.x, translation.y),
}
.overlay(),
@ -263,8 +275,10 @@ impl<Message: Clone> Widget<Message, crate::Theme, crate::Renderer> for ContextM
}
}
impl<'a, Message: Clone + 'a> From<ContextMenu<'a, Message>> for crate::Element<'a, Message> {
fn from(widget: ContextMenu<'a, Message>) -> Self {
impl<'a, Message: Clone + 'static> From<ContextMenu<'static, Message>>
for crate::Element<'static, Message>
{
fn from(widget: ContextMenu<'static, Message>) -> Self {
Self::new(widget)
}
}
@ -283,4 +297,5 @@ fn touch_lifted(event: &Event) -> bool {
pub struct LocalState {
context_cursor: Point,
fingers_pressed: HashSet<Finger>,
menu_state: MenuBarState,
}

View file

@ -1,12 +1,14 @@
// From iced_aw, license MIT
use iced_core::widget::Tree;
use iced_core::{Widget, widget::Tree};
use iced_widget::core::{
Alignment, Element, Padding, Point, Size,
layout::{Limits, Node},
renderer,
};
use crate::widget::RcElementWrapper;
/// The main axis of a flex layout.
#[derive(Debug)]
pub enum Axis {
@ -217,3 +219,170 @@ where
Node::with_children(size.expand(padding), nodes)
}
/// Computes the flex layout with the given axis and limits, applying spacing,
/// padding and alignment to the items as needed.
///
/// It returns a new layout [`Node`].
pub fn resolve_wrapper<'a, Message>(
axis: &Axis,
renderer: &crate::Renderer,
limits: &Limits,
padding: Padding,
spacing: f32,
align_items: Alignment,
items: &[&RcElementWrapper<Message>],
tree: &mut [&mut Tree],
) -> Node {
let limits = limits.shrink(padding);
let total_spacing = spacing * items.len().saturating_sub(1) as f32;
let max_cross = axis.cross(limits.max());
let mut fill_sum = 0;
let mut cross = axis.cross(limits.min()).max(axis.cross(Size::INFINITY));
let mut available = axis.main(limits.max()) - total_spacing;
let mut nodes: Vec<Node> = Vec::with_capacity(items.len());
nodes.resize(items.len(), Node::default());
if align_items == Alignment::Center {
let mut fill_cross = axis.cross(limits.min());
for (child, tree) in items.iter().zip(tree.iter_mut()) {
let c_size = child.size();
let cross_fill_factor = match axis {
Axis::Horizontal => c_size.height,
Axis::Vertical => c_size.width,
}
.fill_factor();
if cross_fill_factor == 0 {
let (max_width, max_height) = axis.pack(available, max_cross);
let child_limits = Limits::new(Size::ZERO, Size::new(max_width, max_height));
let layout = child.layout(tree, renderer, &child_limits);
let size = layout.size();
fill_cross = fill_cross.max(axis.cross(size));
}
}
cross = fill_cross;
}
for (i, (child, tree)) in items.iter().zip(tree.iter_mut()).enumerate() {
let c_size = child.size();
let fill_factor = match axis {
Axis::Horizontal => c_size.width,
Axis::Vertical => c_size.height,
}
.fill_factor();
if fill_factor == 0 {
let (min_width, min_height) = if align_items == Alignment::Center {
axis.pack(0.0, cross)
} else {
axis.pack(0.0, 0.0)
};
let (max_width, max_height) = if align_items == Alignment::Center {
axis.pack(available, cross)
} else {
axis.pack(available, max_cross)
};
let child_limits = Limits::new(
Size::new(min_width, min_height),
Size::new(max_width, max_height),
);
let layout = child.layout(tree, renderer, &child_limits);
let size = layout.size();
available -= axis.main(size);
if align_items != Alignment::Center {
cross = cross.max(axis.cross(size));
}
nodes[i] = layout;
} else {
fill_sum += fill_factor;
}
}
let remaining = available.max(0.0);
for (i, (child, tree)) in items.iter().zip(tree.iter_mut()).enumerate() {
let c_size = child.size();
let fill_factor = match axis {
Axis::Horizontal => c_size.width,
Axis::Vertical => c_size.height,
}
.fill_factor();
if fill_factor != 0 {
let max_main = remaining * f32::from(fill_factor) / f32::from(fill_sum);
let min_main = if max_main.is_infinite() {
0.0
} else {
max_main
};
let (min_width, min_height) = if align_items == Alignment::Center {
axis.pack(min_main, cross)
} else {
axis.pack(min_main, axis.cross(limits.min()))
};
let (max_width, max_height) = if align_items == Alignment::Center {
axis.pack(max_main, cross)
} else {
axis.pack(max_main, max_cross)
};
let child_limits = Limits::new(
Size::new(min_width, min_height),
Size::new(max_width, max_height),
);
let layout = child.layout(tree, renderer, &child_limits);
if align_items != Alignment::Center {
cross = cross.max(axis.cross(layout.size()));
}
nodes[i] = layout;
}
}
let pad = axis.pack(padding.left, padding.top);
let mut main = pad.0;
for (i, node) in nodes.iter_mut().enumerate() {
if i > 0 {
main += spacing;
}
let (x, y) = axis.pack(main, pad.1);
let node_ = node.clone().move_to(Point::new(x, y));
let node_ = match axis {
Axis::Horizontal => node_.align(Alignment::Start, align_items, Size::new(0.0, cross)),
Axis::Vertical => node_.align(align_items, Alignment::Start, Size::new(cross, 0.0)),
};
let size = node_.bounds().size();
*node = node_;
main += axis.main(size);
}
let (width, height) = axis.pack(main - pad.0, cross);
let size = limits.resolve(width, height, Size::new(width, height));
Node::with_children(size.expand(padding), nodes)
}

View file

@ -7,26 +7,42 @@ use super::{
},
menu_tree::MenuTree,
};
use crate::style::menu_bar::StyleSheet;
use crate::{
Renderer,
style::menu_bar::StyleSheet,
widget::{
RcWrapper,
dropdown::menu::{self, State},
},
};
use iced::{Point, Vector};
use iced::{Point, Vector, window};
use iced_core::Border;
use iced_widget::core::{
Alignment, Clipboard, Element, Layout, Length, Padding, Rectangle, Shell, Widget, event,
layout::{Limits, Node},
mouse::{self, Cursor},
overlay, renderer, touch,
overlay,
renderer::{self, Renderer as IcedRenderer},
touch,
widget::{Tree, tree},
};
/// A `MenuBar` collects `MenuTree`s and handles all the layout, event processing, and drawing.
pub fn menu_bar<Message, Renderer: iced_core::Renderer>(
menu_roots: Vec<MenuTree<Message, Renderer>>,
) -> MenuBar<Message, Renderer> {
pub fn menu_bar<Message>(menu_roots: Vec<MenuTree<Message>>) -> MenuBar<Message>
where
Message: Clone + 'static,
{
MenuBar::new(menu_roots)
}
#[derive(Clone, Default)]
pub(crate) struct MenuBarState {
pub(crate) inner: RcWrapper<MenuBarStateInner>,
}
pub(crate) struct MenuBarStateInner {
pub(crate) tree: Tree,
pub(crate) pressed: bool,
pub(crate) view_cursor: Cursor,
pub(crate) open: bool,
@ -35,7 +51,7 @@ pub(crate) struct MenuBarState {
pub(crate) vertical_direction: Direction,
pub(crate) menu_states: Vec<MenuState>,
}
impl MenuBarState {
impl MenuBarStateInner {
pub(super) fn get_trimmed_indices(&self) -> impl Iterator<Item = usize> + '_ {
self.menu_states
.iter()
@ -49,9 +65,10 @@ impl MenuBarState {
self.menu_states.clear();
}
}
impl Default for MenuBarState {
impl Default for MenuBarStateInner {
fn default() -> Self {
Self {
tree: Tree::empty(),
pressed: false,
view_cursor: Cursor::Available([-0.5, -0.5].into()),
open: false,
@ -63,11 +80,9 @@ impl Default for MenuBarState {
}
}
pub(crate) fn menu_roots_children<Message, Renderer>(
menu_roots: &Vec<MenuTree<'_, Message, Renderer>>,
) -> Vec<Tree>
pub(crate) fn menu_roots_children<Message>(menu_roots: &Vec<MenuTree<Message>>) -> Vec<Tree>
where
Renderer: renderer::Renderer,
Message: Clone + 'static,
{
/*
menu bar
@ -85,7 +100,7 @@ where
let flat = root
.flattern()
.iter()
.map(|mt| Tree::new(mt.item.as_widget()))
.map(|mt| Tree::new(mt.item.clone()))
.collect();
tree.children = flat;
tree
@ -94,11 +109,9 @@ where
}
#[allow(invalid_reference_casting)]
pub(crate) fn menu_roots_diff<Message, Renderer>(
menu_roots: &mut Vec<MenuTree<'_, Message, Renderer>>,
tree: &mut Tree,
) where
Renderer: renderer::Renderer,
pub(crate) fn menu_roots_diff<Message>(menu_roots: &mut Vec<MenuTree<Message>>, tree: &mut Tree)
where
Message: Clone + 'static,
{
if tree.children.len() > menu_roots.len() {
tree.children.truncate(menu_roots.len());
@ -112,7 +125,7 @@ pub(crate) fn menu_roots_diff<Message, Renderer>(
.flattern()
.iter()
.map(|mt| {
let widget = mt.item.as_widget();
let widget = &mt.item;
let widget_ptr = widget as *const dyn Widget<Message, crate::Theme, Renderer>;
let widget_ptr_mut =
widget_ptr as *mut dyn Widget<Message, crate::Theme, Renderer>;
@ -130,7 +143,7 @@ pub(crate) fn menu_roots_diff<Message, Renderer>(
let flat = root
.flattern()
.iter()
.map(|mt| Tree::new(mt.item.as_widget()))
.map(|mt| Tree::new(mt.item.clone()))
.collect();
tree.children = flat;
tree
@ -141,10 +154,7 @@ pub(crate) fn menu_roots_diff<Message, Renderer>(
/// A `MenuBar` collects `MenuTree`s and handles all the layout, event processing, and drawing.
#[allow(missing_debug_implementations)]
pub struct MenuBar<'a, Message, Renderer = crate::Renderer>
where
Renderer: renderer::Renderer,
{
pub struct MenuBar<Message> {
width: Length,
height: Length,
spacing: f32,
@ -156,17 +166,20 @@ where
item_width: ItemWidth,
item_height: ItemHeight,
path_highlight: Option<PathHighlight>,
menu_roots: Vec<MenuTree<'a, Message, Renderer>>,
menu_roots: Vec<MenuTree<Message>>,
style: <crate::Theme as StyleSheet>::Style,
window_id: window::Id,
#[cfg(feature = "wayland")]
positioner: iced_runtime::platform_specific::wayland::popup::SctkPositioner,
}
impl<'a, Message, Renderer> MenuBar<'a, Message, Renderer>
impl<Message> MenuBar<Message>
where
Renderer: renderer::Renderer,
Message: Clone + 'static,
{
/// Creates a new [`MenuBar`] with the given menu roots
#[must_use]
pub fn new(menu_roots: Vec<MenuTree<'a, Message, Renderer>>) -> Self {
pub fn new(menu_roots: Vec<MenuTree<Message>>) -> Self {
let mut menu_roots = menu_roots;
menu_roots.iter_mut().for_each(MenuTree::set_index);
@ -188,6 +201,9 @@ where
path_highlight: Some(PathHighlight::MenuActive),
menu_roots,
style: <crate::Theme as StyleSheet>::Style::default(),
window_id: window::Id::NONE,
#[cfg(feature = "wayland")]
positioner: iced_runtime::platform_specific::wayland::popup::SctkPositioner::default(),
}
}
@ -278,10 +294,31 @@ where
self.width = width;
self
}
#[cfg(feature = "wayland")]
pub fn with_positioner(
mut self,
positioner: iced_runtime::platform_specific::wayland::popup::SctkPositioner,
) -> Self {
self.positioner = positioner;
self
}
pub fn window_id(mut self, id: window::Id) -> Self {
self.window_id = id;
self
}
pub fn window_id_maybe(mut self, id: Option<window::Id>) -> Self {
if let Some(id) = id {
self.window_id = id;
}
self
}
}
impl<Message, Renderer> Widget<Message, crate::Theme, Renderer> for MenuBar<'_, Message, Renderer>
impl<Message> Widget<Message, crate::Theme, Renderer> for MenuBar<Message>
where
Renderer: renderer::Renderer,
Message: Clone + 'static,
{
fn size(&self) -> iced_core::Size<Length> {
iced_core::Size::new(self.width, self.height)
@ -318,7 +355,7 @@ where
.iter_mut()
.map(|t| &mut t.children[0])
.collect::<Vec<_>>();
flex::resolve(
flex::resolve_wrapper(
&flex::Axis::Horizontal,
renderer,
&limits,
@ -361,12 +398,14 @@ where
match event {
Mouse(ButtonReleased(Left)) | Touch(FingerLifted { .. } | FingerLost { .. }) => {
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
}
state.inner.with_data_mut(|state| {
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
}
});
}
_ => (),
}
@ -385,49 +424,51 @@ where
) {
let state = tree.state.downcast_ref::<MenuBarState>();
let cursor_pos = view_cursor.position().unwrap_or_default();
let position = if state.open && (cursor_pos.x < 0.0 || cursor_pos.y < 0.0) {
state.view_cursor
} else {
view_cursor
};
state.inner.with_data_mut(|state| {
let position = if state.open && (cursor_pos.x < 0.0 || cursor_pos.y < 0.0) {
state.view_cursor
} else {
view_cursor
};
// draw path highlight
if self.path_highlight.is_some() {
let styling = theme.appearance(&self.style);
if let Some(active) = state.active_root {
let active_bounds = layout
.children()
.nth(active)
.expect("Active child not found in menu?")
.bounds();
let path_quad = renderer::Quad {
bounds: active_bounds,
border: Border {
radius: styling.bar_border_radius.into(),
..Default::default()
},
shadow: Default::default(),
};
// draw path highlight
if self.path_highlight.is_some() {
let styling = theme.appearance(&self.style);
if let Some(active) = state.active_root {
let active_bounds = layout
.children()
.nth(active)
.expect("Active child not found in menu?")
.bounds();
let path_quad = renderer::Quad {
bounds: active_bounds,
border: Border {
radius: styling.bar_border_radius.into(),
..Default::default()
},
shadow: Default::default(),
};
renderer.fill_quad(path_quad, styling.path);
renderer.fill_quad(path_quad, styling.path);
}
}
}
self.menu_roots
.iter()
.zip(&tree.children)
.zip(layout.children())
.for_each(|((root, t), lo)| {
root.item.as_widget().draw(
&t.children[root.index],
renderer,
theme,
style,
lo,
position,
viewport,
);
});
self.menu_roots
.iter()
.zip(&tree.children)
.zip(layout.children())
.for_each(|((root, t), lo)| {
root.item.draw(
&t.children[root.index],
renderer,
theme,
style,
lo,
position,
viewport,
);
});
});
}
fn overlay<'b>(
@ -441,14 +482,14 @@ where
// return None;
let state = tree.state.downcast_ref::<MenuBarState>();
if !state.open {
if state.inner.with_data_mut(|state| !state.open) {
return None;
}
};
Some(
Menu {
tree,
menu_roots: &mut self.menu_roots,
tree: state.clone(),
menu_roots: std::borrow::Cow::Borrowed(&mut self.menu_roots),
bounds_expand: self.bounds_expand,
menu_overlays_parent: false,
close_condition: self.close_condition,
@ -459,27 +500,26 @@ where
cross_offset: self.cross_offset,
root_bounds_list: layout.children().map(|lo| lo.bounds()).collect(),
path_highlight: self.path_highlight,
style: &self.style,
style: std::borrow::Cow::Borrowed(&self.style),
position: Point::new(translation.x, translation.y),
}
.overlay(),
)
}
}
impl<'a, Message, Renderer> From<MenuBar<'a, Message, Renderer>>
for Element<'a, Message, crate::Theme, Renderer>
impl<'a, Message> From<MenuBar<Message>> for Element<'a, Message, crate::Theme, Renderer>
where
Message: 'a,
Renderer: 'a + renderer::Renderer,
Message: Clone + 'static,
{
fn from(value: MenuBar<'a, Message, Renderer>) -> Self {
fn from(value: MenuBar<Message>) -> Self {
Self::new(value)
}
}
#[allow(unused_results, clippy::too_many_arguments)]
fn process_root_events<Message, Renderer>(
menu_roots: &mut [MenuTree<'_, Message, Renderer>],
fn process_root_events<Message>(
menu_roots: &mut [MenuTree<Message>],
view_cursor: Cursor,
tree: &mut Tree,
event: &event::Event,
@ -490,7 +530,6 @@ fn process_root_events<Message, Renderer>(
viewport: &Rectangle,
) -> event::Status
where
Renderer: renderer::Renderer,
{
menu_roots
.iter_mut()
@ -498,7 +537,7 @@ where
.zip(layout.children())
.map(|((root, t), lo)| {
// assert!(t.tag == tree::Tag::stateless());
root.item.as_widget_mut().on_event(
root.item.on_event(
&mut t.children[root.index],
event.clone(),
lo,

File diff suppressed because it is too large Load diff

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.
@ -216,19 +211,13 @@ where
/// # Returns
/// - A vector of `MenuTree`.
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 {
@ -263,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);
@ -281,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);
@ -311,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(
RcElementWrapper::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(),
));
)));
}
}
}

View file

@ -69,7 +69,7 @@ impl<'a, Message: Clone + 'static> NavBar<'a, Message> {
}
#[inline]
pub fn context_menu(mut self, context_menu: Option<Vec<menu::Tree<'a, Message>>>) -> Self {
pub fn context_menu(mut self, context_menu: Option<Vec<menu::Tree<Message>>>) -> Self {
self.segmented_button = self.segmented_button.context_menu(context_menu);
self
}

View file

@ -127,7 +127,7 @@ where
pub(super) style: Style,
/// The context menu to display when a context is activated
#[setters(skip)]
pub(super) context_menu: Option<Vec<menu::Tree<'a, Message, crate::Renderer>>>,
pub(super) context_menu: Option<Vec<menu::Tree<Message>>>,
/// Emits the ID of the item that was activated.
#[setters(skip)]
pub(super) on_activate: Option<Box<dyn Fn(Entity) -> Message + 'static>>,
@ -198,13 +198,13 @@ where
}
}
pub fn context_menu(mut self, context_menu: Option<Vec<menu::Tree<'a, Message>>>) -> Self
pub fn context_menu(mut self, context_menu: Option<Vec<menu::Tree<Message>>>) -> Self
where
Message: 'static,
Message: Clone + 'static,
{
self.context_menu = context_menu.map(|menus| {
vec![menu::Tree::with_children(
crate::widget::row::<'static, Message>(),
crate::Element::from(crate::widget::row::<'static, Message>()),
menus,
)]
});
@ -577,6 +577,7 @@ where
fn state(&self) -> tree::State {
#[allow(clippy::default_trait_access)]
tree::State::new(LocalState {
menu_state: Default::default(),
paragraphs: SecondaryMap::new(),
text_hashes: SecondaryMap::new(),
buttons_visible: Default::default(),
@ -955,8 +956,10 @@ where
let menu_state =
tree.children[0].state.downcast_mut::<MenuBarState>();
menu_state.open = true;
menu_state.view_cursor = cursor_position;
menu_state.inner.with_data_mut(|data| {
data.open = true;
data.view_cursor = cursor_position;
});
shell.publish(on_context(key));
return event::Status::Captured;
@ -1346,7 +1349,11 @@ where
let center_y = bounds.center_y();
let menu_open = !tree.children.is_empty()
&& tree.children[0].state.downcast_ref::<MenuBarState>().open;
&& tree.children[0]
.state
.downcast_ref::<MenuBarState>()
.inner
.with_data(|data| data.open);
let key_is_active = self.model.is_active(key);
let key_is_hovered = self.button_is_hovered(state, key);
@ -1556,6 +1563,7 @@ where
translation: Vector,
) -> Option<iced_core::overlay::Element<'b, Message, crate::Theme, Renderer>> {
let state = tree.state.downcast_ref::<LocalState>();
let menu_state = state.menu_state.clone();
let Some(entity) = state.show_context else {
return None;
@ -1575,7 +1583,12 @@ where
return None;
};
if !tree.children[0].state.downcast_ref::<MenuBarState>().open {
if !tree.children[0]
.state
.downcast_ref::<MenuBarState>()
.inner
.with_data(|data| data.open)
{
return None;
}
@ -1584,8 +1597,8 @@ where
Some(
crate::widget::menu::Menu {
tree: &mut tree.children[0],
menu_roots: context_menu,
tree: menu_state,
menu_roots: std::borrow::Cow::Borrowed(context_menu),
bounds_expand: 16,
menu_overlays_parent: true,
close_condition: CloseCondition {
@ -1600,7 +1613,7 @@ where
cross_offset: 0,
root_bounds_list: vec![bounds],
path_highlight: Some(PathHighlight::MenuActive),
style: &crate::theme::menu_bar::MenuBarStyle::Default,
style: std::borrow::Cow::Borrowed(&crate::theme::menu_bar::MenuBarStyle::Default),
position: Point::new(translation.x, translation.y),
}
.overlay(),
@ -1653,6 +1666,8 @@ where
/// State that is maintained by each individual widget.
pub struct LocalState {
/// Menu state
pub(crate) menu_state: MenuBarState,
/// Defines how many buttons to show at a time.
pub(super) buttons_visible: usize,
/// Button visibility offset, when collapsed.

View file

@ -44,11 +44,12 @@ where
#[setters(skip)]
pub(super) on_item_mb_right: Option<Box<dyn Fn(Entity) -> Message + 'static>>,
#[setters(skip)]
pub(super) item_context_builder: Box<dyn Fn(&Item) -> Option<Vec<menu::Tree<'a, Message>>>>,
pub(super) item_context_builder: Box<dyn Fn(&Item) -> Option<Vec<menu::Tree<Message>>>>,
}
impl<'a, SelectionMode, Item, Category, Message>
From<CompactTableView<'a, SelectionMode, Item, Category, Message>> for Element<'a, Message>
impl<SelectionMode, Item, Category, Message>
From<CompactTableView<'static, SelectionMode, Item, Category, Message>>
for Element<'static, Message>
where
Category: ItemCategory,
Item: ItemInterface<Category>,
@ -56,7 +57,7 @@ where
SelectionMode: Default,
Message: Clone + 'static,
{
fn from(val: CompactTableView<'a, SelectionMode, Item, Category, Message>) -> Self {
fn from(val: CompactTableView<'static, SelectionMode, Item, Category, Message>) -> Self {
let cosmic_theme::Spacing { space_xxxs, .. } = theme::spacing();
val.model
.iter()
@ -97,7 +98,7 @@ where
]
})
.flatten()
.collect::<Vec<Element<'a, Message>>>();
.collect::<Vec<Element<'static, Message>>>();
elements.pop();
elements
.apply(widget::row::with_children)
@ -171,7 +172,7 @@ where
)
.apply(Element::from)
})
.collect::<Vec<Element<'a, Message>>>()
.collect::<Vec<Element<'static, Message>>>()
.apply(widget::column::with_children)
.spacing(val.item_spacing)
.padding(val.element_padding)
@ -247,7 +248,7 @@ where
pub fn item_context<F>(mut self, context_menu_builder: F) -> Self
where
F: Fn(&Item) -> Option<Vec<menu::Tree<'a, Message>>> + 'static,
F: Fn(&Item) -> Option<Vec<menu::Tree<Message>>> + 'static,
Message: 'static,
{
self.item_context_builder = Box::new(context_menu_builder);

View file

@ -51,7 +51,7 @@ where
#[setters(skip)]
pub(super) on_item_mb_right: Option<Box<dyn Fn(Entity) -> Message + 'static>>,
#[setters(skip)]
pub(super) item_context_builder: Box<dyn Fn(&Item) -> Option<Vec<menu::Tree<'a, Message>>>>,
pub(super) item_context_builder: Box<dyn Fn(&Item) -> Option<Vec<menu::Tree<Message>>>>,
// Item DND
// === Category Interaction ===
@ -64,12 +64,11 @@ where
#[setters(skip)]
pub(super) on_category_mb_right: Option<Box<dyn Fn(Category) -> Message + 'static>>,
#[setters(skip)]
pub(super) category_context_builder:
Box<dyn Fn(Category) -> Option<Vec<menu::Tree<'a, Message>>>>,
pub(super) category_context_builder: Box<dyn Fn(Category) -> Option<Vec<menu::Tree<Message>>>>,
}
impl<'a, SelectionMode, Item, Category, Message>
From<TableView<'a, SelectionMode, Item, Category, Message>> for Element<'a, Message>
impl<SelectionMode, Item, Category, Message>
From<TableView<'static, SelectionMode, Item, Category, Message>> for Element<'static, Message>
where
Category: ItemCategory,
Item: ItemInterface<Category>,
@ -77,13 +76,13 @@ where
SelectionMode: Default,
Message: Clone + 'static,
{
fn from(val: TableView<'a, SelectionMode, Item, Category, Message>) -> Self {
fn from(val: TableView<'static, SelectionMode, Item, Category, Message>) -> Self {
// Header row
let header_row = val
.model
.categories
.iter()
.cloned()
.copied()
.map(|category| {
let cat_context_tree = (val.category_context_builder)(category);
@ -126,7 +125,7 @@ where
.apply(|mouse_area| widget::context_menu(mouse_area, cat_context_tree))
.apply(Element::from)
})
.collect::<Vec<Element<'a, Message>>>()
.collect::<Vec<Element<'static, Message>>>()
.apply(widget::row::with_children)
.apply(Element::from);
// Build the items
@ -167,7 +166,7 @@ where
.align_y(Alignment::Center)
.apply(Element::from)
})
.collect::<Vec<Element<'a, Message>>>()
.collect::<Vec<Element<'static, Message>>>()
.apply(widget::row::with_children)
.apply(container)
.padding(val.item_padding)
@ -235,12 +234,12 @@ where
]
})
.flatten()
.collect::<Vec<Element<'a, Message>>>()
.collect::<Vec<Element<'static, Message>>>()
};
vec![vec![header_row], items_full]
.into_iter()
.flatten()
.collect::<Vec<Element<'a, Message>>>()
.collect::<Vec<Element<'static, Message>>>()
.apply(widget::column::with_children)
.width(val.width)
.height(val.height)
@ -328,7 +327,7 @@ where
pub fn item_context<F>(mut self, context_menu_builder: F) -> Self
where
F: Fn(&Item) -> Option<Vec<menu::Tree<'a, Message>>> + 'static,
F: Fn(&Item) -> Option<Vec<menu::Tree<Message>>> + 'static,
Message: 'static,
{
self.item_context_builder = Box::new(context_menu_builder);
@ -367,7 +366,7 @@ where
pub fn category_context<F>(mut self, context_menu_builder: F) -> Self
where
F: Fn(Category) -> Option<Vec<menu::Tree<'a, Message>>> + 'static,
F: Fn(Category) -> Option<Vec<menu::Tree<Message>>> + 'static,
Message: 'static,
{
self.category_context_builder = Box::new(context_menu_builder);

View file

@ -1,4 +1,5 @@
use std::{
borrow::Borrow,
cell::RefCell,
rc::Rc,
thread::{self, ThreadId},
@ -14,6 +15,12 @@ pub struct RcWrapper<T> {
pub(crate) thread_id: ThreadId,
}
impl<T: Default> Default for RcWrapper<T> {
fn default() -> Self {
Self::new(T::default())
}
}
impl<T> Clone for RcWrapper<T> {
fn clone(&self) -> Self {
Self {
@ -75,6 +82,12 @@ impl<M> RcElementWrapper<M> {
}
}
impl<M: 'static> Borrow<dyn Widget<M, crate::Theme, crate::Renderer>> for RcElementWrapper<M> {
fn borrow(&self) -> &(dyn Widget<M, crate::Theme, crate::Renderer> + 'static) {
self
}
}
impl<M> Widget<M, crate::Theme, crate::Renderer> for RcElementWrapper<M> {
fn size(&self) -> Size<Length> {
self.element.with_data(|e| e.as_widget().size())