This commit is contained in:
sxyazi 2024-10-13 18:35:30 +08:00
parent 2f6f916d81
commit 53b12469d1
No known key found for this signature in database

View file

@ -1,20 +1,16 @@
use std::borrow::Cow;
use yazi_macro::render_and; use yazi_macro::render_and;
use yazi_proxy::AppProxy; use yazi_proxy::AppProxy;
use yazi_shared::{event::{Cmd, Data}, fs::Url}; use yazi_shared::event::Cmd;
use crate::tab::Tab; use crate::tab::Tab;
struct Opt<'a> { struct Opt {
url: Option<Cow<'a, Url>>,
state: Option<bool>, state: Option<bool>,
} }
impl<'a> From<Cmd> for Opt<'a> { impl From<Cmd> for Opt {
fn from(mut c: Cmd) -> Self { fn from(mut c: Cmd) -> Self {
Self { Self {
url: c.take("url").and_then(Data::into_url).map(Cow::Owned),
state: match c.take_first_str().as_deref() { state: match c.take_first_str().as_deref() {
Some("on") => Some(true), Some("on") => Some(true),
Some("off") => Some(false), Some("off") => Some(false),
@ -24,18 +20,17 @@ impl<'a> From<Cmd> for Opt<'a> {
} }
} }
impl<'a> Tab { impl Tab {
#[yazi_codegen::command] #[yazi_codegen::command]
pub fn toggle(&mut self, opt: Opt<'a>) { pub fn toggle(&mut self, opt: Opt) {
let Some(url) = opt.url.or_else(|| self.current.hovered().map(|h| Cow::Borrowed(&h.url))) let Some(url) = self.current.hovered().map(|h| &h.url) else {
else {
return; return;
}; };
let b = match opt.state { let b = match opt.state {
Some(true) => render_and!(self.selected.add(&url)), Some(true) => render_and!(self.selected.add(url)),
Some(false) => render_and!(self.selected.remove(&url)) | true, Some(false) => render_and!(self.selected.remove(url)) | true,
None => render_and!(self.selected.remove(&url) || self.selected.add(&url)), None => render_and!(self.selected.remove(url) || self.selected.add(url)),
}; };
if !b { if !b {