An LLM trial on an old Kryon saw unsized Text fill the viewport, a Dropdown without a width draw nothing and an unsized container report a zero content width. Today's Kryon gets all three right; widget-tree-test now checks them in a padded column: Text hugs its line, the Dropdown fills the column and the container knows its content width when it opens. Local tests: widget-tree-test.
206 lines
9.0 KiB
Ziran
206 lines
9.0 KiB
Ziran
// Unkeyed widgets take their identity from the call site: a checkbox keeps
|
|
// its identity when a conditional sibling appears before it, and one call
|
|
// site in a loop gives each repeat its own identity, the same every frame.
|
|
// A chip takes its caller's identity, not the line of the button it draws,
|
|
// and an avatar's own state key follows its caller too. A slider whose
|
|
// column fills its width spans the column and maps a drag where it was
|
|
// placed, and a keycap placed ahead submits its node at the placed rect.
|
|
// A card spaces its children by the theme's Space2 unless CSS sets a gap.
|
|
// In a column, Text without bounds hugs its line, a Dropdown without a width
|
|
// fills the column, and a container without bounds knows its content width
|
|
// when it opens.
|
|
// Run from the Kryon root: make widget-tree-test.
|
|
Avatar :: #import "kryon/avatar";
|
|
Button :: #import "kryon/button";
|
|
Card :: #import "kryon/card";
|
|
Checkbox :: #import "kryon/checkbox";
|
|
Dropdown :: #import "kryon/dropdown";
|
|
Chip :: #import "kryon/chip";
|
|
// The headless bindings also cover pictures, so the test links them.
|
|
#import "kryon/image"
|
|
Keycap :: #import "kryon/keycap";
|
|
Layout :: #import "kryon/layout";
|
|
Slider :: #import "kryon/slider";
|
|
Text :: #import "kryon/text";
|
|
Theme :: #import "kryon/theme";
|
|
#import "kryon/geometry"
|
|
#import "kryon/input_protocol"
|
|
#import "kryon/session"
|
|
#import "kryon/tree"
|
|
#import "kryon/tree_input"
|
|
#import "kryon/widget_kind"
|
|
using WidgetKind;
|
|
#import "widget_test_session"
|
|
#import "ziran_widget_noop_host"
|
|
#import "ziran_mesh_noop_host"
|
|
|
|
failures: s32;
|
|
|
|
Expect :: (ok: bool, what: string) {
|
|
if ok { return }
|
|
failures += 1
|
|
print("widget tree: FAIL %\n", what)
|
|
}
|
|
|
|
Keys :: struct {
|
|
single: u64
|
|
loop: [3]u64
|
|
count: s32
|
|
// The last chip's button and the last avatar.
|
|
chip: u64
|
|
avatar: u64
|
|
}
|
|
|
|
// One frame: an optional button, a checkbox, then three checkboxes from one
|
|
// call site; the keys of the checkboxes in submission order.
|
|
Frame :: (with_button: bool) -> Keys {
|
|
session := TestSession()
|
|
keys: Keys
|
|
TreeStart(session, cast(u64)1, Rectangle.{0.0, 0.0, 400.0, 600.0})
|
|
column := Layout.Draw(session, Layout.Props.{.bounds = .{0.0, 0.0, 400.0, 600.0}})
|
|
if column.opened {
|
|
if with_button { unused Button.Draw(session, Button.Props.{.label = "Extra", .bounds = .{height = 32}}) }
|
|
unused Checkbox.Draw(session, Checkbox.Props.{.label = "Single", .bounds = .{height = 32}})
|
|
for 0..2 { unused Checkbox.Draw(session, Checkbox.Props.{.label = "Row", .bounds = .{height = 32}}) }
|
|
if with_button { unused Chip.Draw(session, Chip.Props.{.label = "First"}) }
|
|
unused Chip.Draw(session, Chip.Props.{.label = "Last"})
|
|
if with_button { unused Avatar.Draw(session, Avatar.Props.{.name = "Ada Byron", .bounds = .{width = 32, height = 32}}) }
|
|
unused Avatar.Draw(session, Avatar.Props.{.name = "Alan Kay", .bounds = .{width = 32, height = 32}})
|
|
unused End(session)
|
|
}
|
|
for index: 0..TreeSubmittedCount(session) - 1 {
|
|
node := TreeSubmittedNodeAt(session, index)
|
|
if node.kind == cast(s32)WidgetKindButton { keys.chip = node.key }
|
|
if node.kind == cast(s32)WidgetKindImage { keys.avatar = node.key }
|
|
if node.kind != cast(s32)WidgetKindCheckbox { continue }
|
|
if keys.count == 0 { keys.single = node.key } else if keys.count <= 3 { keys.loop[keys.count - 1] = node.key }
|
|
keys.count += 1
|
|
}
|
|
unused TreeFinish(session)
|
|
return keys
|
|
}
|
|
|
|
level: s32;
|
|
keycap: Rectangle;
|
|
|
|
// A heading, then a slider the padded column fills; the slider's placed rect.
|
|
SliderFrame :: () -> Rectangle {
|
|
session := TestSession()
|
|
TreeStart(session, cast(u64)2, Rectangle.{0.0, 0.0, 400.0, 300.0})
|
|
column := Layout.Draw(session, Layout.Props.{.bounds = .{0.0, 0.0, 400.0, 300.0},
|
|
.padding = Layout.Gap(.ThemeMetricSpace4)})
|
|
placed: Rectangle
|
|
if column.opened {
|
|
Text.Draw(session, Text.Props.{.text = "Level", .bounds = .{height = 40}})
|
|
moved := Slider.DiscreteSlider(session, Slider.DiscreteSliderProps.{.semantic_label = "Level",
|
|
.value = level, .min = 0, .max = 10, .bounds = .{height = 32}})
|
|
if moved.changed { level = moved.value }
|
|
cap := Keycap.Draw(session, Keycap.Props.{.bounds = .{width = 120, height = 32}, .keys = "Ctrl+K"})
|
|
if cap.node >= 0 { keycap = TreeSubmittedNodeAt(session, cap.node).bounds }
|
|
for index: 0..TreeSubmittedCount(session) - 1 {
|
|
node := TreeSubmittedNodeAt(session, index)
|
|
if node.kind == cast(s32)WidgetKindSlider { placed = node.bounds }
|
|
}
|
|
unused End(session)
|
|
}
|
|
unused TreeFinish(session)
|
|
return placed
|
|
}
|
|
|
|
// The gap between a card's two text children.
|
|
CardGap :: () -> float32 {
|
|
session := TestSession()
|
|
TreeStart(session, cast(u64)3, Rectangle.{0.0, 0.0, 400.0, 300.0})
|
|
column := Layout.Draw(session, Layout.Props.{.bounds = .{0.0, 0.0, 400.0, 300.0}})
|
|
texts: [2]Rectangle
|
|
found: s32 = 0
|
|
if column.opened {
|
|
card := Card.Draw(session, Card.Props.{.variant = .CardOutlined})
|
|
if card.opened {
|
|
Text.Draw(session, Text.Props.{.text = "Title", .bounds = .{height = 24}})
|
|
Text.Draw(session, Text.Props.{.text = "Body", .bounds = .{height = 20}})
|
|
unused End(session)
|
|
}
|
|
unused End(session)
|
|
}
|
|
unused TreeFinish(session)
|
|
for index: 0..TreeCount(session) - 1 {
|
|
node := TreeNodeAt(session, index)
|
|
if node.kind == cast(s32)WidgetKindText && found < 2 { texts[found] = node.bounds; found += 1 }
|
|
}
|
|
if found < 2 { return -1.0 }
|
|
return texts[1].y - (texts[0].y + texts[0].height)
|
|
}
|
|
|
|
AutoSizes :: struct {
|
|
text: Rectangle
|
|
dropdown: Rectangle
|
|
inner_width: float32
|
|
}
|
|
|
|
// Unsized widgets in a 400 px column with 16 px padding.
|
|
AutoSizeFrame :: () -> AutoSizes {
|
|
session := TestSession()
|
|
sizes: AutoSizes
|
|
TreeStart(session, cast(u64)4, Rectangle.{0.0, 0.0, 400.0, 600.0})
|
|
column := Layout.Draw(session, Layout.Props.{.bounds = .{0.0, 0.0, 400.0, 600.0}, .padding = 16, .padding_override = true})
|
|
if column.opened {
|
|
Text.Draw(session, Text.Props.{.text = "A line of text"})
|
|
options: [2]Dropdown.Option
|
|
options[0].label = "One"
|
|
options[1].label = "Two"
|
|
unused Dropdown.Draw(session, Dropdown.Props.{.bounds = .{height = 32}}, options[:])
|
|
inner := Layout.Draw(session, Layout.Props.{})
|
|
if inner.opened {
|
|
sizes.inner_width = inner.content.width
|
|
unused End(session)
|
|
}
|
|
unused End(session)
|
|
}
|
|
unused TreeFinish(session)
|
|
for index: 0..TreeCount(session) - 1 {
|
|
node := TreeNodeAt(session, index)
|
|
if node.kind == cast(s32)WidgetKindText { sizes.text = node.bounds }
|
|
if node.kind == cast(s32)WidgetKindDropdown { sizes.dropdown = node.bounds }
|
|
}
|
|
return sizes
|
|
}
|
|
|
|
#program_export
|
|
Main :: () -> s32 {
|
|
unused Theme.InstallDefaultTheme(false)
|
|
plain := Frame(false)
|
|
again := Frame(false)
|
|
extra := Frame(true)
|
|
Expect(plain.count == 4 && extra.count == 4, "four checkboxes each frame")
|
|
Expect(plain.single == again.single && plain.single == extra.single,
|
|
"a checkbox keeps its identity when a button appears before it")
|
|
Expect(plain.loop[0] != plain.loop[1] && plain.loop[1] != plain.loop[2] && plain.loop[0] != plain.loop[2],
|
|
"each repeat of one call site has its own identity")
|
|
Expect(plain.loop[0] == extra.loop[0] && plain.loop[2] == extra.loop[2],
|
|
"repeats keep their identities when a sibling appears")
|
|
Expect(plain.single != plain.loop[0], "different call sites differ")
|
|
Expect(plain.chip == extra.chip, "a chip keeps its identity when another chip appears before it")
|
|
Expect(plain.avatar == extra.avatar && plain.avatar != cast(u64)0,
|
|
"an avatar keeps its state key when another avatar appears before it")
|
|
track := SliderFrame()
|
|
Expect(track.width > 300.0 && track.x > 0.0 && track.y >= 40.0, "the column places and fills the slider")
|
|
// Press and drag at four fifths of the placed track.
|
|
x := track.x + track.width * 0.8
|
|
y := track.y + track.height * 0.5
|
|
TreePointerUpdate(TestSession(), PointerFrame.{x, y, true, true, false})
|
|
unused SliderFrame()
|
|
Expect(level >= 7 && level <= 9, "a drag on the placed slider sets about four fifths")
|
|
Expect(keycap.x == track.x && keycap.y > track.y && keycap.width == 120.0 && keycap.height == 32.0,
|
|
"a keycap placed ahead submits its node where the column put it")
|
|
sizes := AutoSizeFrame()
|
|
Expect(sizes.text.height > 0.0 && sizes.text.height < 100.0, "unsized text hugs its line")
|
|
Expect(sizes.dropdown.width == 368.0 && sizes.dropdown.height == 32.0, "a dropdown without a width fills the column")
|
|
Expect(sizes.inner_width == 368.0, "an unsized container knows its content width when it opens")
|
|
gap := CardGap()
|
|
Expect(gap == Theme.CurrentThemeMetric(.ThemeMetricSpace2), "a card spaces its children by Space2")
|
|
if failures > 0 { return 1 }
|
|
print("widget tree: identities follow call sites; placed widgets fill and take drags\n")
|
|
return 0
|
|
}
|