This commit is contained in:
sxyazi 2024-10-30 19:01:32 +08:00
parent a51298f0d6
commit a8bd21ad5f
No known key found for this signature in database
4 changed files with 65 additions and 1 deletions

View file

@ -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<Cmd> 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::<Value>() {
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}");
}
}
}

View file

@ -16,7 +16,7 @@ impl App {
#[yazi_codegen::command] #[yazi_codegen::command]
pub fn resize(&mut self, _: Opt) { pub fn resize(&mut self, _: Opt) {
self.cx.manager.active_mut().preview.reset(); self.cx.manager.active_mut().preview.reset();
self.render(); self.reflow(());
self.cx.manager.current_mut().sync_page(true); self.cx.manager.current_mut().sync_page(true);
self.cx.manager.hover(None); self.cx.manager.hover(None);

View file

@ -12,6 +12,12 @@ pub(super) struct Root<'a> {
impl<'a> Root<'a> { impl<'a> Root<'a> {
pub(super) fn new(cx: &'a Ctx) -> Self { Self { cx } } pub(super) fn new(cx: &'a Ctx) -> Self { Self { cx } }
pub(super) fn reflow<'lua>(area: Rect) -> mlua::Result<Table<'lua>> {
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> { impl<'a> Widget for Root<'a> {

View file

@ -131,6 +131,8 @@ function Status:position()
} }
end end
function Status:reflow() return { self } end
function Status:redraw() function Status:redraw()
local left = self:children_redraw(self.LEFT) local left = self:children_redraw(self.LEFT)