Compare commits

...
Sign in to create a new pull request.

12 commits

Author SHA1 Message Date
Ashley Wulber
3d84fb2a89
cleanup 2025-06-10 11:05:29 -04:00
Ashley Wulber
3fd6c4f6bc
popup refactor 2025-06-09 10:38:33 -04:00
Ashley Wulber
51c391a656
wip: on hover switch and fix for nesting 2025-06-05 12:10:07 -04:00
Ashley Wulber
ff41ff0532
avoid closing with children 2025-06-04 10:28:05 -04:00
Ashley Wulber
c5c327482b
wip: hover display working for nested popups 2025-06-04 00:29:24 -04:00
Ashley Wulber
4c03483e25
wip 2025-06-02 13:25:05 -04:00
Ashley Wulber
89700a2ed5
wip 2025-05-29 10:32:13 -04:00
Ashley Wulber
a372ce800f
wip: popup 2025-05-29 00:19:39 -04:00
Ashley Wulber
8642a92270
wip 2025-05-29 00:17:25 -04:00
Ashley Wulber
2cfef8814e
wip 2025-05-29 00:17:01 -04:00
Ashley Wulber
9e9449d302
wip 2025-05-29 00:16:11 -04:00
Ashley Wulber
4fcd09d690
wip 2025-05-29 00:13:10 -04:00
23 changed files with 1855 additions and 927 deletions

View file

@ -25,4 +25,5 @@ features = [
"wgpu", "wgpu",
"single-instance", "single-instance",
"multi-window", "multi-window",
"surface-message",
] ]

View file

@ -46,13 +46,19 @@ impl Page {
#[derive(Clone, Copy, Debug, Eq, PartialEq)] #[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Action { pub enum Action {
Hi, Hi,
Hi2,
Hi3,
} }
impl MenuAction for Action { impl MenuAction for Action {
type Message = Message; type Message = Message;
fn message(&self) -> Message { fn message(&self) -> Message {
Message::Hi match self {
Action::Hi => Message::Hi,
Action::Hi2 => Message::Hi2,
Action::Hi3 => Message::Hi3,
}
} }
} }
@ -86,6 +92,8 @@ pub enum Message {
ToggleHide, ToggleHide,
Surface(cosmic::surface::Action), Surface(cosmic::surface::Action),
Hi, Hi,
Hi2,
Hi3,
} }
/// The [`App`] stores application-specific state. /// The [`App`] stores application-specific state.
@ -176,6 +184,12 @@ impl cosmic::Application for App {
Message::Hi => { Message::Hi => {
dbg!("hi"); dbg!("hi");
} }
Message::Hi2 => {
dbg!("hi 2");
}
Message::Hi3 => {
dbg!("hi 3");
}
} }
Task::none() Task::none()
} }
@ -221,119 +235,80 @@ impl cosmic::Application for App {
} }
fn header_start(&self) -> Vec<Element<Self::Message>> { fn header_start(&self) -> Vec<Element<Self::Message>> {
use cosmic::widget::menu::Tree; vec![cosmic::widget::responsive_menu_bar().into_element(
#[cfg(not(feature = "wayland"))] self.core(),
{ &self.keybinds,
vec![cosmic::widget::menu::bar(vec![ MENU_ID.clone(),
Tree::with_children( Message::Surface,
menu::root("hiiiiiiiiiiiiiiiiiii 1"), vec![
menu::items( (
&self.keybinds, "hi 1".into(),
vec![menu::Item::Button("hi", None, Action::Hi)], vec![
), menu::Item::Button("hi 12", None, Action::Hi),
menu::Item::Button("hi 13", None, Action::Hi2),
],
), ),
Tree::with_children( (
menu::root("hiiiiiiiiiiiiiiiiii 2"), "hi 2".into(),
menu::items( vec![
&self.keybinds, menu::Item::Button("hi 21", None, Action::Hi),
vec![menu::Item::Button("hi 2", None, Action::Hi)], menu::Item::Button("hi 22", None, Action::Hi2),
), menu::Item::Folder(
"nest 3 2 >".into(),
vec![
menu::Item::Button("21", None, Action::Hi),
menu::Item::Button("242", None, Action::Hi2),
menu::Item::Button("2443", None, Action::Hi3),
menu::Item::Folder(
"nest 4 2 >".into(),
vec![
menu::Item::Button("243", None, Action::Hi2),
menu::Item::Button("2444", None, Action::Hi),
],
),
],
),
],
), ),
Tree::with_children( (
menu::root("hiiiiiiiiiiiiiiiiiiiii 3"), "hi 3".into(),
menu::items( vec![
&self.keybinds, menu::Item::Button("hi 31", None, Action::Hi),
vec![ menu::Item::Button("hi 332", None, Action::Hi2),
menu::Item::Button("hi 3", None, Action::Hi), menu::Item::Button("hi 3333", None, Action::Hi3),
menu::Item::Button("hi 3 #2", None, Action::Hi), menu::Item::Button("hi 33334", None, Action::Hi3),
], menu::Item::Button("hi 333335", None, Action::Hi3),
), menu::Item::Button("hi 3333336", None, Action::Hi3),
],
), ),
Tree::with_children( (
menu::root("hi 3"), "hiiiiiiiiiiiiiiiiiii 4".into(),
menu::items( vec![
&self.keybinds, menu::Item::Button("hi 4", None, Action::Hi),
vec![ menu::Item::Button("hi 44", None, Action::Hi2),
menu::Item::Button("hi 3", None, Action::Hi), menu::Item::Button("hi 444", None, Action::Hi3),
menu::Item::Button("hi 3 #2", None, Action::Hi), menu::Item::Folder(
menu::Item::Button("hi 3 #3", None, Action::Hi), "nest 4 >".into(),
], vec![
), menu::Item::Button("hi 41", None, Action::Hi),
menu::Item::Button("hi 442", None, Action::Hi2),
menu::Item::Folder(
"nest 3 4 >".into(),
vec![
menu::Item::Button("hi 443", None, Action::Hi2),
menu::Item::Button("hi 4444", None, Action::Hi),
menu::Item::Button("hi 44444", None, Action::Hi3),
menu::Item::Button("hi 444445", None, Action::Hi3),
menu::Item::Button("hi 4444446", None, Action::Hi3),
menu::Item::Button("hi 44444447", None, Action::Hi3),
],
),
],
),
],
), ),
Tree::with_children( ],
menu::root("hi 4"), )]
menu::items(
&self.keybinds,
vec![
menu::Item::Folder(
"hi 41 extra root",
vec![menu::Item::Button("hi 3", None, Action::Hi)],
),
menu::Item::Button("hi 42", None, Action::Hi),
menu::Item::Button("hi 43", None, Action::Hi),
menu::Item::Button("hi 44", None, Action::Hi),
menu::Item::Button("hi 45", None, Action::Hi),
menu::Item::Button("hi 46", None, Action::Hi),
],
),
),
])
.into()]
}
#[cfg(feature = "wayland")]
{
vec![cosmic::widget::responsive_menu_bar().into_element(
self.core(),
&self.keybinds,
MENU_ID.clone(),
Message::Surface,
vec![
(
"hiiiiiiiiiiiiiiiiiii 1",
vec![menu::Item::Button("hi 1", None, Action::Hi)],
),
(
"hiiiiiiiiiiiiiiiiiii 2".into(),
vec![
menu::Item::Button("hi 2", None, Action::Hi),
menu::Item::Button("hi 22", None, Action::Hi),
],
),
(
"hiiiiiiiiiiiiiiiiiii 3".into(),
vec![
menu::Item::Button("hi 3", None, Action::Hi),
menu::Item::Button("hi 33", None, Action::Hi),
menu::Item::Button("hi 333", None, Action::Hi),
],
),
(
"hiiiiiiiiiiiiiiiiiii 4".into(),
vec![
menu::Item::Button("hi 4", None, Action::Hi),
menu::Item::Button("hi 44", None, Action::Hi),
menu::Item::Button("hi 444", None, Action::Hi),
menu::Item::Folder(
"nest 4".into(),
vec![
menu::Item::Button("hi 4", None, Action::Hi),
menu::Item::Button("hi 44", None, Action::Hi),
menu::Item::Button("hi 444", None, Action::Hi),
menu::Item::Folder(
"nest 2 4".into(),
vec![
menu::Item::Button("hi 4", None, Action::Hi),
menu::Item::Button("hi 44", None, Action::Hi),
menu::Item::Button("hi 444", None, Action::Hi),
],
),
],
),
],
),
],
)]
}
} }
} }

View file

