Files
kryon/tests/android_components/component.zi
T

78 lines
3.0 KiB
Ziran

#import "component_protocol"
Button :: #import "kryon/button";
Text :: #import "kryon/text";
TextField :: #import "kryon/text_field";
#import "geometry"
#import "std/byte_text_linux"
#import "std/vec"
LabelState :: struct { text: string }
EditorState :: struct { bytes: [64]u8; length: s32; cursor: s32; anchor: s32; scroll: s32 }
OwnedState :: struct { label: LabelState; samples: [2]s64; editor: EditorState }
#if #defined(ANDROID_COMPONENT_B) {
component_owned: OwnedState = OwnedState.{.label = LabelState.{.text = "B"}};
component_top: float32 = 180.0;
} else {
component_owned: OwnedState = OwnedState.{.label = LabelState.{.text = "A"}};
component_top: float32 = 0.0;
}
#if #defined(ANDROID_COMPONENT_V2) {
component_schema_version: s64 = 2;
component_count: s64 = 99;
component_suffix_kind: s64 = 1;
} else {
component_schema_version: s64 = 1;
component_count: s64;
}
#program_export
Frame :: () -> s64 {
session := ComponentSession()
bounds := ComponentBounds()
y := bounds.y + component_top
increment := Button.Draw(session, Button.Props.{.key = 101,
.bounds = Rectangle.{bounds.x + 40.0, y + 16.0, 240.0, 44.0}, .label = "Increment"})
if increment != 0 {
#if #defined(ANDROID_COMPONENT_V2) {
component_count += 2
} else {
component_count += 1
}
component_owned.samples[0] += 1
component_owned.samples[1] = component_count
}
editor := *component_owned.editor
current: [64]u8
for i: 0..editor.length - 1 { current[i] = editor.bytes[i] }
value := TextView(current[:editor.length])
input := TextField.Input.{.text = ComponentText(), .backspace = ComponentKey() == 259}
result := TextField.Draw(session, TextField.Props.{.key = 103,
.bounds = Rectangle.{bounds.x + 40.0, y + 112.0, 240.0, 44.0},
.value = value, .placeholder = "Type into this component", .cursor = editor.cursor,
.anchor = editor.anchor, .scroll_x = editor.scroll, .max_bytes = 64, .input = input})
editor.cursor = result.cursor; editor.anchor = result.anchor; editor.scroll = result.scroll_x
if result.edit.changed {
edit := result.edit
next: [64]u8
at: s32
for i: 0..edit.start - 1 { next[at] = editor.bytes[i]; at += 1 }
for byte: edit.replacement { if at < 64 { next[at] = byte; at += 1 } }
for i: edit.end..editor.length - 1 { if at < 64 { next[at] = editor.bytes[i]; at += 1 } }
editor.bytes = next; editor.length = at
}
label: Vec(u8)
BuilderPrint(*label, "% % %", component_owned.label.text, component_count,
TextView(editor.bytes[:editor.length]))
#if #defined(ANDROID_COMPONENT_V2) {
BuilderPrint(*label, " v2")
}
unused Text.Draw(session, Text.Props.{.key = 102,
.bounds = Rectangle.{bounds.x + 40.0, y + 70.0, 280.0, 28.0},
.text = BuilderFinish(label), .size = 18})
if component_count > 0 && component_owned.samples[1] != component_count { return 8 }
#if #defined(ANDROID_COMPONENT_FAIL) {
if component_count > 0 { return 9 }
}
return 0
}