forked from rivo/tview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.go
282 lines (246 loc) · 6.34 KB
/
application.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package tview
import (
"sync"
"github.com/gdamore/tcell"
)
// Application represents the top node of an application.
//
// It is not strictly required to use this class as none of the other classes
// depend on it. However, it provides useful tools to set up an application and
// plays nicely with all widgets.
type Application struct {
sync.RWMutex
// The application's screen.
screen tcell.Screen
// The primitive which currently has the keyboard focus.
focus Primitive
// The root primitive to be seen on the screen.
root Primitive
// Whether or not the application resizes the root primitive.
rootAutoSize bool
// Key overrides.
keyOverrides map[tcell.Key]func(p Primitive) bool
// Rune overrides.
runeOverrides map[rune]func(p Primitive) bool
}
// NewApplication creates and returns a new application.
func NewApplication() *Application {
return &Application{
keyOverrides: make(map[tcell.Key]func(p Primitive) bool),
runeOverrides: make(map[rune]func(p Primitive) bool),
}
}
// SetKeyCapture installs a global capture function for the given key. It
// intercepts all events for the given key and routes them to the handler.
// The handler receives the Primitive to which the key is originally redirected,
// the one which has focus, or nil if it was not directed to a Primitive. The
// handler also returns whether or not the key event is then forwarded to that
// Primitive.
//
// Special keys (e.g. Escape, Enter, or Ctrl-A) are defined by the "key"
// argument. The "ch" rune is ignored. Other keys (e.g. "a", "h", or "5") are
// specified by their rune, with key set to tcell.KeyRune. See also
// https://godoc.org/github.com/gdamore/tcell#EventKey for more information.
//
// To remove a handler again, provide a nil handler for the same key.
//
// The application itself will exit when Ctrl-C is pressed. You can intercept
// this with this function as well.
func (a *Application) SetKeyCapture(key tcell.Key, ch rune, handler func(p Primitive) bool) *Application {
if key == tcell.KeyRune {
if handler != nil {
a.runeOverrides[ch] = handler
} else {
if _, ok := a.runeOverrides[ch]; ok {
delete(a.runeOverrides, ch)
}
}
} else {
if handler != nil {
a.keyOverrides[key] = handler
} else {
if _, ok := a.keyOverrides[key]; ok {
delete(a.keyOverrides, key)
}
}
}
return a
}
// Run starts the application and thus the event loop. This function returns
// when Stop() was called.
func (a *Application) Run() error {
var err error
a.Lock()
// Make a screen.
a.screen, err = tcell.NewScreen()
if err != nil {
a.Unlock()
return err
}
if err = a.screen.Init(); err != nil {
a.Unlock()
return err
}
// We catch panics to clean up because they mess up the terminal.
defer func() {
if p := recover(); p != nil {
if a.screen != nil {
a.screen.Fini()
}
panic(p)
}
}()
// Draw the screen for the first time.
a.Unlock()
a.Draw()
// Start event loop.
for {
a.RLock()
if a.screen == nil {
a.RUnlock()
break
}
event := a.screen.PollEvent()
a.RUnlock()
if event == nil {
break // The screen was finalized.
}
switch event := event.(type) {
case *tcell.EventKey:
a.RLock()
p := a.focus
a.RUnlock()
// Intercept keys.
if event.Key() == tcell.KeyRune {
if handler, ok := a.runeOverrides[event.Rune()]; ok {
if !handler(p) {
break
}
}
} else {
if handler, ok := a.keyOverrides[event.Key()]; ok {
pr := p
if event.Key() == tcell.KeyCtrlC {
pr = nil
}
if !handler(pr) {
break
}
}
}
// Ctrl-C closes the application.
if event.Key() == tcell.KeyCtrlC {
a.Stop()
}
// Pass other key events to the currently focused primitive.
if p != nil {
if handler := p.InputHandler(); handler != nil {
handler(event, func(p Primitive) {
a.SetFocus(p)
})
a.Draw()
}
}
case *tcell.EventResize:
if a.rootAutoSize && a.root != nil {
a.Lock()
width, height := a.screen.Size()
a.root.SetRect(0, 0, width, height)
a.Unlock()
a.Draw()
}
a.Draw()
case *tcell.EventInterrupt:
if p, ok := event.Data().(Primitive); ok {
a.SetFocus(p)
a.Draw()
}
}
}
return nil
}
func (a *Application) Interrupt(v interface{}) {
a.screen.PostEvent(tcell.NewEventInterrupt(v))
}
// Stop stops the application, causing Run() to return.
func (a *Application) Stop() {
a.RLock()
defer a.RUnlock()
if a.screen == nil {
return
}
a.screen.Fini()
a.screen = nil
}
// Draw refreshes the screen. It calls the Draw() function of the application's
// root primitive and then syncs the screen buffer.
func (a *Application) Draw() *Application {
a.RLock()
defer a.RUnlock()
// Maybe we're not ready yet or not anymore.
if a.screen == nil || a.root == nil {
return a
}
// Resize if requested.
if a.rootAutoSize && a.root != nil {
width, height := a.screen.Size()
a.root.SetRect(0, 0, width, height)
}
// Draw all primitives.
a.root.Draw(a.screen)
// Sync screen.
a.screen.Show()
return a
}
// SetRoot sets the root primitive for this application. This function must be
// called or nothing will be displayed when the application starts.
//
// It also calls SetFocus() on the primitive.
func (a *Application) SetRoot(root Primitive, autoSize bool) *Application {
a.Lock()
a.root = root
a.rootAutoSize = autoSize
if a.screen != nil {
a.screen.Clear()
}
a.Unlock()
a.SetFocus(root)
return a
}
// ResizeToFullScreen resizes the given primitive such that it fills the entire
// screen.
func (a *Application) ResizeToFullScreen(p Primitive) *Application {
a.RLock()
width, height := a.screen.Size()
a.RUnlock()
p.SetRect(0, 0, width, height)
return a
}
// SetFocus sets the focus on a new primitive. All key events will be redirected
// to that primitive. Callers must ensure that the primitive will handle key
// events.
//
// Blur() will be called on the previously focused primitive. Focus() will be
// called on the new primitive.
func (a *Application) SetFocus(p Primitive) *Application {
a.Lock()
if a.focus != nil {
a.focus.Blur()
}
a.focus = p
if a.screen != nil {
a.screen.HideCursor()
}
a.Unlock()
p.Focus(func(p Primitive) {
a.SetFocus(p)
})
return a
}
// GetFocus returns the primitive which has the current focus. If none has it,
// nil is returned.
func (a *Application) GetFocus() Primitive {
a.RLock()
defer a.RUnlock()
return a.focus
}