diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index f03af4da..6ad65887 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -13,5 +13,5 @@ Resolves # diff --git a/yazi-plugin/preset/components/modal.lua b/yazi-plugin/preset/components/modal.lua index 7d19f87a..7af64c98 100644 --- a/yazi-plugin/preset/components/modal.lua +++ b/yazi-plugin/preset/components/modal.lua @@ -38,7 +38,7 @@ end function Modal:children_redraw() local elements = {} for _, child in ipairs(self._children) do - elements = ya.list_merge(elements, ya.redraw_with(child[1]:new(self._area))) + elements = ya.list_merge(elements, ui.redraw(child[1]:new(self._area))) end return elements end diff --git a/yazi-plugin/preset/components/parent.lua b/yazi-plugin/preset/components/parent.lua index 34fe8bf0..9e7c0130 100644 --- a/yazi-plugin/preset/components/parent.lua +++ b/yazi-plugin/preset/components/parent.lua @@ -26,9 +26,7 @@ function Parent:redraw() } end - return { - ui.List(items):area(self._area), - } + return ui.List(items):area(self._area) end -- Mouse events diff --git a/yazi-plugin/preset/components/progress.lua b/yazi-plugin/preset/components/progress.lua index 18042460..a50ed6b6 100644 --- a/yazi-plugin/preset/components/progress.lua +++ b/yazi-plugin/preset/components/progress.lua @@ -40,9 +40,7 @@ function Progress:redraw() end local left = progress.total - progress.succ - return { - gauge - :percent(percent) - :label(ui.Span(string.format("%3d%%, %d left", percent, left)):style(th.status.progress_label)), - } + return gauge + :percent(percent) + :label(ui.Span(string.format("%3d%%, %d left", percent, left)):style(th.status.progress_label)) end diff --git a/yazi-plugin/preset/components/rail.lua b/yazi-plugin/preset/components/rail.lua index fdfdad62..ec79af70 100644 --- a/yazi-plugin/preset/components/rail.lua +++ b/yazi-plugin/preset/components/rail.lua @@ -24,7 +24,7 @@ function Rail:reflow() return {} end function Rail:redraw() local elements = self._base or {} for _, child in ipairs(self._children) do - elements = ya.list_merge(elements, ya.redraw_with(child)) + elements = ya.list_merge(elements, ui.redraw(child)) end return elements end diff --git a/yazi-plugin/preset/components/root.lua b/yazi-plugin/preset/components/root.lua index 5667dd01..f7055f3f 100644 --- a/yazi-plugin/preset/components/root.lua +++ b/yazi-plugin/preset/components/root.lua @@ -15,7 +15,7 @@ function Root:layout() :direction(ui.Layout.VERTICAL) :constraints({ ui.Constraint.Length(1), - ui.Constraint.Length(#cx.tabs > 1 and 1 or 0), + ui.Constraint.Length(Tabs.height()), ui.Constraint.Fill(1), ui.Constraint.Length(1), }) @@ -43,7 +43,7 @@ end function Root:redraw() local elements = self._base or {} for _, child in ipairs(self._children) do - elements = ya.list_merge(elements, ya.redraw_with(child)) + elements = ya.list_merge(elements, ui.redraw(child)) end return elements end diff --git a/yazi-plugin/preset/components/status.lua b/yazi-plugin/preset/components/status.lua index 8a1b3f89..4dfa2aff 100644 --- a/yazi-plugin/preset/components/status.lua +++ b/yazi-plugin/preset/components/status.lua @@ -142,7 +142,7 @@ function Status:redraw() ui.Text(""):area(self._area):style(th.status.overall), ui.Line(left):area(self._area), ui.Line(right):area(self._area):align(ui.Line.RIGHT), - table.unpack(ya.redraw_with(Progress:new(self._area, right_width))), + table.unpack(ui.redraw(Progress:new(self._area, right_width))), } end diff --git a/yazi-plugin/preset/components/tab.lua b/yazi-plugin/preset/components/tab.lua index 575aea02..6dbd1ca0 100644 --- a/yazi-plugin/preset/components/tab.lua +++ b/yazi-plugin/preset/components/tab.lua @@ -41,7 +41,7 @@ end function Tab:redraw() local elements = self._base or {} for _, child in ipairs(self._children) do - elements = ya.list_merge(elements, ya.redraw_with(child)) + elements = ya.list_merge(elements, ui.redraw(child)) end return elements end diff --git a/yazi-plugin/preset/components/tabs.lua b/yazi-plugin/preset/components/tabs.lua index 8ae7b08e..6329556e 100644 --- a/yazi-plugin/preset/components/tabs.lua +++ b/yazi-plugin/preset/components/tabs.lua @@ -38,9 +38,11 @@ function Tabs:redraw() end lines[#lines + 1] = ui.Line(th.tabs.sep_outer.close):fg(th.tabs.inactive.bg) - return { ui.Line(lines):area(self._area) } + return ui.Line(lines):area(self._area) end +function Tabs.height() return #cx.tabs > 1 and 1 or 0 end + function Tabs:inner_width() local si, so = th.tabs.sep_inner, th.tabs.sep_outer return math.max(0, self._area.w - ui.Line({ si.open, si.close, so.open, so.close }):width()) diff --git a/yazi-plugin/src/elements/elements.rs b/yazi-plugin/src/elements/elements.rs index 9b3d0a55..b2bd2e22 100644 --- a/yazi-plugin/src/elements/elements.rs +++ b/yazi-plugin/src/elements/elements.rs @@ -1,4 +1,4 @@ -use mlua::{AnyUserData, IntoLua, Lua, Table, Value}; +use mlua::{AnyUserData, IntoLua, Lua, Value}; use tracing::error; use super::Renderable; @@ -25,25 +25,35 @@ pub fn compose(lua: &Lua) -> mlua::Result { b"Text" => super::Text::compose(lua)?, b"width" => super::Utils::width(lua)?, + b"redraw" => super::Utils::redraw(lua)?, _ => return Ok(Value::Nil), } .into_lua(lua) }) } -pub fn render_once(widgets: Table, buf: &mut ratatui::buffer::Buffer, trans: F) +pub fn render_once(value: Value, buf: &mut ratatui::buffer::Buffer, trans: F) where F: Fn(yazi_config::popup::Position) -> ratatui::layout::Rect + Copy, { - for widget in widgets.sequence_values::() { - let Ok(widget) = widget else { - error!("Failed to convert to renderable UserData: {}", widget.unwrap_err()); - continue; - }; + match value { + Value::Table(tbl) => { + for widget in tbl.sequence_values::() { + let Ok(widget) = widget else { + error!("Failed to convert to renderable UserData: {}", widget.unwrap_err()); + continue; + }; - match Renderable::try_from(widget) { + match Renderable::try_from(widget) { + Ok(w) => w.render(buf, trans), + Err(e) => error!("{e}"), + } + } + } + Value::UserData(ud) => match Renderable::try_from(ud) { Ok(w) => w.render(buf, trans), Err(e) => error!("{e}"), - } + }, + _ => error!("Expected a renderable UserData, or a table of them, got: {value:?}"), } } diff --git a/yazi-plugin/src/elements/utils.rs b/yazi-plugin/src/elements/utils.rs index 27dc5beb..2e15ef0a 100644 --- a/yazi-plugin/src/elements/utils.rs +++ b/yazi-plugin/src/elements/utils.rs @@ -1,5 +1,7 @@ -use mlua::{ExternalError, IntoLua, Lua, Value}; +use mlua::{ExternalError, IntoLua, Lua, ObjectLike, Table, Value}; +use tracing::error; use unicode_width::UnicodeWidthStr; +use yazi_config::LAYOUT; use super::{Line, Span}; @@ -32,4 +34,33 @@ impl Utils { f.into_lua(lua) } + + pub(super) fn redraw(lua: &Lua) -> mlua::Result { + let f = lua.create_function(|lua, c: Table| { + let id: mlua::String = c.get("_id")?; + + let mut layout = LAYOUT.get(); + match id.as_bytes().as_ref() { + b"current" => layout.current = *c.raw_get::("_area")?, + b"preview" => layout.preview = *c.raw_get::("_area")?, + b"progress" => layout.progress = *c.raw_get::("_area")?, + _ => {} + } + + LAYOUT.set(layout); + match c.call_method::("redraw", ())? { + Value::Table(tbl) => Ok(tbl), + Value::UserData(ud) => lua.create_sequence_from([ud]), + _ => { + error!( + "Failed to `redraw()` the `{}` component: expected a table or UserData", + id.display(), + ); + lua.create_table() + } + } + })?; + + f.into_lua(lua) + } } diff --git a/yazi-plugin/src/utils/call.rs b/yazi-plugin/src/utils/call.rs index 8afdd93a..4712c95f 100644 --- a/yazi-plugin/src/utils/call.rs +++ b/yazi-plugin/src/utils/call.rs @@ -1,6 +1,4 @@ -use mlua::{Function, Lua, ObjectLike, Table}; -use tracing::error; -use yazi_config::LAYOUT; +use mlua::{Function, Lua, Table}; use yazi_dds::Sendable; use yazi_macro::{emit, render}; use yazi_shared::{Layer, event::Cmd}; @@ -15,29 +13,6 @@ impl Utils { }) } - pub(super) fn redraw_with(lua: &Lua) -> mlua::Result { - lua.create_function(|lua, c: Table| { - let id: mlua::String = c.get("_id")?; - - let mut layout = LAYOUT.get(); - match id.as_bytes().as_ref() { - b"current" => layout.current = *c.raw_get::("_area")?, - b"preview" => layout.preview = *c.raw_get::("_area")?, - b"progress" => layout.progress = *c.raw_get::("_area")?, - _ => {} - } - - LAYOUT.set(layout); - match c.call_method::("redraw", ()) { - Err(e) => { - error!("Failed to `redraw()` the `{}` component:\n{e}", id.display()); - lua.create_table() - } - ok => ok, - } - }) - } - pub(super) fn emit(lua: &Lua) -> mlua::Result { lua.create_function(|_, (name, args): (mlua::String, Table)| { let mut cmd = Cmd::new_or(&name.to_str()?, Layer::Mgr)?; diff --git a/yazi-plugin/src/utils/utils.rs b/yazi-plugin/src/utils/utils.rs index 2f0984b3..44b139ea 100644 --- a/yazi-plugin/src/utils/utils.rs +++ b/yazi-plugin/src/utils/utils.rs @@ -16,7 +16,6 @@ pub fn compose(lua: &Lua, isolate: bool) -> mlua::Result { // Call b"render" => Utils::render(lua)?, - b"redraw_with" => Utils::redraw_with(lua)?, b"emit" => Utils::emit(lua)?, b"app_emit" => Utils::app_emit(lua)?, b"mgr_emit" => Utils::mgr_emit(lua)?,