aboutsummaryrefslogtreecommitdiffstats
path: root/gui/widget/focus.go
blob: d41b5b13be6ec6e449bb9cbf19ddee09cb05f20f (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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package widget

import (
	"fmt"
	"image"
)

type Direction int

const (
	UP Direction = iota
	DOWN
	LEFT
	RIGHT
)

type FocusSlave struct {
	gain  <-chan bool
	lose  <-chan Direction // forward to yield if widget accepts focus loss
	yield chan<- Direction
}

type focusSlave struct {
	gain chan bool
	lose chan Direction
}

func (fs focusSlave) close() {
	close(fs.gain)
	close(fs.lose)
}

type FocusMaster struct {
	slaves  [][]focusSlave
	yield   chan Direction
	focused image.Point // coordinates of focused slave
}

func NewFocusMaster(rows []int) *FocusMaster {
	fm := &FocusMaster{
		slaves:  make([][]focusSlave, len(rows)),
		yield:   make(chan Direction),
		focused: image.Point{0, 0},
	}
	for y := range fm.slaves {
		fm.slaves[y] = make([]focusSlave, rows[y])
		for x := range fm.slaves[y] {
			fm.slaves[y][x] = focusSlave{make(chan bool), make(chan Direction)}
		}
	}

	go func() {
		fm.slaves[0][0].gain <- true

		for dir := range fm.yield {
			fm.focused = fm.neighborPos(fm.focused, dir)
			fm.slaves[fm.focused.Y][fm.focused.X].gain <- true
		}
	}()

	return fm
}

func (fm FocusMaster) Slave(y, x int) FocusSlave {
	return FocusSlave{
		gain:  fm.slaves[y][x].gain,
		lose:  fm.slaves[y][x].lose,
		yield: fm.yield,
	}
}

func (fm FocusMaster) Close() {
	for y := range fm.slaves {
		for x := range fm.slaves[y] {
			fm.slaves[y][x].close()
		}
	}
	close(fm.yield)
}

func (fm FocusMaster) Shift(dir Direction) {
	fm.slaves[fm.focused.Y][fm.focused.X].lose <- dir
}

func (fm FocusMaster) neighborPos(pos image.Point, dir Direction) image.Point {
	switch dir {
	case UP:
		return fm.upNeighborPos(pos)
	case DOWN:
		return fm.downNeighborPos(pos)
	case LEFT:
		return fm.leftNeighborPos(pos)
	case RIGHT:
		return fm.rightNeighborPos(pos)
	default:
		panic(fmt.Sprintf("invalid Direction: %v", dir))
	}
}

func (fm FocusMaster) upNeighborPos(pos image.Point) image.Point {
	if pos.Y <= 0 {
		pos.Y = len(fm.slaves) - 1
	} else {
		pos.Y--
	}
	pos.X = min(pos.X, len(fm.slaves[pos.Y])-1)
	return pos
}

func (fm FocusMaster) downNeighborPos(pos image.Point) image.Point {
	if pos.Y >= len(fm.slaves)-1 {
		pos.Y = 0
	} else {
		pos.Y++
	}
	pos.X = min(pos.X, len(fm.slaves[pos.Y])-1)
	return pos
}

func (fm FocusMaster) leftNeighborPos(pos image.Point) image.Point {
	if pos.X <= 0 {
		pos.X = len(fm.slaves[pos.Y]) - 1
	} else {
		pos.X--
	}
	return pos
}

func (fm FocusMaster) rightNeighborPos(pos image.Point) image.Point {
	if pos.X >= len(fm.slaves[pos.Y])-1 {
		pos.X = 0
	} else {
		pos.X++
	}
	return pos
}