Files
css/tests/css_integration_behavior.zi
T

324 lines
14 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";
#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/Theme"
#import "kryon/tree"
#import "kryon/tree_draw"
using ButtonState;
using ElevationRole;
using FontRole;
using FrameStatus;
using MotionEasing;
using StyleField;
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)
}
main :: () -> s32 {
if !SessionValid(integration_session) { integration_session = SessionOpen() }
Integration()
LayoutIntegration()
LayoutSizing()
ViewportGrid()
if SessionValid(integration_session) { unused SessionClose(integration_session) }
if test_failures == 0 { TestSay("css integration behavior: PASS\n") }
return test_failures
}