-
Notifications
You must be signed in to change notification settings - Fork 6
/
window.go
244 lines (214 loc) · 5.43 KB
/
window.go
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// Copyright (c) 2016 Company 0, LLC.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package ttk
import (
"fmt"
"unicode/utf8"
"github.com/nsf/termbox-go"
)
// Window contains a window context.
type Window struct {
id int // window id
x int // max x
y int // max y
mgr Windower // key handler + renderer
backingStore []Cell // output buffer
widgets []Widgeter // window widgets
focus int // currently focused widget
}
// Windower interface. Each window has a Windower interface associated with
// it. It provides all functions to deal with rendering and user interaction.
type Windower interface {
Init(w *Window)
Render(w *Window)
KeyHandler(*Window, Key)
}
// AddWidget is the generic function to add a widget to a window. This
// function should only be called by widgets. Application code, by convention,
// should call the non-generic type asserted call (i.e. AddLabel).
// AddWidget shall be called from queue context.
func (w *Window) AddWidget(id string, x, y int) (Widgeter, error) {
rw, found := registeredWidgets[id]
if !found {
return nil, ErrWidgetNotRegistered
}
widget, err := rw(w, x, y)
if err != nil {
return nil, err
}
w.widgets = append(w.widgets, widget)
return widget, err
}
// printf prints into the backend buffer.
// This will not show immediately.
// printf shall be called from queue context.
func (w *Window) printf(x, y int, a Attributes, format string,
args ...interface{}) {
out := fmt.Sprintf(format, args...)
xx := 0
c := Cell{}
c.Fg = a.Fg
c.Bg = a.Bg
mx := w.x - x
var rw int
for i := 0; i < len(out); i += rw {
if x+xx+1 > mx {
break
}
v, width := utf8.DecodeRuneInString(out[i:])
if v == '\x1b' {
// see if we understand this escape seqeunce
cc, skip, err := DecodeColor(out[i:])
if err == nil {
c.Fg = cc.Fg
c.Bg = cc.Bg
rw = skip
continue
}
}
rw = width
c.Ch = v
w.setCell(x+xx, y, c)
xx++
}
}
// setCell sets the content of the window cell at the x and y coordinate.
// setCell shall be called from queue context.
func (w *Window) setCell(x, y int, c Cell) {
c.dirty = true
pos := x + (y * w.x)
if pos < len(w.backingStore) {
w.backingStore[pos] = c
}
}
// getCell returns the content of the window cell at the x and y coordinate.
// getCell shall be called from queue context.
func (w *Window) getCell(x, y int) *Cell {
pos := x + (y * w.x)
if pos < len(w.backingStore) {
return &w.backingStore[pos]
}
return nil
}
// resize sets new x and y maxima.
// resize shall be called from queue context.
func (w *Window) resize(x, y int) {
w.x = x
w.y = y
w.backingStore = make([]Cell, x*y)
// iterate over widgets
for _, widget := range w.widgets {
widget.Resize()
}
}
// render calls the user provided Render and than renders the widgets in the
// window.
func (w *Window) render() {
w.mgr.Render(w)
// iterate over widgets
for _, widget := range w.widgets {
widget.Render()
}
// focus on a widget
w.focusWidget()
}
// focusWidget focuses on the current widget. If focus is -1 it'll focus on
// the first available widget.
// focusWidget shall be called from queue context.
func (w *Window) focusWidget() {
setCursor(-1, -1) // hide
if w.focus < 0 {
for i, widget := range w.widgets {
if widget.CanFocus() {
w.focus = i
widget.Focus()
return
}
}
// nothing to do
return
}
// make sure we are in bounds
if w.focus > len(w.widgets) {
// this really should not happen
return
}
w.widgets[w.focus].Focus()
}
// focusNext focuses on the next available widget.
// focusNext shall be called from queue context.
func (w *Window) focusNext() {
if w.focus < 0 {
w.focusWidget()
return
}
// find next widget
for i, widget := range w.widgets[w.focus+1:] {
if !widget.CanFocus() {
continue
}
setCursor(-1, -1) // hide
w.focus = i + w.focus + 1
widget.Focus()
return
}
// if we get here there was nothing to focus on so focus on first widget
w.focus = -1
w.focusWidget()
}
// FocusNext focuses on the next available widget.
func (w *Window) FocusNext() {
Queue(func() {
w.focusNext()
flush()
})
}
// focusPrevious focuses on the previous available widget.
// focusPrevious shall be called from queue context.
func (w *Window) focusPrevious() {
// it is ok to be negative since that'll focus on the first widget
w.focus--
if w.focus < 0 {
w.focusWidget()
return
}
// find previous widget
for i := w.focus; i > 0; i-- {
widget := w.widgets[i]
if !widget.CanFocus() {
continue
}
setCursor(-1, -1) // hide
w.focus = i
widget.Focus()
return
}
// if we get here we need to focus on last focusable widget
for i := len(w.widgets) - 1; i > w.focus; i-- {
widget := w.widgets[i]
if !widget.CanFocus() {
continue
}
setCursor(-1, -1) // hide
w.focus = i
widget.Focus()
return
}
// if we get here it means we found nothing usable and give up
}
// FocusPrevious focuses on the next available widget.
func (w *Window) FocusPrevious() {
Queue(func() {
w.focusPrevious()
flush()
})
}
// keyHandler routes event to proper widget. This is called from queue context
// so be careful to not use blocking calls.
func (w *Window) keyHandler(ev termbox.Event) (bool, Windower, Widgeter) {
if w.focus < 0 || w.focus > len(w.widgets) {
return false, w.mgr, nil // not used
}
return w.widgets[w.focus].KeyHandler(ev), w.mgr, w.widgets[w.focus]
}