feat(flow.widgets): Add nil gui object (#6)

Skipped over by rendering library, and not included in output formspec.
This commit is contained in:
Lazerbeak12345 2023-04-18 22:54:04 -06:00 committed by GitHub
parent 5df202fee1
commit 435efd014e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 0 deletions

View File

@ -271,6 +271,21 @@ gui.Label{label="I am centered!", align_h = "centre"},
This applies to other elements as well, because using HBox and Spacer to centre
elements creates unnecessary containers.
#### `gui.Nil`
A tool to allow for ternary-ish conditional widgets:
```lua
gui.VBox{
gui.Label{ label = "The box below is only present if the boolean is truthy" },
the_boolean and gui.Box{ color = "#FF0000" } or gui.Nil{},
}
```
Use sparingly, flow still has to process each `Nil` object to be able to know to
remove it, and thus could still slow things down. The fastest element is one
that doesn't exist, and thus doesn't need processing.
#### `gui.Stack`
This container element places its children on top of each other. All child

View File

@ -1173,6 +1173,14 @@ function gui.Spacer(def)
return def
end
-- For use in inline <bool> and <a> or <b> type inline ifs
function gui.Nil(def)
-- Tooltip elements are ignored when layouting and setting visible = false
-- ensures that the element won't get added to the resulting formspec
def.visible = false
return gui.Tooltip(def)
end
-- Prevent any further modifications to the gui table
function gui_mt.__newindex()
error("Cannot modifiy gui table")

View File

@ -280,6 +280,19 @@ describe("Flow", function()
]])
end)
it("ignores gui.Nil", function()
test_render(gui.VBox{
min_h = 5, -- Make sure gui.Nil doesn't expand
gui.Box{w = 1, h = 1, color = "red"},
gui.Nil{},
gui.Box{w = 1, h = 1, color = "green"},
}, [[
size[1.6,5.6]
box[0.3,0.3;1,1;red]
box[0.3,1.5;1,1;green]
]])
end)
it("registers inventory formspecs", function ()
local stupid_simple_inv_expected =
"formspec_version[6]" ..