From 835a213379b37aa3e8677048bb05c4d9d7471e8e Mon Sep 17 00:00:00 2001 From: sxyazi Date: Wed, 22 Apr 2026 17:52:00 +0800 Subject: [PATCH] Redesign --- yazi-actor/src/app/mouse.rs | 12 +---- yazi-binding/src/elements/pad.rs | 4 +- yazi-binding/src/elements/pos.rs | 4 +- yazi-binding/src/elements/rect.rs | 4 +- yazi-binding/src/id.rs | 4 +- yazi-binding/src/image.rs | 4 +- yazi-binding/src/process/output.rs | 4 +- yazi-binding/src/process/status.rs | 4 +- yazi-config/src/mgr/mouse.rs | 4 -- yazi-plugin/preset/components/current.lua | 4 ++ yazi-plugin/preset/components/parent.lua | 4 ++ yazi-plugin/preset/components/preview.lua | 4 ++ yazi-plugin/preset/components/rail.lua | 56 +++++++++++++---------- yazi-plugin/preset/components/rails.lua | 48 +++++++++++++++++++ yazi-plugin/preset/components/root.lua | 48 +++---------------- yazi-plugin/preset/components/tab.lua | 9 ++-- yazi-plugin/src/standard.rs | 1 + yazi-runner/src/loader/loader.rs | 1 + yazi-widgets/src/clear.rs | 4 +- 19 files changed, 123 insertions(+), 100 deletions(-) create mode 100644 yazi-plugin/preset/components/rails.lua diff --git a/yazi-actor/src/app/mouse.rs b/yazi-actor/src/app/mouse.rs index dc935fa4..cc49e45d 100644 --- a/yazi-actor/src/app/mouse.rs +++ b/yazi-actor/src/app/mouse.rs @@ -4,8 +4,7 @@ use mlua::{ObjectLike, Table}; use tracing::error; use yazi_actor::lives::Lives; use yazi_binding::runtime_scope; -use yazi_config::YAZI; -use yazi_macro::{render, succ}; +use yazi_macro::succ; use yazi_parser::app::MouseForm; use yazi_plugin::LUA; use yazi_shared::data::Data; @@ -30,10 +29,6 @@ impl Actor for Mouse { LUA.globals().raw_get::("Root")?.call_method::
("new", area) })?; - if matches!(event.kind, MouseEventKind::Down(_) if YAZI.mgr.mouse_events.get().draggable()) { - root.raw_set("_drag_start", event)?; - } - match event.kind { MouseEventKind::Down(_) => root.call_method("click", (event, false))?, MouseEventKind::Up(_) => root.call_method("click", (event, true))?, @@ -45,10 +40,7 @@ impl Actor for Mouse { MouseEventKind::ScrollLeft => root.call_method("touch", (event, -1))?, MouseEventKind::Moved => root.call_method("move", event)?, - MouseEventKind::Drag(_) => { - root.call_method::<()>("drag", event)?; - render!(); - } + MouseEventKind::Drag(_) => root.call_method("drag", event)?, } Ok(()) diff --git a/yazi-binding/src/elements/pad.rs b/yazi-binding/src/elements/pad.rs index 97c7ecba..c7eb25b4 100644 --- a/yazi-binding/src/elements/pad.rs +++ b/yazi-binding/src/elements/pad.rs @@ -1,6 +1,6 @@ use std::ops::{Add, AddAssign, Deref}; -use mlua::{FromLua, IntoLua, Lua, MetaMethod, Table, UserData, Value}; +use mlua::{FromLua, IntoLua, Lua, MetaMethod, Table, UserData, UserDataFields, Value}; #[derive(Clone, Copy, Default, FromLua)] pub struct Pad(ratatui::widgets::Padding); @@ -52,7 +52,7 @@ impl Pad { } impl UserData for Pad { - fn add_fields>(fields: &mut F) { + fn add_fields>(fields: &mut F) { fields.add_field_method_get("left", |_, me| Ok(me.left)); fields.add_field_method_get("right", |_, me| Ok(me.right)); fields.add_field_method_get("top", |_, me| Ok(me.top)); diff --git a/yazi-binding/src/elements/pos.rs b/yazi-binding/src/elements/pos.rs index 4e701c4c..d509d945 100644 --- a/yazi-binding/src/elements/pos.rs +++ b/yazi-binding/src/elements/pos.rs @@ -1,6 +1,6 @@ use std::{ops::Deref, str::FromStr}; -use mlua::{AnyUserData, ExternalError, ExternalResult, FromLua, IntoLua, Lua, MetaMethod, Table, UserData, UserDataMethods, Value}; +use mlua::{AnyUserData, ExternalError, ExternalResult, FromLua, IntoLua, Lua, MetaMethod, Table, UserData, UserDataFields, UserDataMethods, Value}; use yazi_shim::strum::IntoStr; use super::Pad; @@ -81,7 +81,7 @@ impl Pos { } impl UserData for Pos { - fn add_fields>(fields: &mut F) { + fn add_fields>(fields: &mut F) { // TODO: cache fields.add_field_method_get("1", |_, me| Ok(me.origin.into_str())); fields.add_field_method_get("x", |_, me| Ok(me.offset.x)); diff --git a/yazi-binding/src/elements/rect.rs b/yazi-binding/src/elements/rect.rs index 0a280c8d..f1d2e64d 100644 --- a/yazi-binding/src/elements/rect.rs +++ b/yazi-binding/src/elements/rect.rs @@ -1,6 +1,6 @@ use std::ops::Deref; -use mlua::{FromLua, IntoLua, Lua, MetaMethod, Table, UserData, UserDataMethods, Value}; +use mlua::{FromLua, IntoLua, Lua, MetaMethod, Table, UserData, UserDataFields, UserDataMethods, Value}; use super::Pad; @@ -51,7 +51,7 @@ impl Rect { } impl UserData for Rect { - fn add_fields>(fields: &mut F) { + fn add_fields>(fields: &mut F) { fields.add_field_method_get("x", |_, me| Ok(me.x)); fields.add_field_method_get("y", |_, me| Ok(me.y)); fields.add_field_method_get("w", |_, me| Ok(me.width)); diff --git a/yazi-binding/src/id.rs b/yazi-binding/src/id.rs index 1b38b478..ca608ef8 100644 --- a/yazi-binding/src/id.rs +++ b/yazi-binding/src/id.rs @@ -1,6 +1,6 @@ use std::ops::Deref; -use mlua::{ExternalError, ExternalResult, FromLua, Lua, UserData, Value}; +use mlua::{ExternalError, ExternalResult, FromLua, Lua, UserData, UserDataFields, Value}; #[derive(Clone, Copy, Default)] pub struct Id(pub yazi_shared::Id); @@ -22,7 +22,7 @@ impl FromLua for Id { } impl UserData for Id { - fn add_fields>(fields: &mut F) { + fn add_fields>(fields: &mut F) { fields.add_field_method_get("value", |_, me| Ok(me.0.get())); } } diff --git a/yazi-binding/src/image.rs b/yazi-binding/src/image.rs index ff21c70b..4a5bdfa9 100644 --- a/yazi-binding/src/image.rs +++ b/yazi-binding/src/image.rs @@ -1,6 +1,6 @@ use std::ops::Deref; -use mlua::{MetaMethod, UserData, UserDataMethods}; +use mlua::{MetaMethod, UserData, UserDataFields, UserDataMethods}; pub struct ImageInfo(yazi_adapter::ImageInfo); @@ -15,7 +15,7 @@ impl From for ImageInfo { } impl UserData for ImageInfo { - fn add_fields>(fields: &mut F) { + fn add_fields>(fields: &mut F) { fields.add_field_method_get("w", |_, me| Ok(me.width)); fields.add_field_method_get("h", |_, me| Ok(me.height)); fields.add_field_method_get("ori", |_, me| Ok(me.orientation.map(|o| o.to_exif()))); diff --git a/yazi-binding/src/process/output.rs b/yazi-binding/src/process/output.rs index f0892486..97508849 100644 --- a/yazi-binding/src/process/output.rs +++ b/yazi-binding/src/process/output.rs @@ -1,6 +1,6 @@ use std::mem; -use mlua::{UserData, Value}; +use mlua::{UserData, UserDataFields, Value}; use super::Status; use crate::{cached_field, cached_field_mut}; @@ -20,7 +20,7 @@ impl Output { } impl UserData for Output { - fn add_fields>(fields: &mut F) { + fn add_fields>(fields: &mut F) { cached_field!(fields, status, |_, me| Ok(Status::new(me.inner.status))); cached_field_mut!(fields, stdout, |lua, me| { lua.create_external_string(mem::take(&mut me.inner.stdout)) diff --git a/yazi-binding/src/process/status.rs b/yazi-binding/src/process/status.rs index a81025e0..7a47024f 100644 --- a/yazi-binding/src/process/status.rs +++ b/yazi-binding/src/process/status.rs @@ -1,4 +1,4 @@ -use mlua::UserData; +use mlua::{UserData, UserDataFields}; pub struct Status { inner: std::process::ExitStatus, @@ -9,7 +9,7 @@ impl Status { } impl UserData for Status { - fn add_fields>(fields: &mut F) { + fn add_fields>(fields: &mut F) { fields.add_field_method_get("success", |_, me| Ok(me.inner.success())); fields.add_field_method_get("code", |_, me| Ok(me.inner.code())); } diff --git a/yazi-config/src/mgr/mouse.rs b/yazi-config/src/mgr/mouse.rs index b1d68b7f..218d4967 100644 --- a/yazi-config/src/mgr/mouse.rs +++ b/yazi-config/src/mgr/mouse.rs @@ -15,10 +15,6 @@ bitflags! { } } -impl MouseEvents { - pub const fn draggable(self) -> bool { self.contains(Self::DRAG) } -} - impl TryFrom> for MouseEvents { type Error = anyhow::Error; diff --git a/yazi-plugin/preset/components/current.lua b/yazi-plugin/preset/components/current.lua index d8364d90..74f73fe4 100644 --- a/yazi-plugin/preset/components/current.lua +++ b/yazi-plugin/preset/components/current.lua @@ -49,6 +49,10 @@ end -- Mouse events function Current:click(event, up) + if up or event.is_middle then + return + end + local y = event.y - self._area.y + 1 if self._folder.window[y] then Entity:new(self._folder.window[y]):click(event, up) diff --git a/yazi-plugin/preset/components/parent.lua b/yazi-plugin/preset/components/parent.lua index 3b0e0a0d..d96524d3 100644 --- a/yazi-plugin/preset/components/parent.lua +++ b/yazi-plugin/preset/components/parent.lua @@ -34,6 +34,10 @@ end -- Mouse events function Parent:click(event, up) + if up or event.is_middle then + return + end + local y = event.y - self._area.y + 1 local window = self._folder and self._folder.window or {} if window[y] then diff --git a/yazi-plugin/preset/components/preview.lua b/yazi-plugin/preset/components/preview.lua index 25caeb62..bbff77b8 100644 --- a/yazi-plugin/preset/components/preview.lua +++ b/yazi-plugin/preset/components/preview.lua @@ -16,6 +16,10 @@ function Preview:redraw() return {} end -- Mouse events function Preview:click(event, up) + if up or event.is_middle then + return + end + local y = event.y - self._area.y + 1 local window = self._folder and self._folder.window or {} if window[y] then diff --git a/yazi-plugin/preset/components/rail.lua b/yazi-plugin/preset/components/rail.lua index d932de45..137b9b08 100644 --- a/yazi-plugin/preset/components/rail.lua +++ b/yazi-plugin/preset/components/rail.lua @@ -1,37 +1,45 @@ -Rail = { - _id = "rail", -} +Rail = {} -function Rail:new(chunks, tab) - local me = setmetatable({ _chunks = chunks, _tab = tab }, { __index = self }) - me:build() - return me +function Rail:new(id, area, chunks) + return setmetatable({ + _id = id, + _area = area, + _chunks = chunks, + }, { __index = self }) end -function Rail:build() - self._base = { - ui.Bar(ui.Edge.RIGHT):area(self._chunks[1]):symbol(th.mgr.border_symbol):style(th.mgr.border_style), - ui.Bar(ui.Edge.LEFT):area(self._chunks[3]):symbol(th.mgr.border_symbol):style(th.mgr.border_style), - } - self._children = { - Marker:new(self._chunks[1], self._tab.parent), - Marker:new(self._chunks[2], self._tab.current), - } -end - -function Rail:reflow() return {} end +function Rail:reflow() return { self } end function Rail:redraw() - local elements = self._base or {} - for _, child in ipairs(self._children) do - elements = ya.list_merge(elements, ui.redraw(child)) - end - return elements + return { + ui.Bar(ui.Edge.LEFT):area(self._area):symbol(th.mgr.border_symbol):style(th.mgr.border_style), + } end -- Mouse events function Rail:click(event, up) end +function Rail:drag(event) + local c, parent, current, preview = self._chunks, 0, 0, 0 + if self._id == "rail-left" then + local x = math.min(event.x, c[2].right - 2) + parent = math.max(1, x - c[1].x) + current = math.max(1, c[1].w + c[2].w - parent) + preview = math.max(1, c[3].w) + else + local x = math.max(event.x, c[2].x + 2) + preview = math.max(1, c[3].right - x) + current = math.max(1, c[2].w + c[3].w - preview) + parent = math.max(1, c[1].w) + end + + local r = rt.mgr.ratio + if r.parent ~= parent or r.current ~= current or r.preview ~= preview then + rt.mgr.ratio = { parent, current, preview } + ui.render() + end +end + function Rail:scroll(event, step) end function Rail:touch(event, step) end diff --git a/yazi-plugin/preset/components/rails.lua b/yazi-plugin/preset/components/rails.lua new file mode 100644 index 00000000..da351271 --- /dev/null +++ b/yazi-plugin/preset/components/rails.lua @@ -0,0 +1,48 @@ +Rails = { + _id = "rails", +} + +function Rails:new(chunks, tab) + local me = setmetatable({ _chunks = chunks, _tab = tab }, { __index = self }) + me:build() + return me +end + +function Rails:build() + local c, children = self._chunks, {} + if c[1].w > 0 then + children[#children + 1] = + Rail:new("rail-left", ui.Rect { x = c[2].x, y = c[2].y, w = math.min(1, c[2].w), h = c[2].h }, c) + end + if c[3].w > 0 then + children[#children + 1] = Rail:new( + "rail-right", + ui.Rect { x = math.max(0, c[2].right - 1), y = c[2].y, w = math.min(1, c[2].w), h = c[2].h }, + c + ) + end + self._children = children +end + +function Rails:reflow() + local components = {} + for _, child in ipairs(self._children) do + components = ya.list_merge(components, child:reflow()) + end + return components +end + +function Rails:redraw() + local elements = {} + for _, child in ipairs(self._children) do + elements = ya.list_merge(elements, ui.redraw(child)) + end + return elements +end + +-- Mouse events +function Rails:click(event, up) end + +function Rails:scroll(event, step) end + +function Rails:touch(event, step) end diff --git a/yazi-plugin/preset/components/root.lua b/yazi-plugin/preset/components/root.lua index 621c8b45..e4fb0a97 100644 --- a/yazi-plugin/preset/components/root.lua +++ b/yazi-plugin/preset/components/root.lua @@ -1,7 +1,6 @@ Root = { _id = "root", - _drag_start = ui.Rect {}, - _drag_which = nil, -- "left" or "right" when dragging a panel separator + _dragging = nil, } function Root:new(area) @@ -55,26 +54,9 @@ function Root:click(event, up) return end - if up then - Root._drag_which = nil - elseif event.is_left then - -- Check if clicking on a panel separator (Tab is self._children[3]) - local tab = self._children[3] - if tab and tab._chunks then - local c = tab._chunks - local lx = c[2].x - 1 -- left separator center x - local rx = c[3].x -- right separator center x - if c[1].w > 0 and event.x >= lx - 1 and event.x <= lx + 1 then - Root._drag_which = "left" - return - elseif c[3].w > 0 and event.x >= rx - 1 and event.x <= rx + 1 then - Root._drag_which = "right" - return - end - end - end + local c = Root._dragging or ya.child_at(ui.Rect { x = event.x, y = event.y }, self:reflow()) + Root._dragging = not up and c or nil - local c = ya.child_at(ui.Rect { x = event.x, y = event.y }, self:reflow()) return c and c:click(event, up) end @@ -97,28 +79,10 @@ end function Root:move(event) end function Root:drag(event) - if not Root._drag_which then + if tostring(cx.layer) ~= "mgr" then return end - local tab = self._children[3] - if not tab or not tab._chunks then - return - end - - local c = tab._chunks - local ratio = rt.mgr.ratio - - if Root._drag_which == "left" then - -- Dragging the left separator: adjust parent and current widths - local new_parent = math.max(1, event.x - c[1].x + 1) - local new_current = math.max(1, c[1].w + c[2].w - new_parent) - rt.mgr.ratio = { new_parent, new_current, c[3].w > 0 and c[3].w or 1 } - elseif Root._drag_which == "right" then - -- Dragging the right separator: adjust current and preview widths - local right_edge = c[3].x + c[3].w - 1 - local new_preview = math.max(1, right_edge - event.x) - local new_current = math.max(1, c[2].w + c[3].w - new_preview) - rt.mgr.ratio = { c[1].w > 0 and c[1].w or 1, new_current, new_preview } - end + local c = Root._dragging + return c and c.drag and c:drag(event) end diff --git a/yazi-plugin/preset/components/tab.lua b/yazi-plugin/preset/components/tab.lua index 58d06747..bea19d3b 100644 --- a/yazi-plugin/preset/components/tab.lua +++ b/yazi-plugin/preset/components/tab.lua @@ -23,11 +23,12 @@ end function Tab:build() local c = self._chunks + local p = c[2].w > 0 and 0 or 1 self._children = { - Parent:new(c[1]:pad(ui.Pad.x(1)), self._tab), - Current:new(c[2]:pad(ui.Pad(0, c[3].w > 0 and 0 or 1, 0, c[1].w > 0 and 0 or 1)), self._tab), - Preview:new(c[3]:pad(ui.Pad.x(1)), self._tab), - Rail:new(c, self._tab), + Parent:new(c[1]:pad(ui.Pad(0, p, 0, 1)), self._tab), + Current:new(c[2]:pad(ui.Pad.x(1)), self._tab), + Preview:new(c[3]:pad(ui.Pad(0, 1, 0, p)), self._tab), + Rails:new(c, self._tab), } end diff --git a/yazi-plugin/src/standard.rs b/yazi-plugin/src/standard.rs index dac2cc76..11c890a7 100644 --- a/yazi-plugin/src/standard.rs +++ b/yazi-plugin/src/standard.rs @@ -52,6 +52,7 @@ fn stage_1(lua: &Lua) -> Result<()> { lua.load(preset!("components/preview")).set_name("preview.lua").exec()?; lua.load(preset!("components/progress")).set_name("progress.lua").exec()?; lua.load(preset!("components/rail")).set_name("rail.lua").exec()?; + lua.load(preset!("components/rails")).set_name("rails.lua").exec()?; lua.load(preset!("components/root")).set_name("root.lua").exec()?; lua.load(preset!("components/status")).set_name("status.lua").exec()?; lua.load(preset!("components/tab")).set_name("tab.lua").exec()?; diff --git a/yazi-runner/src/loader/loader.rs b/yazi-runner/src/loader/loader.rs index e1726530..e5eb025b 100644 --- a/yazi-runner/src/loader/loader.rs +++ b/yazi-runner/src/loader/loader.rs @@ -65,6 +65,7 @@ impl Default for Loader { ("preview".to_owned(), [][..].into()), ("progress".to_owned(), [][..].into()), ("rail".to_owned(), [][..].into()), + ("rails".to_owned(), [][..].into()), ("root".to_owned(), [][..].into()), ("status".to_owned(), [][..].into()), ("tab".to_owned(), [][..].into()), diff --git a/yazi-widgets/src/clear.rs b/yazi-widgets/src/clear.rs index 805706ca..2b615fe5 100644 --- a/yazi-widgets/src/clear.rs +++ b/yazi-widgets/src/clear.rs @@ -30,7 +30,7 @@ impl Widget for Clear { } const fn is_overlapping(a: Rect, b: Rect) -> bool { - a.x < b.x + b.width && a.x + a.width > b.x && a.y < b.y + b.height && a.y + a.height > b.y + a.x < b.right() && a.right() > b.x && a.y < b.y + b.height && a.y + a.height > b.y } fn overlap(a: Rect, b: Rect) -> Option { @@ -40,7 +40,7 @@ fn overlap(a: Rect, b: Rect) -> Option { let x = a.x.max(b.x); let y = a.y.max(b.y); - let width = (a.x + a.width).min(b.x + b.width) - x; + let width = a.right().min(b.right()) - x; let height = (a.y + a.height).min(b.y + b.height) - y; Some(Rect { x, y, width, height }) }