mirror of
https://github.com/sxyazi/yazi.git
synced 2026-05-13 08:16:40 +00:00
feat: new cx.which API to access the which component state (#3617)
This commit is contained in:
parent
06c665a086
commit
592e70a1c9
31 changed files with 190 additions and 151 deletions
|
|
@ -18,9 +18,9 @@ impl Preflight {
|
|||
Ok(Lives::scope(cx.core, || {
|
||||
let mut body = opt.1.into_lua(&LUA)?;
|
||||
for (id, cb) in handlers {
|
||||
runtime_mut!(LUA)?.push(&id);
|
||||
let blocking = runtime_mut!(LUA)?.critical_push(&id, true);
|
||||
let result = cb.call::<Value>(&body);
|
||||
runtime_mut!(LUA)?.pop();
|
||||
runtime_mut!(LUA)?.critical_pop(blocking);
|
||||
|
||||
match result {
|
||||
Ok(Value::Nil) => {
|
||||
|
|
|
|||
|
|
@ -1,11 +1,14 @@
|
|||
use std::ops::Deref;
|
||||
|
||||
use mlua::{AnyUserData, UserData, UserDataFields};
|
||||
use mlua::{AnyUserData, UserData, UserDataFields, Value};
|
||||
use yazi_binding::cached_field;
|
||||
|
||||
use super::{Lives, PtrCell};
|
||||
|
||||
pub(super) struct Which {
|
||||
inner: PtrCell<yazi_core::which::Which>,
|
||||
|
||||
v_cands: Option<Value>,
|
||||
}
|
||||
|
||||
impl Deref for Which {
|
||||
|
|
@ -16,10 +19,18 @@ impl Deref for Which {
|
|||
|
||||
impl Which {
|
||||
pub(super) fn make(inner: &yazi_core::which::Which) -> mlua::Result<AnyUserData> {
|
||||
Lives::scoped_userdata(Self { inner: inner.into() })
|
||||
Lives::scoped_userdata(Self { inner: inner.into(), v_cands: None })
|
||||
}
|
||||
}
|
||||
|
||||
impl UserData for Which {
|
||||
fn add_fields<F: UserDataFields<Self>>(fields: &mut F) {}
|
||||
fn add_fields<F: UserDataFields<Self>>(fields: &mut F) {
|
||||
fields.add_field_method_get("times", |_, me| Ok(me.inner.times));
|
||||
cached_field!(fields, cands, |lua, me| {
|
||||
lua.create_sequence_from(me.inner.cands.iter().cloned().map(yazi_binding::ChordCow))
|
||||
});
|
||||
|
||||
fields.add_field_method_get("active", |_, me| Ok(me.inner.active));
|
||||
fields.add_field_method_get("silent", |_, me| Ok(me.inner.silent));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -87,12 +87,7 @@ impl UpdateFiles {
|
|||
|
||||
fn update_hovered(cx: &mut Ctx, op: FilesOp) -> Result<Data> {
|
||||
let (id, url) = (cx.tab().id, op.cwd());
|
||||
let (_, folder) = cx
|
||||
.tab_mut()
|
||||
.history
|
||||
.raw_entry_mut()
|
||||
.from_key(url)
|
||||
.or_insert_with(|| (url.clone(), Folder::from(url)));
|
||||
let folder = cx.tab_mut().history.entry_ref(url).or_insert_with(|| Folder::from(url));
|
||||
|
||||
if folder.update_pub(id, op) {
|
||||
act!(mgr:peek, cx, true)?;
|
||||
|
|
@ -108,10 +103,8 @@ impl UpdateFiles {
|
|||
|
||||
tab
|
||||
.history
|
||||
.raw_entry_mut()
|
||||
.from_key(op.cwd())
|
||||
.or_insert_with(|| (op.cwd().clone(), Folder::from(op.cwd())))
|
||||
.1
|
||||
.entry_ref(op.cwd())
|
||||
.or_insert_with(|| Folder::from(op.cwd()))
|
||||
.update_pub(tab.id, op);
|
||||
|
||||
if leave {
|
||||
|
|
|
|||
|
|
@ -2,17 +2,17 @@ use anyhow::Result;
|
|||
use yazi_core::which::WhichSorter;
|
||||
use yazi_dds::spark::SparkKind;
|
||||
use yazi_macro::{render, succ};
|
||||
use yazi_parser::which::ShowOpt;
|
||||
use yazi_parser::which::ActivateOpt;
|
||||
use yazi_shared::{Source, data::Data};
|
||||
|
||||
use crate::{Actor, Ctx};
|
||||
|
||||
pub struct Show;
|
||||
pub struct Activate;
|
||||
|
||||
impl Actor for Show {
|
||||
type Options = ShowOpt;
|
||||
impl Actor for Activate {
|
||||
type Options = ActivateOpt;
|
||||
|
||||
const NAME: &str = "show";
|
||||
const NAME: &str = "activate";
|
||||
|
||||
fn act(cx: &mut Ctx, mut opt: Self::Options) -> Result<Data> {
|
||||
opt.cands.retain(|c| c.on.len() > opt.times);
|
||||
|
|
@ -26,14 +26,14 @@ impl Actor for Show {
|
|||
which.times = opt.times;
|
||||
which.cands = opt.cands;
|
||||
|
||||
which.visible = true;
|
||||
which.active = true;
|
||||
which.silent = opt.silent;
|
||||
succ!(render!());
|
||||
}
|
||||
|
||||
fn hook(cx: &Ctx, _opt: &Self::Options) -> Option<SparkKind> {
|
||||
match cx.source() {
|
||||
Source::Unknown => Some(SparkKind::IndWhichShow),
|
||||
Source::Unknown => Some(SparkKind::IndWhichActivate),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
|
@ -1 +1 @@
|
|||
yazi_macro::mod_flat!(callback show);
|
||||
yazi_macro::mod_flat!(activate callback);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue