Files
kryon/examples/onetimecode/onetimecode.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

107 lines
3.6 KiB
Ziran

#import "kryon/geometry"
#import "kryon/session"
#import "kryon/frame_snapshot"
#import "kryon/style_sheet"
Box :: #import "kryon/box";
OneTimeCode :: #import "kryon/one_time_code";
Text :: #import "kryon/text";
TextField :: #import "kryon/text_field";
#import "kryon/drawing_props"
#import "kryon/key_input"
#import "kryon/tree_input"
#import "kryon/input_protocol"
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
}
bounds: Rectangle;
one_time_state: OneTimeCode.OneTimeCodeState;
inputs_ready: bool;
gallery_input_key: s32;
gallery_typed: [128]u8;
Prepare :: (session: Session, scene: s32) {
bounds = GalleryBounds(Rectangle.{24.0, 96.0, 432.0, 104.0})
if !inputs_ready {
one_time_state = OneTimeCode.OneTimeCodeStateFor(6, "482913", true)
inputs_ready = true
}
}
#program_export
Frame :: (session: Session, viewport: Rectangle) -> s32 {
gallery_input: TextField.Input
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 = "OneTimeCode", .size = 23})
disabled := example_input.disabled
scene: s32 = 1
Prepare(session, scene)
if !gallery_live { gallery_input.focus = true }
if gallery_live {
gallery_input_key = KeyboardTake(session)
gallery_input = TextFieldInputFor(gallery_input_key, KeyboardModifiers(session), TypedTextTake(session, gallery_typed[:]))
}
code_style := OneTimeCode.OneTimeCodeDefaultStyle()
code_style.slot_width = ifx gallery_live && bounds.width < 340.0 then 28.0 else 40.0
code_style.slot_height = 48.0
code_style.gap = ifx gallery_live && bounds.width < 340.0 then 4.0 else 7.0
code_style.group_gap = 14.0
unused OneTimeCode.Draw(session, OneTimeCode.Props.{.bounds = bounds, .length = 6, .focus = !gallery_live, .disabled = disabled,
.text = gallery_input.text, .left = gallery_input.left, .right = gallery_input.right,
.backspace = gallery_input.backspace, .delete_forward = gallery_input.delete_forward, .escape = gallery_input.escape,
.style = code_style}, *one_time_state)
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
}