Files
kryon/examples/plotlines/plotlines.zi
T
waozi 9994b820b0 Drop widget keys from the examples and older templates
Widgets are known by their call site now, so the examples and the pages,
tui and web templates stop numbering them: literal keys, `100 + index`
loop keys and the key parameters threaded through Label, Heading,
Paragraph and link helpers are gone. Keys stay where something points at
them: a guided tour's target, the gallery site's test handles and the
dashboard.
2026-11-22 23:07:25 -03:00

93 lines
3.3 KiB
Ziran

#import "kryon/geometry"
#import "kryon/session"
#import "kryon/frame_snapshot"
#import "kryon/style_sheet"
Box :: #import "kryon/box";
Text :: #import "kryon/text";
#import "kryon/control_props"
#import "kryon/drawing_props"
Plot :: #import "plot/PlotWidget";
Charts :: #import "plot/Plot";
Theme :: #import "kryon/theme";
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
}
installed_style: s32 = -1;
InstallStyles :: (dark: bool) {
wanted: s32 = ifx dark then 1 else 0
if wanted == installed_style { return }
installed_style = wanted
palette := Theme.DefaultPalette(dark)
rules := ActiveStyleRules()
rule: StyleRule
rule.selector = StyleDefaultSelector(); rule.selector.kind = StyleKindPlot()
rule.style.fields = cast(u32)StyleField.StyleBackground | cast(u32)StyleField.StyleForeground | cast(u32)StyleField.StyleBorder
rule.style.background = palette.surface; rule.style.foreground = palette.accent; rule.style.border = palette.border
rule.order = rules.count; rules.items[rules.count] = rule; rules.count += 1
unused InstallStyleRules(rules)
}
bounds: Rectangle;
values: [8]float32 = .{3.0, 5.0, 4.0, 8.0, 6.0, 9.0, 8.0, 11.0};
Prepare :: (session: Session, scene: s32) {
bounds = GalleryBounds(Rectangle.{24.0, 64.0, 432.0, 208.0})
}
#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 = "Plot · lines", .size = 23})
disabled := example_input.disabled
scene: s32 = 0
InstallStyles(example_input.theme == 2)
Prepare(session, scene)
unused Plot.Plot(session, Plot.PlotProps.{.bounds = bounds, .semantic_label = "Eight example values", .scale_min = 0.0, .scale_max = 12.0, .mode = cast(Plot.PlotMode)scene}, values[:])
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
}