Change context menu based on selection

This commit is contained in:
Jeremy Soller 2024-01-10 10:15:16 -07:00
parent 07c91042db
commit 716991a302
No known key found for this signature in database
GPG key ID: DCFCA852D3906975
2 changed files with 34 additions and 35 deletions

View file

@ -655,8 +655,7 @@ impl Application for App {
Message::TabContextMenu(entity, point_opt) Message::TabContextMenu(entity, point_opt)
}); });
} }
let mut popover = let mut popover = widget::popover(mouse_area, menu::context_menu(entity, &tab));
widget::popover(mouse_area, menu::context_menu(entity, &tab.location));
match tab.context_menu { match tab.context_menu {
Some(point) => { Some(point) => {
let rounded = Point::new(point.x.round(), point.y.round()); let rounded = Point::new(point.x.round(), point.y.round());

View file

@ -1,17 +1,14 @@
// SPDX-License-Identifier: GPL-3.0-only // SPDX-License-Identifier: GPL-3.0-only
use cosmic::{ use cosmic::{
//TODO: export in cosmic::widget //TODO: export iced::widget::horizontal_rule in cosmic::widget
iced::{ iced::{widget::horizontal_rule, Alignment, Background, Length},
widget::{column, horizontal_rule},
Alignment, Background, Length,
},
theme, theme,
widget::{self, segmented_button}, widget::{self, segmented_button},
Element, Element,
}; };
use crate::{fl, Action, Location, Message}; use crate::{fl, Action, Location, Message, Tab};
macro_rules! menu_button { macro_rules! menu_button {
($($x:expr),+ $(,)?) => ( ($($x:expr),+ $(,)?) => (
@ -28,42 +25,45 @@ macro_rules! menu_button {
); );
} }
pub fn context_menu<'a>( pub fn context_menu<'a>(entity: segmented_button::Entity, tab: &Tab) -> Element<'a, Message> {
entity: segmented_button::Entity,
location: &Location,
) -> Element<'a, Message> {
let menu_action = |label, action| { let menu_action = |label, action| {
menu_button!(widget::text(label)).on_press(Message::TabContextAction(entity, action)) menu_button!(widget::text(label)).on_press(Message::TabContextAction(entity, action))
}; };
//TODO: change items based on selection let selected = tab
let column = match location { .items_opt
.as_ref()
.map_or(0, |items| items.iter().filter(|x| x.selected).count());
let mut children: Vec<Element<_>> = Vec::new();
match tab.location {
Location::Path(_) => { Location::Path(_) => {
column!( children.push(menu_action(fl!("new-file"), Action::NewFile).into());
menu_action(fl!("new-file"), Action::NewFile), children.push(menu_action(fl!("new-folder"), Action::NewFolder).into());
menu_action(fl!("new-folder"), Action::NewFolder), children.push(horizontal_rule(1).into());
horizontal_rule(1), if selected > 0 {
menu_action(fl!("copy"), Action::Copy), children.push(menu_action(fl!("copy"), Action::Copy).into());
menu_action(fl!("paste"), Action::Paste), children.push(menu_action(fl!("paste"), Action::Paste).into());
menu_action(fl!("select-all"), Action::SelectAll), }
horizontal_rule(1), children.push(menu_action(fl!("select-all"), Action::SelectAll).into());
menu_action(fl!("move-to-trash"), Action::MoveToTrash), if selected > 0 {
horizontal_rule(1), children.push(horizontal_rule(1).into());
menu_action(fl!("properties"), Action::Properties), children.push(menu_action(fl!("move-to-trash"), Action::MoveToTrash).into());
) }
} }
Location::Trash => { Location::Trash => {
column!( children.push(menu_action(fl!("select-all"), Action::SelectAll).into());
menu_action(fl!("select-all"), Action::SelectAll), if selected > 0 {
horizontal_rule(1), children.push(horizontal_rule(1).into());
menu_action(fl!("restore-from-trash"), Action::RestoreFromTrash), children
horizontal_rule(1), .push(menu_action(fl!("restore-from-trash"), Action::RestoreFromTrash).into());
menu_action(fl!("properties"), Action::Properties), }
)
} }
}; }
children.push(horizontal_rule(1).into());
children.push(menu_action(fl!("properties"), Action::Properties).into());
widget::container(column) widget::container(widget::column::with_children(children))
.padding(1) .padding(1)
//TODO: move style to libcosmic //TODO: move style to libcosmic
.style(theme::Container::custom(|theme| { .style(theme::Container::custom(|theme| {