Skip to content

Commit f446052

Browse files
committed
feat: new relay-notify-push DDS event to allow custom notification handlers (#3642)
1 parent dfdb235 commit f446052

33 files changed

Lines changed: 114 additions & 141 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/):
1515
### Added
1616

1717
- Allow using `ps.sub()` in `init.lua` directly without a plugin ([#3638])
18+
- New `relay-notify-push` DDS event to allow custom notification handlers ([#3642])
1819
- New `cx.which` API to access the which component state ([#3617])
1920
- New `ind-which-activate` DDS event to change the which component behavior ([#3608])
2021

@@ -1636,3 +1637,4 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/):
16361637
[#3633]: https://github.com/sxyazi/yazi/pull/3633
16371638
[#3634]: https://github.com/sxyazi/yazi/pull/3634
16381639
[#3638]: https://github.com/sxyazi/yazi/pull/3638
1640+
[#3642]: https://github.com/sxyazi/yazi/pull/3642

Cargo.lock

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ core-foundation-sys = "0.8.7"
3535
crossterm = { version = "0.29.0", features = [ "event-stream" ] }
3636
dirs = "6.0.0"
3737
dyn-clone = "1.0.20"
38+
either = { version = "1.15.0", features = [ "serde" ] }
3839
foldhash = "0.2.0"
3940
futures = "0.3.31"
4041
globset = "0.4.18"

yazi-actor/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ yazi-widgets = { path = "../yazi-widgets", version = "26.1.22" }
3838
# External dependencies
3939
anyhow = { workspace = true }
4040
crossterm = { workspace = true }
41+
either = { workspace = true }
4142
futures = { workspace = true }
4243
hashbrown = { workspace = true }
4344
mlua = { workspace = true }

yazi-actor/src/app/accept_payload.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use anyhow::Result;
22
use mlua::IntoLua;
33
use tracing::error;
44
use yazi_actor::lives::Lives;
5-
use yazi_binding::runtime_mut;
5+
use yazi_binding::runtime_scope;
66
use yazi_dds::{LOCAL, Payload, REMOTE};
77
use yazi_macro::succ;
88
use yazi_plugin::LUA;
@@ -31,11 +31,9 @@ impl Actor for AcceptPayload {
3131
succ!(Lives::scope(&cx.core, || {
3232
let body = payload.body.into_lua(&LUA)?;
3333
for (id, cb) in handlers {
34-
let blocking = runtime_mut!(LUA)?.critical_push(&id, true);
35-
if let Err(e) = cb.call::<()>(body.clone()) {
34+
if let Err(e) = runtime_scope!(LUA, &id, cb.call::<()>(body.clone())) {
3635
error!("Failed to run `{kind}` event handler in your `{id}` plugin: {e}");
3736
}
38-
runtime_mut!(LUA)?.critical_pop(blocking)?;
3937
}
4038
Ok(())
4139
})?);

yazi-actor/src/app/mouse.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crossterm::event::MouseEventKind;
33
use mlua::{ObjectLike, Table};
44
use tracing::error;
55
use yazi_actor::lives::Lives;
6+
use yazi_binding::runtime_scope;
67
use yazi_config::YAZI;
78
use yazi_macro::succ;
89
use yazi_parser::app::MouseOpt;
@@ -20,11 +21,14 @@ impl Actor for Mouse {
2021

2122
fn act(cx: &mut Ctx, opt: Self::Options) -> Result<Data> {
2223
let event = yazi_binding::MouseEvent::from(opt.event);
24+
2325
let Some(size) = cx.term.as_ref().and_then(|t| t.size().ok()) else { succ!() };
26+
let area = yazi_binding::elements::Rect::from(size);
2427

2528
let result = Lives::scope(&cx.core, move || {
26-
let area = yazi_binding::elements::Rect::from(size);
27-
let root = LUA.globals().raw_get::<Table>("Root")?.call_method::<Table>("new", area)?;
29+
let root = runtime_scope!(LUA, "root", {
30+
LUA.globals().raw_get::<Table>("Root")?.call_method::<Table>("new", area)
31+
})?;
2832

2933
if matches!(event.kind, MouseEventKind::Down(_) if YAZI.mgr.mouse_events.get().draggable()) {
3034
root.raw_set("_drag_start", event)?;

yazi-actor/src/core/preflight.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use anyhow::Result;
22
use mlua::{ErrorContext, ExternalError, IntoLua, Value};
3-
use yazi_binding::runtime_mut;
3+
use yazi_binding::runtime_scope;
44
use yazi_dds::{LOCAL, spark::{Spark, SparkKind}};
55
use yazi_plugin::LUA;
66

@@ -18,11 +18,7 @@ impl Preflight {
1818
Ok(Lives::scope(cx.core, || {
1919
let mut body = opt.1.into_lua(&LUA)?;
2020
for (id, cb) in handlers {
21-
let blocking = runtime_mut!(LUA)?.critical_push(&id, true);
22-
let result = cb.call::<Value>(&body);
23-
runtime_mut!(LUA)?.critical_pop(blocking)?;
24-
25-
match result {
21+
match runtime_scope!(LUA, &id, cb.call::<Value>(&body)) {
2622
Ok(Value::Nil) => {
2723
Err(format!("`{kind}` event cancelled by `{id}` plugin on preflight").into_lua_err())?
2824
}

yazi-actor/src/lives/lives.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ pub(super) static FILE_CACHE: RoCell<RefCell<HashMap<PtrCell<yazi_fs::File>, Any
1616
pub struct Lives;
1717

1818
impl Lives {
19-
pub fn scope<T>(core: &yazi_core::Core, f: impl FnOnce() -> mlua::Result<T>) -> mlua::Result<T> {
19+
pub fn scope<T, F>(core: &yazi_core::Core, f: F) -> mlua::Result<T>
20+
where
21+
F: FnOnce() -> mlua::Result<T>,
22+
{
2023
FILE_CACHE.init(Default::default());
2124
defer! { FILE_CACHE.drop(); }
2225

yazi-actor/src/mgr/toggle_all.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ impl Actor for ToggleAll {
1414
const NAME: &str = "toggle_all";
1515

1616
fn act(cx: &mut Ctx, opt: Self::Options) -> Result<Data> {
17-
use yazi_shared::Either::*;
17+
use either::Either::*;
1818
let tab = cx.tab_mut();
1919

2020
let it = tab.current.files.iter().map(|f| &f.url);

yazi-actor/src/notify/push.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
use std::time::{Duration, Instant};
1+
use std::time::Instant;
22

33
use anyhow::Result;
44
use yazi_core::notify::Message;
5-
use yazi_macro::succ;
5+
use yazi_dds::spark::SparkKind;
6+
use yazi_macro::{act, succ};
67
use yazi_parser::notify::PushOpt;
7-
use yazi_proxy::NotifyProxy;
8-
use yazi_shared::data::Data;
8+
use yazi_shared::{Source, data::Data};
99

1010
use crate::{Actor, Ctx};
1111

@@ -24,8 +24,15 @@ impl Actor for Push {
2424

2525
if cx.notify.messages.iter().all(|m| m != &msg) {
2626
cx.notify.messages.push(msg);
27-
NotifyProxy::tick(Duration::ZERO);
27+
act!(notify:tick, cx)?;
2828
}
2929
succ!();
3030
}
31+
32+
fn hook(cx: &Ctx, _: &Self::Options) -> Option<SparkKind> {
33+
match cx.source() {
34+
Source::Relay => Some(SparkKind::RelayNotifyPush),
35+
_ => None,
36+
}
37+
}
3138
}

0 commit comments

Comments
 (0)