aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSam Anthony <sam@samanthony.xyz>2024-02-08 15:32:34 -0500
committerSam Anthony <sam@samanthony.xyz>2024-02-08 15:32:34 -0500
commit83ce69a4785d6464fafd07e60b20b968529f018a (patch)
treebd43809eb775b672b04b3a170a70144b70bdae23
parent5b74ac13ea875f0bb645dc0054319a6f1e08d966 (diff)
downloadvolute-83ce69a4785d6464fafd07e60b20b968529f018a.zip
move Focus to gui/widget package
-rw-r--r--focus.go68
-rw-r--r--gui/widget/focus.go70
-rw-r--r--main.go9
-rw-r--r--ui.go14
4 files changed, 84 insertions, 77 deletions
diff --git a/focus.go b/focus.go
deleted file mode 100644
index cb34f74..0000000
--- a/focus.go
+++ /dev/null
@@ -1,68 +0,0 @@
-package main
-
-import "image"
-
-type Focus struct {
- widgets [][]chan bool
- p image.Point // currently focused widget
-}
-
-func NewFocus(rows []int) Focus {
- f := Focus{
- make([][]chan bool, len(rows)),
- image.Point{},
- }
- for i := range f.widgets {
- f.widgets[i] = make([]chan bool, rows[i])
- for j := range f.widgets[i] {
- f.widgets[i][j] = make(chan bool)
- }
- }
- return f
-}
-
-func (f *Focus) Close() {
- for i := range f.widgets {
- for j := range f.widgets[i] {
- close(f.widgets[i][j])
- }
- }
-}
-
-func (f *Focus) Focus(focus bool) {
- f.widgets[f.p.Y][f.p.X] <- focus
-}
-
-func (f *Focus) Left() {
- f.Focus(false)
- if f.p.X <= 0 {
- f.p.X = len(f.widgets[f.p.Y]) - 1
- } else {
- f.p.X--
- }
- f.Focus(true)
-}
-
-func (f *Focus) Right() {
- f.Focus(false)
- f.p.X = (f.p.X + 1) % len(f.widgets[f.p.Y])
- f.Focus(true)
-}
-
-func (f *Focus) Up() {
- f.Focus(false)
- if f.p.Y <= 0 {
- f.p.Y = len(f.widgets) - 1
- } else {
- f.p.Y--
- }
- f.p.X = min(f.p.X, len(f.widgets[f.p.Y])-1)
- f.Focus(true)
-}
-
-func (f *Focus) Down() {
- f.Focus(false)
- f.p.Y = (f.p.Y + 1) % len(f.widgets)
- f.p.X = min(f.p.X, len(f.widgets[f.p.Y])-1)
- f.Focus(true)
-}
diff --git a/gui/widget/focus.go b/gui/widget/focus.go
new file mode 100644
index 0000000..e2d1074
--- /dev/null
+++ b/gui/widget/focus.go
@@ -0,0 +1,70 @@
+package widget
+
+import "image"
+
+// Focus keeps track of the currently selected widget.
+// A widget receives true when it gains focus and false when it loses focus.
+type Focus struct {
+ Widgets [][]chan bool
+ p image.Point // coordinates of currently focused widget
+}
+
+func NewFocus(rows []int) Focus {
+ f := Focus{
+ make([][]chan bool, len(rows)),
+ image.Point{},
+ }
+ for i := range f.Widgets {
+ f.Widgets[i] = make([]chan bool, rows[i])
+ for j := range f.Widgets[i] {
+ f.Widgets[i][j] = make(chan bool)
+ }
+ }
+ return f
+}
+
+func (f *Focus) Close() {
+ for i := range f.Widgets {
+ for j := range f.Widgets[i] {
+ close(f.Widgets[i][j])
+ }
+ }
+}
+
+func (f *Focus) Focus(focus bool) {
+ f.Widgets[f.p.Y][f.p.X] <- focus
+}
+
+func (f *Focus) Left() {
+ f.Focus(false)
+ if f.p.X <= 0 {
+ f.p.X = len(f.Widgets[f.p.Y]) - 1
+ } else {
+ f.p.X--
+ }
+ f.Focus(true)
+}
+
+func (f *Focus) Right() {
+ f.Focus(false)
+ f.p.X = (f.p.X + 1) % len(f.Widgets[f.p.Y])
+ f.Focus(true)
+}
+
+func (f *Focus) Up() {
+ f.Focus(false)
+ if f.p.Y <= 0 {
+ f.p.Y = len(f.Widgets) - 1
+ } else {
+ f.p.Y--
+ }
+ f.p.X = min(f.p.X, len(f.Widgets[f.p.Y])-1)
+ f.Focus(true)
+}
+
+func (f *Focus) Down() {
+ f.Focus(false)
+ f.p.Y = (f.p.Y + 1) % len(f.Widgets)
+ f.p.X = min(f.p.X, len(f.Widgets[f.p.Y])-1)
+ f.Focus(true)
+}
diff --git a/main.go b/main.go
index 740e4be..edd76fe 100644
--- a/main.go
+++ b/main.go
@@ -7,6 +7,7 @@ import (
"github.com/faiface/mainthread"
"volute/gui"
+ "volute/gui/widget"
"volute/gui/win"
)
@@ -26,7 +27,7 @@ func run() {
wg := new(sync.WaitGroup)
defer wg.Wait()
- focus := NewFocus([]int{1, POINTS, POINTS, POINTS, POINTS})
+ focus := widget.NewFocus([]int{1, POINTS, POINTS, POINTS, POINTS})
defer focus.Close()
displacementChan := make(chan uint)
@@ -56,7 +57,9 @@ func run() {
displacementChan,
rpmChan, veChan, imapChan, actChan,
flowChan,
- &focus, mux, wg,
+ &focus,
+ mux,
+ wg,
)
for i := 0; i < POINTS; i++ {
@@ -73,7 +76,7 @@ func run() {
eventLoop(env, &focus)
}
-func eventLoop(env gui.Env, focus *Focus) {
+func eventLoop(env gui.Env, focus *widget.Focus) {
for event := range env.Events() {
switch event := event.(type) {
case win.WiClose:
diff --git a/ui.go b/ui.go
index 132ae89..eae60b0 100644
--- a/ui.go
+++ b/ui.go
@@ -14,7 +14,9 @@ func spawnWidgets(
displacementChan chan uint,
rpmChan, veChan, imapChan, actChan [POINTS]chan uint,
flowChan [POINTS]chan float64,
- focus *Focus, mux *gui.Mux, wg *sync.WaitGroup,
+ focus *widget.Focus,
+ mux *gui.Mux,
+ wg *sync.WaitGroup,
) {
bounds := layout.Grid{
Rows: []int{2, 7, 7, 7, 7, 7},
@@ -34,7 +36,7 @@ func spawnWidgets(
go widget.Input(
displacementChan,
bounds[1],
- focus.widgets[0][0],
+ focus.Widgets[0][0],
mux.MakeEnv(),
wg,
)
@@ -53,7 +55,7 @@ func spawnWidgets(
go widget.Input( // speed
rpmChan[i],
bounds[3+i],
- focus.widgets[1][i],
+ focus.Widgets[1][i],
mux.MakeEnv(),
wg,
)
@@ -61,7 +63,7 @@ func spawnWidgets(
go widget.Input( // VE
veChan[i],
bounds[4+POINTS+i],
- focus.widgets[2][i],
+ focus.Widgets[2][i],
mux.MakeEnv(),
wg,
)
@@ -69,7 +71,7 @@ func spawnWidgets(
go widget.Input( // IMAP
imapChan[i],
bounds[5+2*POINTS+i],
- focus.widgets[3][i],
+ focus.Widgets[3][i],
mux.MakeEnv(),
wg,
)
@@ -77,7 +79,7 @@ func spawnWidgets(
go widget.Input( // ACT
actChan[i],
bounds[6+3*POINTS+i],
- focus.widgets[4][i],
+ focus.Widgets[4][i],
mux.MakeEnv(),
wg,
)