Added category context menus

This commit is contained in:
Adam Cosner 2025-01-06 12:15:44 -08:00
parent 4111fcbd05
commit 68fdd8fbe4
3 changed files with 23 additions and 12 deletions

View file

@ -11,7 +11,7 @@ use cosmic::widget::table;
use cosmic::widget::{self, nav_bar}; use cosmic::widget::{self, nav_bar};
use cosmic::{executor, iced}; use cosmic::{executor, iced};
#[derive(Debug, Default, PartialEq, Eq, Clone, Copy)] #[derive(Debug, Default, PartialEq, Eq, Clone, Copy, Hash)]
pub enum Category { pub enum Category {
#[default] #[default]
Name, Name,

View file

@ -4,7 +4,7 @@ use crate::widget::Icon;
/// Implementation of std::fmt::Display allows user to customize the header /// Implementation of std::fmt::Display allows user to customize the header
/// Ideally, this is implemented on an enum. /// Ideally, this is implemented on an enum.
pub trait ItemCategory: Default + std::fmt::Display + Clone + Copy + PartialEq + Eq { pub trait ItemCategory: Default + std::fmt::Display + Clone + Copy + PartialEq + Eq + std::hash::Hash {
/// Function that gets the width of the data /// Function that gets the width of the data
fn width(&self) -> iced::Length; fn width(&self) -> iced::Length;
} }

View file

@ -40,7 +40,8 @@ where
#[setters(skip)] #[setters(skip)]
pub(super) item_context_tree: Option<Vec<menu::Tree<'a, Message>>>, pub(super) item_context_tree: Option<Vec<menu::Tree<'a, Message>>>,
#[setters(skip)] #[setters(skip)]
pub(super) category_context_tree: Option<Vec<menu::Tree<'a, Message>>>, pub(super) category_context_trees:
std::collections::HashMap<Category, Option<Vec<menu::Tree<'a, Message>>>>,
#[setters(skip)] #[setters(skip)]
pub(super) on_item_select: Option<Box<dyn Fn(Entity) -> Message + 'a>>, pub(super) on_item_select: Option<Box<dyn Fn(Entity) -> Message + 'a>>,
@ -64,11 +65,15 @@ where
fn from(val: TableView<'a, SelectionMode, Item, Category, Message>) -> Self { fn from(val: TableView<'a, SelectionMode, Item, Category, Message>) -> Self {
let cosmic_theme::Spacing { space_xxxs, .. } = theme::active().cosmic().spacing; let cosmic_theme::Spacing { space_xxxs, .. } = theme::active().cosmic().spacing;
let mut category_contexts = val.category_context_trees.into_values();
let header_row = val let header_row = val
.model .model
.categories .categories
.iter() .iter()
.map(|category| { .map(|category| {
let cat_context_tree = category_contexts.next().unwrap();
widget::row() widget::row()
.spacing(space_xxxs) .spacing(space_xxxs)
.push(widget::text::heading(category.to_string())) .push(widget::text::heading(category.to_string()))
@ -110,6 +115,7 @@ where
mouse_area mouse_area
} }
}) })
.apply(|mouse_area| widget::context_menu(mouse_area, cat_context_tree))
.apply(Element::from) .apply(Element::from)
}) })
.collect::<Vec<Element<'a, Message>>>() .collect::<Vec<Element<'a, Message>>>()
@ -217,7 +223,7 @@ where
.. ..
} = theme::active().cosmic().spacing; } = theme::active().cosmic().spacing;
Self { let mut result = Self {
model, model,
element_padding: Padding::from(0), element_padding: Padding::from(0),
@ -232,8 +238,14 @@ where
on_category_select: None, on_category_select: None,
on_category_context: None, on_category_context: None,
category_context_tree: None, category_context_trees: std::collections::HashMap::new(),
};
for category in model.categories.iter().cloned() {
result.category_context_trees.insert(category, None);
} }
result
} }
pub fn on_item_select<F>(mut self, on_select: F) -> Self pub fn on_item_select<F>(mut self, on_select: F) -> Self
@ -266,14 +278,13 @@ where
self self
} }
pub fn category_context(mut self, context_menu: Option<Vec<menu::Tree<'a, Message>>>) -> Self pub fn category_context(mut self, category: Category, context_menu: Option<Vec<menu::Tree<'a, Message>>>) -> Self
where where
Message: 'static, Message: 'static,
{ {
self.category_context_tree = *self.category_context_trees.get_mut(&category).unwrap() =
context_menu.map(|menus| vec![menu::Tree::with_children(widget::row(), menus)]); context_menu.map(|menus| vec![menu::Tree::with_children(widget::row(), menus)]);
if let Some(ref mut context_menu) = self.category_context_trees.get_mut(&category).unwrap() {
if let Some(ref mut context_menu) = self.category_context_tree {
context_menu.iter_mut().for_each(menu::Tree::set_index); context_menu.iter_mut().for_each(menu::Tree::set_index);
} }