feat: custom modal component API (#2205)

This commit is contained in:
三咲雅 · Misaki Masa 2025-01-15 21:46:39 +08:00 committed by GitHub
parent 6a5fa714ae
commit 63eb82aa72
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 51 additions and 17 deletions

View file

@ -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

View file

@ -1,6 +1,5 @@
Rail = {
_id = "rail",
_area = ui.Rect.default,
}
function Rail:new(chunks, tab)
@ -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 {}

View file

@ -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
@ -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

View file

@ -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

View file

@ -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()?;