Skip to content

Commit 82d8d6c

Browse files
committed
- Added Layout System
- Added Grid Layout - Removed Flexbox this will make it easy to apply layouts on all container types.
1 parent 278ad08 commit 82d8d6c

6 files changed

Lines changed: 191 additions & 837 deletions

File tree

layouts/grid.lua

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
local grid = {}
2+
3+
--- Calculates positions for all children in a grid layout
4+
--- @param instance table The layout instance
5+
--- - container: the container to layout
6+
--- - options: layout options
7+
--- - rows: number of rows (optional, auto-calculated if not provided)
8+
--- - columns: number of columns (optional, auto-calculated if not provided)
9+
--- - spacing: gap between cells (default: 0)
10+
--- - padding: padding around the grid (default: 0)
11+
function grid.calculate(instance)
12+
local container = instance.container
13+
local options = instance.options or {}
14+
15+
local children = container.get("children")
16+
local containerWidth = container.get("width")
17+
local containerHeight = container.get("height")
18+
19+
local spacing = options.spacing or 0
20+
local padding = options.padding or 0
21+
local rows = options.rows
22+
local columns = options.columns
23+
24+
local childCount = #children
25+
if not rows and not columns then
26+
columns = math.ceil(math.sqrt(childCount))
27+
rows = math.ceil(childCount / columns)
28+
elseif rows and not columns then
29+
columns = math.ceil(childCount / rows)
30+
elseif columns and not rows then
31+
rows = math.ceil(childCount / columns)
32+
end
33+
34+
local availableWidth = containerWidth - (2 * padding) - ((columns - 1) * spacing)
35+
local availableHeight = containerHeight - (2 * padding) - ((rows - 1) * spacing)
36+
local cellWidth = math.floor(availableWidth / columns)
37+
local cellHeight = math.floor(availableHeight / rows)
38+
39+
local positions = {}
40+
41+
for i, child in ipairs(children) do
42+
local row = math.floor((i - 1) / columns)
43+
local col = (i - 1) % columns
44+
45+
local x = padding + 1 + (col * (cellWidth + spacing))
46+
local y = padding + 1 + (row * (cellHeight + spacing))
47+
48+
positions[child] = {
49+
x = x,
50+
y = y,
51+
width = cellWidth,
52+
height = cellHeight
53+
}
54+
end
55+
56+
instance._positions = positions
57+
end
58+
59+
return grid

src/elements/Container.lua

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,9 @@ end
205205
function Container:sortChildren()
206206
self.set("visibleChildren", sortAndFilterChildren(self, self._values.children))
207207
self.set("childrenSorted", true)
208+
if self._layoutInstance then
209+
self:updateLayout()
210+
end
208211
return self
209212
end
210213

@@ -685,6 +688,49 @@ function Container:render()
685688
end
686689
end
687690

691+
--- Applies a layout from a file to this container
692+
--- @shortDescription Applies a layout to the container
693+
--- @param layoutPath string Path to the layout file (e.g. "layouts/grid")
694+
--- @param options? table Optional layout-specific options
695+
--- @return Container self For method chaining
696+
function Container:applyLayout(layoutPath, options)
697+
local LayoutManager = require("layoutManager")
698+
699+
if self._layoutInstance then
700+
LayoutManager.destroy(self._layoutInstance)
701+
end
702+
703+
self._layoutInstance = LayoutManager.apply(self, layoutPath)
704+
if options then
705+
self._layoutInstance.options = options
706+
end
707+
708+
return self
709+
end
710+
711+
--- Updates the current layout (recalculates positions)
712+
--- @shortDescription Updates the layout
713+
--- @return Container self For method chaining
714+
function Container:updateLayout()
715+
if self._layoutInstance then
716+
local LayoutManager = require("layoutManager")
717+
LayoutManager.update(self._layoutInstance)
718+
end
719+
return self
720+
end
721+
722+
--- Removes the current layout
723+
--- @shortDescription Clears the layout
724+
--- @return Container self For method chaining
725+
function Container:clearLayout()
726+
if self._layoutInstance then
727+
local LayoutManager = require("layoutManager")
728+
LayoutManager.destroy(self._layoutInstance)
729+
self._layoutInstance = nil
730+
end
731+
return self
732+
end
733+
688734

689735
--- @private
690736
function Container:destroy()

0 commit comments

Comments
 (0)