aboutsummaryrefslogtreecommitdiffstats
path: root/focus.go
blob: e6de390866aba490f5e8d8a586096940555943bc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
}