Lay out rows and columns like CSS flex without sizes from the app

In a placing Row an auto-width container takes the width its earlier
siblings left and a container in a fixed-height Row stretches to it.
CSS flex-grow on a container in a fixed-height Column takes the height
that is left, and an auto inline margin on a fixed-width container in a
row pushes it to the row's end. Text placed with no size fills a column
or grid track (a row keeps the text's own width) and takes its wrapped
line height, through Layout.LayoutChildWidth. Badge draws its label
where its parent placed it, and a full-width Button fills the column
that places it. The app starter's digests are re-recorded: one line of
its text anti-aliases differently.
This commit is contained in:
waozi committed 2026-11-22 18:34:42 -03:00
1 parent fa327303a5
commit a04a3ba198
7 files changed
+122 -20

No files matched your search

+2
View File
@@ -2,6 +2,8 @@
## Unreleased
- Containers lay out like CSS flex and block boxes without sizes from the app: in a placing Row an auto-width container takes the width its earlier siblings left and a container in a fixed-height Row stretches to it; CSS `flex: 1` on a container in a fixed-height Column takes the remaining height; `margin-left: auto` pushes a fixed-width container to the row's end. Text placed with no size fills a column or grid track (a row keeps its own width) and takes its wrapped line height. Badge draws its label where it was placed, and a full-width Button fills the column that places it.
- Row, Column and Grid paint their CSS background, border and border radius beneath their children, like a styled div; a content-sized one paints its box at `End` and moves it under the children in place. The frame fills the painting slot, so a program that never paints carries no paint queue.
- `make budget-test` fails when a 1,000-row ListItem frame grows past its recorded tree nodes or paint commands, or the app starter's portable bundle past its recorded bytes: counts that are the same on every machine.
+22 -10
View File
@@ -109,7 +109,9 @@ if sc.opened {
### B. Measured flex and grid containers
- **Block flow** (`div` stacks, `display: flex` rows without justification or growth, `flex-wrap: wrap` chip rows) needs no measuring: use a content-sized `Layout.Draw`, `Layout.Row`, `Layout.Flow` or item-less `Layout.Grid` and draw the children.
- **Block flow** (`div` stacks, `display: flex` rows without justification or growth, `flex-wrap: wrap` chip rows) needs no measuring: use a content-sized `Layout.Draw`, `Layout.Row`, `Layout.Flow` or item-less `Layout.Grid` and draw the children. Text placed with no size fills a column or grid track (a row keeps the text's own width) and takes its wrapped line height; Button and Badge measure themselves, and `.full_width = true` fills the placing column.
- **Sidebar + main, header with actions at the end.** In a placing `Row` an auto-width container takes the width its earlier siblings left, and a container in a `Row` of fixed height stretches to it. CSS `flex: 1` on a container in a fixed-height `Column` takes the remaining height (`main { display: flex; flex-direction: column }` with a `flex: 1` body). `width: 246px; margin-left: auto` on a container in a row pushes it to the row's end.
- **Composite widgets** that lay out their own children from their bounds (Alert, EmptyState, Card, TitleBar, the loading spinner) go in a `Layout.Stack(session, .{.bounds = .{height = h}})` slot; draw them with `slot.content`.
- **CSS Flexbox** with justification, growth or cross-axis alignment uses `Layout.Row(session, props, items[:], placements[:])` or the same arguments to `Layout.Draw` for a column. Pass a `[]Layout.FlexItem` with measured sizes and each styled child's `kind`, `id` and `class_name`. The container resolves CSS direction, wrapping, gaps, padding, justification and alignment. Children can set basis, grow, shrink and self-alignment in CSS. Draw in `placements[i].bounds` and close the layout scope once. The low-level `BeginFlexCursor` and `FlexStep` functions place premeasured children in a single line.
- **CSS Grid** uses `Layout.Grid(session, Grid.GridProps.{.key, .class_name}, items[:])`.
Give each item a height or a `measure_height(width, key)` callback. A zero height calls the measurement with the item's allocated width after tracks and spans are resolved. The tallest item sets each row's height. Pass an output `[]Rectangle` as the fourth argument to measure and place all items in one pass, then draw children using those bounds. Two-argument calls place unpositioned single-track children using their submitted sizes.
@@ -130,33 +132,43 @@ Grid.charts {
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 14px;
}
Card.panel { background: #151f30; border: 1px solid #29384f; border-radius: 16px; padding: 20px; }
Column.panel { background: #151f30; border: 1px solid #29384f; border-radius: 16px; padding: 20px; gap: 8px; }
@media (max-width: 820px) {
Grid.charts { grid-template-columns: minmax(0, 1fr); }
Card.panel { padding: 12px; }
Column.panel { padding: 12px; }
}
```
```zi
CSS :: #import "css/Css";
Grid :: #import "kryon/grid";
Layout :: #import "kryon/layout";
Text :: #import "kryon/text";
// Inside a Column or Section scope, after installing the viewport styles:
items: [3]Grid.GridItem = .[.{.height = 340, .column_span = -1}, .{.height = 240}, .{.height = 240}]
charts := Layout.Grid(session, Grid.GridProps.{.key = 30, .class_name = CSS.StyleClass("charts")}, items[:])
// Inside a Column scope, after installing the viewport styles. The grid and
// each card size to their children; the CSS above paints the cards.
charts := Layout.Grid(session, GridProps.{.class_name = CSS.StyleClass("charts")})
if charts.container.opened {
cursor := charts.cursor
Layout.GridSpan(session, charts.container, -1) // grid-column: 1 / -1
for i: 0..2 {
cursor = Grid.GridStep(cursor, items[i].height, items[i].column_span)
// Draw each keyed Card/Plot in cursor.item.
card := Layout.Draw(session, Layout.Props.{.class_name = CSS.StyleClass("panel")})
if card.opened {
Text.Draw(session, Text.Props.{.text = titles[i]})
// A chart or table goes here; give it a height, it fills the width.
unused End(session)
}
}
unused End(session)
}
```
`examples/dashboard` is a complete port: sidebar, header, KPI cards, search,
filters, a scrolling result list and an inspector, all placed by containers
from one stylesheet with 760 px media rules. Its frame code has no rectangle
arithmetic and no computed keys.
| Dashboard HTML/CSS | Native authoring |
| --- | --- |
| `.card { background; border; border-radius; padding }` | Same CSS on a `Column` (`Layout.Draw`) with that class: it paints the box under its children. |
| `main { max-width: 1480px; margin: 0 auto }` | Use the same declarations on a `Column`, `Row` or `Grid`: the container caps its width and centers itself in its parent's inner width. |
| `margin`, `min-width`, `max-height`, `overflow: hidden` | Same CSS on `Row`, `Column` and `Grid`. Margins move the box inside the slot its parent places, so siblings keep their gaps. `overflow: hidden` clips descendants to the box. |
| `display: grid`, equal `1fr` tracks | Select `Layout.Grid` in `.zi`. Use `1fr`, `minmax(0, 1fr)`, or `repeat(N, ...)` in CSS. `N` is 1–1024. |
+9 -3
View File
@@ -160,7 +160,12 @@ Draw :: (session: Session, props: Props) -> BadgeResult {
TreeSetLabel(session, result.node, props.text)
TreeSetStyleId(session, result.node, props.id)
TreeSetStyleClass(session, result.node, props.class_name)
TreeSetChildClip(session, result.node, result.bounds)
// Paint on the node is in the measured rect and moves with it; the
// icon and label are child widgets, drawn where the parent placed it.
placed := TreeSubmittedBounds(session, result.node)
dx := placed.x - result.bounds.x
dy := placed.y - result.bounds.y
TreeSetChildClip(session, result.node, placed)
opacity := StyleOpacityValue(face.fields, face.opacity)
result.background = face.background
result.foreground = face.foreground
@@ -185,7 +190,7 @@ Draw :: (session: Session, props: Props) -> BadgeResult {
cursor += 6.0 + face.gap
}
if props.icon_type > 0 {
icon_bounds := Rectangle.{cursor, center_y - face.icon_size * 0.5, face.icon_size, face.icon_size}
icon_bounds := Rectangle.{cursor + dx, center_y + dy - face.icon_size * 0.5, face.icon_size, face.icon_size}
icon := ImageForIcon(props.icon_type, icon_bounds, props.text)
icon.key = WidgetPartKey(key, 1)
icon.tint = Opacity(face.foreground, opacity)
@@ -194,7 +199,7 @@ Draw :: (session: Session, props: Props) -> BadgeResult {
}
if props.text != "" {
text := Text.Props.{.key = WidgetPartKey(key, 2), .text = props.text,
.bounds = Rectangle.{cursor, result.bounds.y,
.bounds = Rectangle.{cursor + dx, placed.y,
result.bounds.x + result.bounds.width - face.padding_x - cursor,
result.bounds.height}, .size = font, .typeface = face.typeface,
.vertical_align = .TextAlignCenter,
@@ -202,5 +207,6 @@ Draw :: (session: Session, props: Props) -> BadgeResult {
Text.Draw(session, text)
}
unused TreePopScope(session)
result.bounds = placed
return result
}
+7
View File
@@ -29,6 +29,7 @@ Theme :: #import "theme";
#import "tree"
#import "tree_input"
#import "widget_kind"
Layout :: #import "layout";
using ArrowDirection;
using WidgetKind;
@@ -486,8 +487,14 @@ Theme :: #import "theme";
}
available_width: float32 = 0.0
if TreeIsBuilding(session) {
// A full-width button fills the column, stack or grid track that
// places it, or else the rest of the window.
root: Rectangle = TreeRootBounds(session)
available_width = root.x + root.width - props.bounds.x
if props.bounds.x == 0.0 && props.bounds.y == 0.0 {
fill := Layout.LayoutChildWidth(session)
if fill > 0.0 { available_width = fill }
}
}
bounds: Rectangle = MeasureBounds(props, face,
StyleMinHeightValue(face.fields, face.min_height,
+58 -3
View File
@@ -219,6 +219,27 @@ LayoutAutoWidth :: (declared: float32, face: StyleData) -> bool {
return declared <= 0.0 && !(StyleHasField(face, cast(u32)StyleWidth) && face.width > 0.0)
}
// What the placing parent row or column has left on its main axis after
// the children it already placed; zero or less when nothing is left.
LayoutRemaining :: (session: Session, horizontal: bool) -> float32 {
index := TreeCurrentParent(session)
info := TreeLayoutAt(session, index)
progress := TreeLayoutProgressAt(session, index)
content := LayoutMetricsFor(info.bounds, info.gap, info.padding, info.padding_y).content
used := progress.extent + cast(float32)(info.gap * progress.count)
return ifx horizontal then content.width - used else content.height - used
}
// CSS `flex-grow` above zero on the container itself, as a child.
LayoutGrows :: (props: PropsModule.Props, kind: s32) -> bool {
style_kind := ifx kind == WidgetKindRow then StyleKindRow() else StyleKindColumn()
facts := StyleDefaultFacts(style_kind)
facts.id = props.id
facts.class_name = props.class_name
item := ResolveInstalledFlexItem(facts)
return (item.fields & cast(u32)StyleFlexGrow) != 0 && item.grow > 0.0
}
LayoutAutoHeight :: (declared: float32, face: StyleData) -> bool {
return declared <= 0.0 && !(StyleHasField(face, cast(u32)StyleHeight) && face.height > 0.0)
}
@@ -336,10 +357,29 @@ ContainerFor :: (session: Session, props: PropsModule.Props,
auto_width := LayoutAutoWidth(props.bounds.width, face)
result.bounds.width = LayoutExtentFor(props.bounds.width, parent.width,
face.width, StyleHasField(face, cast(u32)StyleWidth))
content_sized := (kind == WidgetKindRow || kind == WidgetKindColumn) &&
LayoutAutoHeight(props.bounds.height, face)
// In a placing row an auto width takes what the earlier siblings left,
// and an auto height stretches to a row of definite height. In a column
// of definite height, CSS flex-grow takes the height that is left.
holder := TreeSubmittedNodeAt(session, TreeCurrentParent(session))
placed := holder.places_children && props.bounds.x == 0.0 && props.bounds.y == 0.0
in_row := placed && holder.kind == WidgetKindRow
if in_row && auto_width {
rest := LayoutRemaining(session, true)
result.bounds.width = ifx rest > 0.0 then rest else 0.0
}
auto_height := LayoutAutoHeight(props.bounds.height, face)
content_sized := (kind == WidgetKindRow || kind == WidgetKindColumn) && auto_height &&
!(in_row && parent.height > 0.0)
result.bounds.height = LayoutExtentFor(props.bounds.height, parent.height,
face.height, StyleHasField(face, cast(u32)StyleHeight))
if content_sized && placed && holder.kind == WidgetKindColumn && parent.height > 0.0 &&
LayoutGrows(props, kind) {
rest := LayoutRemaining(session, false)
if rest > 0.0 {
result.bounds.height = rest
content_sized = false
}
}
if (box.fields & cast(u32)LayoutBoxWidthFields) != 0 {
result.bounds.width = LayoutBoxWidth(AutoGridChildWidth(session, result.bounds), auto_width, box)
}
@@ -362,7 +402,9 @@ ContainerFor :: (session: Session, props: PropsModule.Props,
result.gap = metrics.gap
result.padding = metrics.padding
result.padding_y = metrics.padding_y
return OpenContainer(session, props, result, box, parent.width, content_sized, face)
available := parent.width
if in_row { available = LayoutRemaining(session, true) }
return OpenContainer(session, props, result, box, available, content_sized, face)
}
// The slot a parent places: the border box plus its margins. An `auto`
@@ -604,6 +646,19 @@ AutoGridChildWidth :: (session: Session, bounds: Rectangle) -> float32 {
return track.item.width
}
// The width the current container gives a child it places that has none:
// a column's or stack's content width or a grid's track; zero in a row or
// flow, where the child keeps its own width, and outside placing parents.
#program_export
LayoutChildWidth :: (session: Session) -> float32 {
index := TreeCurrentParent(session)
node := TreeSubmittedNodeAt(session, index)
if !node.places_children || node.kind == WidgetKindRow { return 0.0 }
if node.kind == WidgetKindGrid { return AutoGridChildWidth(session, Rectangle.{}) }
info := TreeLayoutAt(session, index)
return LayoutMetricsFor(info.bounds, info.gap, info.padding, info.padding_y).content.width
}
#program_export
Grid :: (session: Session, props: GridProps, items: []GridItem = EmptyGridItems(),
item_bounds: []Rectangle = EmptyGridBounds()) -> GridResult {
+22 -2
View File
@@ -39,6 +39,7 @@ using Text :: #import "text_props";
#import "tree"
#import "text_selection_host"
#import "widget_kind"
Layout :: #import "layout";
Grapheme :: #import "std/grapheme";
using WidgetKind;
@@ -467,10 +468,29 @@ using Text :: #import "text_props";
DrawSession :: (session: Session, props: Text.Props) {
if !Finite32(props.rotation) || !Finite32(props.origin.x) || !Finite32(props.origin.y) { return }
if props.bounds.width == 0.0 && props.bounds.height == 0.0 {
face: TextFace = TextFaceOf(props)
if TreeIsBuilding(session) && props.bounds.x == 0.0 && props.bounds.y == 0.0 &&
(props.bounds.width <= 0.0 || props.bounds.height <= 0.0) &&
TreeSubmittedNodeAt(session, TreeCurrentParent(session)).places_children {
// Placed by its container: an unset width fills a column, stack or
// grid track (a row keeps the text's own width), an unset height
// takes the wrapped lines.
if props.bounds.width <= 0.0 {
props.bounds.width = Layout.LayoutChildWidth(session)
if props.bounds.width <= 0.0 {
sized := PrepareText(props, face)
props.bounds.width = cast(float32)GlyphWidth(props.text, sized.font, sized.typeface,
sized.glyph_style)
}
}
if props.bounds.height <= 0.0 {
sized := PrepareText(props, face)
props.bounds.height = cast(float32)TextLayoutTotalHeight(sized.line_count,
sized.line_height, sized.line_gap)
}
} else if props.bounds.width == 0.0 && props.bounds.height == 0.0 {
props.bounds = TreeRootBounds(session)
}
face: TextFace = TextFaceOf(props)
lines: [8]TextLinePlan
revision := GlyphMetricsRevision()
prepared: PreparedText = PrepareTextWithLines(props, face, lines[:])
+2 -2
View File
@@ -22,8 +22,8 @@ KRYON = ROOT / "build/bin/kryon"
# starter's look changes on purpose, check build/test/templates/*.png and
# put the digests the failure prints here.
STARTER_DIGESTS = {
"light": "d5e65fc5a5c39e0a09221d04cdfa17f2dead82530dbdba26e5b22bd088278d21",
"dark": "5763f2a2bc575e18f75ab41a546d02e368c68d1416eb0624cfa9ce4e07f71043",
"light": "2efc45f3b103114b9f2b9a721fa21443063d0b9da8736c17d7c0e45dbf0533fb",
"dark": "dc3347a9e1215234318919ad0b0cdd3a84e06aece528a55751d8ac1721c60339",
}