From 9d05946f4b701b538f7bdba9239e9091384afff8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E9=9B=85=20misaki=20masa?= Date: Thu, 26 Jun 2025 18:12:09 +0800 Subject: [PATCH] feat!: make `ya.confirm()` and `ui.Pos()` public API (#2921) --- yazi-config/preset/theme-dark.toml | 2 +- yazi-config/preset/theme-light.toml | 2 +- yazi-config/preset/yazi-default.toml | 16 ++++++------- yazi-config/src/popup/confirm.rs | 16 ++++++------- yazi-config/src/popup/options.rs | 10 ++++---- yazi-config/src/theme/theme.rs | 8 +++---- yazi-core/src/confirm/commands/show.rs | 2 +- yazi-core/src/confirm/confirm.rs | 6 ++--- yazi-fm/src/confirm/{content.rs => body.rs} | 12 +++++----- yazi-fm/src/confirm/confirm.rs | 10 ++++---- yazi-fm/src/confirm/mod.rs | 2 +- yazi-plugin/src/config/theme.rs | 2 +- yazi-plugin/src/elements/pos.rs | 26 ++++++++++++++++++--- yazi-plugin/src/macros.rs | 2 +- yazi-plugin/src/utils/layer.rs | 23 +++++++++++++----- 15 files changed, 85 insertions(+), 54 deletions(-) rename yazi-fm/src/confirm/{content.rs => body.rs} (77%) diff --git a/yazi-config/preset/theme-dark.toml b/yazi-config/preset/theme-dark.toml index 126f962b..940901db 100644 --- a/yazi-config/preset/theme-dark.toml +++ b/yazi-config/preset/theme-dark.toml @@ -124,7 +124,7 @@ separator_style = { fg = "darkgray" } [confirm] border = { fg = "blue" } title = { fg = "blue" } -content = {} +body = {} list = {} btn_yes = { reversed = true } btn_no = {} diff --git a/yazi-config/preset/theme-light.toml b/yazi-config/preset/theme-light.toml index eae8bb9a..55f99796 100644 --- a/yazi-config/preset/theme-light.toml +++ b/yazi-config/preset/theme-light.toml @@ -124,7 +124,7 @@ separator_style = { fg = "darkgray" } [confirm] border = { fg = "blue" } title = { fg = "blue" } -content = {} +body = {} list = {} btn_yes = { reversed = true } btn_no = {} diff --git a/yazi-config/preset/yazi-default.toml b/yazi-config/preset/yazi-default.toml index fc1223df..4ce5c56a 100644 --- a/yazi-config/preset/yazi-default.toml +++ b/yazi-config/preset/yazi-default.toml @@ -200,16 +200,16 @@ delete_origin = "center" delete_offset = [ 0, 0, 70, 20 ] # overwrite -overwrite_title = "Overwrite file?" -overwrite_content = "Will overwrite the following file:" -overwrite_origin = "center" -overwrite_offset = [ 0, 0, 50, 15 ] +overwrite_title = "Overwrite file?" +overwrite_body = "Will overwrite the following file:" +overwrite_origin = "center" +overwrite_offset = [ 0, 0, 50, 15 ] # quit -quit_title = "Quit?" -quit_content = "The following tasks are still running, are you sure you want to quit?" -quit_origin = "center" -quit_offset = [ 0, 0, 50, 15 ] +quit_title = "Quit?" +quit_body = "The following tasks are still running, are you sure you want to quit?" +quit_origin = "center" +quit_offset = [ 0, 0, 50, 15 ] [pick] open_title = "Open with:" diff --git a/yazi-config/src/popup/confirm.rs b/yazi-config/src/popup/confirm.rs index 703102b2..d892838b 100644 --- a/yazi-config/src/popup/confirm.rs +++ b/yazi-config/src/popup/confirm.rs @@ -16,16 +16,16 @@ pub struct Confirm { pub delete_offset: Offset, // overwrite - pub overwrite_title: String, - pub overwrite_content: String, - pub overwrite_origin: Origin, - pub overwrite_offset: Offset, + pub overwrite_title: String, + pub overwrite_body: String, + pub overwrite_origin: Origin, + pub overwrite_offset: Offset, // quit - pub quit_title: String, - pub quit_content: String, - pub quit_origin: Origin, - pub quit_offset: Offset, + pub quit_title: String, + pub quit_body: String, + pub quit_origin: Origin, + pub quit_offset: Offset, } impl Confirm { diff --git a/yazi-config/src/popup/options.rs b/yazi-config/src/popup/options.rs index b27d0c65..2900bb90 100644 --- a/yazi-config/src/popup/options.rs +++ b/yazi-config/src/popup/options.rs @@ -26,7 +26,7 @@ pub struct PickCfg { pub struct ConfirmCfg { pub position: Position, pub title: Line<'static>, - pub content: Paragraph<'static>, + pub body: Paragraph<'static>, pub list: Paragraph<'static>, } @@ -107,13 +107,13 @@ impl ConfirmCfg { fn new( title: String, (origin, offset): (Origin, Offset), - content: Option>, + body: Option>, list: Option>, ) -> Self { Self { position: Position::new(origin, offset), title: Line::raw(title), - content: content.map(|c| Paragraph::new(c).wrap(Wrap { trim: false })).unwrap_or_default(), + body: body.map(|c| Paragraph::new(c).wrap(Wrap { trim: false })).unwrap_or_default(), list: list.map(|l| Paragraph::new(l).wrap(Wrap { trim: false })).unwrap_or_default(), } } @@ -140,7 +140,7 @@ impl ConfirmCfg { Self::new( YAZI.confirm.overwrite_title.to_owned(), (YAZI.confirm.overwrite_origin, YAZI.confirm.overwrite_offset), - Some(Text::raw(&YAZI.confirm.overwrite_content)), + Some(Text::raw(&YAZI.confirm.overwrite_body)), Some(url.to_string().into()), ) } @@ -149,7 +149,7 @@ impl ConfirmCfg { Self::new( Self::replace_number(&YAZI.confirm.quit_title, len), (YAZI.confirm.quit_origin, YAZI.confirm.quit_offset), - Some(Text::raw(&YAZI.confirm.quit_content)), + Some(Text::raw(&YAZI.confirm.quit_body)), Self::truncate_list(names.into_iter(), len, 10), ) } diff --git a/yazi-config/src/theme/theme.rs b/yazi-config/src/theme/theme.rs index 57d1985a..b5e31a77 100644 --- a/yazi-config/src/theme/theme.rs +++ b/yazi-config/src/theme/theme.rs @@ -132,10 +132,10 @@ pub struct Which { #[derive(Deserialize, DeserializeOver2)] pub struct Confirm { - pub border: Style, - pub title: Style, - pub content: Style, - pub list: Style, + pub border: Style, + pub title: Style, + pub body: Style, + pub list: Style, pub btn_yes: Style, pub btn_no: Style, diff --git a/yazi-core/src/confirm/commands/show.rs b/yazi-core/src/confirm/commands/show.rs index a2d71cd1..c4b6e076 100644 --- a/yazi-core/src/confirm/commands/show.rs +++ b/yazi-core/src/confirm/commands/show.rs @@ -26,7 +26,7 @@ impl Confirm { self.close(false); self.title = opt.cfg.title; - self.content = opt.cfg.content; + self.body = opt.cfg.body; self.list = opt.cfg.list; self.position = opt.cfg.position; diff --git a/yazi-core/src/confirm/confirm.rs b/yazi-core/src/confirm/confirm.rs index 60a0ebde..b6496c33 100644 --- a/yazi-core/src/confirm/confirm.rs +++ b/yazi-core/src/confirm/confirm.rs @@ -4,9 +4,9 @@ use yazi_config::popup::Position; #[derive(Default)] pub struct Confirm { - pub title: Line<'static>, - pub content: Paragraph<'static>, - pub list: Paragraph<'static>, + pub title: Line<'static>, + pub body: Paragraph<'static>, + pub list: Paragraph<'static>, pub position: Position, pub offset: usize, diff --git a/yazi-fm/src/confirm/content.rs b/yazi-fm/src/confirm/body.rs similarity index 77% rename from yazi-fm/src/confirm/content.rs rename to yazi-fm/src/confirm/body.rs index 015d6a9c..070ef31d 100644 --- a/yazi-fm/src/confirm/content.rs +++ b/yazi-fm/src/confirm/body.rs @@ -3,20 +3,20 @@ use yazi_config::THEME; use crate::Ctx; -pub(crate) struct Content<'a> { +pub(crate) struct Body<'a> { cx: &'a Ctx, border: bool, } -impl<'a> Content<'a> { +impl<'a> Body<'a> { pub(crate) fn new(cx: &'a Ctx, border: bool) -> Self { Self { cx, border } } } -impl Widget for Content<'_> { +impl Widget for Body<'_> { fn render(self, area: Rect, buf: &mut Buffer) { let confirm = &self.cx.confirm; - // Content area + // Inner area let inner = area.inner(Margin::new(1, 0)); // Border @@ -27,11 +27,11 @@ impl Widget for Content<'_> { }; confirm - .content + .body .clone() .alignment(ratatui::layout::Alignment::Center) .block(block) - .style(THEME.confirm.content.derive(Styled::style(&confirm.content))) + .style(THEME.confirm.body.derive(Styled::style(&confirm.body))) .render(inner, buf); } } diff --git a/yazi-fm/src/confirm/confirm.rs b/yazi-fm/src/confirm/confirm.rs index 895d97ee..63621ae4 100644 --- a/yazi-fm/src/confirm/confirm.rs +++ b/yazi-fm/src/confirm/confirm.rs @@ -25,21 +25,21 @@ impl Widget for Confirm<'_> { .title_alignment(Alignment::Center) .render(area, buf); - let content_border = confirm.list.line_count(area.width) != 0; - let content_height = confirm.content.line_count(area.width) as u16; + let body_border = confirm.list.line_count(area.width) != 0; + let body_height = confirm.body.line_count(area.width) as u16; let chunks = Layout::vertical([ - Constraint::Length(if content_height == 0 { + Constraint::Length(if body_height == 0 { 0 } else { - content_height.saturating_add(content_border as u16) + body_height.saturating_add(body_border as u16) }), Constraint::Fill(1), Constraint::Length(1), ]) .split(area.inner(Margin::new(0, 1))); - super::Content::new(self.cx, content_border).render(chunks[0], buf); + super::Body::new(self.cx, body_border).render(chunks[0], buf); super::List::new(self.cx).render(chunks[1], buf); super::Buttons.render(chunks[2], buf); } diff --git a/yazi-fm/src/confirm/mod.rs b/yazi-fm/src/confirm/mod.rs index b743a906..a8eb78c7 100644 --- a/yazi-fm/src/confirm/mod.rs +++ b/yazi-fm/src/confirm/mod.rs @@ -1 +1 @@ -yazi_macro::mod_flat!(buttons confirm content list); +yazi_macro::mod_flat!(buttons confirm body list); diff --git a/yazi-plugin/src/config/theme.rs b/yazi-plugin/src/config/theme.rs index 865cb7ce..f30fab37 100644 --- a/yazi-plugin/src/config/theme.rs +++ b/yazi-plugin/src/config/theme.rs @@ -158,7 +158,7 @@ impl Theme { match key { b"border" => Style::from(t.border).into_lua(lua), b"title" => Style::from(t.title).into_lua(lua), - b"content" => Style::from(t.content).into_lua(lua), + b"body" => Style::from(t.body).into_lua(lua), b"list" => Style::from(t.list).into_lua(lua), b"btn_yes" => Style::from(t.btn_yes).into_lua(lua), diff --git a/yazi-plugin/src/elements/pos.rs b/yazi-plugin/src/elements/pos.rs index e8b3fce0..4687e4b5 100644 --- a/yazi-plugin/src/elements/pos.rs +++ b/yazi-plugin/src/elements/pos.rs @@ -1,9 +1,11 @@ use std::{ops::Deref, str::FromStr}; -use mlua::{AnyUserData, ExternalResult, IntoLua, Lua, MetaMethod, Table, UserData, Value}; +use mlua::{AnyUserData, ExternalError, ExternalResult, IntoLua, Lua, MetaMethod, Table, UserData, Value}; use super::Pad; +const EXPECTED: &str = "expected a Pos"; + #[derive(Clone, Copy, Default)] pub struct Pos { inner: yazi_config::popup::Position, @@ -45,6 +47,24 @@ impl TryFrom for Pos { } } +impl TryFrom for Pos { + type Error = mlua::Error; + + fn try_from(value: Value) -> Result { + Ok(match value { + Value::Table(tbl) => Self::try_from(tbl)?, + Value::UserData(ud) => { + if let Ok(pos) = ud.borrow() { + *pos + } else { + Err(EXPECTED.into_lua_err())? + } + } + _ => Err(EXPECTED.into_lua_err())?, + }) + } +} + impl Pos { pub fn compose(lua: &Lua) -> mlua::Result { let new = lua.create_function(|_, (_, t): (Table, Table)| Self::try_from(t))?; @@ -55,8 +75,8 @@ impl Pos { position.into_lua(lua) } - pub fn new_input(t: mlua::Table) -> mlua::Result { - let mut p = Self::try_from(t)?; + pub fn new_input(v: Value) -> mlua::Result { + let mut p = Self::try_from(v)?; p.inner.offset.height = 3; Ok(p) } diff --git a/yazi-plugin/src/macros.rs b/yazi-plugin/src/macros.rs index 142e441e..9585290d 100644 --- a/yazi-plugin/src/macros.rs +++ b/yazi-plugin/src/macros.rs @@ -76,7 +76,7 @@ macro_rules! runtime_mut { #[macro_export] macro_rules! deprecate { ($lua:ident, $tt:tt) => {{ - let id = match $lua.named_registry_value::<$crate::RtRef>("ir")?.current() { + let id = match $crate::runtime!($lua)?.current() { Some(id) => &format!("`{id}.yazi` plugin"), None => "`init.lua` config", }; diff --git a/yazi-plugin/src/utils/layer.rs b/yazi-plugin/src/utils/layer.rs index 71e4e8cf..2723fb4d 100644 --- a/yazi-plugin/src/utils/layer.rs +++ b/yazi-plugin/src/utils/layer.rs @@ -9,7 +9,7 @@ use yazi_proxy::{AppProxy, ConfirmProxy, InputProxy}; use yazi_shared::{Debounce, event::Cmd}; use super::Utils; -use crate::{bindings::InputRx, elements::{Line, Pos, Text}}; +use crate::{bindings::InputRx, deprecate, elements::{Line, Pos, Text}}; impl Utils { pub(super) fn which(lua: &Lua) -> mlua::Result { @@ -40,13 +40,24 @@ impl Utils { pub(super) fn input(lua: &Lua) -> mlua::Result { lua.create_async_function(|lua, t: Table| async move { + let mut pos = t.raw_get::("pos")?; + if pos.is_nil() { + pos = t.raw_get("position")?; + if !pos.is_nil() { + deprecate!( + lua, + "The `position` property of `ya.input()` is deprecated, use `pos` instead in your {}\nSee #2921 for more details: https://github.com/sxyazi/yazi/pull/2921" + ); + } + } + let realtime = t.raw_get("realtime").unwrap_or_default(); let rx = UnboundedReceiverStream::new(InputProxy::show(InputCfg { title: t.raw_get("title")?, value: t.raw_get("value").unwrap_or_default(), cursor: None, // TODO obscure: t.raw_get("obscure").unwrap_or_default(), - position: Pos::new_input(t.raw_get::("position")?)?.into(), + position: Pos::new_input(pos)?.into(), realtime, completion: false, })); @@ -67,8 +78,8 @@ impl Utils { } pub(super) fn confirm(lua: &Lua) -> mlua::Result { - fn content(t: &Table) -> mlua::Result> { - Ok(match t.raw_get::("content")? { + fn body(t: &Table) -> mlua::Result> { + Ok(match t.raw_get::("body")? { Value::Nil => Default::default(), v => Text::try_from(v)?.into(), }) @@ -76,9 +87,9 @@ impl Utils { lua.create_async_function(|_, t: Table| async move { let result = ConfirmProxy::show(ConfirmCfg { - position: Pos::try_from(t.raw_get::
("pos")?)?.into(), + position: Pos::try_from(t.raw_get::("pos")?)?.into(), title: Line::try_from(t.raw_get::("title")?)?.into(), - content: content(&t)?, + body: body(&t)?, list: Default::default(), // TODO });