aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSam Anthony <sam@samanthony.xyz>2024-01-19 21:26:16 -0500
committerSam Anthony <sam@samanthony.xyz>2024-01-19 21:26:16 -0500
commit39a3b948e525fe2b22b90e07df323ab77f6a81f6 (patch)
tree2054a4b5da06685b27a8346326aa286e0bed3634
parentb039cf83c33630396f245fbcba79383e0b2db383 (diff)
downloadvolute-39a3b948e525fe2b22b90e07df323ab77f6a81f6.zip
keep track of focused widget
-rw-r--r--focus.go33
-rw-r--r--gui/widget/widget.go44
-rw-r--r--main.go24
3 files changed, 78 insertions, 23 deletions
diff --git a/focus.go b/focus.go
new file mode 100644
index 0000000..e6de390
--- /dev/null
+++ b/focus.go
@@ -0,0 +1,33 @@
+package main
+
+type Focus struct {
+ widgets []chan bool
+ i int // index of focused widget
+}
+
+func NewFocus(nWidgets int) Focus {
+ f := Focus{make([]chan bool, nWidgets), 0}
+ for i := range f.widgets {
+ f.widgets[i] = make(chan bool)
+ }
+ return f
+}
+
+func (f *Focus) Next() {
+ f.widgets[f.i] <- false
+ f.i = (f.i + 1) % len(f.widgets)
+ f.widgets[f.i] <- true
+}
+
+func (f *Focus) Prev() {
+ f.widgets[f.i] <- false
+ f.i = abs(f.i-1) % len(f.widgets)
+ f.widgets[f.i] <- true
+}
+
+func abs(n int) int {
+ if n < 0 {
+ return -n
+ }
+ return n
+}
diff --git a/gui/widget/widget.go b/gui/widget/widget.go
index 216b146..94d1915 100644
--- a/gui/widget/widget.go
+++ b/gui/widget/widget.go
@@ -28,7 +28,7 @@ func Label(text string, r image.Rectangle, env gui.Env) {
close(env.Draw())
}
-func Input(val chan<- uint, r image.Rectangle, env gui.Env) {
+func Input(val chan<- uint, r image.Rectangle, focusChan <-chan bool, env gui.Env) {
redraw := func(text []byte) func(draw.Image) image.Rectangle {
return func(drw draw.Image) image.Rectangle {
drawText(text, drw, r)
@@ -40,27 +40,27 @@ func Input(val chan<- uint, r image.Rectangle, env gui.Env) {
env.Draw() <- redraw(text)
- for event := range env.Events() {
- switch event := event.(type) {
- case win.WiFocus:
- if event.Focused {
- env.Draw() <- redraw(text)
- }
- case win.MoDown:
- if event.Point.In(r) {
- focus = true
- }
- case win.KbType:
- if focus && isDigit(event.Rune) {
- text = fmt.Appendf(text, "%c", event.Rune)
- env.Draw() <- redraw(text)
- val <- atoi(text)
- }
- case win.KbDown:
- if focus && event.Key == win.KeyBackspace && len(text) > 0 {
- text = text[:len(text)-1]
- env.Draw() <- redraw(text)
- val <- atoi(text)
+ for {
+ select {
+ case focus = <-focusChan:
+ case event := <-env.Events():
+ switch event := event.(type) {
+ case win.WiFocus:
+ if event.Focused {
+ env.Draw() <- redraw(text)
+ }
+ case win.KbType:
+ if focus && isDigit(event.Rune) {
+ text = fmt.Appendf(text, "%c", event.Rune)
+ env.Draw() <- redraw(text)
+ val <- atoi(text)
+ }
+ case win.KbDown:
+ if focus && event.Key == win.KeyBackspace && len(text) > 0 {
+ text = text[:len(text)-1]
+ env.Draw() <- redraw(text)
+ val <- atoi(text)
+ }
}
}
}
diff --git a/main.go b/main.go
index a173fd4..0161a26 100644
--- a/main.go
+++ b/main.go
@@ -20,6 +20,7 @@ func run() {
var (
displacementChan = make(chan uint)
output = make(chan uint)
+ focus = NewFocus(2)
displacement uint = 0
)
@@ -29,6 +30,7 @@ func run() {
go widget.Input(
displacementChan,
r,
+ focus.widgets[0],
mux.MakeEnv(),
)
r = image.Rect(
@@ -40,6 +42,19 @@ func run() {
go widget.Label("cc", r, mux.MakeEnv())
r = image.Rect(
+ r.Max.X+pad,
+ r.Min.Y,
+ r.Max.X+pad+widget.TextWidth(6),
+ r.Max.Y,
+ )
+ go widget.Input(
+ displacementChan,
+ r,
+ focus.widgets[1],
+ mux.MakeEnv(),
+ )
+
+ r = image.Rect(
pad,
r.Max.Y+pad,
pad+widget.TextWidth(6),
@@ -47,6 +62,8 @@ func run() {
)
go widget.Output(output, r, mux.MakeEnv())
+ focus.widgets[focus.i] <- true
+
Loop:
for {
select {
@@ -60,8 +77,13 @@ Loop:
case win.WiClose:
break Loop
case win.KbType:
- if event.Rune == 'q' {
+ switch event.Rune {
+ case 'q':
break Loop
+ case 'j', 'l':
+ focus.Next()
+ case 'k', 'h':
+ focus.Prev()
}
}
}