Skip to content

Commit

Permalink
feat: custom modal component API (#2205)
Browse files Browse the repository at this point in the history
  • Loading branch information
sxyazi authored Jan 15, 2025
1 parent 6a5fa71 commit 63eb82a
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 17 deletions.
42 changes: 42 additions & 0 deletions yazi-plugin/preset/components/modal.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
Modal = {
_id = "modal",

_inc = 1000,
_children = {},
}

function Modal:new(area) return setmetatable({ _area = area }, { __index = self }) end

function Modal:reflow()
local components = {}
for _, child in ipairs(self._children) do
components = ya.list_merge(components, child[1]:new(self._area):reflow())
end
return components
end

function Modal:redraw()
local elements = {}
for _, child in ipairs(self._children) do
elements = ya.list_merge(elements, ya.redraw_with(child[1]:new(self._area)))
end
return elements
end

-- Children
function Modal:children_add(tbl, order)
self._inc = self._inc + 1
self._children[#self._children + 1] = { tbl, id = self._inc, order = order }

table.sort(self._children, function(a, b) return a.order < b.order end)
return self._inc
end

function Modal:children_remove(id)
for i, child in ipairs(self._children) do
if child.id == id then
table.remove(self._children, i)
break
end
end
end
3 changes: 1 addition & 2 deletions yazi-plugin/preset/components/rail.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
Rail = {
_id = "rail",
_area = ui.Rect.default,
}

function Rail:new(chunks, tab)
Expand All @@ -20,7 +19,7 @@ function Rail:build()
}
end

function Rail:reflow() return { self } end
function Rail:reflow() return {} end

function Rail:redraw()
local elements = self._base or {}
Expand Down
7 changes: 4 additions & 3 deletions yazi-plugin/preset/components/root.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ function Root:build()
Header:new(self._chunks[1], cx.active),
Tab:new(self._chunks[2], cx.active),
Status:new(self._chunks[3], cx.active),
Modal:new(self._area),
}
end

Expand All @@ -47,17 +48,17 @@ end

-- Mouse events
function Root:click(event, up)
local c = ya.child_at(ui.Rect { x = event.x, y = event.y }, self._children)
local c = ya.child_at(ui.Rect { x = event.x, y = event.y }, self:reflow())
return c and c:click(event, up)
end

function Root:scroll(event, step)
local c = ya.child_at(ui.Rect { x = event.x, y = event.y }, self._children)
local c = ya.child_at(ui.Rect { x = event.x, y = event.y }, self:reflow())
return c and c:scroll(event, step)
end

function Root:touch(event, step)
local c = ya.child_at(ui.Rect { x = event.x, y = event.y }, self._children)
local c = ya.child_at(ui.Rect { x = event.x, y = event.y }, self:reflow())
return c and c:touch(event, step)
end

Expand Down
15 changes: 3 additions & 12 deletions yazi-plugin/preset/components/tab.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,8 @@ function Tab:redraw()
end

-- Mouse events
function Tab:click(event, up)
local c = ya.child_at(ui.Rect { x = event.x, y = event.y }, self._children)
return c and c:click(event, up)
end
function Tab:click(event, up) end

function Tab:scroll(event, step)
local c = ya.child_at(ui.Rect { x = event.x, y = event.y }, self._children)
return c and c:scroll(event, step)
end
function Tab:scroll(event, step) end

function Tab:touch(event, step)
local c = ya.child_at(ui.Rect { x = event.x, y = event.y }, self._children)
return c and c:touch(event, step)
end
function Tab:touch(event, step) end
1 change: 1 addition & 0 deletions yazi-plugin/src/lua.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ fn stage_1(lua: &'static Lua) -> Result<()> {
lua.load(preset!("components/linemode")).set_name("linemode.lua").exec()?;

lua.load(preset!("components/marker")).set_name("marker.lua").exec()?;
lua.load(preset!("components/modal")).set_name("modal.lua").exec()?;
lua.load(preset!("components/parent")).set_name("parent.lua").exec()?;
lua.load(preset!("components/preview")).set_name("preview.lua").exec()?;
lua.load(preset!("components/progress")).set_name("progress.lua").exec()?;
Expand Down

0 comments on commit 63eb82a

Please sign in to comment.