From a281c3f81add594a8c435abc54dde31c6e957de4 Mon Sep 17 00:00:00 2001 From: sxyazi Date: Tue, 28 Nov 2023 00:35:28 +0800 Subject: [PATCH] .. --- yazi-core/src/event.rs | 8 +------ yazi-core/src/input/commands/show.rs | 10 ++------- yazi-core/src/select/commands/show.rs | 10 ++------- yazi-core/src/tasks/commands/mod.rs | 1 + yazi-core/src/tasks/commands/update.rs | 31 ++++++++++++++++++++++++++ yazi-core/src/tasks/scheduler.rs | 4 ++-- yazi-fm/src/app.rs | 4 ---- yazi-fm/src/executor.rs | 1 + yazi-shared/src/event.rs | 5 +++++ 9 files changed, 45 insertions(+), 29 deletions(-) create mode 100644 yazi-core/src/tasks/commands/update.rs diff --git a/yazi-core/src/event.rs b/yazi-core/src/event.rs index d5eb6b61..7099588a 100644 --- a/yazi-core/src/event.rs +++ b/yazi-core/src/event.rs @@ -6,7 +6,7 @@ use yazi_config::open::Opener; use yazi_shared::{fs::Url, term::Term, Exec, Layer, RoCell}; use super::files::FilesOp; -use crate::{preview::PreviewLock, tasks::TasksProgress}; +use crate::preview::PreviewLock; static TX: RoCell> = RoCell::new(); @@ -25,11 +25,8 @@ pub enum Event { Mimetype(BTreeMap), Preview(PreviewLock), - // Input(InputOpt, mpsc::UnboundedSender>), - // Tasks Open(Vec<(OsString, String)>, Option), - Progress(TasksProgress), } impl Event { @@ -83,9 +80,6 @@ macro_rules! emit { (Open($targets:expr, $opener:expr)) => { $crate::Event::Open($targets, $opener).emit(); }; - (Progress($progress:expr)) => { - $crate::Event::Progress($progress).emit(); - }; ($event:ident) => { $crate::Event::$event.emit(); diff --git a/yazi-core/src/input/commands/show.rs b/yazi-core/src/input/commands/show.rs index 796763ec..58593269 100644 --- a/yazi-core/src/input/commands/show.rs +++ b/yazi-core/src/input/commands/show.rs @@ -1,4 +1,4 @@ -use anyhow::bail; +use anyhow::anyhow; use tokio::sync::mpsc; use yazi_config::popup::InputCfg; use yazi_shared::{Exec, InputError, Layer}; @@ -14,13 +14,7 @@ impl TryFrom<&Exec> for Opt { type Error = anyhow::Error; fn try_from(e: &Exec) -> Result { - let Some(data) = e.data.borrow_mut().take() else { - bail!("missing data"); - }; - let Ok(opt) = data.downcast::() else { - bail!("invalid data"); - }; - Ok(*opt) + e.take_data().ok_or_else(|| anyhow!("invalid data")) } } diff --git a/yazi-core/src/select/commands/show.rs b/yazi-core/src/select/commands/show.rs index ca635b33..d5fd50f7 100644 --- a/yazi-core/src/select/commands/show.rs +++ b/yazi-core/src/select/commands/show.rs @@ -1,4 +1,4 @@ -use anyhow::{bail, Result}; +use anyhow::{anyhow, Result}; use tokio::sync::oneshot; use yazi_config::popup::SelectCfg; use yazi_shared::{term::Term, Exec, Layer}; @@ -14,13 +14,7 @@ impl TryFrom<&Exec> for Opt { type Error = anyhow::Error; fn try_from(e: &Exec) -> Result { - let Some(data) = e.data.borrow_mut().take() else { - bail!("missing data"); - }; - let Ok(opt) = data.downcast::() else { - bail!("invalid data"); - }; - Ok(*opt) + e.take_data().ok_or_else(|| anyhow!("invalid data")) } } diff --git a/yazi-core/src/tasks/commands/mod.rs b/yazi-core/src/tasks/commands/mod.rs index e6c3cafd..d032b46a 100644 --- a/yazi-core/src/tasks/commands/mod.rs +++ b/yazi-core/src/tasks/commands/mod.rs @@ -2,3 +2,4 @@ mod arrow; mod cancel; mod inspect; mod toggle; +mod update; diff --git a/yazi-core/src/tasks/commands/update.rs b/yazi-core/src/tasks/commands/update.rs new file mode 100644 index 00000000..2e87c88b --- /dev/null +++ b/yazi-core/src/tasks/commands/update.rs @@ -0,0 +1,31 @@ +use anyhow::anyhow; +use yazi_shared::{Exec, Layer}; + +use crate::{emit, tasks::{Tasks, TasksProgress}}; + +pub struct Opt { + progress: TasksProgress, +} + +impl TryFrom<&Exec> for Opt { + type Error = anyhow::Error; + + fn try_from(e: &Exec) -> Result { + e.take_data().ok_or_else(|| anyhow!("invalid data")) + } +} + +impl Tasks { + pub fn _update(progress: TasksProgress) { + emit!(Call(Exec::call("update", vec![]).with_data(Opt { progress }).vec(), Layer::Tasks)); + } + + pub fn update(&mut self, opt: impl TryInto) -> bool { + let Ok(opt) = opt.try_into() else { + return false; + }; + + self.progress = opt.progress; + true + } +} diff --git a/yazi-core/src/tasks/scheduler.rs b/yazi-core/src/tasks/scheduler.rs index ee66b710..0309892f 100644 --- a/yazi-core/src/tasks/scheduler.rs +++ b/yazi-core/src/tasks/scheduler.rs @@ -7,7 +7,7 @@ use yazi_config::{open::Opener, TASKS}; use yazi_shared::{fs::{unique_path, Url}, Throttle}; use super::{workers::{File, FileOpDelete, FileOpLink, FileOpPaste, FileOpTrash, Precache, PrecacheOpMime, PrecacheOpSize, Process, ProcessOpOpen}, Running, TaskOp, TaskStage, TasksProgress}; -use crate::emit; +use crate::tasks::Tasks; pub struct Scheduler { file: Arc, @@ -157,7 +157,7 @@ impl Scheduler { let new = TasksProgress::from(&*running.read()); if last != new { last = new; - emit!(Progress(new)); + Tasks::_update(new); } } }); diff --git a/yazi-fm/src/app.rs b/yazi-fm/src/app.rs index a6b206e5..3e858a4e 100644 --- a/yazi-fm/src/app.rs +++ b/yazi-fm/src/app.rs @@ -195,10 +195,6 @@ impl App { tasks.file_open(&targets); } } - Event::Progress(progress) => { - tasks.progress = progress; - emit!(Render); - } _ => unreachable!(), } diff --git a/yazi-fm/src/executor.rs b/yazi-fm/src/executor.rs index 51c6beb8..f60d2a12 100644 --- a/yazi-fm/src/executor.rs +++ b/yazi-fm/src/executor.rs @@ -163,6 +163,7 @@ impl<'a> Executor<'a> { }; } + on!(update); on!(toggle, "close"); on!(arrow); on!(inspect); diff --git a/yazi-shared/src/event.rs b/yazi-shared/src/event.rs index 8d804e65..ee28c6d7 100644 --- a/yazi-shared/src/event.rs +++ b/yazi-shared/src/event.rs @@ -41,6 +41,11 @@ impl Exec { self.data = RefCell::new(Some(Box::new(data))); self } + + #[inline] + pub fn take_data(&self) -> Option { + self.data.replace(None).and_then(|d| d.downcast::().ok()).map(|d| *d) + } } impl Display for Exec {