aboutsummaryrefslogtreecommitdiffstats
path: root/gui/widget/input.go
diff options
context:
space:
mode:
Diffstat (limited to 'gui/widget/input.go')
-rw-r--r--gui/widget/input.go74
1 files changed, 74 insertions, 0 deletions
diff --git a/gui/widget/input.go b/gui/widget/input.go
new file mode 100644
index 0000000..d4d6738
--- /dev/null
+++ b/gui/widget/input.go
@@ -0,0 +1,74 @@
+package widget
+
+import (
+ "fmt"
+ "sync"
+
+ "image"
+ "image/draw"
+
+ "volute/gui"
+ "volute/gui/text"
+ "volute/gui/win"
+)
+
+func Input(val chan<- uint, r image.Rectangle, focus FocusSlave, env gui.Env, wg *sync.WaitGroup) {
+ defer wg.Done()
+ defer close(env.Draw())
+ defer close(val)
+
+ text := []byte{'0'}
+ focused := false
+ env.Draw() <- inputDraw(text, focused, r)
+Loop:
+ for {
+ select {
+ case _, ok := <-focus.gain:
+ if !ok {
+ break Loop
+ }
+ focused = true
+ env.Draw() <- inputDraw(text, focused, r)
+ case dir, ok := <-focus.lose:
+ if !ok {
+ break Loop
+ }
+ focus.yield <- dir
+ focused = false
+ env.Draw() <- inputDraw(text, focused, r)
+ case event, ok := <-env.Events():
+ if !ok {
+ break Loop
+ }
+ switch event := event.(type) {
+ case win.WiFocus:
+ if event.Focused {
+ env.Draw() <- inputDraw(text, focused, r)
+ }
+ case win.KbType:
+ if focused && isDigit(event.Rune) {
+ text = fmt.Appendf(text, "%c", event.Rune)
+ env.Draw() <- inputDraw(text, focused, r)
+ val <- atoi(text)
+ }
+ case win.KbDown:
+ if focused && event.Key == win.KeyBackspace && len(text) > 0 {
+ text = text[:len(text)-1]
+ env.Draw() <- inputDraw(text, focused, r)
+ val <- atoi(text)
+ }
+ }
+ }
+ }
+}
+
+func inputDraw(str []byte, focused bool, r image.Rectangle) func(draw.Image) image.Rectangle {
+ return func(drw draw.Image) image.Rectangle {
+ if focused {
+ text.Draw(str, drw, r, GREEN, FOCUS_COLOR)
+ } else {
+ text.Draw(str, drw, r, GREEN, WHITE)
+ }
+ return r
+ }
+}