Skip to content

Commit

Permalink
fix: use complete rendering instead of partial rendering for no task …
Browse files Browse the repository at this point in the history
…progress report (#1876)
  • Loading branch information
sxyazi authored Nov 1, 2024
1 parent a510673 commit 0baccdc
Show file tree
Hide file tree
Showing 45 changed files with 172 additions and 188 deletions.
8 changes: 0 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ strip = true
[workspace.dependencies]
ansi-to-tui = "7.0.0"
anyhow = "1.0.91"
arc-swap = "1.7.1"
base64 = "0.22.1"
bitflags = "2.6.0"
clap = { version = "4.5.20", features = [ "derive" ] }
Expand Down
1 change: 0 additions & 1 deletion yazi-adapter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ yazi-shared = { path = "../yazi-shared", version = "0.3.3" }
# External dependencies
ansi-to-tui = { workspace = true }
anyhow = { workspace = true }
arc-swap = { workspace = true }
base64 = { workspace = true }
color_quant = "1.1.0"
crossterm = { workspace = true }
Expand Down
8 changes: 4 additions & 4 deletions yazi-adapter/src/adapter.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{env, fmt::Display, path::Path, sync::Arc};
use std::{env, fmt::Display, path::Path};

use anyhow::Result;
use ratatui::layout::Rect;
Expand Down Expand Up @@ -52,7 +52,7 @@ impl Adapter {
}

pub fn image_hide(self) -> Result<()> {
if let Some(area) = SHOWN.swap(None) { self.image_erase(*area) } else { Ok(()) }
if let Some(area) = SHOWN.replace(None) { self.image_erase(area) } else { Ok(()) }
}

pub fn image_erase(self, area: Rect) -> Result<()> {
Expand All @@ -67,10 +67,10 @@ impl Adapter {
}

#[inline]
pub fn shown_load(self) -> Option<Rect> { SHOWN.load_full().map(|r| *r) }
pub fn shown_load(self) -> Option<Rect> { SHOWN.get() }

#[inline]
pub(super) fn shown_store(area: Rect) { SHOWN.store(Some(Arc::new(area))); }
pub(super) fn shown_store(area: Rect) { SHOWN.set(Some(area)); }

pub(super) fn start(self) { Ueberzug::start(self); }

Expand Down
7 changes: 2 additions & 5 deletions yazi-adapter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ yazi_macro::mod_flat!(
adapter chafa dimension emulator iip image kgp kgp_old mux sixel ueberzug
);

use yazi_shared::{RoCell, env_exists, in_wsl};
use yazi_shared::{RoCell, SyncCell, env_exists, in_wsl};
pub static ADAPTOR: RoCell<Adapter> = RoCell::new();

// Tmux support
Expand All @@ -17,7 +17,7 @@ static CLOSE: RoCell<&'static str> = RoCell::new();
pub static WSL: RoCell<bool> = RoCell::new();

// Image state
static SHOWN: RoCell<arc_swap::ArcSwapOption<ratatui::layout::Rect>> = RoCell::new();
static SHOWN: SyncCell<Option<ratatui::layout::Rect>> = SyncCell::new(None);

pub fn init() {
// Tmux support
Expand All @@ -38,9 +38,6 @@ pub fn init() {
// WSL support
WSL.init(in_wsl());

// Image state
SHOWN.with(<_>::default);

ADAPTOR.init(Adapter::matches());
ADAPTOR.start();
}
1 change: 0 additions & 1 deletion yazi-config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ yazi-shared = { path = "../yazi-shared", version = "0.3.3" }

# External dependencies
anyhow = { workspace = true }
arc-swap = { workspace = true }
bitflags = { workspace = true }
crossterm = { workspace = true }
globset = { workspace = true }
Expand Down
11 changes: 9 additions & 2 deletions yazi-config/src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ use ratatui::layout::Rect;

#[derive(Clone, Copy, Default, PartialEq, Eq)]
pub struct Layout {
pub current: Rect,
pub preview: Rect,
pub current: Rect,
pub preview: Rect,
pub progress: Rect,
}

impl Layout {
pub const fn default() -> Self {
Self { current: Rect::ZERO, preview: Rect::ZERO, progress: Rect::ZERO }
}
}
8 changes: 3 additions & 5 deletions yazi-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ yazi_macro::mod_flat!(layout pattern preset priority);

use std::str::FromStr;

use yazi_shared::{RoCell, Xdg};

pub static LAYOUT: RoCell<arc_swap::ArcSwap<Layout>> = RoCell::new();
use yazi_shared::{RoCell, SyncCell, Xdg};

pub static KEYMAP: RoCell<keymap::Keymap> = RoCell::new();
pub static LOG: RoCell<log::Log> = RoCell::new();
Expand All @@ -23,6 +21,8 @@ pub static CONFIRM: RoCell<popup::Confirm> = RoCell::new();
pub static PICK: RoCell<popup::Pick> = RoCell::new();
pub static WHICH: RoCell<which::Which> = RoCell::new();

pub static LAYOUT: SyncCell<Layout> = SyncCell::new(Layout::default());

pub fn init() -> anyhow::Result<()> {
if let Err(e) = try_init(true) {
eprintln!("{e}");
Expand Down Expand Up @@ -101,8 +101,6 @@ fn try_init(merge: bool) -> anyhow::Result<()> {
let pick = <_>::from_str(&yazi_toml)?;
let which = <_>::from_str(&yazi_toml)?;

LAYOUT.with(<_>::default);

KEYMAP.init(keymap);
LOG.init(log);
MANAGER.init(manager);
Expand Down
2 changes: 1 addition & 1 deletion yazi-core/src/tab/tab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl Tab {
pub fn hovered_rect(&self) -> Option<Rect> {
let y = self.current.files.position(self.hovered()?.urn())? - self.current.offset;

let mut rect = LAYOUT.load().current;
let mut rect = LAYOUT.get().current;
rect.y = rect.y.saturating_sub(1) + y as u16;
rect.height = 1;
Some(rect)
Expand Down
2 changes: 1 addition & 1 deletion yazi-core/src/tasks/progress.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use serde::Serialize;
use yazi_scheduler::Ongoing;

#[derive(Clone, Copy, Default, Eq, PartialEq, Serialize)]
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Serialize)]
pub struct TasksProgress {
pub total: u32,
pub succ: u32,
Expand Down
9 changes: 4 additions & 5 deletions yazi-fm/src/app/commands/reflow.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::sync::Arc;

use mlua::Value;
use ratatui::layout::Position;
use tracing::error;
Expand All @@ -23,7 +21,7 @@ impl App {
#[yazi_codegen::command]
pub fn reflow(&mut self, _: Opt) {
let Some(size) = self.term.as_ref().and_then(|t| t.size().ok()) else { return };
let mut layout = *LAYOUT.load_full();
let mut layout = LAYOUT.get();

let result = Lives::scope(&self.cx, |_| {
let comps = Root::reflow((Position::ORIGIN, size).into())?;
Expand All @@ -38,14 +36,15 @@ impl App {
match id.to_str()? {
"current" => layout.current = *t.raw_get::<_, yazi_plugin::elements::Rect>("_area")?,
"preview" => layout.preview = *t.raw_get::<_, yazi_plugin::elements::Rect>("_area")?,
"progress" => layout.progress = *t.raw_get::<_, yazi_plugin::elements::Rect>("_area")?,
_ => {}
}
}
Ok(())
});

if layout != *LAYOUT.load_full() {
LAYOUT.store(Arc::new(layout));
if layout != LAYOUT.get() {
LAYOUT.set(layout);
render!();
}

Expand Down
15 changes: 8 additions & 7 deletions yazi-fm/src/app/commands/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl App {
Self::patch(frame, self.cx.cursor());
}
if !self.cx.notify.messages.is_empty() {
self.render_notify();
self.render_partially();
}

// Reload preview if collision is resolved
Expand All @@ -40,18 +40,19 @@ impl App {
}
}

pub(crate) fn render_notify(&mut self) {
let Some(term) = &mut self.term else {
return;
};

pub(crate) fn render_partially(&mut self) {
let Some(term) = &mut self.term else { return };
if !term.can_partial() {
return self.render();
}

let frame = term
.draw_partial(|f| {
f.render_widget(crate::notify::Layout::new(&self.cx), f.area());
_ = Lives::scope(&self.cx, |_| {
f.render_widget(crate::tasks::Progress, f.area());
f.render_widget(crate::notify::Notify::new(&self.cx), f.area());
Ok(())
});

if let Some(pos) = self.cx.cursor() {
f.set_cursor_position(pos);
Expand Down
4 changes: 2 additions & 2 deletions yazi-fm/src/app/commands/update_notify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ impl App {
pub(crate) fn update_notify(&mut self, cmd: Cmd) {
let WindowSize { rows, columns, .. } = Dimension::available();
let area =
notify::Layout::available(Rect { x: 0, y: 0, width: columns, height: rows });
notify::Notify::available(Rect { x: 0, y: 0, width: columns, height: rows });

self.cx.notify.tick(cmd, area);

if self.cx.notify.messages.is_empty() {
self.render();
} else {
self.render_notify();
self.render_partially();
}
}
}
34 changes: 10 additions & 24 deletions yazi-fm/src/app/commands/update_progress.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use ratatui::backend::Backend;
use yazi_core::tasks::TasksProgress;
use yazi_macro::render;
use yazi_shared::event::Cmd;

use crate::{app::App, components::Progress, lives::Lives};
use crate::app::App;

pub struct Opt {
progress: TasksProgress,
Expand All @@ -19,41 +18,28 @@ impl TryFrom<Cmd> for Opt {

impl App {
pub(crate) fn update_progress(&mut self, opt: impl TryInto<Opt>) {
let Ok(opt) = opt.try_into() else {
return;
};
let Ok(opt) = opt.try_into() else { return };

// Update the progress of all tasks.
let tasks = &mut self.cx.tasks;
let progressed = tasks.progress != opt.progress;
tasks.progress = opt.progress;

// If the task manager 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)
{
if tasks.summaries != new {
tasks.summaries = new;
tasks.arrow(0);
return render!();
}
}

// Otherwise, only partially update the progress.
let Some(term) = &mut self.term else {
return;
};

_ = Lives::scope(&self.cx, |_| {
for patch in Progress::partial_render(term.current_buffer_mut()) {
term.backend_mut().draw(patch.iter().map(|(x, y, cell)| (*x, *y, cell)))?;
if let Some(pos) = self.cx.cursor() {
term.show_cursor()?;
term.set_cursor_position(pos)?;
}
term.backend_mut().flush()?;
}
Ok(())
});
if !progressed {
} else if tasks.progress.total == 0 {
render!();
} else {
self.render_partially();
}
}
}
3 changes: 0 additions & 3 deletions yazi-fm/src/components/mod.rs

This file was deleted.

41 changes: 0 additions & 41 deletions yazi-fm/src/components/progress.rs

This file was deleted.

6 changes: 3 additions & 3 deletions yazi-fm/src/help/layout.rs → yazi-fm/src/help/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ use yazi_config::{KEYMAP, THEME};
use super::Bindings;
use crate::Ctx;

pub(crate) struct Layout<'a> {
pub(crate) struct Help<'a> {
cx: &'a Ctx,
}

impl<'a> Layout<'a> {
impl<'a> Help<'a> {
pub fn new(cx: &'a Ctx) -> Self { Self { cx } }

fn tips() -> String {
Expand All @@ -19,7 +19,7 @@ impl<'a> Layout<'a> {
}
}

impl<'a> Widget for Layout<'a> {
impl<'a> Widget for Help<'a> {
fn render(self, area: Rect, buf: &mut Buffer) {
let help = &self.cx.help;
yazi_plugin::elements::Clear::default().render(area, buf);
Expand Down
2 changes: 1 addition & 1 deletion yazi-fm/src/help/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
yazi_macro::mod_flat!(bindings layout);
yazi_macro::mod_flat!(bindings help);
2 changes: 1 addition & 1 deletion yazi-fm/src/lives/folder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl Folder {
let window = match window {
Some(w) => w,
None => {
let limit = LAYOUT.load().preview.height as usize;
let limit = LAYOUT.get().preview.height as usize;
inner.offset..inner.files.len().min(inner.offset + limit)
}
};
Expand Down
Loading

0 comments on commit 0baccdc

Please sign in to comment.