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
|
update = state event \ event
|
||||||
| UpdateText newText \ state.{ text = newText }
|
| ArrowLeft \ state.{ cursorPos = max 0 (state.cursorPos - 1) }
|
||||||
| Submit _ \ state.{ text = "" }
|
| ArrowRight \ state.{ cursorPos = min (len state.text) (state.cursorPos + 1) }
|
||||||
| Go \ state.{ text = "" };
|
| 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 \
|
view = state viewport \
|
||||||
Padding {
|
# charWidth = 9.65;
|
||||||
amount = 20,
|
# cursorX = state.cursorPos * charWidth;
|
||||||
child = Column {
|
textBeforeCursor = slice state.text 0 state.cursorPos;
|
||||||
gap = 20,
|
cursorX = measureText textBeforeCursor;
|
||||||
|
|
||||||
|
Column {
|
||||||
|
gap = 20,
|
||||||
|
children = [
|
||||||
|
Text {
|
||||||
|
content = "Text: " & state.text & " | Cursor: " & str(state.cursorPos),
|
||||||
|
x = 0,
|
||||||
|
y = 20
|
||||||
|
},
|
||||||
|
|
||||||
|
# Text Input Component
|
||||||
|
Stack {
|
||||||
children = [
|
children = [
|
||||||
Text {
|
Rect { w = 300, h = 40, color = "white", radius = 4 },
|
||||||
content = "window: " & str(viewport.width) & " x " & str(viewport.height),
|
|
||||||
x = 0,
|
# Text content
|
||||||
y = 20
|
Positioned {
|
||||||
},
|
x = 8,
|
||||||
Text { content = "You typed: " & state.text, x = 0, y = 20 },
|
y = 8,
|
||||||
Stack {
|
child = Text { content = state.text, x = 0, y = 17 }
|
||||||
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
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
},
|
||||||
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 }
|
{ init = init, update = update, view = view }
|
||||||
|
|||||||
Loading…
Reference in New Issue