@ -92,6 +92,7 @@ impl cosmic::Application for App {
|date| Message::DateSelected(date), |date| Message::DateSelected(date),
|| Message::PrevMonth, || Message::PrevMonth,
|| Message::NextMonth, || Message::NextMonth,
chrono::Weekday::Sun,
); );
content = content.push(calendar); content = content.push(calendar);

View file

@ -11,4 +11,12 @@ tracing-log = "0.2.0"
[dependencies.libcosmic] [dependencies.libcosmic]
path = "../../" path = "../../"
default-features = false default-features = false
features = ["debug", "winit", "tokio", "xdg-portal", "multi-window"] features = [
"debug",
"winit",
"tokio",
"xdg-portal",
"multi-window",
"surface-message",
"wayland",
]

View file

@ -93,7 +93,7 @@ impl cosmic::Application for App {
/// Creates a view after each update. /// Creates a view after each update.
fn view(&self) -> Element<Self::Message> { fn view(&self) -> Element<Self::Message> {
let widget = cosmic::widget::context_menu( let widget = cosmic::widget::context_menu(
cosmic::widget::button::text(&self.button_label).on_press(Message::Clicked), cosmic::widget::button::text(self.button_label.to_string()).on_press(Message::Clicked),
self.context_menu(), self.context_menu(),
); );

View file

@ -15,6 +15,7 @@ use cosmic::widget::menu::action::MenuAction;
use cosmic::widget::menu::key_bind::KeyBind; use cosmic::widget::menu::key_bind::KeyBind;
use cosmic::widget::menu::key_bind::Modifier; use cosmic::widget::menu::key_bind::Modifier;
use cosmic::widget::menu::{self, ItemHeight, ItemWidth}; use cosmic::widget::menu::{self, ItemHeight, ItemWidth};
use cosmic::widget::RcElementWrapper;
use cosmic::{executor, Element}; use cosmic::{executor, Element};
/// Runs application with these settings /// Runs application with these settings
@ -155,7 +156,7 @@ impl cosmic::Application for App {
pub fn menu_bar<'a>(config: &Config, key_binds: &HashMap<KeyBind, Action>) -> Element<'a, Message> { pub fn menu_bar<'a>(config: &Config, key_binds: &HashMap<KeyBind, Action>) -> Element<'a, Message> {
menu::bar(vec![menu::Tree::with_children( menu::bar(vec![menu::Tree::with_children(
menu::root("File"), RcElementWrapper::new(Element::from(menu::root("File"))),
menu::items( menu::items(
key_binds, key_binds,
vec![ vec![

View file

@ -209,11 +209,11 @@ impl cosmic::Application for App {
if size.width < 600.0 { if size.width < 600.0 {
widget::compact_table(&self.table_model) widget::compact_table(&self.table_model)
.on_item_left_click(Message::ItemSelect) .on_item_left_click(Message::ItemSelect)
.item_context(|item| { .item_context(move |item| {
Some(widget::menu::items( Some(widget::menu::items(
&HashMap::new(), &HashMap::new(),
vec![widget::menu::Item::Button( vec![widget::menu::Item::Button(
format!("Action on {}", item.name), format!("Action on {}", item.name.to_string()),
None, None,
Action::None, Action::None,
)], )],

View file

@ -85,7 +85,7 @@ pub fn simple_subsurface<Message: 'static, V>(
/// Used to create a popup message from within a widget. /// Used to create a popup message from within a widget.
#[cfg(all(feature = "wayland", feature = "winit"))] #[cfg(all(feature = "wayland", feature = "winit"))]
#[must_use] #[must_use]
pub fn simple_popup<Message: 'static, V>( pub fn simple_popup<Message: 'static>(
settings: impl Fn() -> iced_runtime::platform_specific::wayland::popup::SctkPopupSettings settings: impl Fn() -> iced_runtime::platform_specific::wayland::popup::SctkPopupSettings
+ Send + Send
+ Sync + Sync

View file

@ -42,7 +42,7 @@ pub enum MenuBarStyle {
#[default] #[default]
Default, Default,
/// A [`Theme`] that uses a `Custom` palette. /// A [`Theme`] that uses a `Custom` palette.
Custom(Arc<dyn StyleSheet<Style = Theme>>), Custom(Arc<dyn StyleSheet<Style = Theme> + Send + Sync>),
} }
impl From<fn(&Theme) -> Appearance> for MenuBarStyle { impl From<fn(&Theme) -> Appearance> for MenuBarStyle {

View file

@ -460,7 +460,6 @@ impl<'a, Message: 'a + Clone> Widget<Message, crate::Theme, crate::Renderer>
if !self.selected && matches!(self.style, crate::theme::Button::HeaderBar) { if !self.selected && matches!(self.style, crate::theme::Button::HeaderBar) {
headerbar_alpha = Some(0.8); headerbar_alpha = Some(0.8);
} }
theme.hovered(state.is_focused, self.selected, &self.style) theme.hovered(state.is_focused, self.selected, &self.style)
} }
} else { } else {

View file

@ -4,26 +4,26 @@
//! A context menu is a menu in a graphical user interface that appears upon user interaction, such as a right-click mouse operation. //! A context menu is a menu in a graphical user interface that appears upon user interaction, such as a right-click mouse operation.
use crate::widget::menu::{ use crate::widget::menu::{
self, CloseCondition, ItemHeight, ItemWidth, MenuBarState, PathHighlight, self, CloseCondition, ItemHeight, ItemWidth, MenuBarState, PathHighlight, menu_roots_diff,
}; };
use derive_setters::Setters; use derive_setters::Setters;
use iced::touch::Finger; use iced::touch::Finger;
use iced::{Event, Vector}; use iced::{Event, Vector, window};
use iced_core::widget::{Tree, Widget, tree}; use iced_core::widget::{Tree, Widget, tree};
use iced_core::{Length, Point, Size, event, mouse, touch}; use iced_core::{Length, Point, Size, event, mouse, touch};
use std::collections::HashSet; use std::collections::HashSet;
/// A context menu is a menu in a graphical user interface that appears upon user interaction, such as a right-click mouse operation. /// 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>( pub fn context_menu<Message: 'static + Clone>(
content: impl Into<crate::Element<'a, Message>> + 'a, content: impl Into<crate::Element<'static, Message>> + 'static,
// on_context: Message, // on_context: Message,
context_menu: Option<Vec<menu::Tree<'a, Message>>>, context_menu: Option<Vec<menu::Tree<Message>>>,
) -> ContextMenu<'a, Message> { ) -> ContextMenu<'static, Message> {
let mut this = ContextMenu { let mut this = ContextMenu {
content: content.into(), content: content.into(),
context_menu: context_menu.map(|menus| { context_menu: context_menu.map(|menus| {
vec![menu::Tree::with_children( vec![menu::Tree::with_children(
crate::widget::row::<'static, Message>(), crate::Element::from(crate::widget::row::<'static, Message>()),
menus, menus,
)] )]
}), }),
@ -43,10 +43,12 @@ pub struct ContextMenu<'a, Message> {
#[setters(skip)] #[setters(skip)]
content: crate::Element<'a, Message>, content: crate::Element<'a, Message>,
#[setters(skip)] #[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 { fn tag(&self) -> tree::Tag {
tree::Tag::of::<LocalState>() tree::Tag::of::<LocalState>()
} }
@ -56,6 +58,7 @@ impl<Message: Clone> Widget<Message, crate::Theme, crate::Renderer> for ContextM
tree::State::new(LocalState { tree::State::new(LocalState {
context_cursor: Point::default(), context_cursor: Point::default(),
fingers_pressed: Default::default(), fingers_pressed: Default::default(),
menu_bar_state: Default::default(),
}) })
} }
@ -67,7 +70,6 @@ impl<Message: Clone> Widget<Message, crate::Theme, crate::Renderer> for ContextM
// Assign the context menu's elements as this widget's children. // Assign the context menu's elements as this widget's children.
if let Some(ref context_menu) = self.context_menu { if let Some(ref context_menu) = self.context_menu {
let mut tree = Tree::empty(); let mut tree = Tree::empty();
tree.state = tree::State::new(MenuBarState::default());
tree.children = context_menu tree.children = context_menu
.iter() .iter()
.map(|root| { .map(|root| {
@ -75,7 +77,7 @@ impl<Message: Clone> Widget<Message, crate::Theme, crate::Renderer> for ContextM
let flat = root let flat = root
.flattern() .flattern()
.iter() .iter()
.map(|mt| Tree::new(mt.item.as_widget())) .map(|mt| Tree::new(mt.item.clone()))
.collect(); .collect();
tree.children = flat; tree.children = flat;
tree tree
@ -90,6 +92,10 @@ impl<Message: Clone> Widget<Message, crate::Theme, crate::Renderer> for ContextM
fn diff(&mut self, tree: &mut Tree) { fn diff(&mut self, tree: &mut Tree) {
tree.children[0].diff(self.content.as_widget_mut()); tree.children[0].diff(self.content.as_widget_mut());
let state = tree.state.downcast_mut::<LocalState>();
state.menu_bar_state.inner.with_data_mut(|inner| {
menu_roots_diff(self.context_menu.as_mut().unwrap(), &mut inner.tree);
});
// if let Some(ref mut context_menus) = self.context_menu { // if let Some(ref mut context_menus) = self.context_menu {
// for (menu, tree) in context_menus // for (menu, tree) in context_menus
@ -183,10 +189,12 @@ impl<Message: Clone> Widget<Message, crate::Theme, crate::Renderer> for ContextM
&& (right_button_released(&event) || (touch_lifted(&event) && fingers_pressed == 2)) && (right_button_released(&event) || (touch_lifted(&event) && fingers_pressed == 2))
{ {
state.context_cursor = cursor.position().unwrap_or_default(); state.context_cursor = cursor.position().unwrap_or_default();
let state = tree.state.downcast_mut::<LocalState>();
let menu_state = tree.children[1].state.downcast_mut::<MenuBarState>(); state.menu_bar_state.inner.with_data_mut(|state| {
menu_state.open = true; state.open = true;
menu_state.view_cursor = cursor; state.view_cursor = cursor;
});
return event::Status::Captured; return event::Status::Captured;
} }
@ -213,22 +221,19 @@ impl<Message: Clone> Widget<Message, crate::Theme, crate::Renderer> for ContextM
) -> Option<iced_core::overlay::Element<'b, Message, crate::Theme, crate::Renderer>> { ) -> Option<iced_core::overlay::Element<'b, Message, crate::Theme, crate::Renderer>> {
let state = tree.state.downcast_ref::<LocalState>(); let state = tree.state.downcast_ref::<LocalState>();
let Some(context_menu) = self.context_menu.as_mut() else { let context_menu = self.context_menu.as_mut()?;
return None;
};
if !tree.children[1].state.downcast_ref::<MenuBarState>().open { if !state.menu_bar_state.inner.with_data(|state| state.open) {
return None; return None;
} }
let mut bounds = layout.bounds(); let mut bounds = layout.bounds();
bounds.x = state.context_cursor.x; bounds.x = state.context_cursor.x;
bounds.y = state.context_cursor.y; bounds.y = state.context_cursor.y;
Some( Some(
crate::widget::menu::Menu { crate::widget::menu::Menu {
tree: &mut tree.children[1], tree: state.menu_bar_state.clone(),
menu_roots: context_menu, menu_roots: std::borrow::Cow::Owned(context_menu.clone()),
bounds_expand: 16, bounds_expand: 16,
menu_overlays_parent: true, menu_overlays_parent: true,
close_condition: CloseCondition { close_condition: CloseCondition {
@ -243,8 +248,12 @@ impl<Message: Clone> Widget<Message, crate::Theme, crate::Renderer> for ContextM
cross_offset: 0, cross_offset: 0,
root_bounds_list: vec![bounds], root_bounds_list: vec![bounds],
path_highlight: Some(PathHighlight::MenuActive), 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), position: Point::new(translation.x, translation.y),
is_overlay: true,
window_id: window::Id::NONE,
depth: 0,
on_surface_action: None,
} }
.overlay(), .overlay(),
) )
@ -263,8 +272,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> { impl<'a, Message: Clone + 'static> From<ContextMenu<'static, Message>>
fn from(widget: ContextMenu<'a, Message>) -> Self { for crate::Element<'static, Message>
{
fn from(widget: ContextMenu<'static, Message>) -> Self {
Self::new(widget) Self::new(widget)
} }
} }
@ -283,4 +294,5 @@ fn touch_lifted(event: &Event) -> bool {
pub struct LocalState { pub struct LocalState {
context_cursor: Point, context_cursor: Point,
fingers_pressed: HashSet<Finger>, fingers_pressed: HashSet<Finger>,
menu_bar_state: MenuBarState,
} }

View file

@ -536,15 +536,7 @@ pub fn update<
let on_close = surface::action::destroy_popup(id); let on_close = surface::action::destroy_popup(id);
let on_surface_action_clone = on_surface_action.clone(); let on_surface_action_clone = on_surface_action.clone();
let translation = layout.virtual_offset(); let translation = layout.virtual_offset();
let get_popup_action = surface::action::simple_popup::< let get_popup_action = surface::action::simple_popup::<AppMessage>(
AppMessage,
Box<
dyn Fn() -> Element<'static, crate::Action<AppMessage>>
+ Send
+ Sync
+ 'static,
>,
>(
move || { move || {
SctkPopupSettings { SctkPopupSettings {
parent, parent,

View file

@ -1,12 +1,14 @@
// From iced_aw, license MIT // From iced_aw, license MIT
use iced_core::widget::Tree; use iced_core::{Widget, widget::Tree};
use iced_widget::core::{ use iced_widget::core::{
Alignment, Element, Padding, Point, Size, Alignment, Element, Padding, Point, Size,
layout::{Limits, Node}, layout::{Limits, Node},
renderer, renderer,
}; };
use crate::widget::RcElementWrapper;
/// The main axis of a flex layout. /// The main axis of a flex layout.
#[derive(Debug)] #[derive(Debug)]
pub enum Axis { pub enum Axis {
@ -217,3 +219,170 @@ where
Node::with_children(size.expand(padding), nodes) 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

@ -1,73 +1,98 @@
// From iced_aw, license MIT // From iced_aw, license MIT
//! A widget that handles menu trees //! A widget that handles menu trees
use std::{collections::HashMap, sync::Arc};
use super::{ use super::{
menu_inner::{ menu_inner::{
CloseCondition, Direction, ItemHeight, ItemWidth, Menu, MenuState, PathHighlight, CloseCondition, Direction, ItemHeight, ItemWidth, Menu, MenuState, PathHighlight,
}, },
menu_tree::MenuTree, menu_tree::MenuTree,
}; };
use crate::style::menu_bar::StyleSheet; use crate::{
Renderer,
style::menu_bar::StyleSheet,
widget::{
RcWrapper,
dropdown::menu::{self, State},
menu::menu_inner::init_root_menu,
},
};
use iced::{Point, Vector}; use iced::{Point, Shadow, Vector, window};
use iced_core::Border; use iced_core::Border;
use iced_widget::core::{ use iced_widget::core::{
Alignment, Clipboard, Element, Layout, Length, Padding, Rectangle, Shell, Widget, event, Alignment, Clipboard, Element, Layout, Length, Padding, Rectangle, Shell, Widget, event,
layout::{Limits, Node}, layout::{Limits, Node},
mouse::{self, Cursor}, mouse::{self, Cursor},
overlay, renderer, touch, overlay,
renderer::{self, Renderer as IcedRenderer},
touch,
widget::{Tree, tree}, widget::{Tree, tree},
}; };
/// A `MenuBar` collects `MenuTree`s and handles all the layout, event processing, and drawing. /// A `MenuBar` collects `MenuTree`s and handles all the layout, event processing, and drawing.
pub fn menu_bar<Message, Renderer: iced_core::Renderer>( pub fn menu_bar<Message>(menu_roots: Vec<MenuTree<Message>>) -> MenuBar<Message>
menu_roots: Vec<MenuTree<Message, Renderer>>, where
) -> MenuBar<Message, Renderer> { Message: Clone + 'static,
{
MenuBar::new(menu_roots) MenuBar::new(menu_roots)
} }
#[derive(Clone, Default)]
pub(crate) struct MenuBarState { pub(crate) struct MenuBarState {
pub(crate) inner: RcWrapper<MenuBarStateInner>,
}
pub(crate) struct MenuBarStateInner {
pub(crate) tree: Tree,
pub(crate) popup_id: HashMap<window::Id, window::Id>,
pub(crate) pressed: bool, pub(crate) pressed: bool,
pub(crate) bar_pressed: bool,
pub(crate) view_cursor: Cursor, pub(crate) view_cursor: Cursor,
pub(crate) open: bool, pub(crate) open: bool,
pub(crate) active_root: Option<usize>, pub(crate) active_root: Vec<usize>,
pub(crate) horizontal_direction: Direction, pub(crate) horizontal_direction: Direction,
pub(crate) vertical_direction: Direction, pub(crate) vertical_direction: Direction,
/// List of all menu states
pub(crate) menu_states: Vec<MenuState>, pub(crate) menu_states: Vec<MenuState>,
} }
impl MenuBarState { impl MenuBarStateInner {
pub(super) fn get_trimmed_indices(&self) -> impl Iterator<Item = usize> + '_ { /// get the list of indices hovered for the menu
pub(super) fn get_trimmed_indices(&self, index: usize) -> impl Iterator<Item = usize> + '_ {
self.menu_states self.menu_states
.iter() .iter()
.skip(index)
.take_while(|ms| ms.index.is_some()) .take_while(|ms| ms.index.is_some())
.map(|ms| ms.index.expect("No indices were found in the menu state.")) .map(|ms| ms.index.expect("No indices were found in the menu state."))
} }
pub(super) fn reset(&mut self) { pub(super) fn reset(&mut self) {
self.open = false; self.open = false;
self.active_root = None; self.active_root = Vec::new();
self.menu_states.clear(); self.menu_states.clear();
} }
} }
impl Default for MenuBarState { impl Default for MenuBarStateInner {
fn default() -> Self { fn default() -> Self {
Self { Self {
tree: Tree::empty(),
pressed: false, pressed: false,
view_cursor: Cursor::Available([-0.5, -0.5].into()), view_cursor: Cursor::Available([-0.5, -0.5].into()),
open: false, open: false,
active_root: None, active_root: Vec::new(),
horizontal_direction: Direction::Positive, horizontal_direction: Direction::Positive,
vertical_direction: Direction::Positive, vertical_direction: Direction::Positive,
menu_states: Vec::new(), menu_states: Vec::new(),
popup_id: HashMap::new(),
bar_pressed: false,
} }
} }
} }
pub(crate) fn menu_roots_children<Message, Renderer>( pub(crate) fn menu_roots_children<Message>(menu_roots: &Vec<MenuTree<Message>>) -> Vec<Tree>
menu_roots: &Vec<MenuTree<'_, Message, Renderer>>,
) -> Vec<Tree>
where where
Renderer: renderer::Renderer, Message: Clone + 'static,
{ {
/* /*
menu bar menu bar
@ -85,7 +110,7 @@ where
let flat = root let flat = root
.flattern() .flattern()
.iter() .iter()
.map(|mt| Tree::new(mt.item.as_widget())) .map(|mt| Tree::new(mt.item.clone()))
.collect(); .collect();
tree.children = flat; tree.children = flat;
tree tree
@ -94,11 +119,9 @@ where
} }
#[allow(invalid_reference_casting)] #[allow(invalid_reference_casting)]
pub(crate) fn menu_roots_diff<Message, Renderer>( pub(crate) fn menu_roots_diff<Message>(menu_roots: &mut Vec<MenuTree<Message>>, tree: &mut Tree)
menu_roots: &mut Vec<MenuTree<'_, Message, Renderer>>, where
tree: &mut Tree, Message: Clone + 'static,
) where
Renderer: renderer::Renderer,
{ {
if tree.children.len() > menu_roots.len() { if tree.children.len() > menu_roots.len() {
tree.children.truncate(menu_roots.len()); tree.children.truncate(menu_roots.len());
@ -112,7 +135,7 @@ pub(crate) fn menu_roots_diff<Message, Renderer>(
.flattern() .flattern()
.iter() .iter()
.map(|mt| { .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 = widget as *const dyn Widget<Message, crate::Theme, Renderer>;
let widget_ptr_mut = let widget_ptr_mut =
widget_ptr as *mut dyn Widget<Message, crate::Theme, Renderer>; widget_ptr as *mut dyn Widget<Message, crate::Theme, Renderer>;
@ -130,7 +153,7 @@ pub(crate) fn menu_roots_diff<Message, Renderer>(
let flat = root let flat = root
.flattern() .flattern()
.iter() .iter()
.map(|mt| Tree::new(mt.item.as_widget())) .map(|mt| Tree::new(mt.item.clone()))
.collect(); .collect();
tree.children = flat; tree.children = flat;
tree tree
@ -139,12 +162,18 @@ pub(crate) fn menu_roots_diff<Message, Renderer>(
} }
} }
pub fn get_mut_or_default<T: Default>(vec: &mut Vec<T>, index: usize) -> &mut T {
if index < vec.len() {
&mut vec[index]
} else {
vec.resize_with(index + 1, T::default);
&mut vec[index]
}
}
/// A `MenuBar` collects `MenuTree`s and handles all the layout, event processing, and drawing. /// A `MenuBar` collects `MenuTree`s and handles all the layout, event processing, and drawing.
#[allow(missing_debug_implementations)] #[allow(missing_debug_implementations)]
pub struct MenuBar<'a, Message, Renderer = crate::Renderer> pub struct MenuBar<Message> {
where
Renderer: renderer::Renderer,
{
width: Length, width: Length,
height: Length, height: Length,
spacing: f32, spacing: f32,
@ -156,17 +185,22 @@ where
item_width: ItemWidth, item_width: ItemWidth,
item_height: ItemHeight, item_height: ItemHeight,
path_highlight: Option<PathHighlight>, path_highlight: Option<PathHighlight>,
menu_roots: Vec<MenuTree<'a, Message, Renderer>>, menu_roots: Vec<MenuTree<Message>>,
style: <crate::Theme as StyleSheet>::Style, style: <crate::Theme as StyleSheet>::Style,
window_id: window::Id,
#[cfg(all(feature = "wayland", feature = "winit"))]
positioner: iced_runtime::platform_specific::wayland::popup::SctkPositioner,
pub(crate) on_surface_action:
Option<Arc<dyn Fn(crate::surface::Action) -> Message + Send + Sync + 'static>>,
} }
impl<'a, Message, Renderer> MenuBar<'a, Message, Renderer> impl<Message> MenuBar<Message>
where where
Renderer: renderer::Renderer, Message: Clone + 'static,
{ {
/// Creates a new [`MenuBar`] with the given menu roots /// Creates a new [`MenuBar`] with the given menu roots
#[must_use] #[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; let mut menu_roots = menu_roots;
menu_roots.iter_mut().for_each(MenuTree::set_index); menu_roots.iter_mut().for_each(MenuTree::set_index);
@ -188,6 +222,10 @@ where
path_highlight: Some(PathHighlight::MenuActive), path_highlight: Some(PathHighlight::MenuActive),
menu_roots, menu_roots,
style: <crate::Theme as StyleSheet>::Style::default(), style: <crate::Theme as StyleSheet>::Style::default(),
window_id: window::Id::NONE,
#[cfg(all(feature = "wayland", feature = "winit"))]
positioner: iced_runtime::platform_specific::wayland::popup::SctkPositioner::default(),
on_surface_action: None,
} }
} }
@ -278,17 +316,196 @@ where
self.width = width; self.width = width;
self self
} }
#[cfg(all(feature = "wayland", feature = "winit"))]
pub fn with_positioner(
mut self,
positioner: iced_runtime::platform_specific::wayland::popup::SctkPositioner,
) -> Self {
self.positioner = positioner;
self
}
#[must_use]
pub fn window_id(mut self, id: window::Id) -> Self {
self.window_id = id;
self
}
#[must_use]
pub fn window_id_maybe(mut self, id: Option<window::Id>) -> Self {
if let Some(id) = id {
self.window_id = id;
}
self
}
#[must_use]
pub fn on_surface_action(
mut self,
handler: impl Fn(crate::surface::Action) -> Message + Send + Sync + 'static,
) -> Self {
self.on_surface_action = Some(Arc::new(handler));
self
}
#[cfg(all(feature = "wayland", feature = "winit", feature = "surface-message"))]
#[allow(clippy::too_many_lines)]
fn create_popup(
&mut self,
layout: Layout<'_>,
view_cursor: Cursor,
renderer: &Renderer,
shell: &mut Shell<'_, Message>,
viewport: &Rectangle,
my_state: &mut MenuBarState,
) {
if self.window_id != window::Id::NONE && self.on_surface_action.is_some() {
use crate::surface::action::destroy_popup;
use iced_runtime::platform_specific::wayland::popup::{
SctkPopupSettings, SctkPositioner,
};
let surface_action = self.on_surface_action.as_ref().unwrap();
let old_active_root = my_state
.inner
.with_data(|state| state.active_root.get(0).copied());
// if position is not on menu bar button skip.
let hovered_root = layout
.children()
.position(|lo| view_cursor.is_over(lo.bounds()));
if old_active_root
.zip(hovered_root)
.is_some_and(|r| r.0 == r.1)
{
return;
}
let (id, root_list) = my_state.inner.with_data_mut(|state| {
if let Some(id) = state.popup_id.get(&self.window_id).copied() {
// close existing popups
state.menu_states.clear();
state.active_root.clear();
shell.publish(surface_action(destroy_popup(id)));
state.view_cursor = view_cursor;
(id, layout.children().map(|lo| lo.bounds()).collect())
} else {
(
window::Id::unique(),
layout.children().map(|lo| lo.bounds()).collect(),
)
}
});
let mut popup_menu: Menu<'static, _> = Menu {
tree: my_state.clone(),
menu_roots: std::borrow::Cow::Owned(self.menu_roots.clone()),
bounds_expand: self.bounds_expand,
menu_overlays_parent: false,
close_condition: self.close_condition,
item_width: self.item_width,
item_height: self.item_height,
bar_bounds: layout.bounds(),
main_offset: self.main_offset,
cross_offset: self.cross_offset,
root_bounds_list: root_list,
path_highlight: self.path_highlight,
style: std::borrow::Cow::Owned(self.style.clone()),
position: Point::new(0., 0.),
is_overlay: false,
window_id: id,
depth: 0,
on_surface_action: self.on_surface_action.clone(),
};
init_root_menu(
&mut popup_menu,
renderer,
shell,
view_cursor.position().unwrap(),
viewport.size(),
Vector::new(0., 0.),
layout.bounds(),
self.main_offset as f32,
);
let (anchor_rect, gravity) = my_state.inner.with_data_mut(|state| {
state.popup_id.insert(self.window_id, id);
(state
.menu_states
.iter()
.find(|s| s.index.is_none())
.map(|s| s.menu_bounds.parent_bounds)
.map_or_else(
|| {
let bounds = layout.bounds();
Rectangle {
x: bounds.x as i32,
y: bounds.y as i32,
width: bounds.width as i32,
height: bounds.height as i32,
}
},
|r| Rectangle {
x: r.x as i32,
y: r.y as i32,
width: r.width as i32,
height: r.height as i32,
},
), match (state.horizontal_direction, state.vertical_direction) {
(Direction::Positive, Direction::Positive) => cctk::wayland_protocols::xdg::shell::client::xdg_positioner::Gravity::BottomRight,
(Direction::Positive, Direction::Negative) => cctk::wayland_protocols::xdg::shell::client::xdg_positioner::Gravity::TopRight,
(Direction::Negative, Direction::Positive) => cctk::wayland_protocols::xdg::shell::client::xdg_positioner::Gravity::BottomLeft,
(Direction::Negative, Direction::Negative) => cctk::wayland_protocols::xdg::shell::client::xdg_positioner::Gravity::TopLeft,
})
});
let menu_node = popup_menu.layout(renderer, Limits::NONE.min_width(1.).min_height(1.));
let popup_size = menu_node.size();
let positioner = SctkPositioner {
size: Some((
popup_size.width.ceil() as u32 + 2,
popup_size.height.ceil() as u32 + 2,
)),
anchor_rect,
anchor:
cctk::wayland_protocols::xdg::shell::client::xdg_positioner::Anchor::BottomLeft,
gravity,
reactive: true,
..Default::default()
};
let parent = self.window_id;
shell.publish((surface_action)(crate::surface::action::simple_popup(
move || SctkPopupSettings {
parent,
id,
positioner: positioner.clone(),
parent_size: None,
grab: true,
close_with_children: false,
input_zone: None,
},
Some(move || {
Element::from(crate::widget::container(popup_menu.clone()).center(Length::Fill))
.map(crate::action::app)
}),
)));
}
}
} }
impl<Message, Renderer> Widget<Message, crate::Theme, Renderer> for MenuBar<'_, Message, Renderer> impl<Message> Widget<Message, crate::Theme, Renderer> for MenuBar<Message>
where where
Renderer: renderer::Renderer, Message: Clone + 'static,
{ {
fn size(&self) -> iced_core::Size<Length> { fn size(&self) -> iced_core::Size<Length> {
iced_core::Size::new(self.width, self.height) iced_core::Size::new(self.width, self.height)
} }
fn diff(&mut self, tree: &mut Tree) { fn diff(&mut self, tree: &mut Tree) {
menu_roots_diff(&mut self.menu_roots, tree); let state = tree.state.downcast_mut::<MenuBarState>();
state
.inner
.with_data_mut(|inner| menu_roots_diff(&mut self.menu_roots, &mut inner.tree));
} }
fn tag(&self) -> tree::Tag { fn tag(&self) -> tree::Tag {
@ -318,7 +535,7 @@ where
.iter_mut() .iter_mut()
.map(|t| &mut t.children[0]) .map(|t| &mut t.children[0])
.collect::<Vec<_>>(); .collect::<Vec<_>>();
flex::resolve( flex::resolve_wrapper(
&flex::Axis::Horizontal, &flex::Axis::Horizontal,
renderer, renderer,
&limits, &limits,
@ -330,6 +547,7 @@ where
) )
} }
#[allow(clippy::too_many_lines)]
fn on_event( fn on_event(
&mut self, &mut self,
tree: &mut Tree, tree: &mut Tree,
@ -357,19 +575,70 @@ where
viewport, viewport,
); );
let state = tree.state.downcast_mut::<MenuBarState>(); let my_state = tree.state.downcast_mut::<MenuBarState>();
// XXX this should reset the state if there are no other copies of the state, which implies no dropdown menus open.
let reset = self.window_id != window::Id::NONE
&& my_state
.inner
.with_data(|d| !d.open && !d.active_root.is_empty());
let open = my_state.inner.with_data_mut(|state| {
if reset {
if let Some(popup_id) = state.popup_id.get(&self.window_id).copied() {
if let Some(handler) = self.on_surface_action.as_ref() {
shell.publish((handler)(crate::surface::Action::DestroyPopup(popup_id)));
state.reset();
}
}
}
state.open
});
match event { match event {
Mouse(ButtonReleased(Left)) | Touch(FingerLifted { .. } | FingerLost { .. }) => { Mouse(ButtonReleased(Left)) | Touch(FingerLifted { .. } | FingerLost { .. }) => {
if state.menu_states.is_empty() && view_cursor.is_over(layout.bounds()) { let create_popup = my_state.inner.with_data_mut(|state| {
state.view_cursor = view_cursor; let mut create_popup = false;
state.open = true; if state.menu_states.is_empty() && view_cursor.is_over(layout.bounds()) {
// #[cfg(feature = "wayland")] state.view_cursor = view_cursor;
// TODO emit Message to open menu state.open = true;
create_popup = true;
} else if let Some(_id) = state.popup_id.remove(&self.window_id) {
state.menu_states.clear();
state.active_root.clear();
state.open = false;
#[cfg(all(
feature = "wayland",
feature = "winit",
feature = "surface-message"
))]
{
let surface_action = self.on_surface_action.as_ref().unwrap();
shell.publish(surface_action(crate::surface::action::destroy_popup(
_id,
)));
}
state.view_cursor = view_cursor;
}
create_popup
});
if !create_popup {
return event::Status::Ignored;
} }
#[cfg(all(feature = "wayland", feature = "winit", feature = "surface-message"))]
self.create_popup(layout, view_cursor, renderer, shell, viewport, my_state);
}
Mouse(mouse::Event::CursorMoved { .. } | mouse::Event::CursorEntered)
if open && view_cursor.is_over(layout.bounds()) =>
{
#[cfg(all(feature = "wayland", feature = "winit", feature = "surface-message"))]
self.create_popup(layout, view_cursor, renderer, shell, viewport, my_state);
} }
_ => (), _ => (),
} }
root_status root_status
} }
@ -385,49 +654,51 @@ where
) { ) {
let state = tree.state.downcast_ref::<MenuBarState>(); let state = tree.state.downcast_ref::<MenuBarState>();
let cursor_pos = view_cursor.position().unwrap_or_default(); 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.inner.with_data_mut(|state| {
state.view_cursor let position = if state.open && (cursor_pos.x < 0.0 || cursor_pos.y < 0.0) {
} else { state.view_cursor
view_cursor } else {
}; view_cursor
};
// draw path highlight // draw path highlight
if self.path_highlight.is_some() { if self.path_highlight.is_some() {
let styling = theme.appearance(&self.style); let styling = theme.appearance(&self.style);
if let Some(active) = state.active_root { if let Some(active) = state.active_root.first() {
let active_bounds = layout let active_bounds = layout
.children() .children()
.nth(active) .nth(*active)
.expect("Active child not found in menu?") .expect("Active child not found in menu?")
.bounds(); .bounds();
let path_quad = renderer::Quad { let path_quad = renderer::Quad {
bounds: active_bounds, bounds: active_bounds,
border: Border { border: Border {
radius: styling.bar_border_radius.into(), radius: styling.bar_border_radius.into(),
..Default::default() ..Default::default()
}, },
shadow: Default::default(), shadow: Shadow::default(),
}; };
renderer.fill_quad(path_quad, styling.path); renderer.fill_quad(path_quad, styling.path);
}
} }
}
self.menu_roots self.menu_roots
.iter() .iter()
.zip(&tree.children) .zip(&tree.children)
.zip(layout.children()) .zip(layout.children())
.for_each(|((root, t), lo)| { .for_each(|((root, t), lo)| {
root.item.as_widget().draw( root.item.draw(
&t.children[root.index], &t.children[root.index],
renderer, renderer,
theme, theme,
style, style,
lo, lo,
position, position,
viewport, viewport,
); );
}); });
});
} }
fn overlay<'b>( fn overlay<'b>(
@ -437,18 +708,18 @@ where
_renderer: &Renderer, _renderer: &Renderer,
translation: Vector, translation: Vector,
) -> Option<overlay::Element<'b, Message, crate::Theme, Renderer>> { ) -> Option<overlay::Element<'b, Message, crate::Theme, Renderer>> {
// #[cfg(feature = "wayland")] #[cfg(all(feature = "wayland", feature = "winit", feature = "surface-message"))]
// return None; return None;
let state = tree.state.downcast_ref::<MenuBarState>(); let state = tree.state.downcast_ref::<MenuBarState>();
if !state.open { if state.inner.with_data(|state| !state.open) {
return None; return None;
} }
Some( Some(
Menu { Menu {
tree, tree: state.clone(),
menu_roots: &mut self.menu_roots, menu_roots: std::borrow::Cow::Owned(self.menu_roots.clone()),
bounds_expand: self.bounds_expand, bounds_expand: self.bounds_expand,
menu_overlays_parent: false, menu_overlays_parent: false,
close_condition: self.close_condition, close_condition: self.close_condition,
@ -459,27 +730,30 @@ where
cross_offset: self.cross_offset, cross_offset: self.cross_offset,
root_bounds_list: layout.children().map(|lo| lo.bounds()).collect(), root_bounds_list: layout.children().map(|lo| lo.bounds()).collect(),
path_highlight: self.path_highlight, path_highlight: self.path_highlight,
style: &self.style, style: std::borrow::Cow::Borrowed(&self.style),
position: Point::new(translation.x, translation.y), position: Point::new(translation.x, translation.y),
is_overlay: true,
window_id: window::Id::NONE,
depth: 0,
on_surface_action: self.on_surface_action.clone(),
} }
.overlay(), .overlay(),
) )
} }
} }
impl<'a, Message, Renderer> From<MenuBar<'a, Message, Renderer>>
for Element<'a, Message, crate::Theme, Renderer> impl<Message> From<MenuBar<Message>> for Element<'_, Message, crate::Theme, Renderer>
where where
Message: 'a, Message: Clone + 'static,
Renderer: 'a + renderer::Renderer,
{ {
fn from(value: MenuBar<'a, Message, Renderer>) -> Self { fn from(value: MenuBar<Message>) -> Self {
Self::new(value) Self::new(value)
} }
} }
#[allow(unused_results, clippy::too_many_arguments)] #[allow(unused_results, clippy::too_many_arguments)]
fn process_root_events<Message, Renderer>( fn process_root_events<Message>(
menu_roots: &mut [MenuTree<'_, Message, Renderer>], menu_roots: &mut [MenuTree<Message>],
view_cursor: Cursor, view_cursor: Cursor,
tree: &mut Tree, tree: &mut Tree,
event: &event::Event, event: &event::Event,
@ -490,7 +764,6 @@ fn process_root_events<Message, Renderer>(
viewport: &Rectangle, viewport: &Rectangle,
) -> event::Status ) -> event::Status
where where
Renderer: renderer::Renderer,
{ {
menu_roots menu_roots
.iter_mut() .iter_mut()
@ -498,7 +771,7 @@ where
.zip(layout.children()) .zip(layout.children())
.map(|((root, t), lo)| { .map(|((root, t), lo)| {
// assert!(t.tag == tree::Tag::stateless()); // assert!(t.tag == tree::Tag::stateless());
root.item.as_widget_mut().on_event( root.item.on_event(
&mut t.children[root.index], &mut t.children[root.index],
event.clone(), event.clone(),
lo, 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::iced_core::{Alignment, Length};
use crate::widget::menu::action::MenuAction; use crate::widget::menu::action::MenuAction;
use crate::widget::menu::key_bind::KeyBind; use crate::widget::menu::key_bind::KeyBind;
use crate::widget::{Button, icon}; use crate::widget::{Button, RcElementWrapper, icon};
use crate::{theme, widget}; use crate::{theme, widget};
/// Nested menu is essentially a tree of items, a menu is a collection of items /// 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 /// 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 /// has children, it's a menu, otherwise it's an item
#[allow(missing_debug_implementations)] #[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 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 /// the `index` field is the index of the item in that vector
pub(crate) index: usize, pub(crate) index: usize,
/// The item of the menu tree /// 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 /// 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 /// The width of the menu tree
pub(crate) width: Option<u16>, pub(crate) width: Option<u16>,
/// The height of the menu tree /// The height of the menu tree
pub(crate) height: Option<u16>, pub(crate) height: Option<u16>,
} }
impl<'a, Message, Renderer> MenuTree<'a, Message, Renderer> impl<Message: Clone + 'static> MenuTree<Message> {
where
Renderer: renderer::Renderer,
{
/// Create a new menu tree from a widget /// 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 { Self {
index: 0, index: 0,
item: item.into(), item: item.into(),
@ -55,8 +53,8 @@ where
/// Create a menu tree from a widget and a vector of sub trees /// Create a menu tree from a widget and a vector of sub trees
pub fn with_children( pub fn with_children(
item: impl Into<Element<'a, Message, crate::Theme, Renderer>>, item: impl Into<RcElementWrapper<Message>>,
children: Vec<impl Into<MenuTree<'a, Message, Renderer>>>, children: Vec<impl Into<MenuTree<Message>>>,
) -> Self { ) -> Self {
Self { Self {
index: 0, index: 0,
@ -92,7 +90,7 @@ where
/// Set the index of each item /// Set the index of each item
pub(crate) fn set_index(&mut self) { pub(crate) fn set_index(&mut self) {
/// inner counting function. /// 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 // keep items under the same menu line up
mt.children.iter_mut().for_each(|c| { mt.children.iter_mut().for_each(|c| {
c.index = *count; c.index = *count;
@ -109,18 +107,18 @@ where
} }
/// Flatten the menu tree /// Flatten the menu tree
pub(crate) fn flattern(&'a self) -> Vec<&Self> { pub(crate) fn flattern(&self) -> Vec<&Self> {
/// Inner flattening function /// Inner flattening function
fn rec<'a, Message, Renderer>( fn rec<'a, Message: Clone + 'static>(
mt: &'a MenuTree<'a, Message, Renderer>, mt: &'a MenuTree<Message>,
flat: &mut Vec<&MenuTree<'a, Message, Renderer>>, flat: &mut Vec<&'a MenuTree<Message>>,
) { ) {
mt.children.iter().for_each(|c| { mt.children.iter().for_each(|c| {
flat.push(c); flat.push(c);
}); });
mt.children.iter().for_each(|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>> impl<Message: Clone + 'static> From<crate::Element<'static, Message>> for MenuTree<Message> {
for MenuTree<'a, Message, Renderer> fn from(value: crate::Element<'static, Message>) -> Self {
where Self::new(RcElementWrapper::new(value))
Renderer: renderer::Renderer,
{
fn from(value: Element<'a, Message, crate::Theme, Renderer>) -> Self {
Self::new(value)
} }
} }
@ -160,6 +154,7 @@ where
.class(theme::Button::MenuItem) .class(theme::Button::MenuItem)
} }
#[derive(Clone)]
/// Represents a menu item that performs an action when selected or a separator between menu items. /// 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. /// - `Action` - Represents a menu item that performs an action when selected.
@ -215,20 +210,15 @@ where
/// ///
/// # Returns /// # Returns
/// - A vector of `MenuTree`. /// - A vector of `MenuTree`.
#[must_use]
pub fn menu_items< pub fn menu_items<
'a,
A: MenuAction<Message = Message>, A: MenuAction<Message = Message>,
L: Into<Cow<'static, str>> + 'static, L: Into<Cow<'static, str>> + 'static,
Message, Message: 'static + std::clone::Clone,
Renderer: renderer::Renderer + 'a,
>( >(
key_binds: &HashMap<KeyBind, A>, key_binds: &HashMap<KeyBind, A>,
children: Vec<MenuItem<A, L>>, children: Vec<MenuItem<A, L>>,
) -> Vec<MenuTree<'a, Message, Renderer>> ) -> Vec<MenuTree<Message>> {
where
Element<'a, Message, crate::Theme, Renderer>: From<widget::button::Button<'a, Message>>,
Message: 'a + Clone,
{
fn find_key<A: MenuAction>(action: &A, key_binds: &HashMap<KeyBind, A>) -> String { fn find_key<A: MenuAction>(action: &A, key_binds: &HashMap<KeyBind, A>) -> String {
for (key_bind, key_action) in key_binds { for (key_bind, key_action) in key_binds {
if action == key_action { if action == key_action {
@ -249,9 +239,10 @@ where
match item { match item {
MenuItem::Button(label, icon, action) => { MenuItem::Button(label, icon, action) => {
let l: Cow<'static, str> = label.into();
let key = find_key(&action, key_binds); let key = find_key(&action, key_binds);
let mut items = vec![ let mut items = vec![
widget::text(label).into(), widget::text(l.clone()).into(),
widget::horizontal_space().into(), widget::horizontal_space().into(),
widget::text(key).into(), widget::text(key).into(),
]; ];
@ -261,15 +252,18 @@ where
items.insert(1, widget::Space::with_width(spacing.space_xxs).into()); 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()); 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) => { MenuItem::ButtonDisabled(label, icon, action) => {
let l: Cow<'static, str> = label.into();
let key = find_key(&action, key_binds); let key = find_key(&action, key_binds);
let mut items = vec![ let mut items = vec![
widget::text(label).into(), widget::text(l.clone()).into(),
widget::horizontal_space().into(), widget::horizontal_space().into(),
widget::text(key).into(), widget::text(key).into(),
]; ];
@ -281,7 +275,7 @@ where
let menu_button = menu_button(items); 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) => { MenuItem::CheckBox(label, icon, value, action) => {
let key = find_key(&action, key_binds); let key = find_key(&action, key_binds);
@ -311,36 +305,42 @@ where
items.insert(2, widget::icon::icon(icon).size(14).into()); 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) => { MenuItem::Folder(label, children) => {
trees.push(MenuTree::<Message, Renderer>::with_children( let l: Cow<'static, str> = label.into();
menu_button(vec![
widget::text(label).into(), trees.push(MenuTree::<Message>::with_children(
widget::horizontal_space().into(), RcElementWrapper::new(crate::Element::from(
widget::icon::from_name("pan-end-symbolic") menu_button::<'static, _>(vec![
.size(16) widget::text(l.clone()).into(),
.icon() widget::horizontal_space().into(),
.into(), widget::icon::from_name("pan-end-symbolic")
]) .size(16)
.class( .icon()
// Menu folders have no on_press so they take on the disabled style by default .into(),
if children.is_empty() { ])
// This will make the folder use the disabled style if it has no children .class(
theme::Button::MenuItem // Menu folders have no on_press so they take on the disabled style by default
} else { if children.is_empty() {
// This will make the folder use the enabled style if it has children // This will make the folder use the disabled style if it has no children
theme::Button::MenuFolder 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), menu_items(key_binds, children),
)); ));
} }
MenuItem::Divider => { MenuItem::Divider => {
if i != size - 1 { if i != size - 1 {
trees.push(MenuTree::<Message, Renderer>::new( trees.push(MenuTree::<Message>::from(Element::from(
widget::divider::horizontal::light(), widget::divider::horizontal::light(),
)); )));
} }
} }
} }

View file

@ -69,7 +69,7 @@ impl<'a, Message: Clone + 'static> NavBar<'a, Message> {
} }
#[inline] #[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.segmented_button = self.segmented_button.context_menu(context_menu);
self self
} }

View file

@ -9,6 +9,7 @@ use crate::{
use super::menu::{self, ItemHeight, ItemWidth}; use super::menu::{self, ItemHeight, ItemWidth};
#[must_use]
pub fn responsive_menu_bar() -> ResponsiveMenuBar { pub fn responsive_menu_bar() -> ResponsiveMenuBar {
ResponsiveMenuBar::default() ResponsiveMenuBar::default()
} }
@ -33,18 +34,21 @@ impl Default for ResponsiveMenuBar {
impl ResponsiveMenuBar { impl ResponsiveMenuBar {
/// Set the item width /// Set the item width
#[must_use]
pub fn item_width(mut self, item_width: ItemWidth) -> Self { pub fn item_width(mut self, item_width: ItemWidth) -> Self {
self.item_width = item_width; self.item_width = item_width;
self self
} }
/// Set the item height /// Set the item height
#[must_use]
pub fn item_height(mut self, item_height: ItemHeight) -> Self { pub fn item_height(mut self, item_height: ItemHeight) -> Self {
self.item_height = item_height; self.item_height = item_height;
self self
} }
/// Set the spacing /// Set the spacing
#[must_use]
pub fn spacing(mut self, spacing: f32) -> Self { pub fn spacing(mut self, spacing: f32) -> Self {
self.spacing = spacing; self.spacing = spacing;
self self
@ -56,14 +60,14 @@ impl ResponsiveMenuBar {
pub fn into_element< pub fn into_element<
'a, 'a,
Message: Clone + 'static, Message: Clone + 'static,
A: menu::Action<Message = Message>, A: menu::Action<Message = Message> + Clone,
S: Into<std::borrow::Cow<'static, str>> + 'static, S: Into<std::borrow::Cow<'static, str>> + 'static,
>( >(
self, self,
core: &Core, core: &Core,
key_binds: &HashMap<menu::KeyBind, A>, key_binds: &HashMap<menu::KeyBind, A>,
id: crate::widget::Id, id: crate::widget::Id,
action_message: impl Fn(crate::surface::Action) -> Message + 'static, action_message: impl Fn(crate::surface::Action) -> Message + Send + Sync + Clone + 'static,
trees: Vec<(S, Vec<menu::Item<A, S>>)>, trees: Vec<(S, Vec<menu::Item<A, S>>)>,
) -> Element<'a, Message> { ) -> Element<'a, Message> {
use crate::widget::id_container; use crate::widget::id_container;
@ -80,17 +84,21 @@ impl ResponsiveMenuBar {
menu::bar( menu::bar(
trees trees
.into_iter() .into_iter()
.map(|mt| { .map(|mt: (S, Vec<menu::Item<A, S>>)| {
menu::Tree::<_>::with_children( menu::Tree::<_>::with_children(
menu::root(mt.0), crate::widget::RcElementWrapper::new(Element::from(
menu::items(key_binds, mt.1.into()), menu::root(mt.0),
)),
menu::items(key_binds, mt.1),
) )
}) })
.collect(), .collect(),
) )
.item_width(self.item_width) .item_width(self.item_width)
.item_height(self.item_height) .item_height(self.item_height)
.spacing(self.spacing), .spacing(self.spacing)
.on_surface_action(action_message.clone())
.window_id_maybe(core.main_window_id()),
crate::widget::Id::new(format!("menu_bar_expanded_{id}")), crate::widget::Id::new(format!("menu_bar_expanded_{id}")),
), ),
id, id,
@ -123,7 +131,9 @@ impl ResponsiveMenuBar {
)]) )])
.item_height(self.item_height) .item_height(self.item_height)
.item_width(self.collapsed_item_width) .item_width(self.collapsed_item_width)
.spacing(self.spacing), .spacing(self.spacing)
.on_surface_action(action_message.clone())
.window_id_maybe(core.main_window_id()),
crate::widget::Id::new(format!("menu_bar_collapsed_{id}")), crate::widget::Id::new(format!("menu_bar_collapsed_{id}")),
), ),
id, id,

View file

@ -17,7 +17,7 @@ use iced::clipboard::mime::AllowedMimeTypes;
use iced::touch::Finger; use iced::touch::Finger;
use iced::{ use iced::{
Alignment, Background, Color, Event, Length, Padding, Rectangle, Size, Task, Vector, alignment, Alignment, Background, Color, Event, Length, Padding, Rectangle, Size, Task, Vector, alignment,
event, keyboard, mouse, touch, event, keyboard, mouse, touch, window,
}; };
use iced_core::mouse::ScrollDelta; use iced_core::mouse::ScrollDelta;
use iced_core::text::{LineHeight, Renderer as TextRenderer, Shaping, Wrapping}; use iced_core::text::{LineHeight, Renderer as TextRenderer, Shaping, Wrapping};
@ -127,7 +127,7 @@ where
pub(super) style: Style, pub(super) style: Style,
/// The context menu to display when a context is activated /// The context menu to display when a context is activated
#[setters(skip)] #[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. /// Emits the ID of the item that was activated.
#[setters(skip)] #[setters(skip)]
pub(super) on_activate: Option<Box<dyn Fn(Entity) -> Message + 'static>>, 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 where
Message: 'static, Message: Clone + 'static,
{ {
self.context_menu = context_menu.map(|menus| { self.context_menu = context_menu.map(|menus| {
vec![menu::Tree::with_children( vec![menu::Tree::with_children(
crate::widget::row::<'static, Message>(), crate::Element::from(crate::widget::row::<'static, Message>()),
menus, menus,
)] )]
}); });
@ -577,6 +577,7 @@ where
fn state(&self) -> tree::State { fn state(&self) -> tree::State {
#[allow(clippy::default_trait_access)] #[allow(clippy::default_trait_access)]
tree::State::new(LocalState { tree::State::new(LocalState {
menu_state: Default::default(),
paragraphs: SecondaryMap::new(), paragraphs: SecondaryMap::new(),
text_hashes: SecondaryMap::new(), text_hashes: SecondaryMap::new(),
buttons_visible: Default::default(), buttons_visible: Default::default(),
@ -955,8 +956,10 @@ where
let menu_state = let menu_state =
tree.children[0].state.downcast_mut::<MenuBarState>(); tree.children[0].state.downcast_mut::<MenuBarState>();
menu_state.open = true; menu_state.inner.with_data_mut(|data| {
menu_state.view_cursor = cursor_position; data.open = true;
data.view_cursor = cursor_position;
});
shell.publish(on_context(key)); shell.publish(on_context(key));
return event::Status::Captured; return event::Status::Captured;
@ -1346,7 +1349,11 @@ where
let center_y = bounds.center_y(); let center_y = bounds.center_y();
let menu_open = !tree.children.is_empty() 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_active = self.model.is_active(key);
let key_is_hovered = self.button_is_hovered(state, key); let key_is_hovered = self.button_is_hovered(state, key);
@ -1556,6 +1563,7 @@ where
translation: Vector, translation: Vector,
) -> Option<iced_core::overlay::Element<'b, Message, crate::Theme, Renderer>> { ) -> Option<iced_core::overlay::Element<'b, Message, crate::Theme, Renderer>> {
let state = tree.state.downcast_ref::<LocalState>(); let state = tree.state.downcast_ref::<LocalState>();
let menu_state = state.menu_state.clone();
let Some(entity) = state.show_context else { let Some(entity) = state.show_context else {
return None; return None;
@ -1575,7 +1583,12 @@ where
return None; 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; return None;
} }
@ -1584,8 +1597,8 @@ where
Some( Some(
crate::widget::menu::Menu { crate::widget::menu::Menu {
tree: &mut tree.children[0], tree: menu_state,
menu_roots: context_menu, menu_roots: std::borrow::Cow::Borrowed(context_menu),
bounds_expand: 16, bounds_expand: 16,
menu_overlays_parent: true, menu_overlays_parent: true,
close_condition: CloseCondition { close_condition: CloseCondition {
@ -1600,8 +1613,12 @@ where
cross_offset: 0, cross_offset: 0,
root_bounds_list: vec![bounds], root_bounds_list: vec![bounds],
path_highlight: Some(PathHighlight::MenuActive), 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), position: Point::new(translation.x, translation.y),
is_overlay: true,
window_id: window::Id::NONE,
depth: 0,
on_surface_action: None,
} }
.overlay(), .overlay(),
) )
@ -1653,6 +1670,8 @@ where
/// State that is maintained by each individual widget. /// State that is maintained by each individual widget.
pub struct LocalState { pub struct LocalState {
/// Menu state
pub(crate) menu_state: MenuBarState,
/// Defines how many buttons to show at a time. /// Defines how many buttons to show at a time.
pub(super) buttons_visible: usize, pub(super) buttons_visible: usize,
/// Button visibility offset, when collapsed. /// Button visibility offset, when collapsed.

View file

@ -20,9 +20,9 @@ pub type MultiSelectTableView<'a, Item, Category, Message> =
TableView<'a, MultiSelect, Item, Category, Message>; TableView<'a, MultiSelect, Item, Category, Message>;
pub type MultiSelectModel<Item, Category> = Model<MultiSelect, Item, Category>; pub type MultiSelectModel<Item, Category> = Model<MultiSelect, Item, Category>;
pub fn table<'a, SelectionMode, Item, Category, Message>( pub fn table<SelectionMode, Item, Category, Message>(
model: &'a Model<SelectionMode, Item, Category>, model: &Model<SelectionMode, Item, Category>,
) -> TableView<'a, SelectionMode, Item, Category, Message> ) -> TableView<'_, SelectionMode, Item, Category, Message>
where where
Message: Clone, Message: Clone,
SelectionMode: Default, SelectionMode: Default,
@ -33,9 +33,9 @@ where
TableView::new(model) TableView::new(model)
} }
pub fn compact_table<'a, SelectionMode, Item, Category, Message>( pub fn compact_table<SelectionMode, Item, Category, Message>(
model: &'a Model<SelectionMode, Item, Category>, model: &Model<SelectionMode, Item, Category>,
) -> CompactTableView<'a, SelectionMode, Item, Category, Message> ) -> CompactTableView<'_, SelectionMode, Item, Category, Message>
where where
Message: Clone, Message: Clone,
SelectionMode: Default, SelectionMode: Default,

View file

@ -44,7 +44,7 @@ where
#[setters(skip)] #[setters(skip)]
pub(super) on_item_mb_right: Option<Box<dyn Fn(Entity) -> Message + 'static>>, pub(super) on_item_mb_right: Option<Box<dyn Fn(Entity) -> Message + 'static>>,
#[setters(skip)] #[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> impl<'a, SelectionMode, Item, Category, Message>
@ -97,7 +97,7 @@ where
] ]
}) })
.flatten() .flatten()
.collect::<Vec<Element<'a, Message>>>(); .collect::<Vec<Element<'static, Message>>>();
elements.pop(); elements.pop();
elements elements
.apply(widget::row::with_children) .apply(widget::row::with_children)
@ -247,7 +247,7 @@ where
pub fn item_context<F>(mut self, context_menu_builder: F) -> Self pub fn item_context<F>(mut self, context_menu_builder: F) -> Self
where where
F: Fn(&Item) -> Option<Vec<menu::Tree<'a, Message>>> + 'static, F: Fn(&Item) -> Option<Vec<menu::Tree<Message>>> + 'static,
Message: 'static, Message: 'static,
{ {
self.item_context_builder = Box::new(context_menu_builder); self.item_context_builder = Box::new(context_menu_builder);

View file

@ -51,7 +51,7 @@ where
#[setters(skip)] #[setters(skip)]
pub(super) on_item_mb_right: Option<Box<dyn Fn(Entity) -> Message + 'static>>, pub(super) on_item_mb_right: Option<Box<dyn Fn(Entity) -> Message + 'static>>,
#[setters(skip)] #[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 // Item DND
// === Category Interaction === // === Category Interaction ===
@ -64,8 +64,7 @@ where
#[setters(skip)] #[setters(skip)]
pub(super) on_category_mb_right: Option<Box<dyn Fn(Category) -> Message + 'static>>, pub(super) on_category_mb_right: Option<Box<dyn Fn(Category) -> Message + 'static>>,
#[setters(skip)] #[setters(skip)]
pub(super) category_context_builder: pub(super) category_context_builder: Box<dyn Fn(Category) -> Option<Vec<menu::Tree<Message>>>>,
Box<dyn Fn(Category) -> Option<Vec<menu::Tree<'a, Message>>>>,
} }
impl<'a, SelectionMode, Item, Category, Message> impl<'a, SelectionMode, Item, Category, Message>
@ -83,7 +82,7 @@ where
.model .model
.categories .categories
.iter() .iter()
.cloned() .copied()
.map(|category| { .map(|category| {
let cat_context_tree = (val.category_context_builder)(category); let cat_context_tree = (val.category_context_builder)(category);
@ -167,7 +166,7 @@ where
.align_y(Alignment::Center) .align_y(Alignment::Center)
.apply(Element::from) .apply(Element::from)
}) })
.collect::<Vec<Element<'a, Message>>>() .collect::<Vec<Element<'static, Message>>>()
.apply(widget::row::with_children) .apply(widget::row::with_children)
.apply(container) .apply(container)
.padding(val.item_padding) .padding(val.item_padding)
@ -328,7 +327,7 @@ where
pub fn item_context<F>(mut self, context_menu_builder: F) -> Self pub fn item_context<F>(mut self, context_menu_builder: F) -> Self
where where
F: Fn(&Item) -> Option<Vec<menu::Tree<'a, Message>>> + 'static, F: Fn(&Item) -> Option<Vec<menu::Tree<Message>>> + 'static,
Message: 'static, Message: 'static,
{ {
self.item_context_builder = Box::new(context_menu_builder); 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 pub fn category_context<F>(mut self, context_menu_builder: F) -> Self
where where
F: Fn(Category) -> Option<Vec<menu::Tree<'a, Message>>> + 'static, F: Fn(Category) -> Option<Vec<menu::Tree<Message>>> + 'static,
Message: 'static, Message: 'static,
{ {
self.category_context_builder = Box::new(context_menu_builder); self.category_context_builder = Box::new(context_menu_builder);

View file

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