Files
kryon/examples/stack/stack.zi
T
waozi 856e4bd5c8 Port the layout container demos into a page column
Column, Row, Grid, Stack, Group and Screen demos now sit in a padded page
column with their demo container 208 tall in it, and the stack demo's
offset child is placed from its container rather than the viewport.

Shot digests, light theme, before -> after:
  column            6f5cb1f058d9 -> c82aa8991b1e
  grid              abaa5239c632 -> 80336dcb0e5c
  group             62f0aab179e6 -> e8ae16d8bc10
  row               34144c33e7d0 -> 31d9b73ff9bf
  screen            08e9622cadf2 -> 5ede7ea4a923
  stack             6f17ccb82881 -> d79500101d30
2026-11-23 01:01:30 -03:00

95 lines
3.8 KiB
Ziran

#import "kryon/geometry"
#import "kryon/session"
#import "kryon/frame_snapshot"
#import "kryon/style_sheet"
Box :: #import "kryon/box";
Button :: #import "kryon/button";
Layout :: #import "kryon/layout";
Text :: #import "kryon/text";
#import "kryon/control_props"
#import "kryon/drawing_props"
#import "kryon/grid_props"
#import "kryon/tree"
#import "kryon/system_theme"
#import "std/number_text"
WidgetContext :: struct { theme: s32; now: float32; disabled: bool; reduced_motion: bool }
WidgetInput :: (session: Session) -> WidgetContext {
snapshot := CaptureFrameInput(session)
result: WidgetContext
result.now = cast(float32)snapshot.elapsed_ms / 1000.0
result.theme = ifx StyleTokenColor("canvas").r < 128 then 2 else 1
result.disabled = example_disabled
result.reduced_motion = example_reduced_motion
return result
}
gallery_view: Rectangle = Rectangle.{.x = 0.0, .y = 0.0, .width = 480.0, .height = 320.0};
gallery_live: bool;
gallery_now: float32;
gallery_reduced_motion: bool;
SetGalleryViewport :: (view: Rectangle, live: bool, now: float32, reduced_motion: bool) {
gallery_view = view; gallery_live = live; gallery_now = now
gallery_reduced_motion = reduced_motion
}
GalleryBounds :: (bounds: Rectangle) -> Rectangle {
if !gallery_live { return bounds }
bounds.x = gallery_view.x + bounds.x * gallery_view.width / 480.0
bounds.y += gallery_view.y
bounds.width *= gallery_view.width / 480.0
return bounds
}
clicks: s32;
#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")})
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 = "Stack", .size = 23})
disabled := example_input.disabled
scene: s32 = 38
props: Layout.Props = .{.bounds = .{height = 208}, .gap = 12, .padding = 8}
container: Layout.LayoutContainer
if scene == 36 { container = Layout.Draw(session, props) }
else if scene == 37 { container = Layout.Row(session, props) }
else if scene == 38 { container = Layout.Stack(session, props) }
else if scene == 39 { container = Layout.Group(session, props) }
else if scene == 48 { container = Layout.Screen(session, props) }
else { grid := Layout.Grid(session, GridProps.{.bounds = props.bounds, .columns = 2, .gap = 12, .padding = 8}); container = grid.container }
if container.opened {
first: Rectangle = .{0.0, 0.0, ifx gallery_live then 164.0 * gallery_view.width / 480.0 else 164.0, 44.0}
second: Rectangle = first
if scene == 39 || scene == 48 {
first.x = container.content.x; first.y = container.content.y
second.x = first.x; second.y = first.y + 60.0
} else if scene == 38 { second = Rectangle.{container.bounds.x + 80.0, container.bounds.y + 48.0, 164.0, 44.0} }
if Button.Draw(session, Button.Props.{.label = "First item", .bounds = first, .disabled = disabled}) == 1 { clicks += 1 }
if Button.Draw(session, Button.Props.{.label = "Second item", .bounds = second, .tone = ButtonTone.ButtonToneAccent, .disabled = disabled}) == 1 { clicks += 1 }
unused End(session)
}
unused End(session)
return 0
}
example_disabled: bool;
example_reduced_motion: bool;
// Hosts publish these options before the unchanged application entry point.
#program_export
SetExampleOptions :: (disabled: bool, reduced_motion: bool) {
example_disabled = disabled
example_reduced_motion = reduced_motion
}