Make Card a container that sizes itself

Card painted a surface at given bounds and handed back a content rect, so
every app measured its content up front and drew it into a Layout.Stack
slot. Card.Draw now opens Layout.Box with the card's resolved face: a
padded column whose fill, border, radius and elevation paint at its final
size, growing to its content when it has no height. Draw the content while
`opened`, then End(session). A clickable card is one press target on that
column, with hover and pressed faces from the previous frame;
CardContentBounds is gone.

The starter's task cards and the cheat sheet's note cards drop their
measured heights and Stack slots, the card and style picker examples draw
in a Layout column, and the gallery site's and two gallery scenes' cards
close with End. The dashboard follows with its Kryon pin. Layout's
LayoutContentBounds stays while the gallery scenes still use it.

Shot digests, light theme, before -> after:
  card              670ccd2d21da -> 8cc61de0c49b
  stylepicker       c67487dfbb15 -> 08c9cae998f1
This commit is contained in:
waozi committed 2026-11-22 23:57:10 -03:00
1 parent f3894d7110
commit 347202066b
14 files changed
+92 -115

No files matched your search

+6 -10
View File
@@ -22,9 +22,9 @@ No display needed; `build/shot.json` lists each node's kind, role, label and bou
- Containers (Layout, Grid, Flow, Modal) place children: never compute `x` or `y`;
a child declares its size, and zero width fills a column. A Row, Column, Grid
or Flow without a height sizes to its children when it closes with `End`. If `opened`, draw the
children, then `End(session)`: a missing `End` fails the whole frame. A widget given a
rectangle with an origin (`bar.trailing_bounds`) is drawn there, over the column. A Card
draws into a `Layout.Stack` slot: pass `slot.bounds`.
children, then `End(session)`: a missing `End` fails the whole frame; a Card is such a
container. A widget given a rectangle with an origin (`bar.trailing_bounds`) is drawn
there, over the column.
- Size and space come from theme tokens. Act on a result in the same frame.
- Typed text lives in `TextField.State` bytes; zero `length cursor anchor scroll_x` to clear.
- A returned `string` must view file-scope bytes (as `NoteAt` does), never a local buffer.
@@ -136,12 +136,8 @@ NotesScreen :: (session: Session) {
}
Text.Draw(session, Text.Props.{.bounds = .{height = control}, .text = NoteCountText()})
for i: 0..note_count - 1 {
slot := Layout.Stack(session, Layout.Props.{.bounds = .{height = control + 2.0 * Theme.CurrentThemeMetric(.ThemeMetricSpace3)}})
if slot.opened {
card := Card.Draw(session, Card.Props.{.bounds = slot.bounds, .variant = .CardOutlined})
Text.Draw(session, Text.Props.{.bounds = card.content, .text = NoteAt(i)})
unused End(session)
}
card := Card.Draw(session, Card.Props.{.variant = .CardOutlined}) // a padded column that grows to its content
if card.opened { Text.Draw(session, Text.Props.{.text = NoteAt(i)}); unused End(session) }
}
if Button.Draw(session, Button.Props.{.label = "All widgets", .bounds = .{height = control}}) == 1 { showcase = true }
}
@@ -195,7 +191,7 @@ Widgets :: (session: Session) {
```
Also: `kryon/text_field` (`Bind(session, *state, props)`), `kryon/card`
(`.variant` -> `.content`), `kryon/title_bar` (`.trailing_width` ->
(`.variant` -> `opened`, then `End`), `kryon/title_bar` (`.trailing_width` ->
`.trailing_bounds`), `kryon/box` (`.fill .border .radius`), `kryon/dropdown`
(`Draw(session, props, options)`), `kryon/modal` (`opened` -> `End`,
`.dismissed`), `kryon/avatar` (`.name`), `kryon/image` (`.asset_path .fit`),
+1
View File
@@ -132,6 +132,7 @@ Draw :: (frame: App.Frame) {
.key = 6, .bounds = Rectangle.{c.x, c.y + 128.0, c.width, 22.0},
.text = status, .size = 14
})
if card.opened { unused End(session) } // the card is a container
}
```
+1 -1
View File
@@ -34,7 +34,7 @@ are built-in Ziran types.
| --- | --- | --- |
| `<html>`, `<body>` | `Page :: #import "kryon/page";` -> `Page.Draw(session, Page.Props.{...})` or `Layout.Screen` | Call `End(session)` if `result.opened`. Also fill root background with `Box.Draw`. |
| `<main>`, `<section>` | `Page.Section(session, Page.SectionProps.{.bounds, .label, .key})` | Sets `SemanticSection`; close with `End(session)` when `opened`. |
| `<article>`, `.card` | `Card :: #import "kryon/card";` -> `Card.Draw(session, Card.Props.{.bounds, .variant})` | Variants: `.CardFilled`, `.CardElevated`, `.CardOutlined`. CSS padding sets `result.content`. Draw children inside that rectangle. |
| `<article>`, `.card` | `Card :: #import "kryon/card";` -> `card := Card.Draw(session, Card.Props.{.variant})`, then the content and `End` while `card.opened` | Variants: `.CardFilled`, `.CardElevated`, `.CardOutlined`. CSS padding sets `result.content`. Draw children inside that rectangle. |
| `<div class="card">` (a box around content) | `Layout.Draw(session, .{.class_name = CSS.StyleClass("card")})`, or `Layout.Row` / `Layout.Grid` | A container whose CSS sets `background` (color or two-stop gradient), `border` or `border-radius` paints that box beneath its children, sized to them when it has no height. |
| `<div>` (decorative box only) | `Box :: #import "kryon/box";` -> `Box.Draw(session, Box.Props.{.bounds, .fill, .border, .radius})` | Decorative only; captures no input. |
| `<header>` / toolbar | `Toolbar :: #import "kryon/toolbar";` or `TitleBar :: #import "kryon/title_bar";` | Or `Layout.Row` + `Button` / `Text`. |
+10 -9
View File
@@ -119,16 +119,17 @@ icons.
## Cards
`Card.Draw(session, Card.Props)` draws a filled, elevated or outlined Material
surface. Import it as `Card :: #import "kryon/card";`. Set `variant` to
`CardFilled` (the default), `CardElevated` or `CardOutlined`, and supply
`bounds` and a stable `key`. The returned `content` rectangle has 16 units of
padding, reduced for small bounds. Draw your content in that rectangle; Card
does not open a scope.
`Card.Draw(session, Card.Props)` opens a filled, elevated or outlined Material
surface as a padded column: while the result is `opened`, draw the content,
which the column places, then call `End(session)`. Import it as
`Card :: #import "kryon/card";`. Set `variant` to `CardFilled` (the default),
`CardElevated` or `CardOutlined`. Without a height the card grows to its
content; with `bounds` it keeps them, and `content` is the area inside its
16 units of padding.
Cards are decorative by default. `clickable` enables the standard Button's
pointer, keyboard, focus and disabled behavior; `activated` reports activation.
An optional `label` is drawn centered and announced by the clickable surface.
Cards are decorative by default. `clickable` makes the card one press target
with pointer, keyboard, focus and disabled behavior; `activated` reports activation.
An optional `label` names the clickable card for assistive technology.
Selected and explicit state props keep their usual meaning. Installed Card
style rules apply by id, class and emphasis (soft, filled and outline for the
three variants). Elevated cards and floating menus/dialogs use two fixed,
+13 -7
View File
@@ -4,6 +4,8 @@
#import "kryon/style_sheet"
Box :: #import "kryon/box";
Card :: #import "kryon/card";
Layout :: #import "kryon/layout";
#import "kryon/tree"
Text :: #import "kryon/text";
#import "kryon/drawing_props"
#import "kryon/system_theme"
@@ -47,16 +49,20 @@ Frame :: (session: Session, viewport: Rectangle) -> s32 {
example_input := WidgetInput(session)
SetGalleryViewport(viewport, true, example_input.now, example_input.reduced_motion)
Box.Draw(session, Box.Props.{.bounds = viewport, .fill = StyleTokenColor("canvas")})
Text.Draw(session, Text.Props.{.bounds = .{viewport.x + 24.0, viewport.y + 12.0, viewport.width - 48.0, 36.0}, .text = "Card", .size = 23})
disabled := example_input.disabled
page := Layout.Draw(session, Layout.Props.{.bounds = viewport, .padding = Layout.Gap(.ThemeMetricSpace6),
.gap = Layout.Gap(.ThemeMetricSpace4), .gap_override = true})
if !page.opened { return 0 }
Text.Draw(session, Text.Props.{.bounds = .{height = 36}, .text = "Card", .size = 23})
variants: [3]Card.CardVariant = .{.CardFilled, .CardElevated, .CardOutlined}
for index: cast(s32)0..2 {
card_bounds := GalleryBounds(Rectangle.{24.0, 48.0 + cast(float32)index * 82.0, 432.0, 64.0})
result := Card.Draw(session, Card.Props.{.bounds = card_bounds, .variant = variants[index]})
Text.Draw(session, Text.Props.{.bounds = result.content,
.text = ifx index == 0 then "Filled card" else ifx index == 1 then "Elevated card" else "Outlined card"})
card := Card.Draw(session, Card.Props.{.variant = variants[index]})
if card.opened {
Text.Draw(session, Text.Props.{.bounds = .{height = 20},
.text = ifx index == 0 then "Filled card" else ifx index == 1 then "Elevated card" else "Outlined card"})
unused End(session)
}
}
unused End(session)
return 0
}
+2
View File
@@ -487,6 +487,7 @@ Compose :: (session: Session, scene: s32, disabled: bool) {
} else if scene == 53 {
card := Card.Draw(session, Card.Props.{.key = 10, .bounds = b})
Text.Draw(session, Text.Props.{.bounds = card.content, .text = "One modern style"})
if card.opened { unused End(session) } // a Card is a container
Label(session, "Colors and light/dark remain customizable.", 150.0)
} else if scene == 54 {
variants: [3]Card.CardVariant = .{.CardFilled, .CardElevated, .CardOutlined}
@@ -496,6 +497,7 @@ Compose :: (session: Session, scene: s32, disabled: bool) {
.bounds = card_bounds, .variant = variants[index]})
Text.Draw(session, Text.Props.{.bounds = result.content,
.text = ifx index == 0 then "Filled card" else ifx index == 1 then "Elevated card" else "Outlined card"})
if result.opened { unused End(session) }
}
}
}
+5 -2
View File
@@ -411,7 +411,8 @@ SiteOverview :: (session: Session, view: Rectangle) {
SiteMuted(session, 10035, .{x, y, width, 24.0}, "A SMALL TASTE OF KRYON", 12)
y += 38.0
column := ifx large then (width - 20.0) * 0.5 else width
unused Card.Draw(session, Card.Props.{.key = 10036, .bounds = .{x, y, column, 250.0}, .variant = .CardOutlined})
// Each card is a container: its controls sit inside it until End.
first := Card.Draw(session, Card.Props.{.key = 10036, .bounds = .{x, y, column, 250.0}, .variant = .CardOutlined})
digits: [32]u8
SiteText(session, 10037, .{x + 20.0, y + 20.0, column - 40.0, 32.0}, "Every click counts", 20)
if Button.Draw(session, Button.Props.{.key = 10038, .bounds = .{x + 20.0, y + 72.0, column - 40.0, 42.0},
@@ -420,8 +421,9 @@ SiteOverview :: (session: Session, view: Rectangle) {
.bounds = .{x + 20.0, y + 136.0, column - 40.0, 40.0}, .label = "Notifications", .value = site_notifications}).value
site_volume = Slider.Draw(session, Slider.Props.{.key = 10040,
.bounds = .{x + 20.0, y + 188.0, column - 40.0, 48.0}, .label = "Volume", .value = site_volume, .max = 100.0}).value
if first.opened { unused End(session) }
if large { x += column + 20.0 } else { y += 270.0 }
unused Card.Draw(session, Card.Props.{.key = 10041, .bounds = .{x, y, column, 250.0}, .variant = .CardOutlined})
second := Card.Draw(session, Card.Props.{.key = 10041, .bounds = .{x, y, column, 250.0}, .variant = .CardOutlined})
SiteText(session, 10042, .{x + 20.0, y + 20.0, column - 40.0, 32.0}, "Actions with intent", 20)
held := HoldButton.Draw(session, HoldButton.Props.{.button = .{.key = 10043,
.bounds = .{x + 20.0, y + 72.0, column - 40.0, 42.0}, .label = "Hold to complete"},
@@ -432,6 +434,7 @@ SiteOverview :: (session: Session, view: Rectangle) {
.confirm_button = .{.label = "Confirm delete"}, .now = site_now, .key = site_key}, *site_confirm)
if confirmed.committed { site_confirm_count += 1 }
SiteText(session, 10045, .{x + 20.0, y + 196.0, column - 40.0, 34.0}, "Hold, release, confirm or Escape.", 13)
if second.opened { unused End(session) }
x = (view.width - width) * 0.5
y += 306.0
Box.Draw(session, Box.Props.{.key = 10204, .bounds = .{x, y, width, 1.0}, .fill = SiteBorder()})
+13 -12
View File
@@ -4,6 +4,8 @@
#import "kryon/style_sheet"
Box :: #import "kryon/box";
Card :: #import "kryon/card";
Layout :: #import "kryon/layout";
#import "kryon/tree"
Text :: #import "kryon/text";
#import "kryon/drawing_props"
#import "kryon/system_theme"
@@ -43,24 +45,23 @@ GalleryBounds :: (bounds: Rectangle) -> Rectangle {
}
Label :: (session: Session, value: string, y: float32 = 76.0) {
Text.Draw(session, Text.Props.{.bounds = GalleryBounds(Rectangle.{28.0, y, 420.0, 38.0}), .text = value})
}
b: Rectangle;
#program_export
Frame :: (session: Session, viewport: Rectangle) -> s32 {
example_input := WidgetInput(session)
SetGalleryViewport(viewport, true, example_input.now, example_input.reduced_motion)
Box.Draw(session, Box.Props.{.bounds = viewport, .fill = StyleTokenColor("canvas")})
Text.Draw(session, Text.Props.{.bounds = .{viewport.x + 24.0, viewport.y + 12.0, viewport.width - 48.0, 36.0}, .text = "Modern style", .size = 23})
b = Rectangle.{viewport.x + 24.0, viewport.y + 64.0, viewport.width - 48.0, 44.0}
disabled := example_input.disabled
card := Card.Draw(session, Card.Props.{.bounds = b})
Text.Draw(session, Text.Props.{.bounds = card.content, .text = "One modern style"})
Label(session, "Colors and light/dark remain customizable.", 150.0)
page := Layout.Draw(session, Layout.Props.{.bounds = viewport, .padding = Layout.Gap(.ThemeMetricSpace6),
.gap = Layout.Gap(.ThemeMetricSpace4), .gap_override = true})
if !page.opened { return 0 }
Text.Draw(session, Text.Props.{.bounds = .{height = 36}, .text = "Modern style", .size = 23})
card := Card.Draw(session, Card.Props.{})
if card.opened {
Text.Draw(session, Text.Props.{.bounds = .{height = 20}, .text = "One modern style"})
unused End(session)
}
Text.Draw(session, Text.Props.{.bounds = .{height = 38}, .text = "Colors and light/dark remain customizable."})
unused End(session)
return 0
}
+29 -36
View File
@@ -1,61 +1,54 @@
#import "tree"
using PropsModule :: #import "card_props";
Box :: #import "box";
Button :: #import "button";
Layout :: #import "layout";
using BehaviorModule :: #import "card_behavior";
#import "control_props"
#import "drawing_props"
#import "elevation"
#import "session"
#import "surface"
#import "style"
#import "style_sheet"
#import "surface"
#import "tree_input"
#import "widget_kind"
Theme :: #import "theme";
// Composed from existing host capabilities; portable apps need no new effect.
using StyleField;
using WidgetKind;
// A card is a padded column with the card's face: draw its content while it
// is opened, then End(session). Without a height it grows to its content; a
// clickable card is one press target with hover and pressed faces.
Draw :: (session: Session, props: PropsModule.Props, location := #caller_location) -> CardResult {
site := TreeCallSite(session, location)
defer { TreeCallSiteEnd(session, site) }
result: CardResult
result.content = CardContentBounds(props.bounds)
if props.bounds.width <= 0.0 || props.bounds.height <= 0.0 { return result }
previous: s32 = -1
if props.clickable && TreeIsBuilding(session) {
previous = TreePrevious(session, TreeCurrentParent(session), props.key, WidgetKindColumn)
}
state := ResolveState(cast(s32)props.state, props.disabled, false,
false, false, false, props.selected)
TreePressedAt(session, previous), TreeHoveredAt(session, previous), false, props.selected)
emphasis := CardEmphasis(props.variant)
facts := StyleControlRoleFacts(StyleKindCard(), props.id, props.class_name,
StyleAny(), cast(s32)ButtonTone.ButtonToneNeutral, cast(s32)emphasis,
cast(s32)ControlSize.ControlSizeMedium, state)
face := ResolveInstalledStyleRules(CardDefaultFace(emphasis, state), facts, state)
padding_x := ifx StyleHasField(face, cast(u32)StyleField.StylePaddingX) then face.padding_x else 16.0
padding_y := ifx StyleHasField(face, cast(u32)StyleField.StylePaddingY) then face.padding_y else 16.0
result.content = CardContentBounds(props.bounds, padding_x, padding_y)
if !StyleHasField(face, cast(u32)StylePaddingX) { face.padding_x = 16.0 }
if !StyleHasField(face, cast(u32)StylePaddingY) { face.padding_y = 16.0 }
if !StyleHasField(face, cast(u32)StyleGap) { face.gap = Theme.CurrentThemeMetric(.ThemeMetricSpace2) }
face.fields |= cast(u32)StylePaddingX | cast(u32)StylePaddingY | cast(u32)StyleGap
opacity := StyleOpacityValue(face.fields, face.opacity)
if props.disabled && opacity == 1.0 { opacity = 0.45 }
if props.variant == .CardElevated {
// Two precomputed alpha edges, each one unit wide. No blurred surface,
// bitmap cache, clock or temporary allocation; interiors paint once.
for layer: cast(s32)1..2 {
edge := ElevationEdgeBounds(props.bounds, layer, face.radius)
alpha := ElevationAlphaForRole(face.elevation, layer)
if alpha != cast(u32)0 {
unused Box.Draw(session, Box.Props.{.bounds = edge,
.fill = ColorFromPacked(Opacity(alpha, opacity))})
}
}
}
if props.clickable {
result.activated = Button.Draw(session, Button.Props.{.key = props.key,
.bounds = props.bounds, .id = props.id, .class_name = props.class_name,
.label = props.label, .style_kind = StyleKindCard(),
.emphasis = emphasis, .disabled = props.disabled,
.selected = props.selected, .state = props.state}) == 1
} else {
border: Color
if face.border_width > 0.0 { border = ColorFromPacked(Opacity(face.border, opacity)) }
unused Box.Draw(session, Box.Props.{.key = props.key, .bounds = props.bounds,
.id = props.id, .class_name = props.class_name,
.fill = ColorFromPacked(Opacity(face.background, opacity)),
.border = border, .radius = face.radius})
face.background = Opacity(face.background, opacity)
face.border = Opacity(face.border, opacity)
face.opacity = 1.0
box := Layout.Box(session, Layout.Props.{.key = props.key, .bounds = props.bounds}, face)
result.opened = box.opened
result.content = box.content
if props.clickable && box.node >= 0 {
TreeSetInteractive(session, box.node, props.disabled, false, props.id)
TreeSetLabel(session, box.node, props.label)
result.activated = !props.disabled && TreeTakeActivationAt(session, previous)
}
return result
}
-16
View File
@@ -47,19 +47,3 @@ CardEmphasis :: (variant: CardVariant) -> ButtonEmphasis {
return .ButtonEmphasisSoft
}
// Return usable space without changing the caller's parent or clipping children.
#program_export
CardContentBounds :: (bounds: Rectangle, padding_x: float32 = 16.0,
padding_y: float32 = 16.0) -> Rectangle {
x := bounds.width * 0.5
y := bounds.height * 0.5
if x > padding_x { x = padding_x }
if y > padding_y { y = padding_y }
if x < 0.0 { x = 0.0 }
if y < 0.0 { y = 0.0 }
width := bounds.width - x * 2.0
height := bounds.height - y * 2.0
if width < 0.0 { width = 0.0 }
if height < 0.0 { height = 0.0 }
return Rectangle.{bounds.x + x, bounds.y + y, width, height}
}
-1
View File
@@ -1,4 +1,3 @@
ziran-api-examples-v1
function CardContentBounds card_behavior_api_example.zi
function CardEmphasis card_behavior_api_example.zi
function CardDefaultFace card_behavior_api_example.zi
+1 -3
View File
@@ -7,10 +7,8 @@ Theme :: #import "theme";
#program_export
main :: () -> s32 {
unused Theme.InstallDefaultTheme(false)
outer := Rectangle.{20.0, 20.0, 320.0, 140.0}
inner := CardContentBounds(outer)
emphasis := CardEmphasis(.CardOutlined)
face := CardDefaultFace(emphasis, cast(s32)ButtonState.ButtonStateNormal)
if inner.width != 288.0 || inner.height != 108.0 || face.border_width != 1.0 { return 1 }
if face.border_width != 1.0 { return 1 }
return 0
}
+2
View File
@@ -21,6 +21,8 @@ CardVariant :: enum {
}
CardResult :: struct {
// Draw the card's content while it is opened, then End(session).
opened: bool
activated: bool
content: Rectangle
}
+9 -18
View File
@@ -63,26 +63,17 @@ AppBar :: (session: Session) {
if switch.changed { dark = switch.value }
}
// One task as a card: a checkbox with its title, and a detail line.
// One task as a card: a checkbox with its title, and a detail line. The
// card grows to its content.
TaskCard :: (session: Session, index: s32) {
task := TaskAt(index)
lines: [2]Rectangle = .[.{height = Theme.CurrentThemeMetric(.ThemeMetricControlHeightMedium)},
.{height = LineHeight(.ThemeMetricTextSM)}]
gap := Layout.Gap(.ThemeMetricSpace1)
padding := Layout.Gap(.ThemeMetricSpace4)
measured := Layout.LayoutContentBounds(Rectangle.{}, .FlexColumn, lines[:], gap, padding)
slot := Layout.Stack(session, Layout.Props.{.bounds = .{.height = measured.height}})
if !slot.opened { return }
card := Card.Draw(session, Card.Props.{.bounds = slot.bounds, .variant = .CardOutlined})
column := Layout.Draw(session, Layout.Props.{.bounds = card.content, .gap = gap, .gap_override = true})
if column.opened {
check := Checkbox.Draw(session, Checkbox.Props.{.bounds = lines[0],
.label = task.title, .checked = task.done})
if check.changed { SetTaskDone(index, check.checked) }
Text.Draw(session, Text.Props.{.bounds = lines[1], .text = task.detail,
.size = cast(s32)Theme.CurrentThemeMetric(.ThemeMetricTextSM)})
unused End(session)
}
card := Card.Draw(session, Card.Props.{.variant = .CardOutlined})
if !card.opened { return }
check := Checkbox.Draw(session, Checkbox.Props.{.label = task.title, .checked = task.done,
.bounds = .{height = Theme.CurrentThemeMetric(.ThemeMetricControlHeightMedium)}})
if check.changed { SetTaskDone(index, check.checked) }
Text.Draw(session, Text.Props.{.bounds = .{height = LineHeight(.ThemeMetricTextSM)}, .text = task.detail,
.size = cast(s32)Theme.CurrentThemeMetric(.ThemeMetricTextSM)})
unused End(session)
}