Files
css/tests/css_integration_behavior.zi
T
waozi d8044da394 Test painted, stretched and self-sizing containers; pin Kryon a04a3ba1
The layout pack now covers a content-sized card that paints its CSS box
beneath its child, a fixed-width column that stretches to its row, an
auto-width column that takes what the row left, flex: 1 taking a
column's remaining height, margin-left: auto, and Text sizing itself
where a container places it. The raster stub provides RasterTriangles
for the Text import.
2026-11-22 18:40:55 -03:00

455 lines
23 KiB
Ziran

#import "css_export"
#import "css_parser"
#import "css_sheet"
#import "css_test_support"
#import "style_pack_layout"
using RasterHost :: #import "css_button_raster_host";
BoxWidget :: #import "kryon/box";
ButtonWidget :: #import "kryon/button";
Layout :: #import "kryon/layout";
Grid :: #import "kryon/grid";
Page :: #import "kryon/page";
TextWidget :: #import "kryon/text";
#import "kryon/grid_props"
#import "kryon/control_props"
#import "kryon/drawing_props"
#import "kryon/geometry"
#import "kryon/input_protocol"
#import "kryon/session"
#import "kryon/style"
#import "kryon/style_sheet"
#import "kryon/text_style"
#import "kryon/Theme"
#import "kryon/tree"
#import "kryon/Frame"
#import "kryon/paint_queue"
using ButtonState;
using ElevationRole;
using FontRole;
using FrameStatus;
using MotionEasing;
using StyleField;
using PaintKind;
integration_sheet: CssSheet;
integration_session: Session;
integration_css :: #string CSS
Button.primary {
background: #010203;
color: #ffffff;
min-height: 32px;
}
#save {
background: hwb(.5turn 20% 20%);
color: #f0e0d0;
border-radius: 9px;
min-block-size: 44px;
line-height: 1.4;
font-size: 15px;
font-family: "Integration Sans";
-kryon-font-role: body;
-kryon-elevation: floating;
-kryon-motion-duration: 177ms;
-kryon-motion-multiplier: 1.25;
-kryon-motion-easing: standard;
-kryon-motion-reduced: no-preference;
}
#save:hover {
color: #00ff00;
-kryon-motion-duration: 200ms;
}
#other:hover {
color: #ff0000;
}
Button#leading {
line-height: 54px;
padding-block: 0;
}
Row.actions, Column.actions {
gap: 4px 12px;
padding: 3px 7px;
}
Row.actions {
column-gap: 0;
}
.sized {
width: 7.5rem;
height: 40px;
padding: 4px;
}
Row.sized {
width: 132.5PX /* fractional logical pixels */;
width: calc(100% - 2rem);
}
Text.dashboard { color: #ddecff; }
Heading.dashboard { font-size: 34px; }
ParagraphText.dashboard { font-size: 15px; }
Link.dashboard { font-size: 17px; }
CSS;
PropsFor :: (id: s32, state: s32) -> ButtonWidget.Props {
props: ButtonWidget.Props
props.key = cast(u64)id
props.id = id
props.class_name = StyleClass("primary")
props.label = "Save"
props.bounds = Rectangle.{10.0, 20.0, 80.0, 0.0}
props.state = cast(ButtonState)state
return props
}
Draw :: (props: ButtonWidget.Props) -> bool {
BeginFrame(integration_session, cast(u64)props.id,
Rectangle.{0.0, 0.0, 200.0, 100.0})
if ButtonWidget.Draw(integration_session, props) != 0 { return false }
return EndFrame(integration_session) == cast(FrameStatus)FrameOk
}
ColorIs :: (color: Color, value: u32) -> bool {
return color.r == cast(u8)(value >> cast(u32)24) &&
color.g == cast(u8)(value >> cast(u32)16) &&
color.b == cast(u8)(value >> cast(u32)8) && color.a == cast(u8)value
}
Install :: () -> bool {
CssSheetReset(*integration_sheet)
if !CssSheetParse(*integration_sheet, integration_css, "integration.css", CssNoImports) {
TestSay(" diagnostic: ")
TestSay(CssSheetDiagnostic(*integration_sheet, test_diagnostic[:]))
TestSay("\n")
return false
}
return CssSheetInstall(*integration_sheet, CssDefaultEnvironment(), null)
}
Integration :: () {
Check(InstallDefaultTheme(false), "theme installs")
Check(Install(), "integration sheet installs")
class_name := StyleClass("dashboard")
state := cast(s32)ButtonStateNormal
base: StyleData
heading := StyleTextFacts(0, class_name, StyleKindHeading(), state)
face_heading := ResolveInstalledStyleRules(base, heading, state)
Check(face_heading.foreground == cast(u32)0xddecffff && face_heading.font_size == 34.0,
"headings accept generic and heading selectors")
Check(ResolveStyleRules(ActiveStyleRules(), base, heading, state).font_size == 34.0,
"direct cascade matches heading selectors")
paragraph := StyleTextFacts(0, class_name, StyleKindParagraphText(), state)
link := StyleTextFacts(0, class_name, StyleKindLink(), state)
plain := StyleTextFacts(0, class_name, StyleKindText(), state)
Check(ResolveInstalledStyleRules(base, paragraph, state).font_size == 15.0 &&
ResolveInstalledStyleRules(base, link, state).font_size == 17.0 &&
ResolveInstalledStyleRules(base, plain, state).font_size == 0.0,
"semantic selectors reach only the matching text widgets")
save: ButtonWidget.Props = PropsFor(StyleId("save"), ButtonStateNormal)
face: StyleData = ButtonWidget.ButtonInstalledFaceFor(save, ButtonStateNormal)
Check(face.background == cast(u32)0x33ccccff && face.radius == 9.0,
"id wins type and class")
Check(face.min_height == 44.0 && face.line_height == 1.4, "typed geometry")
Check(face.typeface == "Integration Sans" && cast(s32)face.font_role == cast(s32)FontRoleBody,
"typed font")
Check(cast(s32)face.elevation == cast(s32)ElevationRoleFloating &&
face.motion_duration == 177.0 && face.motion_multiplier == 1.25 &&
cast(s32)face.motion_easing == cast(s32)MotionEasingStandard && !face.motion_reduced,
"typed elevation and motion")
CssButtonRasterReset()
Check(Draw(save), "button tree and raster frame")
raster: CssButtonRasterState = CssButtonRasterSample()
Check(!raster.failed && raster.fill_count == 1 && raster.text_count == 1 &&
raster.outline_count == 0, "one button surface and label")
Check(ColorIs(raster.fill_color, cast(u32)0x33ccccff) &&
ColorIs(raster.text_color, cast(u32)0xf0e0d0ff), "raster colors")
Check(raster.fill_bounds.x == 10.0 && raster.fill_bounds.y == 20.0 &&
raster.fill_bounds.width == 80.0 && raster.fill_bounds.height == 44.0 &&
raster.radius == 9.0 && raster.font == 15, "raster geometry, font and type")
CssButtonRasterReset()
other_hover: ButtonWidget.Props = PropsFor(StyleId("other"), ButtonStateHover)
Check(Draw(other_hover), "another button tree and raster frame")
other_raster: CssButtonRasterState = CssButtonRasterSample()
Check(!other_raster.failed && other_raster.fill_count == 1 &&
other_raster.text_count == 1 && ColorIs(other_raster.fill_color, cast(u32)0x010203ff) &&
ColorIs(other_raster.text_color, cast(u32)0xff0000ff),
"another id resolves without stale id styling")
hover: StyleData = ButtonWidget.ButtonInstalledFaceFor(save, ButtonStateHover)
Check(hover.foreground == cast(u32)0x00ff00ff && hover.motion_duration == 200.0,
"state matches the intended id")
other: StyleData = ButtonWidget.ButtonInstalledFaceFor(PropsFor(StyleId("other"), ButtonStateHover), ButtonStateHover)
Check(other.foreground == cast(u32)0xff0000ff &&
ButtonWidget.ButtonInstalledFaceFor(PropsFor(StyleId("third"), ButtonStateNormal), ButtonStateNormal).background ==
cast(u32)0x010203ff,
"different ids do not share id results")
CssButtonRasterReset()
Check(Draw(PropsFor(StyleId("leading"), ButtonStateNormal)), "absolute leading button frame")
Check(CssButtonRasterSample().fill_bounds.height == 54.0, "absolute leading drives native button height")
}
LayoutIntegration :: () {
viewport: Rectangle = .{0.0, 0.0, 200.0, 100.0}
BeginFrame(integration_session, cast(u64)500, viewport)
row := Layout.Row(integration_session,
.{.key = 201, .class_name = StyleClass("actions"), .bounds = .{0.0, 0.0, 100.0, 40.0}})
Check(row.opened && row.gap == 0 && row.content.x == 7.0 && row.content.y == 3.0,
"native row uses zero column gap and separate padding axes")
Check(End(integration_session), "close row")
column := Layout.Draw(integration_session,
.{.key = 202, .class_name = StyleClass("actions"), .bounds = .{0.0, 50.0, 100.0, 40.0}})
Check(column.opened && column.gap == 4 && column.content.x == 7.0 && column.content.y == 53.0,
"native column uses row gap and separate padding axes")
Check(End(integration_session), "close column")
Check(EndFrame(integration_session) == cast(FrameStatus)FrameOk, "native layout frame")
}
LayoutSizing :: () {
viewport: Rectangle = .{0.0, 0.0, 200.0, 100.0}
sized := StyleClass("sized")
BeginFrame(integration_session, cast(u64)501, viewport)
row := Layout.Row(integration_session, .{.key = 211, .class_name = sized})
Check(row.opened && row.bounds.width == 132.5 && row.bounds.height == 40.0 &&
row.content.width == 124.5 && row.content.height == 32.0,
"parsed sizing controls the outer bounds and content")
facts := StyleDefaultFacts(StyleKindRow())
facts.class_name = sized
direct := ResolveStyleRules(ActiveStyleRules(), StyleData.{}, facts, StyleStateAny())
Check(direct.width == row.bounds.width && direct.height == row.bounds.height,
"direct and indexed cascade resolve the same sizes")
facts.kind = StyleKindButton()
unrelated := ResolveInstalledStyleRules(StyleData.{}, facts, StyleStateAny())
Check(!StyleHasField(unrelated, cast(u32)StyleWidth | cast(u32)StyleHeight),
"container sizes do not enter the button cascade")
column := Layout.Draw(integration_session,
.{.key = 212, .class_name = sized, .bounds = .{0.0, 0.0, 44.0, 0.0}})
Check(column.opened && column.bounds.x == 4.0 && column.bounds.y == 4.0 &&
column.bounds.width == 44.0 && column.bounds.height == 40.0,
"nested column uses explicit width and styled height")
Check(End(integration_session), "close sized column")
sibling := BoxWidget.Draw(integration_session, .{.key = 213, .bounds = .{0.0, 0.0, 10.0, 8.0}})
Check(sibling >= 0 && TreeSubmittedBounds(integration_session, sibling).x == 48.0,
"retained siblings advance by the allocated column width")
Check(End(integration_session), "close sized row")
Check(EndFrame(integration_session) == cast(FrameStatus)FrameOk, "parsed size frame")
CssSheetReset(*integration_sheet)
Check(CssSheetLoad(*integration_sheet, StylePackLayout()) &&
CssSheetInstall(*integration_sheet, CssDefaultEnvironment(), null), "sizing pack installs")
packed := StyleClass("packed")
BeginFrame(integration_session, cast(u64)502, viewport)
fixed := Layout.Draw(integration_session, .{.key = 214, .class_name = packed})
Check(fixed.bounds.width == 88.0 && fixed.bounds.height == 35.0, "pack keeps both sizes")
Check(End(integration_session), "close packed column")
fluid := Layout.Row(integration_session, .{.key = 215, .class_name = packed, .id = StyleId("fluid")})
Check(fluid.bounds.width == 200.0 && fluid.bounds.height == 35.0,
"packed auto wins the width cascade and leaves the height intact")
Check(End(integration_session), "close packed row")
Check(EndFrame(integration_session) == cast(FrameStatus)FrameOk, "packed size frame")
}
ResponsiveGrid :: (width: float32, columns: s32, row_gap: s32, section: bool = false) {
env := CssEnvironmentWithViewportWidth(CssDefaultEnvironment(), width)
Check(CssSheetInstall(*integration_sheet, env, null), "install viewport styles")
BeginFrame(integration_session, cast(u64)503, Rectangle.{0.0, 0.0, width, 540.0})
if section {
parent := Page.Section(integration_session, .{.key = 301, .label = "Dashboard",
.class_name = StyleClass("dashboard")})
Check(parent.opened, "open dashboard section")
} else {
parent := Layout.Draw(integration_session, .{.key = 301, .padding = 16, .gap = 10})
Check(parent.opened, "open dashboard column")
}
items: [3]GridItem = .[.{.height = 100, .column_span = -1}, .{.height = 80}, .{.height = 60}]
grid := Layout.Grid(integration_session, GridProps.{.key = 302, .class_name = StyleClass("panels")}, items[:])
cursor := grid.cursor
Check(grid.container.opened && cursor.metrics.columns == columns &&
grid.container.bounds.width == width - 32.0 && grid.container.content.width == width - 52.0,
"grid inherits the parent inner width and resolves viewport track count")
expected_height: s32 = ifx columns == 1 then 256 + 2 * row_gap else 196 + row_gap
Check(grid.container.bounds.height == cast(float32)expected_height && cursor.metrics.row_gap == row_gap,
"rows reserve their tallest child, CSS row gaps and block padding")
cursor = Grid.GridStep(cursor, items[0].height, items[0].column_span)
Check(cursor.item.width == width - 52.0 && cursor.item.x == 26.0 && cursor.item.y == 24.0,
"full-span panel fills the grid content")
cursor = Grid.GridStep(cursor, items[1].height, items[1].column_span)
second := cursor.item
cursor = Grid.GridStep(cursor, items[2].height, items[2].column_span)
if columns == 1 {
Check(second.x == cursor.item.x && cursor.item.y - second.y == cast(float32)(80 + row_gap),
"narrow chart panels stack")
} else {
Check(second.y == cursor.item.y && cursor.item.x - second.x - second.width == 14.0,
"wide chart panels share a row with the CSS column gap")
}
Check(End(integration_session), "close panels")
metrics := Layout.Grid(integration_session, GridProps.{.key = 303, .class_name = StyleClass("metrics")}, items[1:])
Check(metrics.cursor.metrics.columns == 2, "auto-fit collapses unused columns when item spans are declared")
Check(metrics.container.bounds.y == grid.container.bounds.y + grid.container.bounds.height + 10.0,
"following grid advances by measured height")
Check(End(integration_session), "close metrics")
Check(End(integration_session), "close dashboard parent")
Check(EndFrame(integration_session) == cast(FrameStatus)FrameOk, "responsive grid frame")
}
ViewportGrid :: () {
CssSheetReset(*integration_sheet)
Check(CssSheetLoad(*integration_sheet, StylePackLayout()), "load packed grid stylesheet")
base := CssDefaultEnvironment()
Check(CssSheetSameMedia(*integration_sheet, CssEnvironmentWithViewportWidth(base, 550.0),
CssEnvironmentWithViewportWidth(base, 820.0)), "inclusive max-width matches its boundary")
Check(!CssSheetSameMedia(*integration_sheet, CssEnvironmentWithViewportWidth(base, 820.0),
CssEnvironmentWithViewportWidth(base, 820.25)), "media change detects fractional breakpoint crossing")
ResponsiveGrid(550.0, 1, 12)
ResponsiveGrid(1200.0, 2, 20)
ResponsiveGrid(550.0, 1, 12, true)
}
ResponsiveFlex :: (width: float32, wrap: bool) {
env := CssEnvironmentWithViewportWidth(CssDefaultEnvironment(), width)
Check(CssSheetInstall(*integration_sheet, env, null), "install flex viewport styles")
BeginFrame(integration_session, cast(u64)504, Rectangle.{0.0, 0.0, width, 540.0})
items: [3]Layout.FlexItem = .[.{.kind = StyleKindText(), .class_name = StyleClass("primary-filter"),
.width = 120.0, .height = 12.0}, .{.kind = StyleKindText(), .class_name = StyleClass("secondary-filter"),
.width = 200.0, .height = 24.0}, .{.width = 100.0, .height = 20.0}]
placed: [3]Layout.FlexPlacement
row := Layout.Row(integration_session, .{.key = 401, .class_name = StyleClass("filters")}, items[:], placed[:])
first := placed[0].bounds
second := placed[1].bounds
last := placed[2].bounds
Check(row.opened && row.bounds.width == width && first.y == 15.0 && second.width == 200.0,
"CSS resolves item flex basis, growth and self-alignment")
if wrap {
Check(row.bounds.height == 55.0 && first.x == 5.0 && first.width == 330.0 && second.x == 345.0 &&
last.x == 5.0 && last.y == 32.0, "narrow row wraps with independent inline and block gaps")
} else {
Check(row.bounds.height == 30.0 && first.x == 325.0 && first.width == 870.0 && second.x == 115.0 &&
last.x == 5.0 && last.y == 5.0, "wide row evaluates reverse direction and nowrap at the breakpoint")
}
Check(End(integration_session), "close flex row")
Check(EndFrame(integration_session) == cast(FrameStatus)FrameOk, "responsive flex frame")
}
Near :: (bounds: Rectangle, x: float32, y: float32, width: float32, height: float32) -> bool {
return bounds.x == x && bounds.y == y && bounds.width == width && bounds.height == height
}
// Margins, auto centering, min/max sizes, overflow and text-align from the
// packed sheet, at a narrow and a wide viewport.
ResponsiveBox :: (width: float32) {
env := CssEnvironmentWithViewportWidth(CssDefaultEnvironment(), width)
Check(CssSheetInstall(*integration_sheet, env, null), "install box viewport styles")
BeginFrame(integration_session, cast(u64)505, Rectangle.{0.0, 0.0, width, 540.0})
outer := Layout.Draw(integration_session, .{.key = 601})
page := Layout.Draw(integration_session, .{.key = 602, .class_name = StyleClass("page"),
.bounds = .{height = 100.0}})
if width > 820.0 {
Check(Near(page.bounds, 300.0, 0.0, 600.0, 100.0), "max-width with auto margins centers the column")
} else {
Check(Near(page.bounds, 16.0, 0.0, width - 32.0, 100.0), "narrow media drops the maximum for inline margins")
}
Check(page.content.width == page.bounds.width, "content follows the margin box")
Check(End(integration_session), "close page")
inset := Layout.Draw(integration_session, .{.key = 603, .class_name = StyleClass("inset"),
.bounds = .{height = 50.0}})
Check(Near(inset.bounds, 6.0, 104.0, width - 12.0, 50.0), "fixed margins inset the box in its slot")
child := BoxWidget.Draw(integration_session, .{.key = 604, .bounds = .{0.0, 0.0, width, 80.0}})
clipped := TreeSubmittedNodeAt(integration_session, child)
Check(clipped.clipped && Near(clipped.clip, 6.0, 104.0, width - 12.0, 50.0), "overflow hidden clips children")
Check(End(integration_session), "close inset")
after := BoxWidget.Draw(integration_session, .{.key = 605, .bounds = .{height = 10.0}})
Check(TreeSubmittedBounds(integration_session, after).y == 162.0, "the next sibling starts after the bottom margin")
bounded := Layout.Row(integration_session, .{.key = 606, .class_name = StyleClass("bounded"),
.bounds = .{width = 200.0, height = 40.0}})
Check(Near(bounded.bounds, 0.0, 172.0, 300.0, 24.0), "min-width and max-height clamp the declared size")
Check(End(integration_session), "close bounded row")
Check(outer.opened && End(integration_session), "close box column")
Check(EndFrame(integration_session) == cast(FrameStatus)FrameOk, "box frame")
align: s32 = 0
request: TextStyleRequest = .{.kind = StyleKindText(), .class_name = StyleClass("centered"), .align = *align}
unused ResolveInstalledTextStyle(request, StyleData.{})
Check(align == 1, "text-align reaches native Text")
}
// A content-sized Column with a CSS background and border paints its box
// when it closes, beneath the children it already queued.
PaintedBox :: () {
env := CssEnvironmentWithViewportWidth(CssDefaultEnvironment(), 400.0)
Check(CssSheetInstall(*integration_sheet, env, null), "install card styles")
BeginFrame(integration_session, cast(u64)506, Rectangle.{0.0, 0.0, 400.0, 300.0})
outer := Layout.Draw(integration_session, .{.key = 701})
before := PendingPaintCount(integration_session)
card := Layout.Draw(integration_session, .{.key = 702, .class_name = StyleClass("card")})
unused BoxWidget.Draw(integration_session, .{.key = 703, .bounds = .{height = 30.0},
.fill = Color.{1, 2, 3, 255}})
Check(card.opened && End(integration_session), "close card")
Check(Near(TreeSubmittedBounds(integration_session, card.node), 0.0, 0.0, 400.0, 50.0),
"the card wraps its child and padding")
fill := PendingPaintAt(integration_session, before)
line := PendingPaintAt(integration_session, before + 1)
child := PendingPaintAt(integration_session, before + 2)
Check(fill.kind == PaintKindRoundedFill && Near(fill.bounds, 0.0, 0.0, 400.0, 50.0) &&
fill.radius == 8.0 && ColorIs(fill.color, cast(u32)0x102030ff), "the card fill paints first")
Check(line.kind == PaintKindRoundedOutline && line.line_width == 1.0 &&
ColorIs(line.color, cast(u32)0x405060ff), "then its border")
Check(child.kind == PaintKindRoundedFill && ColorIs(child.color, cast(u32)0x010203ff) &&
Near(child.bounds, 10.0, 10.0, 380.0, 30.0), "children paint above the box")
Check(outer.opened && End(integration_session), "close painted column")
Check(EndFrame(integration_session) == cast(FrameStatus)FrameOk, "painted frame")
}
// In a row of fixed height children stretch and an auto width takes what
// is left; flex: 1 takes a column's remaining height; margin-left: auto
// pushes a row child to the end; Text sizes itself where it is placed.
FillLayout :: () {
Check(CssSheetInstall(*integration_sheet, CssDefaultEnvironment(), null), "install fill styles")
BeginFrame(integration_session, cast(u64)507, Rectangle.{0.0, 0.0, 400.0, 300.0})
shell := Layout.Row(integration_session, .{.key = 801, .bounds = .{0.0, 0.0, 400.0, 300.0}})
side := Layout.Draw(integration_session, .{.key = 802, .class_name = StyleClass("side")})
Check(shell.opened && Near(side.bounds, 0.0, 0.0, 120.0, 300.0), "a fixed-width column stretches to the row")
Check(End(integration_session), "close side")
main := Layout.Draw(integration_session, .{.key = 803})
Check(Near(main.bounds, 120.0, 0.0, 280.0, 300.0), "an auto width takes what the row has left")
TextWidget.Draw(integration_session, .{.key = 804, .text = "Services"})
title := TreeSubmittedBounds(integration_session, TreeSubmittedCount(integration_session) - 1)
Check(title.x == 120.0 && title.y == 0.0 && title.width == 280.0 && title.height > 0.0,
"placed text fills the column and takes its line height")
grow := Layout.Draw(integration_session, .{.key = 805, .class_name = StyleClass("grow")})
Check(grow.bounds.y == title.height && grow.bounds.height == 300.0 - title.height,
"flex: 1 takes the column's remaining height")
Check(End(integration_session), "close grow")
row := Layout.Row(integration_session, .{.key = 806})
push := Layout.Row(integration_session, .{.key = 807, .class_name = StyleClass("push")})
Check(row.opened && push.bounds.x == 350.0 && push.bounds.width == 50.0, "margin-left: auto pushes to the row end")
Check(End(integration_session) && End(integration_session), "close push row")
Check(End(integration_session) && End(integration_session), "close main and shell")
Check(EndFrame(integration_session) == cast(FrameStatus)FrameOk, "fill frame")
}
main :: () -> s32 {
if !SessionValid(integration_session) { integration_session = SessionOpen() }
Integration()
LayoutIntegration()
LayoutSizing()
ViewportGrid()
ResponsiveFlex(550.0, true)
ResponsiveFlex(1200.0, false)
ResponsiveBox(550.0)
ResponsiveBox(1200.0)
PaintedBox()
FillLayout()
if SessionValid(integration_session) { unused SessionClose(integration_session) }
if test_failures == 0 { TestSay("css integration behavior: PASS\n") }
return test_failures
}