aboutsummaryrefslogtreecommitdiffstats
path: root/gui
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 /gui
parent5b74ac13ea875f0bb645dc0054319a6f1e08d966 (diff)
downloadvolute-83ce69a4785d6464fafd07e60b20b968529f018a.zip
move Focus to gui/widget package
Diffstat (limited to 'gui')
-rw-r--r--gui/widget/focus.go70
1 files changed, 70 insertions, 0 deletions
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)
+}