creating a CG text input
parent
12d27a1bff
commit
bc186d658c
@ -1,41 +1,78 @@
|
||||
init = { text = "" };
|
||||
# Helpers
|
||||
|
||||
insertChar = text pos char \
|
||||
before = slice text 0 pos;
|
||||
after = slice text pos (len text);
|
||||
before & char & after;
|
||||
|
||||
deleteChar = text pos \
|
||||
(pos == 0
|
||||
| True \ text
|
||||
| False \
|
||||
(before = slice text 0 (pos - 1);
|
||||
after = slice text pos (len text);
|
||||
before & after));
|
||||
|
||||
# test app
|
||||
|
||||
init = {
|
||||
text = "hello world",
|
||||
cursorPos = 5
|
||||
};
|
||||
|
||||
update = state event \ event
|
||||
| UpdateText newText \ state.{ text = newText }
|
||||
| Submit _ \ state.{ text = "" }
|
||||
| Go \ state.{ text = "" };
|
||||
| ArrowLeft \ state.{ cursorPos = max 0 (state.cursorPos - 1) }
|
||||
| ArrowRight \ state.{ cursorPos = min (len state.text) (state.cursorPos + 1) }
|
||||
| Backspace \ {
|
||||
text = deleteChar state.text state.cursorPos,
|
||||
cursorPos = max 0 (state.cursorPos - 1)
|
||||
}
|
||||
| Char c \ {
|
||||
text = insertChar state.text state.cursorPos c,
|
||||
cursorPos = state.cursorPos + 1
|
||||
}
|
||||
| _ \ state;
|
||||
|
||||
view = state viewport \
|
||||
Padding {
|
||||
amount = 20,
|
||||
child = Column {
|
||||
# charWidth = 9.65;
|
||||
# cursorX = state.cursorPos * charWidth;
|
||||
textBeforeCursor = slice state.text 0 state.cursorPos;
|
||||
cursorX = measureText textBeforeCursor;
|
||||
|
||||
Column {
|
||||
gap = 20,
|
||||
children = [
|
||||
Text {
|
||||
content = "window: " & str(viewport.width) & " x " & str(viewport.height),
|
||||
content = "Text: " & state.text & " | Cursor: " & str(state.cursorPos),
|
||||
x = 0,
|
||||
y = 20
|
||||
},
|
||||
Text { content = "You typed: " & state.text, x = 0, y = 20 },
|
||||
|
||||
# Text Input Component
|
||||
Stack {
|
||||
children = [
|
||||
Rect { w = 300, h = 40, color = "blue", radius = 2 },
|
||||
TextInput {
|
||||
value = state.text,
|
||||
placeholder = "Type something...",
|
||||
x = 5,
|
||||
y = 5,
|
||||
w = 290,
|
||||
h = 30,
|
||||
focused = True,
|
||||
onInput = UpdateText,
|
||||
onSubmit = Submit
|
||||
}
|
||||
]
|
||||
Rect { w = 300, h = 40, color = "white", radius = 4 },
|
||||
|
||||
# Text content
|
||||
Positioned {
|
||||
x = 8,
|
||||
y = 8,
|
||||
child = Text { content = state.text, x = 0, y = 17 }
|
||||
},
|
||||
button { label = "Go", event = Go, theme = theme }
|
||||
|
||||
# Cursor
|
||||
Positioned {
|
||||
x = 8 + cursorX,
|
||||
y = 8,
|
||||
child = Rect {
|
||||
w = 2,
|
||||
h = 24,
|
||||
color = "black"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
{ init = init, update = update, view = view }
|
||||
|
||||
Loading…
Reference in New Issue