Tree
#struct TreeA tree of boxes with integer ids. It keeps the results of the last layout and lays out again only what changed.
import gui.flow
func main:
mut t := flow.NewTree()
root := t.Add(-1, flow.FlowStyle { row_gap = flow.Px(4.0) })
a := t.Add(root, flow.FlowStyle { height = flow.Px(20.0) })
b := t.Add(root, flow.FlowStyle {})
t.SetMeasure(b, (_id: int, w: f64, _h: f64): mut lines := 1.0 if w >= 0.0 and w < 200.0: lines = 2.0 end ret flow.Size { w = 200.0, h = lines * 16.0, baseline = 12.0 } end)
t.Layout(300.0, 200.0)
println "valid", t.Valid(b), t.Valid(42), "count", t.Count()
println "parent", t.Parent(b), "children", t.ChildCount(root), "second", t.Child(root, 1)
println "b", t.X(b), t.Y(b), t.W(b), t.H(b), "baseline", t.Baseline(b)
println "calls", t.LayoutCalls(), t.MeasureCalls()
t.ResetCounters()
t.Layout(300.0, 200.0)
println "again", t.LayoutCalls(), t.MeasureCalls(), "dirty", t.IsDirty(root)
mut st := t.Style(a)
st.height = flow.Px(30.0)
t.SetStyle(a, st)
println "after SetStyle dirty", t.IsDirty(a), t.IsDirty(root), t.IsDirty(b)
t.Layout(300.0, 200.0)
println "b moved to", t.Y(b), "calls", t.LayoutCalls()
t.MarkDirty(b)
t.Layout(150.0, 200.0)
println "narrow b", t.W(b), t.H(b)
t.ClearMeasure(b)
t.Move(b, root, 0)
t.Layout(150.0, 200.0)
println "b first", t.Child(root, 0), t.Y(a), t.H(b)
t.Remove(a)
println "removed", t.Valid(a), t.ChildCount(root)
t.Free()
println "freed", t.Count()
endvalid true false count 3 parent 0 children 2 second 2 b 0 24 300 16 baseline 12 calls 5 1 again 0 0 dirty false after SetStyle dirty true true false b moved to 34 calls 3 narrow b 150 32 b first 2 4 0 removed false 1 freed 0