fix: task manager not re-rendering after progress update

This commit is contained in:
sxyazi 2024-02-06 08:16:52 +08:00
parent 59e889a3b5
commit c15fdc02e5
No known key found for this signature in database
8 changed files with 52 additions and 35 deletions

View file

@ -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

View file

@ -12,30 +12,21 @@ impl From<Cmd> for Opt {
}
}
impl From<isize> 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<Opt>) {
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);
}
}

View file

@ -14,6 +14,12 @@ impl From<()> for Opt {
impl Tasks {
pub fn toggle(&mut self, _: impl Into<Opt>) {
self.visible = !self.visible;
if self.visible {
self.summaries = self.paginate();
self.arrow(0);
}
render!();
}
}

View file

@ -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;

View file

@ -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<Scheduler>,
pub visible: bool,
pub cursor: usize,
pub progress: TasksProgress,
pub visible: bool,
pub cursor: usize,
pub progress: TasksProgress,
pub summaries: Vec<TaskSummary>,
}
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<TaskSummary> {

View file

@ -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;
};

View file

@ -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());

View file

@ -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);
}
}