From a8bd21ad5fadce0a9c718fb25609e1e34f2e3d06 Mon Sep 17 00:00:00 2001 From: sxyazi Date: Wed, 30 Oct 2024 19:01:32 +0800 Subject: [PATCH] .. --- yazi-fm/src/app/commands/reflow.rs | 56 ++++++++++++++++++++++++ yazi-fm/src/app/commands/resize.rs | 2 +- yazi-fm/src/root.rs | 6 +++ yazi-plugin/preset/components/status.lua | 2 + 4 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 yazi-fm/src/app/commands/reflow.rs diff --git a/yazi-fm/src/app/commands/reflow.rs b/yazi-fm/src/app/commands/reflow.rs new file mode 100644 index 00000000..bb18acef --- /dev/null +++ b/yazi-fm/src/app/commands/reflow.rs @@ -0,0 +1,56 @@ +use std::sync::Arc; + +use mlua::Value; +use ratatui::layout::Position; +use tracing::error; +use yazi_config::LAYOUT; +use yazi_macro::render; +use yazi_shared::event::Cmd; + +use crate::{Root, app::App, lives::Lives}; + +struct Opt; + +impl From for Opt { + fn from(_: Cmd) -> Self { Self } +} + +impl From<()> for Opt { + fn from(_: ()) -> Self { Self } +} + +impl App { + #[yazi_codegen::command] + pub fn reflow(&mut self, _: Opt) { + let Some(size) = self.term.as_ref().and_then(|t| t.size().ok()) else { return }; + let mut layout = *LAYOUT.load_full(); + + let result = Lives::scope(&self.cx, |_| { + let comps = Root::reflow((Position::ORIGIN, size).into())?; + + for v in comps.sequence_values::() { + let Value::Table(t) = v? else { + error!("`reflow()` must return a table of components"); + continue; + }; + + let id: mlua::String = t.get("_id")?; + match id.to_str()? { + "current" => layout.current = *t.raw_get::<_, yazi_plugin::elements::Rect>("_area")?, + "preview" => layout.preview = *t.raw_get::<_, yazi_plugin::elements::Rect>("_area")?, + _ => {} + } + } + Ok(()) + }); + + if layout != *LAYOUT.load_full() { + LAYOUT.store(Arc::new(layout)); + render!(); + } + + if let Err(e) = result { + error!("Failed to `reflow()` the `Root` component:\n{e}"); + } + } +} diff --git a/yazi-fm/src/app/commands/resize.rs b/yazi-fm/src/app/commands/resize.rs index a8fbe0ad..250226c7 100644 --- a/yazi-fm/src/app/commands/resize.rs +++ b/yazi-fm/src/app/commands/resize.rs @@ -16,7 +16,7 @@ impl App { #[yazi_codegen::command] pub fn resize(&mut self, _: Opt) { self.cx.manager.active_mut().preview.reset(); - self.render(); + self.reflow(()); self.cx.manager.current_mut().sync_page(true); self.cx.manager.hover(None); diff --git a/yazi-fm/src/root.rs b/yazi-fm/src/root.rs index ee26c465..d667cc3a 100644 --- a/yazi-fm/src/root.rs +++ b/yazi-fm/src/root.rs @@ -12,6 +12,12 @@ pub(super) struct Root<'a> { impl<'a> Root<'a> { pub(super) fn new(cx: &'a Ctx) -> Self { Self { cx } } + + pub(super) fn reflow<'lua>(area: Rect) -> mlua::Result> { + let area = yazi_plugin::elements::Rect::from(area); + let root = LUA.globals().raw_get::<_, Table>("Root")?.call_method::<_, Table>("new", area)?; + root.call_method("reflow", ()) + } } impl<'a> Widget for Root<'a> { diff --git a/yazi-plugin/preset/components/status.lua b/yazi-plugin/preset/components/status.lua index 570faf68..e1b4cfa6 100644 --- a/yazi-plugin/preset/components/status.lua +++ b/yazi-plugin/preset/components/status.lua @@ -131,6 +131,8 @@ function Status:position() } end +function Status:reflow() return { self } end + function Status:redraw() local left = self:children_redraw(self.LEFT)