2017-12-15 22:29:21 +08:00
|
|
|
package tview
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/gdamore/tcell"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Application represents the top node of an application.
|
|
|
|
type Application struct {
|
|
|
|
sync.Mutex
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewApplication creates and returns a new application.
|
|
|
|
func NewApplication() *Application {
|
|
|
|
return &Application{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run starts the application and thus the event loop. This function returns
|
|
|
|
// when Stop() was called.
|
|
|
|
func (a *Application) Run() error {
|
|
|
|
var err error
|
2017-12-24 07:08:52 +08:00
|
|
|
a.Lock()
|
2017-12-15 22:29:21 +08:00
|
|
|
|
|
|
|
// Make a screen.
|
|
|
|
a.screen, err = tcell.NewScreen()
|
|
|
|
if err != nil {
|
2017-12-24 07:08:52 +08:00
|
|
|
a.Unlock()
|
2017-12-15 22:29:21 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = a.screen.Init(); err != nil {
|
2017-12-24 07:08:52 +08:00
|
|
|
a.Unlock()
|
2017-12-15 22:29:21 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// We catch panics to clean up because they mess up the terminal.
|
|
|
|
defer func() {
|
|
|
|
if p := recover(); p != nil {
|
2017-12-19 03:04:52 +08:00
|
|
|
if a.screen != nil {
|
|
|
|
a.screen.Fini()
|
|
|
|
}
|
2017-12-15 22:29:21 +08:00
|
|
|
panic(p)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Draw the screen for the first time.
|
|
|
|
if a.rootAutoSize && a.root != nil {
|
|
|
|
width, height := a.screen.Size()
|
|
|
|
a.root.SetRect(0, 0, width, height)
|
|
|
|
}
|
2017-12-24 07:08:52 +08:00
|
|
|
a.Unlock()
|
2017-12-15 22:29:21 +08:00
|
|
|
a.Draw()
|
|
|
|
|
|
|
|
// Start event loop.
|
|
|
|
for {
|
2017-12-19 03:04:52 +08:00
|
|
|
if a.screen == nil {
|
|
|
|
break
|
|
|
|
}
|
2017-12-15 22:29:21 +08:00
|
|
|
event := a.screen.PollEvent()
|
|
|
|
if event == nil {
|
|
|
|
break // The screen was finalized.
|
|
|
|
}
|
|
|
|
switch event := event.(type) {
|
|
|
|
case *tcell.EventKey:
|
|
|
|
if event.Key() == tcell.KeyCtrlC {
|
2017-12-19 03:04:52 +08:00
|
|
|
a.Stop() // Ctrl-C closes the application.
|
2017-12-15 22:29:21 +08:00
|
|
|
}
|
|
|
|
a.Lock()
|
2017-12-19 03:04:52 +08:00
|
|
|
p := a.focus // Pass other key events to the currently focused primitive.
|
2017-12-15 22:29:21 +08:00
|
|
|
a.Unlock()
|
|
|
|
if p != nil {
|
|
|
|
if handler := p.InputHandler(); handler != nil {
|
2017-12-19 03:04:52 +08:00
|
|
|
handler(event, func(p Primitive) {
|
|
|
|
a.SetFocus(p)
|
|
|
|
})
|
2017-12-15 22:29:21 +08:00
|
|
|
a.Draw()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case *tcell.EventResize:
|
|
|
|
if a.rootAutoSize && a.root != nil {
|
|
|
|
a.Lock()
|
2017-12-24 07:08:52 +08:00
|
|
|
width, height := a.screen.Size()
|
2017-12-15 22:29:21 +08:00
|
|
|
a.root.SetRect(0, 0, width, height)
|
|
|
|
a.Unlock()
|
|
|
|
a.Draw()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop stops the application, causing Run() to return.
|
|
|
|
func (a *Application) Stop() {
|
2017-12-19 03:04:52 +08:00
|
|
|
if a.screen == nil {
|
|
|
|
return
|
|
|
|
}
|
2017-12-15 22:29:21 +08:00
|
|
|
a.screen.Fini()
|
2017-12-19 03:04:52 +08:00
|
|
|
a.screen = nil
|
2017-12-15 22:29:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.Lock()
|
|
|
|
defer a.Unlock()
|
|
|
|
|
2017-12-19 03:04:52 +08:00
|
|
|
// Maybe we're not ready yet or not anymore.
|
2017-12-24 07:08:52 +08:00
|
|
|
if a.screen == nil || a.root == nil {
|
2017-12-15 22:29:21 +08:00
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw all primitives.
|
2017-12-24 07:08:52 +08:00
|
|
|
a.root.Draw(a.screen)
|
2017-12-15 22:29:21 +08:00
|
|
|
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// If autoSize is set to true, the application will set the root primitive's
|
|
|
|
// position to (0,0) and its size to the screen's size. It will also resize and
|
|
|
|
// redraw it when the screen resizes.
|
|
|
|
func (a *Application) SetRoot(root Primitive, autoSize bool) *Application {
|
|
|
|
a.Lock()
|
|
|
|
defer a.Unlock()
|
|
|
|
|
|
|
|
a.root = root
|
|
|
|
a.rootAutoSize = autoSize
|
|
|
|
|
|
|
|
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 {
|
|
|
|
if p.InputHandler() == nil {
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
|
|
|
a.Lock()
|
|
|
|
if a.focus != nil {
|
|
|
|
a.focus.Blur()
|
|
|
|
}
|
|
|
|
a.focus = p
|
|
|
|
a.Unlock()
|
2017-12-19 03:04:52 +08:00
|
|
|
p.Focus(func(p Primitive) {
|
|
|
|
a.SetFocus(p)
|
|
|
|
})
|
2017-12-15 22:29:21 +08:00
|
|
|
|
|
|
|
return a
|
|
|
|
}
|
2017-12-21 03:54:49 +08:00
|
|
|
|
|
|
|
// GetFocus returns the primitive which has the current focus. If none has it,
|
|
|
|
// nil is returned.
|
|
|
|
func (a *Application) GetFocus() Primitive {
|
|
|
|
return a.focus
|
|
|
|
}
|