feat(ui): size a grid to the app's box, not the panel
Chrome takes the top of the panel before an app builds anything, and layout has not run yet when it does, so ui.frame() reports what the mount left it.
This commit is contained in:
+19
-3
@@ -57,6 +57,7 @@ local themeName
|
|||||||
-- One tree per state, built by the function ui.mount() was given. Rebuilding is
|
-- One tree per state, built by the function ui.mount() was given. Rebuilding is
|
||||||
-- cheap enough that nothing is retained between screens.
|
-- cheap enough that nothing is retained between screens.
|
||||||
local builder, root, captured, insideCaptured, confirming
|
local builder, root, captured, insideCaptured, confirming
|
||||||
|
local inset = 0
|
||||||
local applyPalette
|
local applyPalette
|
||||||
|
|
||||||
local function mix(a, b, amount)
|
local function mix(a, b, amount)
|
||||||
@@ -214,13 +215,28 @@ end
|
|||||||
---@return integer side
|
---@return integer side
|
||||||
---@return integer columns
|
---@return integer columns
|
||||||
function ui.cardSide(count, pad, gap, reserve)
|
function ui.cardSide(count, pad, gap, reserve)
|
||||||
local columns = gui.getWidth() >= gui.getHeight() and 3 or 2
|
local width, height = ui.frame()
|
||||||
|
local columns = width >= height and 3 or 2
|
||||||
local rows = math.ceil(count / columns)
|
local rows = math.ceil(count / columns)
|
||||||
local byWidth = (gui.getWidth() - 2 * pad - (columns - 1) * gap) // columns
|
local byWidth = (width - 2 * pad - (columns - 1) * gap) // columns
|
||||||
local byHeight = (gui.getHeight() - 2 * pad - (reserve or 0) - (rows - 1) * gap) // rows
|
local byHeight = (height - 2 * pad - (reserve or 0) - (rows - 1) * gap) // rows
|
||||||
return math.min(byWidth, byHeight), columns
|
return math.min(byWidth, byHeight), columns
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---How much of the panel chrome took before the app was built. Set by whatever mounts the
|
||||||
|
---tree, because layout has not run yet when an app sizes itself.
|
||||||
|
---@param px integer
|
||||||
|
function ui.setInset(px)
|
||||||
|
inset = px
|
||||||
|
end
|
||||||
|
|
||||||
|
---The box the app is built into, which is the panel minus the chrome above it.
|
||||||
|
---@return integer w
|
||||||
|
---@return integer h
|
||||||
|
function ui.frame()
|
||||||
|
return gui.getWidth(), gui.getHeight() - inset
|
||||||
|
end
|
||||||
|
|
||||||
---@param spec UiSpec
|
---@param spec UiSpec
|
||||||
---@return NodeId
|
---@return NodeId
|
||||||
function ui.spacer(spec)
|
function ui.spacer(spec)
|
||||||
|
|||||||
@@ -251,6 +251,12 @@ assert(cleared == ui.theme.background)
|
|||||||
|
|
||||||
ui.draw()
|
ui.draw()
|
||||||
|
|
||||||
|
-- The inset chrome took comes off the height budget before anything else.
|
||||||
|
ui.setInset(44)
|
||||||
|
assert(select(2, ui.frame()) == 436, "the frame is the panel minus the chrome")
|
||||||
|
assert(select(1, ui.cardSide(5, 12, 8)) == 132, "a grid fits the app's box, not the panel")
|
||||||
|
ui.setInset(0)
|
||||||
|
|
||||||
-- 320x480 portrait: two columns, and the reserve comes off the height budget.
|
-- 320x480 portrait: two columns, and the reserve comes off the height budget.
|
||||||
local side, columns = ui.cardSide(5, 12, 8)
|
local side, columns = ui.cardSide(5, 12, 8)
|
||||||
assert(columns == 2 and side == 144, "width is the binding constraint in portrait")
|
assert(columns == 2 and side == 144, "width is the binding constraint in portrait")
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user