diff --git a/init.lua b/init.lua index c037e63..64ffeee 100644 --- a/init.lua +++ b/init.lua @@ -384,9 +384,19 @@ local function expand(box) end end - -- Recursively expand - for _, node in ipairs(box) do - expand(node) + -- Recursively expand and remove any invisible nodes + for i = #box, 1, -1 do + local node = box[i] + -- node.visible ~= nil and not node.visible + -- WARNING: I'm not sure whether this should be called `visible`, I may + -- end up renaming it in the future. Use with caution. + if node.visible == false then + -- There's no need to try and expand anything inside invisible + -- nodes since it won't affect the overall size. + table.remove(box, i) + else + expand(node) + end end end @@ -1004,10 +1014,16 @@ end function gui.Spacer(def) def.type = "container" + assert(#def == 0) + + -- Spacers default to expanding if def.expand == nil then def.expand = true end - assert(#def == 0) + + -- Prevent an empty container from being added to the resulting form + def.visible = false + return def end diff --git a/test.lua b/test.lua index 32b28f2..fe11b78 100644 --- a/test.lua +++ b/test.lua @@ -170,4 +170,62 @@ describe("Flow", function() assert.same(state.callbacks, {btn = func}) end) + + it("handles visible = false", function() + test_render(gui.VBox{ + min_w = 10, min_h = 10, + + gui.HBox{ + spacing = 0.5, + gui.Box{w = 1, h = 1, color = "red"}, + gui.Box{w = 1, h = 1, color = "green", visible = false}, + gui.Box{w = 1, h = 1, color = "blue"}, + }, + + gui.HBox{ + gui.Box{w = 1, h = 1, color = "red"}, + gui.Box{w = 1, h = 1, color = "green", visible = false, + expand = true}, + gui.Box{w = 1, h = 1, color = "blue"}, + }, + + gui.HBox{ + gui.Box{w = 1, h = 1, color = "grey"}, + gui.Spacer{}, + gui.Box{w = 1, h = 1, color = "grey"}, + }, + + gui.HBox{ + gui.Box{w = 1, h = 1, color = "red", expand = true}, + gui.Box{w = 1, h = 1, color = "green", visible = false}, + gui.Box{w = 1, h = 1, color = "blue"}, + }, + + gui.Box{w = 1, h = 1, expand = true}, + }, [[ + size[10.6,10.6] + + container[0.3,0.3] + box[0,0;1,1;red] + box[3,0;1,1;blue] + container_end[] + + container[0.3,1.5] + box[0,0;1,1;red] + box[9,0;1,1;blue] + container_end[] + + container[0.3,2.7] + box[0,0;1,1;grey] + box[9,0;1,1;grey] + container_end[] + + container[0.3,3.9] + box[0,0;7.6,1;red] + box[9,0;1,1;blue] + container_end[] + + box[0.3,5.1;10,5.2;] + ]]) + end) end)