From 9d5b2912a7c9d7c45536a3715db399f967918d71 Mon Sep 17 00:00:00 2001 From: l-const Date: Thu, 29 Aug 2024 02:00:30 +0300 Subject: [PATCH] feat: Refresh/reload tab view & search fix(#146). --- src/app.rs | 29 +++++++++++++++++++++++++++++ src/key_bind.rs | 2 ++ 2 files changed, 31 insertions(+) diff --git a/src/app.rs b/src/app.rs index 9041498f..35f88047 100644 --- a/src/app.rs +++ b/src/app.rs @@ -57,6 +57,8 @@ use crate::{ tab::{self, HeadingOptions, ItemMetadata, Location, Tab, HOVER_DURATION}, }; +static REFRESH_VIEW_DELAY_MS: u64 = 20; + #[derive(Clone, Debug)] pub struct Flags { pub config_handler: Option, @@ -90,6 +92,7 @@ pub enum Action { OpenWith, Paste, Properties, + Refresh, Rename, RestoreFromTrash, SearchActivate, @@ -139,6 +142,7 @@ impl Action { Action::OpenWith => Message::ToggleContextPage(ContextPage::OpenWith), Action::Paste => Message::Paste(entity_opt), Action::Properties => Message::ToggleContextPage(ContextPage::Properties(None)), + Action::Refresh => Message::Refresh(entity_opt), Action::Rename => Message::Rename(entity_opt), Action::RestoreFromTrash => Message::RestoreFromTrash(entity_opt), Action::SearchActivate => Message::SearchActivate, @@ -243,6 +247,9 @@ pub enum Message { PendingComplete(u64), PendingError(u64, String), PendingProgress(u64, f32), + Refresh(Option), + RefreshStart(Entity, Location), + RefreshComplete(Entity, Location), RescanTrash, Rename(Option), ReplaceResult(ReplaceResult), @@ -1717,6 +1724,28 @@ impl Application for App { } return self.update_notification(); } + Message::Refresh(entity_opt) => { + let entity = entity_opt.unwrap_or_else(|| self.tab_model.active()); + if let Some(tab) = self.tab_model.data_mut::(entity) { + let location = tab.location.clone(); + tab.set_items(vec![]); + return cosmic::command::message(Message::RefreshStart(entity, location)); + } + return Command::none(); + } + Message::RefreshComplete(entity, location) => { + if self.search_input.is_empty() { + return self.rescan_tab(entity, location, None); + } else { + return self.search(); + } + } + + Message::RefreshStart(entity, location) => { + return Command::perform(tokio::time::sleep(tokio::time::Duration::from_millis(REFRESH_VIEW_DELAY_MS)), move |_| { + cosmic::app::Message::App(Message::RefreshComplete(entity, location)) + }); + } Message::RescanTrash => { // Update trash icon if empty/full let maybe_entity = self.nav_model.iter().find(|&entity| { diff --git a/src/key_bind.rs b/src/key_bind.rs index 255fb897..8bceb4f0 100644 --- a/src/key_bind.rs +++ b/src/key_bind.rs @@ -48,6 +48,8 @@ pub fn key_binds() -> HashMap { bind!([Ctrl], Key::Character("v".into()), Paste); bind!([], Key::Named(Named::Space), Properties); bind!([], Key::Named(Named::F2), Rename); + bind!([], Key::Named(Named::F5), Refresh); + bind!([Ctrl], Key::Character("r".into()), Refresh); bind!([Ctrl], Key::Character("f".into()), SearchActivate); bind!([Ctrl], Key::Character("a".into()), SelectAll); bind!([Ctrl], Key::Character(",".into()), Settings);