From eba580e376695d352eb91e8008295bca3bf58d33 Mon Sep 17 00:00:00 2001 From: Waozi Date: Fri, 20 Nov 2026 12:19:41 -0300 Subject: [PATCH] Compile native width and height for retained layouts --- Makefile | 8 +++-- README.md | 11 ++++-- src/css_sheet.zi | 10 ++++-- src/parser/properties.zi | 49 +++++++++++++++++++++----- tests/css_export_behavior.zi | 4 +++ tests/css_integration_behavior.zi | 58 +++++++++++++++++++++++++++++++ tests/css_layout.css | 8 +++++ tests/css_parse_behavior.zi | 4 ++- tools/css_tool.zi | 18 ++++++++-- ziran.lock | 22 ++++++------ ziran.toml | 2 +- 11 files changed, 165 insertions(+), 29 deletions(-) create mode 100644 tests/css_layout.css diff --git a/Makefile b/Makefile index 46cac63..f7fbe07 100644 --- a/Makefile +++ b/Makefile @@ -21,14 +21,18 @@ define native_test $(BUILD)/$(1) endef -test: +test: $(BUILD)/layout-pack/style_pack_layout.zi $(ZIRAN) check --project src/Css/module.zi $(ZIRAN) check --project src/css_lexer_laws.zi $(call native_test,css_parse_behavior) $(call native_test,css_sheet_behavior) $(call native_test,css_format_behavior) $(call native_test,css_export_behavior) - $(call native_test,css_integration_behavior) + $(call native_test,css_integration_behavior,--module-path $(BUILD)/layout-pack) + +$(BUILD)/layout-pack/style_pack_layout.zi: tests/css_layout.css $(CSS_TOOL) + mkdir -p $(dir $@) + $(CSS_TOOL) pack-file layout $< $@ # Production retained widgets on a Kryon checkout with declarative motion. $(BUILD)/motion-pack/style_pack_motion.zi: tests/css_motion.css $(CSS_TOOL) diff --git a/README.md b/README.md index 44f65da..14690b3 100644 --- a/README.md +++ b/README.md @@ -64,14 +64,14 @@ Properties: `color`, `background` (a color or a two-stop must match), `padding-inline`, `padding-block`, `gap`, `row-gap`, `column-gap`, `font-size`, `font-family`, `font-weight`, `font-style`, -`letter-spacing`, `min-height`, `min-block-size`, `line-height`, +`letter-spacing`, `width`, `height`, `min-height`, `min-block-size`, `line-height`, `translate`, `-kryon-material` (`lightfield`, `flat`, `glass`), `-kryon-icon-size`, `-kryon-font-role` (`inherit`, `control`, `body`, `heading`, `code`), `-kryon-elevation` (`none`, `rest`, `raised`, `floating`), `-kryon-motion-duration`, `-kryon-motion-multiplier`, `-kryon-motion-easing` (`inherit`, `standard`, `linear`, `instant`) and `-kryon-motion-reduced` (`reduce`, `no-preference`). Document layout properties -(`width`, `margin`, `display` and the like) are kept for the web +(`margin`, `display` and the like) are kept for the web document host and skipped by native hosts. Each native widget uses the fields its paint and layout implement: @@ -80,6 +80,13 @@ its paint and layout implement: (zero in native layout) or a nonnegative `px` or `rem` length. A `Row` uses its column gap and a `Column` uses its row gap. Controls that use a uniform gap read the first `gap` value. +- `Row` and `Column` read `width` and `height` as outer sizes, including + padding. They accept positive `px` or `rem` lengths and `auto`. A positive + axis in `props.bounds` takes precedence. Otherwise `auto` uses the ordinary + parent content size and can override an earlier size rule. Other sizing + values, including zero, percentages, intrinsic keywords, `var()` and + `calc()`, are browser-only and are skipped by native layout. Browser export + keeps both sets of values. Native packs accept the native subset. - Native `Text`, `Heading` and `ParagraphText` read `font-weight`, `font-style` and `letter-spacing`. Weight takes `normal` (400), `bold` (700) or a number from 1 to 1000, rounded to a whole number for native fonts. diff --git a/src/css_sheet.zi b/src/css_sheet.zi index 9dc73c2..fb8d059 100644 --- a/src/css_sheet.zi +++ b/src/css_sheet.zi @@ -773,6 +773,7 @@ CssSheetToken :: (sheet: *CssSheet, name: string) -> CssTokenResult { CssPreviousRuleWords :: 42 CssCompiledRuleWords :: 47 +CssSizedRuleWords :: 49 CssCompiledRefWords :: 4 CssCompiledDefinitionWords :: 8 CssCompiledConditionWords :: 10 @@ -803,7 +804,8 @@ CssSheetLoad :: (sheet: *CssSheet, compiled: CssCompiledSheet) -> bool { if sheet.failed { return false } rule_words: s32 = compiled.rule_words if rule_words == 0 { rule_words = CssPreviousRuleWords } - if (rule_words != CssCompiledRuleWords && rule_words != CssPreviousRuleWords) || + if (rule_words != CssSizedRuleWords && rule_words != CssCompiledRuleWords && + rule_words != CssPreviousRuleWords) || cast(s32)compiled.rules.count % rule_words != 0 { CssSheetFail(sheet, "unsupported compiled CSS rule format") return false @@ -903,7 +905,7 @@ CssSheetLoad :: (sheet: *CssSheet, compiled: CssCompiledSheet) -> bool { style.motion_multiplier = CssWordFloat(w[39]) style.motion_easing = cast(MotionEasing)cast(s32)w[40] style.motion_reduced = w[41] != cast(u32)0 - if rule_words == CssCompiledRuleWords { + if rule_words >= CssCompiledRuleWords { style.row_gap = CssWordFloat(w[42]) style.column_gap = CssWordFloat(w[43]) style.font_weight = cast(s32)w[44] @@ -912,6 +914,10 @@ CssSheetLoad :: (sheet: *CssSheet, compiled: CssCompiledSheet) -> bool { } else if (style.fields & cast(u32)StyleLineHeight) != cast(u32)0 { style.line_height_unit = cast(LineHeightUnit)LineHeightFactor } + if rule_words == CssSizedRuleWords { + style.width = CssWordFloat(w[47]) + style.height = CssWordFloat(w[48]) + } sheet.rules[sheet.rule_count] = entry sheet.rule_count += 1 } diff --git a/src/parser/properties.zi b/src/parser/properties.zi index b4c9aa4..63eef4b 100644 --- a/src/parser/properties.zi +++ b/src/parser/properties.zi @@ -1,6 +1,6 @@ // Part of the css_parser module: declarations. Each native property lowers -// to fields of Kryon's StyleData; web document properties (width, margin and -// the other layout properties listed below) style the web document host and +// to fields of Kryon's StyleData; web document properties (such as margin +// and unsupported sizing values) style the web document host and // are skipped by native hosts. Anything else is an error. CssScopeNative :: 0 @@ -397,8 +397,33 @@ CssReadTypeface :: (p: *CssParser, end: s32) { p.cursor.pos = end } -css_web_properties: [32]string = .[ - "width", "height", "min-width", "max-width", "max-height", +// The native Row/Column model uses a positive outer size or its usual auto +// size. Try only a single literal; every other value stays intact for the DOM. +CssReadNativeSize :: (p: *CssParser, end: s32) -> (extent: float32, native: bool) { + status := p.status + number: CssNumber = CssReadNumber(p) + if number.ok && !number.percent && number.value > 0.0 && !CssMoreValue(p, end) { + if CssNameEquals(number.unit, "px") { return cast(float32)number.value, true } + if CssNameEquals(number.unit, "rem") { return cast(float32)(number.value * CssRemPixels), true } + } else if !number.ok && !CssFailed(p) { + keyword := CssReadName(p) + if CssNameEquals(keyword, "auto") && !CssMoreValue(p, end) { return 0.0, true } + } + // The declaration span was already validated. Number/name scanning may + // reject valid browser syntax (for example an exponent or escaped unit). + p.status = status + p.diagnostic_length = 0 + return 0.0, false +} + +CssPassWebDeclaration :: (p: *CssParser) -> s32 { + p.cursor = p.pending_end + p.web_declarations += 1 + return CssScopeWeb +} + +css_web_properties: [30]string = .[ + "min-width", "max-width", "max-height", "margin", "margin-top", "margin-right", "margin-bottom", "margin-left", "margin-inline", "margin-block", "text-align", "text-decoration", "text-transform", "white-space", "cursor", "box-shadow", @@ -533,6 +558,17 @@ CssApplyProperty :: (p: *CssParser, name: CssName, end: s32) -> s32 { value: CssValue = CssReadField(p, cast(s32)CssFieldMinHeight) if !value.ok { return -1 } if !CssSetNonnegative(p, cast(s32)CssFieldMinHeight, value, name) { return -1 } + } else if CssNameEquals(name, "width") || CssNameEquals(name, "height") { + extent, native := CssReadNativeSize(p, end) + if !native { return CssPassWebDeclaration(p) } + if CssNameEquals(name, "width") { + p.template.fields |= cast(u32)StyleWidth + p.template.width = extent + } else { + p.template.fields |= cast(u32)StyleHeight + p.template.height = extent + } + return CssScopeNative } else if CssNameEquals(name, "line-height") { value: CssValue = CssReadField(p, cast(s32)CssFieldLineHeight) if !value.ok { return -1 } @@ -591,10 +627,7 @@ CssApplyProperty :: (p: *CssParser, name: CssName, end: s32) -> s32 { } else if CssReadMotionProperty(p, name, end) { return CssScopeMotion } else if CssWebProperty(name) { - p.cursor.column += end - p.cursor.pos - p.cursor.pos = end - p.web_declarations += 1 - return CssScopeWeb + return CssPassWebDeclaration(p) } else if name.length >= 2 && name.bytes[0] == #char "-" && name.bytes[1] == #char "-" { p.cursor = p.pending_name_cursor CssFail(p, "custom properties belong in :root") diff --git a/tests/css_export_behavior.zi b/tests/css_export_behavior.zi index 6177ee0..8cd0729 100644 --- a/tests/css_export_behavior.zi +++ b/tests/css_export_behavior.zi @@ -113,6 +113,10 @@ Declarations :: () { Check(Contains(output, "#kryon-dom-root { --ink: #102030; }"), "custom properties on the root") Check(Contains(output, " color: var(--ink);\n"), "declarations copied") Check(Contains(output, " width: 40px;\n"), "document properties kept for the web") + sizing: string = Export("Row { width: 132.5px; height: auto; width: calc(100% - 2rem); height: 1e2px; }", + CssDefaultEnvironment()) + Check(Contains(sizing, "width: 132.5px;\n height: auto;\n width: calc(100% - 2rem);\n height: 1e2px;"), + "browser export keeps sizing declarations in source order") Check(!Contains(output, "-kryon-material") && !Contains(output, "-kryon-icon-size"), "-kryon-* declarations dropped") authoring: string = Export("Button { min-block-size: 44px; color: hwb(.5turn 20% 20% / 50%); opacity: 75%; }", CssDefaultEnvironment()) diff --git a/tests/css_integration_behavior.zi b/tests/css_integration_behavior.zi index bb9bdcc..888b875 100644 --- a/tests/css_integration_behavior.zi +++ b/tests/css_integration_behavior.zi @@ -2,7 +2,9 @@ #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"; #import "kryon/control_props" @@ -71,6 +73,17 @@ Row.actions, Column.actions { 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); +} CSS; PropsFor :: (id: s32, state: s32) -> ButtonWidget.Props { @@ -173,10 +186,55 @@ LayoutIntegration :: () { 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") +} + main :: () -> s32 { if !SessionValid(integration_session) { integration_session = SessionOpen() } Integration() LayoutIntegration() + LayoutSizing() if SessionValid(integration_session) { unused SessionClose(integration_session) } if test_failures == 0 { TestSay("css integration behavior: PASS\n") } return test_failures diff --git a/tests/css_layout.css b/tests/css_layout.css new file mode 100644 index 0000000..6d641c1 --- /dev/null +++ b/tests/css_layout.css @@ -0,0 +1,8 @@ +Row.packed, Column.packed { + width: 5.5rem; + height: 35px; +} + +Row.packed#fluid { + width: auto; +} diff --git a/tests/css_parse_behavior.zi b/tests/css_parse_behavior.zi index b238132..6ffac18 100644 --- a/tests/css_parse_behavior.zi +++ b/tests/css_parse_behavior.zi @@ -68,8 +68,10 @@ Values :: () { "motion") } // Document properties are for the web export; natively they are skipped. - if Builds("Button { width: 20px; margin-inline: 4px; color: red; }", "web properties") { + if Builds("Button { width: 20%; height: 1e2px; margin-inline: 4px; color: red; }", "web properties") { Check(Rule(0).style.foreground == cast(u32)0xff0000ff, "web properties skipped") + Check(!Has(Rule(0).style, cast(u32)StyleWidth) && !Has(Rule(0).style, cast(u32)StyleHeight), + "browser-only size values leave native fields unset") } if Builds("Button { padding: 4px 8px; }", "padding pair") { Check(Rule(0).style.padding_y == 4.0 && Rule(0).style.padding_x == 8.0, "padding block inline") diff --git a/tools/css_tool.zi b/tools/css_tool.zi index a2fa23a..dcfc6e4 100644 --- a/tools/css_tool.zi +++ b/tools/css_tool.zi @@ -22,6 +22,9 @@ #import "css_export" #import "kryon/style" #import "kryon/style_sheet" +#import "kryon/control_props" + +using StyleField; libc :: #system_library "libc"; RawWrite :: (descriptor: s32, input: *u8, count: u64) -> s64 #foreign libc "write"; @@ -325,6 +328,13 @@ GeneratePack :: (pack: string, source: string, canonical: bool = true) -> s32 { ok: bool = Emit(ifx canonical then "// Generated from styles/" else "// Generated from ") && Emit(pack) && Emit(".css by tools/css_tool.zi.\n") && Emit(ifx canonical then "// Edit the CSS and run `make style-packs`; do not edit this file.\n" else "// Regenerate this file with css_tool pack-file.\n") && Emit("#import \"css_sheet\"\n\n") + rule_words: s32 = CssCompiledRuleWords + for index: cast(s32)0..sheet.rule_count - 1 { + if StyleHasField(sheet.rules[index].rule.style, cast(u32)StyleWidth | cast(u32)StyleHeight) { + rule_words = CssSizedRuleWords + break + } + } for index: cast(s32)0..sheet.rule_count - 1 { entry: CssSheetRule = sheet.rules[index] selector: StyleSelector = entry.rule.selector @@ -378,8 +388,12 @@ GeneratePack :: (pack: string, source: string, canonical: bool = true) -> s32 { SignedWord(style.font_weight) SignedWord(cast(s32)style.font_style) SignedWord(cast(s32)style.line_height_unit) + if rule_words == CssSizedRuleWords { + Word(FloatBits(style.width)) + Word(FloatBits(style.height)) + } } - ok = ok && EmitWords("rules", pack, CssCompiledRuleWords) + ok = ok && EmitWords("rules", pack, rule_words) for index: cast(s32)0..sheet.ref_count - 1 { ref: CssSheetRef = sheet.refs[index] SignedWord(ref.rule) @@ -424,7 +438,7 @@ GeneratePack :: (pack: string, source: string, canonical: bool = true) -> s32 { ok = ok && Emit("// The ") && Emit(pack) && Emit(" pack, compiled: load it with CssSheetLoad.\n") && Emit("#program_export\nStylePack") && Emit(title) && Emit(" :: () -> CssCompiledSheet {\n") && Emit(" compiled: CssCompiledSheet\n") && - Emit(" compiled.rule_words = ") && EmitNumber(CssCompiledRuleWords) && Emit("\n") + Emit(" compiled.rule_words = ") && EmitNumber(rule_words) && Emit("\n") for part: css_tool_parts { ok = ok && PackPart(pack, part) } diff --git a/ziran.lock b/ziran.lock index 6902328..5a36d57 100644 --- a/ziran.lock +++ b/ziran.lock @@ -1,6 +1,15 @@ { "format": 1, "packages": [ + { + "commit": "b107847a7893d3af1c891ef61eb43f3011658621", + "dependencies": {"audio": "p59deb49c7f103127", "avif": "p3ed49c5d0912e411", "image_codec": "p69c0e968ae5735d9", "libaom": "p74ab3cb6eb3b86fa", "libavif": "p57c0272208aae8db", "libwebp": "p3b3c785a0e17e605", "sdl3": "p16cd0434af3dff7b", "webp": "p2af20e0c835b1c63"}, + "dependency_refs": {"audio": "1b2f35b7b39df6fb9af7a52324b4b7cf18e98df2", "avif": "f5f186bb13569ef13338f81366ff63fb9b9282f6", "image_codec": "360553150917f352e49e1beb78917370792ccaef", "libaom": "b141d9c45db236335351c8a4b2a14b3ade27c937", "libavif": "1aadfad932c98c069a1204261b1856f81f3bc199", "libwebp": "1844042d1ca751e62e61df7617dba19c98588339", "sdl3": "829a65d769d935c4852f8159e964312c0957260a", "webp": "ec6ed304767e10c125ab2820f3b01d6fd773e147"}, + "id": "p03c1a4a8338f2f72", + "name": "kryon", + "ref": "b107847a7893d3af1c891ef61eb43f3011658621", + "url": "https://git.waozi.xyz/kryonlabs/kryon.git" + }, { "commit": "c96f5e3de9c4488f4e6c97f59f5245f22fda22f7", "dependencies": {}, @@ -46,15 +55,6 @@ "ref": "f5f186bb13569ef13338f81366ff63fb9b9282f6", "url": "https://github.com/ziranlang/avif.git" }, - { - "commit": "9ea1567980771a061e7a9f78d7ab8b660e20bcf9", - "dependencies": {"audio": "p59deb49c7f103127", "avif": "p3ed49c5d0912e411", "image_codec": "p69c0e968ae5735d9", "libaom": "p74ab3cb6eb3b86fa", "libavif": "p57c0272208aae8db", "libwebp": "p3b3c785a0e17e605", "sdl3": "p16cd0434af3dff7b", "webp": "p2af20e0c835b1c63"}, - "dependency_refs": {"audio": "1b2f35b7b39df6fb9af7a52324b4b7cf18e98df2", "avif": "f5f186bb13569ef13338f81366ff63fb9b9282f6", "image_codec": "360553150917f352e49e1beb78917370792ccaef", "libaom": "b141d9c45db236335351c8a4b2a14b3ade27c937", "libavif": "1aadfad932c98c069a1204261b1856f81f3bc199", "libwebp": "1844042d1ca751e62e61df7617dba19c98588339", "sdl3": "829a65d769d935c4852f8159e964312c0957260a", "webp": "ec6ed304767e10c125ab2820f3b01d6fd773e147"}, - "id": "p439f82a30e97c7fa", - "name": "kryon", - "ref": "9ea1567980771a061e7a9f78d7ab8b660e20bcf9", - "url": "https://git.waozi.xyz/kryonlabs/kryon.git" - }, { "commit": "1aadfad932c98c069a1204261b1856f81f3bc199", "dependencies": {}, @@ -119,8 +119,8 @@ } ], "root": { - "dependencies": {"kryon": "p439f82a30e97c7fa"}, - "dependency_refs": {"kryon": "9ea1567980771a061e7a9f78d7ab8b660e20bcf9"}, + "dependencies": {"kryon": "p03c1a4a8338f2f72"}, + "dependency_refs": {"kryon": "b107847a7893d3af1c891ef61eb43f3011658621"}, "name": "Css" }, "toolchain": { diff --git a/ziran.toml b/ziran.toml index a66c2bb..b06315e 100644 --- a/ziran.toml +++ b/ziran.toml @@ -8,7 +8,7 @@ ref = "9432a110709c4a4f72c4f732fcfc368e15213a54" [dependencies.kryon] git = "https://git.waozi.xyz/kryonlabs/kryon.git" -ref = "9ea1567980771a061e7a9f78d7ab8b660e20bcf9" +ref = "b107847a7893d3af1c891ef61eb43f3011658621" [exports] Css = "src/Css/module.zi"