From 07c91042db1e95371601408b6a03eb0370eaf870 Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Wed, 10 Jan 2024 09:47:47 -0700 Subject: [PATCH] Improve selection logic --- i18n/en/cosmic_files.ftl | 1 + src/main.rs | 21 ++++++++--- src/menu.rs | 76 +++++++++++++++++++++++--------------- src/tab.rs | 79 +++++++++++++++++++++++++--------------- 4 files changed, 113 insertions(+), 64 deletions(-) diff --git a/i18n/en/cosmic_files.ftl b/i18n/en/cosmic_files.ftl index ede272f..aa40180 100644 --- a/i18n/en/cosmic_files.ftl +++ b/i18n/en/cosmic_files.ftl @@ -24,3 +24,4 @@ copy = Copy paste = Paste select-all = Select all move-to-trash = Move to trash +restore-from-trash = Restore from trash diff --git a/src/main.rs b/src/main.rs index 459894b..de6d4ee 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,7 +10,7 @@ use cosmic::{ widget::{self, segmented_button}, Application, ApplicationExt, Element, }; -use std::{any::TypeId, env, fs, path::PathBuf, process, time::Instant}; +use std::{any::TypeId, env, fs, path::PathBuf, process}; use config::{AppTheme, Config, CONFIG_VERSION}; mod config; @@ -105,6 +105,7 @@ pub enum Action { NewFolder, Paste, Properties, + RestoreFromTrash, SelectAll, Settings, TabNew, @@ -119,6 +120,7 @@ impl Action { Action::NewFolder => Message::NewFolder(Some(entity)), Action::Paste => Message::Paste(Some(entity)), Action::Properties => Message::ToggleContextPage(ContextPage::Properties), + Action::RestoreFromTrash => Message::RestoreFromTrash(Some(entity)), Action::SelectAll => Message::SelectAll(Some(entity)), Action::Settings => Message::ToggleContextPage(ContextPage::Settings), Action::TabNew => Message::TabNew, @@ -137,6 +139,7 @@ pub enum Message { NewFile(Option), NewFolder(Option), Paste(Option), + RestoreFromTrash(Option), SelectAll(Option), SystemThemeModeChange(cosmic_theme::ThemeMode), TabActivate(segmented_button::Entity), @@ -243,7 +246,7 @@ impl App { if let Some(tab) = self.tab_model.data::(entity) { if let Some(ref items) = tab.items_opt { for item in items.iter() { - if item.select_time.is_some() { + if item.selected { children.push(item.property_view(&self.core)); } } @@ -434,17 +437,20 @@ impl Application for App { Message::Paste(entity_opt) => { log::warn!("TODO: PASTE"); } + Message::RestoreFromTrash(entity_opt) => { + log::warn!("TODO: RESTORE FROM TRASH"); + } Message::SelectAll(entity_opt) => { let entity = entity_opt.unwrap_or_else(|| self.tab_model.active()); if let Some(tab) = self.tab_model.data_mut::(entity) { if let Some(ref mut items) = tab.items_opt { - let select_time = Instant::now(); for item in items.iter_mut() { if item.hidden { //TODO: option to show hidden files continue; } - item.select_time = Some(select_time); + item.selected = true; + item.click_time = None; } } } @@ -638,7 +644,9 @@ impl Application for App { tab.view(self.core()) .map(move |message| Message::TabMessage(entity, message)), ) - .on_press(move |_point_opt| Message::TabMessage(entity, tab::Message::Click(None))); + .on_press(move |_point_opt| { + Message::TabMessage(entity, tab::Message::Click(None, false)) + }); if tab.context_menu.is_some() { mouse_area = mouse_area .on_right_press(move |_point_opt| Message::TabContextMenu(entity, None)); @@ -647,7 +655,8 @@ impl Application for App { Message::TabContextMenu(entity, point_opt) }); } - let mut popover = widget::popover(mouse_area, menu::context_menu(entity)); + let mut popover = + widget::popover(mouse_area, menu::context_menu(entity, &tab.location)); match tab.context_menu { Some(point) => { let rounded = Point::new(point.x.round(), point.y.round()); diff --git a/src/menu.rs b/src/menu.rs index fa356ef..dc93358 100644 --- a/src/menu.rs +++ b/src/menu.rs @@ -11,7 +11,7 @@ use cosmic::{ Element, }; -use crate::{fl, Action, Message}; +use crate::{fl, Action, Location, Message}; macro_rules! menu_button { ($($x:expr),+ $(,)?) => ( @@ -28,38 +28,56 @@ macro_rules! menu_button { ); } -pub fn context_menu<'a>(entity: segmented_button::Entity) -> Element<'a, Message> { +pub fn context_menu<'a>( + entity: segmented_button::Entity, + location: &Location, +) -> Element<'a, Message> { let menu_action = |label, action| { menu_button!(widget::text(label)).on_press(Message::TabContextAction(entity, action)) }; //TODO: change items based on selection - widget::container(column!( - menu_action(fl!("new-file"), Action::NewFile), - menu_action(fl!("new-folder"), Action::NewFolder), - horizontal_rule(1), - menu_action(fl!("copy"), Action::Copy), - menu_action(fl!("paste"), Action::Paste), - menu_action(fl!("select-all"), Action::SelectAll), - horizontal_rule(1), - menu_action(fl!("move-to-trash"), Action::MoveToTrash), - horizontal_rule(1), - menu_action(fl!("properties"), Action::Properties), - )) - .padding(1) - //TODO: move style to libcosmic - .style(theme::Container::custom(|theme| { - let cosmic = theme.cosmic(); - let component = &cosmic.background.component; - widget::container::Appearance { - icon_color: Some(component.on.into()), - text_color: Some(component.on.into()), - background: Some(Background::Color(component.base.into())), - border_radius: 8.0.into(), - border_width: 1.0, - border_color: component.divider.into(), + let column = match location { + Location::Path(_) => { + column!( + menu_action(fl!("new-file"), Action::NewFile), + menu_action(fl!("new-folder"), Action::NewFolder), + horizontal_rule(1), + menu_action(fl!("copy"), Action::Copy), + menu_action(fl!("paste"), Action::Paste), + menu_action(fl!("select-all"), Action::SelectAll), + horizontal_rule(1), + menu_action(fl!("move-to-trash"), Action::MoveToTrash), + horizontal_rule(1), + menu_action(fl!("properties"), Action::Properties), + ) } - })) - .width(Length::Fixed(240.0)) - .into() + Location::Trash => { + column!( + menu_action(fl!("select-all"), Action::SelectAll), + horizontal_rule(1), + menu_action(fl!("restore-from-trash"), Action::RestoreFromTrash), + horizontal_rule(1), + menu_action(fl!("properties"), Action::Properties), + ) + } + }; + + widget::container(column) + .padding(1) + //TODO: move style to libcosmic + .style(theme::Container::custom(|theme| { + let cosmic = theme.cosmic(); + let component = &cosmic.background.component; + widget::container::Appearance { + icon_color: Some(component.on.into()), + text_color: Some(component.on.into()), + background: Some(Background::Color(component.base.into())), + border_radius: 8.0.into(), + border_width: 1.0, + border_color: component.divider.into(), + } + })) + .width(Length::Fixed(240.0)) + .into() } diff --git a/src/tab.rs b/src/tab.rs index 5dda4b8..9c89fdd 100644 --- a/src/tab.rs +++ b/src/tab.rs @@ -238,7 +238,8 @@ pub fn scan_path(tab_path: &PathBuf) -> Vec { path, icon_handle_grid, icon_handle_list, - select_time: None, + selected: false, + click_time: None, }); } } @@ -314,7 +315,8 @@ pub fn scan_trash() -> Vec { path, icon_handle_grid, icon_handle_list, - select_time: None, + selected: false, + click_time: None, }); } } @@ -347,7 +349,7 @@ impl Location { #[derive(Clone, Debug)] pub enum Message { - Click(Option), + Click(Option, bool), Location(Location), Parent, View(View), @@ -379,7 +381,8 @@ pub struct Item { pub path: PathBuf, pub icon_handle_grid: widget::icon::Handle, pub icon_handle_list: widget::icon::Handle, - pub select_time: Option, + pub selected: bool, + pub click_time: Option, } impl Item { @@ -458,7 +461,8 @@ impl fmt::Debug for Item { .field("hidden", &self.hidden) .field("path", &self.path) // icon_handles - .field("select_time", &self.select_time) + .field("selected", &self.selected) + .field("click_time", &self.click_time) .finish() } } @@ -503,33 +507,46 @@ impl Tab { pub fn update(&mut self, message: Message) -> bool { let mut cd = None; match message { - Message::Click(click_i_opt) => { + Message::Click(click_i_opt, handle_double_click) => { if let Some(ref mut items) = self.items_opt { for (i, item) in items.iter_mut().enumerate() { if Some(i) == click_i_opt { - if let Some(select_time) = item.select_time { - if select_time.elapsed() < DOUBLE_CLICK_DURATION { - if item.path.is_dir() { - cd = Some(Location::Path(item.path.clone())); - } else { - let mut command = open_command(&item.path); - match command.spawn() { - Ok(_) => (), - Err(err) => { - log::warn!( - "failed to open {:?}: {}", - item.path, - err - ); + item.selected = true; + if handle_double_click { + if let Some(click_time) = item.click_time { + if click_time.elapsed() < DOUBLE_CLICK_DURATION { + match self.location { + Location::Path(_) => { + if item.path.is_dir() { + cd = Some(Location::Path(item.path.clone())); + } else { + let mut command = open_command(&item.path); + match command.spawn() { + Ok(_) => (), + Err(err) => { + log::warn!( + "failed to open {:?}: {}", + item.path, + err + ); + } + } + } + } + Location::Trash => { + //TODO: open properties? } } } } + //TODO: prevent triple-click and beyond from opening file + item.click_time = Some(Instant::now()); + } else { + item.click_time = None; } - //TODO: prevent triple-click and beyond from opening file - item.select_time = Some(Instant::now()); } else { - item.select_time = None; + item.selected = false; + item.click_time = None; } } } @@ -615,14 +632,16 @@ impl Tab { .height(Length::Fixed(128.0)) .width(Length::Fixed(128.0)), ) - .style(button_style(item.select_time.is_some())) - .on_press(Message::Click(Some(i))); + .style(button_style(item.selected)) + .on_press(Message::Click(Some(i), true)); if self.context_menu.is_some() { children.push(button.into()); } else { children.push( crate::mouse_area::MouseArea::new(button) - .on_right_press_no_capture(move |_point_opt| Message::Click(Some(i))) + .on_right_press_no_capture(move |_point_opt| { + Message::Click(Some(i), false) + }) .into(), ); } @@ -701,14 +720,16 @@ impl Tab { .align_items(Alignment::Center) .spacing(space_xxs), ) - .style(button_style(item.select_time.is_some())) - .on_press(Message::Click(Some(i))); + .style(button_style(item.selected)) + .on_press(Message::Click(Some(i), true)); if self.context_menu.is_some() { children.push(button.into()); } else { children.push( crate::mouse_area::MouseArea::new(button) - .on_right_press_no_capture(move |_point_opt| Message::Click(Some(i))) + .on_right_press_no_capture(move |_point_opt| { + Message::Click(Some(i), false) + }) .into(), ); }