Skip to content

Commit

Permalink
Add iterator function
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfgang-ironsoftware committed Apr 16, 2024
1 parent 2c6e49a commit 7de09ea
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
11 changes: 11 additions & 0 deletions composer/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,15 @@ composer.require = Loader.require
composer.unrequire = Loader.unrequire
composer.load = Loader.load

local layout = require(PATH .. '.layout')
composer.VStack = layout.VStack
composer.HStack = layout.HStack
composer.Border = layout.Border
composer.Elem = layout.Elem

local attr = require(PATH .. '.attributes')
composer.Margin = attr.Margin
composer.MinSize = attr.MinSize
composer.Stretch = attr.Stretch

return composer
28 changes: 26 additions & 2 deletions composer/layout.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ function Rect:__tostring()
return F.describe("Rect", self)
end

function Rect:unpack()
return self.x, self.y, self.w, self.h
end

setmetatable(Rect, {
__call = Rect.new
})
Expand Down Expand Up @@ -63,7 +67,7 @@ function Layout:new(...)
}, Layout)
end

function Layout:reshape(x, y, w, h)
function Layout:setFrame(x, y, w, h)
self:expand()
self:layout(Rect(x, y, w, h))
end
Expand Down Expand Up @@ -95,6 +99,26 @@ function Layout:__tostring()
return F.describe("Layout", self)
end

function Layout:eachElement()
return coroutine.wrap(
function()
for _, child in ipairs(self.children) do
-- TODO: nicer to do with a type check (getmetatable)
if child.widget then
coroutine.yield(child)
else
local iter = child:eachElement()
local v = iter()
while v ~= nil do
coroutine.yield(v)
v = iter()
end
end
end
end
)
end

setmetatable(Layout, {
__call = Layout.new,
})
Expand Down Expand Up @@ -296,7 +320,7 @@ function Elem:new(widget, ...)
this.id = id
this.rect = Rect(0, 0, 0, 0)
this.widget = widget

return setmetatable(this, Elem)
end

Expand Down

0 comments on commit 7de09ea

Please sign in to comment.