ludic/examples/library/ui_submit.ludic
Orkuncakilkaya 19fcf60599 wip(0.S3): packages and examples migrated again from their pre-0.S sources in one run
ludic migrate state packages <every example program> packages/ludic.lab/example/plate.ludic
  1804 vars into 126 states, 64 into lets; 23498 edits in 460 files

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-25 15:31:34 +03:00

47 lines
2 KiB
Text

# ui_submit.ludic — on-submit on a text field: Enter (or the pad's A) while it has the focus sends what
# it holds with event.value; the focus stays, and so does the text, unless clear-on-submit empties it.
import "ludic.ui"
program UiSubmit {
numbers float
state UiSubmitState {
said: string = ""
chat: string = ""
name: string = ""
}
view Chat (ui_submit_st: mut UiSubmitState) {
chat = ui_submit_st.chat
name = ui_submit_st.name
on set_chat(v: string) { ui_submit_st.chat = v }
on set_name(v: string) { ui_submit_st.name = v }
on send(v: string) { ui_submit_st.said = ui_submit_st.said + "[" + v + "]" }
}
const PAGE: string = "<ui><screen name='c'><input type='text' label='Say' value='{chat}' clear-on-submit on-change='set_chat(event.value)' on-submit='send(event.value)'/><input type='text' label='Name' value='{name}' on-change='set_name(event.value)' on-submit='send(event.value)'/></screen></ui>"
function frame(ui_st: mut UiState, i: UiInput) -> void {
ui_input(ui_st, i)
ui_show(ui_st, "c", view_chat(), 0.0, 0.0, 400.0, 400.0)
}
function press(ui_st: mut UiState, tab: bool, enter: bool, pad: bool, typed: string) -> void {
let i = new UiInput
i.tab = tab
i.enter = enter
i.pad_a = pad
i.typed = typed
frame(ui_st, i)
frame(ui_st, new UiInput)
}
entry (ui_st: mut UiState, ui_submit_st: UiSubmitState) {
ui_backend(ui_st, new UiBackend)
ui_load_text(ui_st, PAGE, "c.xml")
frame(ui_st, new UiInput)
press(ui_st, true, false, false, "")
press(ui_st, false, false, false, "hi")
press(ui_st, false, true, false, "")
let after = `{ui_submit_st.said} chat '{ui_submit_st.chat}'`
press(ui_st, false, false, false, "yo")
press(ui_st, false, false, true, "")
press(ui_st, true, false, false, "")
press(ui_st, false, false, false, "Ada")
press(ui_st, false, true, false, "")
print(`{after} | {ui_submit_st.said} chat '{ui_submit_st.chat}' name '{ui_submit_st.name}'`)
}
}