From c15fdc02e555862b283404aad9646880d6503f0c Mon Sep 17 00:00:00 2001 From: sxyazi Date: Tue, 6 Feb 2024 08:16:52 +0800 Subject: [PATCH] fix: task manager not re-rendering after progress update --- yazi-config/preset/yazi.toml | 8 ++--- yazi-core/src/tasks/commands/arrow.rs | 33 ++++++++------------- yazi-core/src/tasks/commands/toggle.rs | 6 ++++ yazi-core/src/tasks/mod.rs | 1 + yazi-core/src/tasks/tasks.rs | 12 ++++---- yazi-fm/src/app/commands/update_progress.rs | 20 +++++++++++-- yazi-fm/src/tasks/layout.rs | 3 +- yazi-fm/src/widgets/clear.rs | 4 +-- 8 files changed, 52 insertions(+), 35 deletions(-) diff --git a/yazi-config/preset/yazi.toml b/yazi-config/preset/yazi.toml index 5ff959be..789263cc 100644 --- a/yazi-config/preset/yazi.toml +++ b/yazi-config/preset/yazi.toml @@ -4,10 +4,10 @@ [manager] ratio = [ 1, 4, 3 ] -sort_by = "modified" -sort_sensitive = true -sort_reverse = true -sort_dir_first = true +sort_by = "alphabetical" +sort_sensitive = false +sort_reverse = false +sort_dir_first = false linemode = "none" show_hidden = false show_symlink = true diff --git a/yazi-core/src/tasks/commands/arrow.rs b/yazi-core/src/tasks/commands/arrow.rs index 763c7e43..9b68c9fe 100644 --- a/yazi-core/src/tasks/commands/arrow.rs +++ b/yazi-core/src/tasks/commands/arrow.rs @@ -12,30 +12,21 @@ impl From for Opt { } } +impl From for Opt { + fn from(step: isize) -> Self { Self { step } } +} + impl Tasks { - #[allow(clippy::should_implement_trait)] - fn next(&mut self) { - let limit = Self::limit().min(self.len()); - - let old = self.cursor; - self.cursor = limit.saturating_sub(1).min(self.cursor + 1); - - render!(old != self.cursor); - } - - fn prev(&mut self) { - let old = self.cursor; - self.cursor = self.cursor.saturating_sub(1); - - render!(old != self.cursor); - } - pub fn arrow(&mut self, opt: impl Into) { - let opt = opt.into() as Opt; - if opt.step > 0 { - self.next(); + let old = self.cursor; + if opt.into().step > 0 { + self.cursor += 1; } else { - self.prev(); + self.cursor = self.cursor.saturating_sub(1); } + + let max = Self::limit().min(self.summaries.len()); + self.cursor = self.cursor.min(max.saturating_sub(1)); + render!(self.cursor != old); } } diff --git a/yazi-core/src/tasks/commands/toggle.rs b/yazi-core/src/tasks/commands/toggle.rs index 88a89fcc..1ffc9a56 100644 --- a/yazi-core/src/tasks/commands/toggle.rs +++ b/yazi-core/src/tasks/commands/toggle.rs @@ -14,6 +14,12 @@ impl From<()> for Opt { impl Tasks { pub fn toggle(&mut self, _: impl Into) { self.visible = !self.visible; + + if self.visible { + self.summaries = self.paginate(); + self.arrow(0); + } + render!(); } } diff --git a/yazi-core/src/tasks/mod.rs b/yazi-core/src/tasks/mod.rs index 32ea75be..d8475836 100644 --- a/yazi-core/src/tasks/mod.rs +++ b/yazi-core/src/tasks/mod.rs @@ -5,5 +5,6 @@ mod tasks; pub use progress::*; pub use tasks::*; +pub const TASKS_BORDER: u16 = 2; pub const TASKS_PADDING: u16 = 2; pub const TASKS_PERCENT: u16 = 80; diff --git a/yazi-core/src/tasks/tasks.rs b/yazi-core/src/tasks/tasks.rs index 4d71677b..7ae426d3 100644 --- a/yazi-core/src/tasks/tasks.rs +++ b/yazi-core/src/tasks/tasks.rs @@ -7,15 +7,16 @@ use yazi_plugin::ValueSendable; use yazi_scheduler::{Scheduler, TaskSummary}; use yazi_shared::{emit, event::Cmd, fs::{File, Url}, term::Term, Layer, MIME_DIR}; -use super::{TasksProgress, TASKS_PADDING, TASKS_PERCENT}; +use super::{TasksProgress, TASKS_BORDER, TASKS_PADDING, TASKS_PERCENT}; use crate::{folder::Files, input::Input}; pub struct Tasks { pub(super) scheduler: Arc, - pub visible: bool, - pub cursor: usize, - pub progress: TasksProgress, + pub visible: bool, + pub cursor: usize, + pub progress: TasksProgress, + pub summaries: Vec, } impl Tasks { @@ -25,6 +26,7 @@ impl Tasks { visible: false, cursor: 0, progress: Default::default(), + summaries: Default::default(), }; let running = tasks.scheduler.running.clone(); @@ -46,7 +48,7 @@ impl Tasks { #[inline] pub fn limit() -> usize { - (Term::size().rows * TASKS_PERCENT / 100).saturating_sub(TASKS_PADDING) as usize + (Term::size().rows * TASKS_PERCENT / 100).saturating_sub(TASKS_BORDER + TASKS_PADDING) as usize } pub fn paginate(&self) -> Vec { diff --git a/yazi-fm/src/app/commands/update_progress.rs b/yazi-fm/src/app/commands/update_progress.rs index d6a57fe4..d8c2ce20 100644 --- a/yazi-fm/src/app/commands/update_progress.rs +++ b/yazi-fm/src/app/commands/update_progress.rs @@ -1,6 +1,6 @@ use ratatui::backend::Backend; use yazi_core::tasks::TasksProgress; -use yazi_shared::event::Cmd; +use yazi_shared::{event::Cmd, render}; use crate::{app::App, components::Progress, lives::Lives}; @@ -22,7 +22,23 @@ impl App { return; }; - self.cx.tasks.progress = opt.progress; + // Update the progress of all tasks. + let tasks = &mut self.cx.tasks; + tasks.progress = opt.progress; + + // If the tasks pane is visible, update the summaries with a complete render. + if tasks.visible { + let new = tasks.paginate(); + if new.len() != tasks.summaries.len() + || new.iter().zip(&tasks.summaries).any(|(a, b)| a.name != b.name) + { + tasks.summaries = new; + tasks.arrow(0); + return render!(); + } + } + + // Otherwise, only partially update the progress. let Some(term) = &mut self.term else { return; }; diff --git a/yazi-fm/src/tasks/layout.rs b/yazi-fm/src/tasks/layout.rs index 84b2613c..ac1cf6ab 100644 --- a/yazi-fm/src/tasks/layout.rs +++ b/yazi-fm/src/tasks/layout.rs @@ -43,8 +43,9 @@ impl<'a> Widget for Layout<'a> { let tasks = &self.cx.tasks; let items = tasks - .paginate() + .summaries .iter() + .take(area.height.saturating_sub(2) as usize) .enumerate() .map(|(i, v)| { let mut item = ListItem::new(v.name.clone()); diff --git a/yazi-fm/src/widgets/clear.rs b/yazi-fm/src/widgets/clear.rs index ec4ed5c7..bf6532ee 100644 --- a/yazi-fm/src/widgets/clear.rs +++ b/yazi-fm/src/widgets/clear.rs @@ -34,8 +34,8 @@ impl Widget for Clear { ADAPTOR.image_erase(r).ok(); COLLISION.store(true, Ordering::Relaxed); - for x in r.left()..r.right() { - for y in r.top()..r.bottom() { + for x in area.left()..area.right() { + for y in area.top()..area.bottom() { buf.get_mut(x, y).set_skip(true); } }