|
|
|
|
@@ -27,6 +27,27 @@ function Color:toRGBA()
|
|
|
|
|
return self.r, self.g, self.b, self.a
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--- Convert hex string to color
|
|
|
|
|
---@param hexWithTag string -- e.g. "#RRGGBB" or "#RRGGBBAA"
|
|
|
|
|
---@return Color
|
|
|
|
|
function Color.fromHex(hexWithTag)
|
|
|
|
|
local hex = hexWithTag:gsub("#", "")
|
|
|
|
|
if #hex == 6 then
|
|
|
|
|
local r = tonumber("0x" .. hex:sub(1, 2)) or 0
|
|
|
|
|
local g = tonumber("0x" .. hex:sub(3, 4)) or 0
|
|
|
|
|
local b = tonumber("0x" .. hex:sub(5, 6)) or 0
|
|
|
|
|
return Color.new(r, g, b, 1)
|
|
|
|
|
elseif #hex == 8 then
|
|
|
|
|
local r = tonumber("0x" .. hex:sub(1, 2)) or 0
|
|
|
|
|
local g = tonumber("0x" .. hex:sub(3, 4)) or 0
|
|
|
|
|
local b = tonumber("0x" .. hex:sub(5, 6)) or 0
|
|
|
|
|
local a = tonumber("0x" .. hex:sub(7, 8)) / 255
|
|
|
|
|
return Color.new(r, g, b, a)
|
|
|
|
|
else
|
|
|
|
|
error("Invalid hex string")
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local enums = {
|
|
|
|
|
---@enum TextAlign
|
|
|
|
|
TextAlign = { START = "start", CENTER = "center", END = "end", JUSTIFY = "justify" },
|
|
|
|
|
@@ -83,14 +104,14 @@ local enums = {
|
|
|
|
|
FlexWrap = { NOWRAP = "nowrap", WRAP = "wrap", WRAP_REVERSE = "wrap-reverse" },
|
|
|
|
|
---@enum GridAutoFlow
|
|
|
|
|
GridAutoFlow = { ROW = "row", COLUMN = "column", ROW_DENSE = "row dense", COLUMN_DENSE = "column dense" },
|
|
|
|
|
---@enum JustifyItems
|
|
|
|
|
JustifyItems = {
|
|
|
|
|
---@enum GridJustifyItems
|
|
|
|
|
GridJustifyItems = {
|
|
|
|
|
STRETCH = "stretch",
|
|
|
|
|
START = "start",
|
|
|
|
|
END = "end",
|
|
|
|
|
CENTER = "center",
|
|
|
|
|
},
|
|
|
|
|
---@enum AlignContent (Grid)
|
|
|
|
|
---@enum GridAlignContent
|
|
|
|
|
GridAlignContent = {
|
|
|
|
|
STRETCH = "stretch",
|
|
|
|
|
START = "start",
|
|
|
|
|
@@ -100,7 +121,7 @@ local enums = {
|
|
|
|
|
SPACE_AROUND = "space-around",
|
|
|
|
|
SPACE_EVENLY = "space-evenly",
|
|
|
|
|
},
|
|
|
|
|
---@enum JustifyContent (Grid)
|
|
|
|
|
---@enum GridJustifyContent
|
|
|
|
|
GridJustifyContent = {
|
|
|
|
|
STRETCH = "stretch",
|
|
|
|
|
START = "start",
|
|
|
|
|
@@ -112,7 +133,7 @@ local enums = {
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
local Positioning, FlexDirection, JustifyContent, AlignContent, AlignItems, TextAlign, AlignSelf, JustifySelf, FlexWrap, GridAutoFlow, JustifyItems, GridAlignContent, GridJustifyContent =
|
|
|
|
|
local Positioning, FlexDirection, JustifyContent, AlignContent, AlignItems, TextAlign, AlignSelf, JustifySelf, FlexWrap, GridAutoFlow, GridJustifyItems, GridAlignContent, GridJustifyContent =
|
|
|
|
|
enums.Positioning,
|
|
|
|
|
enums.FlexDirection,
|
|
|
|
|
enums.JustifyContent,
|
|
|
|
|
@@ -123,7 +144,7 @@ local Positioning, FlexDirection, JustifyContent, AlignContent, AlignItems, Text
|
|
|
|
|
enums.JustifySelf,
|
|
|
|
|
enums.FlexWrap,
|
|
|
|
|
enums.GridAutoFlow,
|
|
|
|
|
enums.JustifyItems,
|
|
|
|
|
enums.GridJustifyItems,
|
|
|
|
|
enums.GridAlignContent,
|
|
|
|
|
enums.GridJustifyContent
|
|
|
|
|
|
|
|
|
|
@@ -618,13 +639,7 @@ function Grid.layoutGridItems(element)
|
|
|
|
|
for _, child in ipairs(element.children) do
|
|
|
|
|
-- Skip explicitly absolute positioned children
|
|
|
|
|
if not (child.positioning == Positioning.ABSOLUTE and child._explicitlyAbsolute) then
|
|
|
|
|
local placement = Grid.calculateItemPlacement(
|
|
|
|
|
child,
|
|
|
|
|
#columnSizes,
|
|
|
|
|
#rowSizes,
|
|
|
|
|
autoPlacementCursor,
|
|
|
|
|
gridAutoFlow
|
|
|
|
|
)
|
|
|
|
|
local placement = Grid.calculateItemPlacement(child, #columnSizes, #rowSizes, autoPlacementCursor, gridAutoFlow)
|
|
|
|
|
|
|
|
|
|
-- Ensure placement is within bounds, expand grid if necessary
|
|
|
|
|
local columnStart = math.max(1, math.min(placement.columnStart, #columnSizes + 1))
|
|
|
|
|
@@ -639,19 +654,19 @@ function Grid.layoutGridItems(element)
|
|
|
|
|
local itemHeight = (rowPositions[rowEnd] or (element.y + element.height)) - itemY - rowGap
|
|
|
|
|
|
|
|
|
|
-- Apply alignment within grid cell
|
|
|
|
|
local effectiveJustifySelf = child.justifySelf or element.justifyItems or JustifyItems.STRETCH
|
|
|
|
|
local effectiveJustifySelf = child.justifySelf or element.justifyItems or GridJustifyItems.STRETCH
|
|
|
|
|
local effectiveAlignSelf = child.alignSelf or element.alignItems or AlignItems.STRETCH
|
|
|
|
|
|
|
|
|
|
-- Handle justifySelf (horizontal alignment)
|
|
|
|
|
if effectiveJustifySelf == JustifyItems.STRETCH or effectiveJustifySelf == "stretch" then
|
|
|
|
|
if effectiveJustifySelf == GridJustifyItems.STRETCH or effectiveJustifySelf == "stretch" then
|
|
|
|
|
child.x = itemX + child.padding.left
|
|
|
|
|
child.width = itemWidth - child.padding.left - child.padding.right
|
|
|
|
|
elseif effectiveJustifySelf == JustifyItems.START or effectiveJustifySelf == "start" or effectiveJustifySelf == "flex-start" then
|
|
|
|
|
elseif effectiveJustifySelf == GridJustifyItems.START or effectiveJustifySelf == "start" or effectiveJustifySelf == "flex-start" then
|
|
|
|
|
child.x = itemX + child.padding.left
|
|
|
|
|
-- Keep child's natural width
|
|
|
|
|
elseif effectiveJustifySelf == JustifyItems.END or effectiveJustifySelf == "end" or effectiveJustifySelf == "flex-end" then
|
|
|
|
|
elseif effectiveJustifySelf == GridJustifyItems.END or effectiveJustifySelf == "end" or effectiveJustifySelf == "flex-end" then
|
|
|
|
|
child.x = itemX + itemWidth - child.width - child.padding.right
|
|
|
|
|
elseif effectiveJustifySelf == JustifyItems.CENTER or effectiveJustifySelf == "center" then
|
|
|
|
|
elseif effectiveJustifySelf == GridJustifyItems.CENTER or effectiveJustifySelf == "center" then
|
|
|
|
|
child.x = itemX + (itemWidth - child.width) / 2
|
|
|
|
|
else
|
|
|
|
|
-- Default to stretch
|
|
|
|
|
@@ -663,10 +678,18 @@ function Grid.layoutGridItems(element)
|
|
|
|
|
if effectiveAlignSelf == AlignItems.STRETCH or effectiveAlignSelf == "stretch" then
|
|
|
|
|
child.y = itemY + child.padding.top
|
|
|
|
|
child.height = itemHeight - child.padding.top - child.padding.bottom
|
|
|
|
|
elseif effectiveAlignSelf == AlignItems.FLEX_START or effectiveAlignSelf == "flex-start" or effectiveAlignSelf == "start" then
|
|
|
|
|
elseif
|
|
|
|
|
effectiveAlignSelf == AlignItems.FLEX_START
|
|
|
|
|
or effectiveAlignSelf == "flex-start"
|
|
|
|
|
or effectiveAlignSelf == "start"
|
|
|
|
|
then
|
|
|
|
|
child.y = itemY + child.padding.top
|
|
|
|
|
-- Keep child's natural height
|
|
|
|
|
elseif effectiveAlignSelf == AlignItems.FLEX_END or effectiveAlignSelf == "flex-end" or effectiveAlignSelf == "end" then
|
|
|
|
|
elseif
|
|
|
|
|
effectiveAlignSelf == AlignItems.FLEX_END
|
|
|
|
|
or effectiveAlignSelf == "flex-end"
|
|
|
|
|
or effectiveAlignSelf == "end"
|
|
|
|
|
then
|
|
|
|
|
child.y = itemY + itemHeight - child.height - child.padding.bottom
|
|
|
|
|
elseif effectiveAlignSelf == AlignItems.CENTER or effectiveAlignSelf == "center" then
|
|
|
|
|
child.y = itemY + (itemHeight - child.height) / 2
|
|
|
|
|
@@ -982,7 +1005,7 @@ end
|
|
|
|
|
---@field gridColumn string|number? -- Grid item column placement
|
|
|
|
|
---@field gridRow string|number? -- Grid item row placement
|
|
|
|
|
---@field gridArea string? -- Grid item named area placement
|
|
|
|
|
---@field justifyItems JustifyItems? -- Default horizontal alignment for grid items
|
|
|
|
|
---@field justifyItems GridJustifyItems? -- Default horizontal alignment for grid items
|
|
|
|
|
---@field alignItems AlignItems? -- Default vertical alignment for grid items
|
|
|
|
|
local Element = {}
|
|
|
|
|
Element.__index = Element
|
|
|
|
|
@@ -1033,7 +1056,7 @@ Element.__index = Element
|
|
|
|
|
---@field gridColumn string|number? -- Grid item column placement (e.g., "1", "2 / 4", "span 2")
|
|
|
|
|
---@field gridRow string|number? -- Grid item row placement
|
|
|
|
|
---@field gridArea string? -- Grid item named area placement
|
|
|
|
|
---@field justifyItems JustifyItems? -- Default horizontal alignment for grid items
|
|
|
|
|
---@field justifyItems GridJustifyItems? -- Default horizontal alignment for grid items
|
|
|
|
|
local ElementProps = {}
|
|
|
|
|
|
|
|
|
|
---@param props ElementProps
|
|
|
|
|
@@ -1516,7 +1539,7 @@ function Element.new(props)
|
|
|
|
|
self.gridAutoRows = props.gridAutoRows or "auto"
|
|
|
|
|
self.justifyContent = props.justifyContent or GridJustifyContent.START
|
|
|
|
|
self.alignContent = props.alignContent or GridAlignContent.START
|
|
|
|
|
self.justifyItems = props.justifyItems or JustifyItems.STRETCH
|
|
|
|
|
self.justifyItems = props.justifyItems or GridJustifyItems.STRETCH
|
|
|
|
|
self.alignItems = props.alignItems or AlignItems.STRETCH
|
|
|
|
|
|
|
|
|
|
-- Handle columnGap and rowGap
|
|
|
|
|
@@ -1580,12 +1603,16 @@ function Element:addChild(child)
|
|
|
|
|
|
|
|
|
|
table.insert(self.children, child)
|
|
|
|
|
|
|
|
|
|
-- Only recalculate auto-sizing if the child participates in layout
|
|
|
|
|
-- (CSS: absolutely positioned children don't affect parent auto-sizing)
|
|
|
|
|
if not child._explicitlyAbsolute then
|
|
|
|
|
if self.autosizing.height then
|
|
|
|
|
self.height = self:calculateAutoHeight()
|
|
|
|
|
end
|
|
|
|
|
if self.autosizing.width then
|
|
|
|
|
self.width = self:calculateAutoWidth()
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
self:layoutChildren()
|
|
|
|
|
end
|
|
|
|
|
|