2017-12-15 14:29:21 +00:00
|
|
|
package tview
|
|
|
|
|
|
|
|
import (
|
2018-03-13 07:16:09 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2017-12-15 14:29:21 +00:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/gdamore/tcell"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Application represents the top node of an application.
|
2017-12-26 16:34:58 +00:00
|
|
|
//
|
|
|
|
// 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.
|
2017-12-15 14:29:21 +00:00
|
|
|
type Application struct {
|
2017-12-26 16:34:58 +00:00
|
|
|
sync.RWMutex
|
2017-12-15 14:29:21 +00:00
|
|
|
|
|
|
|
// 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
|
2017-12-26 16:34:58 +00:00
|
|
|
|
2017-12-27 15:04:21 +00:00
|
|
|
// Whether or not the application resizes the root primitive.
|
2018-03-05 15:37:10 +00:00
|
|
|
rootFullscreen bool
|
2017-12-27 15:04:21 +00:00
|
|
|
|
2018-01-14 20:29:34 +00:00
|
|
|
// An optional capture function which receives a key event and returns the
|
|
|
|
// event to be forwarded to the default input handler (nil if nothing should
|
|
|
|
// be forwarded).
|
|
|
|
inputCapture func(event *tcell.EventKey) *tcell.EventKey
|
2018-02-20 16:15:17 +00:00
|
|
|
|
|
|
|
// An optional callback function which is invoked just before the root
|
|
|
|
// primitive is drawn.
|
|
|
|
beforeDraw func(screen tcell.Screen) bool
|
|
|
|
|
|
|
|
// An optional callback function which is invoked after the root primitive
|
|
|
|
// was drawn.
|
|
|
|
afterDraw func(screen tcell.Screen)
|
2018-03-13 07:16:09 +00:00
|
|
|
|
|
|
|
// If this value is true, the application has entered suspended mode.
|
|
|
|
suspended bool
|
2017-12-15 14:29:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewApplication creates and returns a new application.
|
|
|
|
func NewApplication() *Application {
|
2018-01-14 20:29:34 +00:00
|
|
|
return &Application{}
|
2017-12-26 16:34:58 +00:00
|
|
|
}
|
|
|
|
|
2018-01-14 20:29:34 +00:00
|
|
|
// SetInputCapture sets a function which captures all key events before they are
|
|
|
|
// forwarded to the key event handler of the primitive which currently has
|
|
|
|
// focus. This function can then choose to forward that key event (or a
|
|
|
|
// different one) by returning it or stop the key event processing by returning
|
|
|
|
// nil.
|
2017-12-26 16:34:58 +00:00
|
|
|
//
|
2018-01-14 20:29:34 +00:00
|
|
|
// Note that this also affects the default event handling of the application
|
|
|
|
// itself: Such a handler can intercept the Ctrl-C event which closes the
|
2018-01-17 20:17:59 +00:00
|
|
|
// applicatoon.
|
2018-01-14 20:29:34 +00:00
|
|
|
func (a *Application) SetInputCapture(capture func(event *tcell.EventKey) *tcell.EventKey) *Application {
|
|
|
|
a.inputCapture = capture
|
2017-12-26 16:34:58 +00:00
|
|
|
return a
|
2017-12-15 14:29:21 +00:00
|
|
|
}
|
|
|
|
|
2018-03-10 22:30:42 +00:00
|
|
|
// GetInputCapture returns the function installed with SetInputCapture() or nil
|
|
|
|
// if no such function has been installed.
|
|
|
|
func (a *Application) GetInputCapture() func(event *tcell.EventKey) *tcell.EventKey {
|
|
|
|
return a.inputCapture
|
|
|
|
}
|
|
|
|
|
2017-12-15 14:29:21 +00:00
|
|
|
// 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-23 23:08:52 +00:00
|
|
|
a.Lock()
|
2017-12-15 14:29:21 +00:00
|
|
|
|
|
|
|
// Make a screen.
|
|
|
|
a.screen, err = tcell.NewScreen()
|
|
|
|
if err != nil {
|
2017-12-23 23:08:52 +00:00
|
|
|
a.Unlock()
|
2017-12-15 14:29:21 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = a.screen.Init(); err != nil {
|
2017-12-23 23:08:52 +00:00
|
|
|
a.Unlock()
|
2017-12-15 14:29:21 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// We catch panics to clean up because they mess up the terminal.
|
|
|
|
defer func() {
|
|
|
|
if p := recover(); p != nil {
|
2017-12-18 19:04:52 +00:00
|
|
|
if a.screen != nil {
|
|
|
|
a.screen.Fini()
|
|
|
|
}
|
2017-12-15 14:29:21 +00:00
|
|
|
panic(p)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Draw the screen for the first time.
|
2017-12-23 23:08:52 +00:00
|
|
|
a.Unlock()
|
2017-12-15 14:29:21 +00:00
|
|
|
a.Draw()
|
|
|
|
|
|
|
|
// Start event loop.
|
|
|
|
for {
|
2018-03-13 07:16:09 +00:00
|
|
|
a.Lock()
|
2018-01-11 10:52:27 +00:00
|
|
|
screen := a.screen
|
2018-03-13 07:16:09 +00:00
|
|
|
if a.suspended {
|
|
|
|
a.suspended = false // Clear previous suspended flag.
|
|
|
|
}
|
|
|
|
a.Unlock()
|
2018-01-11 10:52:27 +00:00
|
|
|
if screen == nil {
|
2017-12-18 19:04:52 +00:00
|
|
|
break
|
|
|
|
}
|
2018-01-11 10:52:27 +00:00
|
|
|
|
|
|
|
// Wait for next event.
|
2017-12-15 14:29:21 +00:00
|
|
|
event := a.screen.PollEvent()
|
|
|
|
if event == nil {
|
2018-03-13 07:16:09 +00:00
|
|
|
a.Lock()
|
|
|
|
if a.suspended {
|
|
|
|
// This screen was renewed due to suspended mode.
|
|
|
|
a.suspended = false
|
|
|
|
a.Unlock()
|
|
|
|
continue // Resume.
|
|
|
|
}
|
|
|
|
a.Unlock()
|
|
|
|
|
|
|
|
// The screen was finalized. Exit the loop.
|
|
|
|
break
|
2017-12-15 14:29:21 +00:00
|
|
|
}
|
2018-01-11 10:52:27 +00:00
|
|
|
|
2017-12-15 14:29:21 +00:00
|
|
|
switch event := event.(type) {
|
|
|
|
case *tcell.EventKey:
|
2017-12-26 16:34:58 +00:00
|
|
|
a.RLock()
|
|
|
|
p := a.focus
|
|
|
|
a.RUnlock()
|
|
|
|
|
|
|
|
// Intercept keys.
|
2018-01-14 20:29:34 +00:00
|
|
|
if a.inputCapture != nil {
|
|
|
|
event = a.inputCapture(event)
|
|
|
|
if event == nil {
|
|
|
|
break // Don't forward event.
|
2017-12-26 16:34:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ctrl-C closes the application.
|
2017-12-15 14:29:21 +00:00
|
|
|
if event.Key() == tcell.KeyCtrlC {
|
2017-12-26 16:34:58 +00:00
|
|
|
a.Stop()
|
2017-12-15 14:29:21 +00:00
|
|
|
}
|
2017-12-26 16:34:58 +00:00
|
|
|
|
|
|
|
// Pass other key events to the currently focused primitive.
|
2017-12-15 14:29:21 +00:00
|
|
|
if p != nil {
|
|
|
|
if handler := p.InputHandler(); handler != nil {
|
2017-12-18 19:04:52 +00:00
|
|
|
handler(event, func(p Primitive) {
|
|
|
|
a.SetFocus(p)
|
|
|
|
})
|
2017-12-15 14:29:21 +00:00
|
|
|
a.Draw()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case *tcell.EventResize:
|
2018-01-13 11:43:12 +00:00
|
|
|
a.Lock()
|
|
|
|
screen := a.screen
|
|
|
|
a.Unlock()
|
|
|
|
screen.Clear()
|
2017-12-26 00:07:30 +00:00
|
|
|
a.Draw()
|
2017-12-15 14:29:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop stops the application, causing Run() to return.
|
|
|
|
func (a *Application) Stop() {
|
2017-12-26 16:34:58 +00:00
|
|
|
a.RLock()
|
|
|
|
defer a.RUnlock()
|
2017-12-18 19:04:52 +00:00
|
|
|
if a.screen == nil {
|
|
|
|
return
|
|
|
|
}
|
2017-12-15 14:29:21 +00:00
|
|
|
a.screen.Fini()
|
2017-12-18 19:04:52 +00:00
|
|
|
a.screen = nil
|
2017-12-15 14:29:21 +00:00
|
|
|
}
|
|
|
|
|
2018-03-13 07:16:09 +00:00
|
|
|
// Suspend temporarily suspends the application by exiting terminal UI mode and
|
|
|
|
// invoking the provided function "f". When "f" returns, terminal UI mode is
|
|
|
|
// entered again and the application resumes.
|
|
|
|
//
|
|
|
|
// A return value of true indicates that the application was suspended and "f"
|
|
|
|
// was called. If false is returned, the application was already suspended,
|
|
|
|
// terminal UI mode was not exited, and "f" was not called.
|
|
|
|
func (a *Application) Suspend(f func()) bool {
|
|
|
|
a.Lock()
|
|
|
|
|
|
|
|
if a.suspended || a.screen == nil {
|
|
|
|
// Application is already suspended.
|
|
|
|
a.Unlock()
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enter suspended mode.
|
|
|
|
a.suspended = true
|
|
|
|
a.Unlock()
|
|
|
|
a.Stop()
|
|
|
|
|
|
|
|
// Deal with panics during suspended mode. Exit the program.
|
|
|
|
defer func() {
|
|
|
|
if p := recover(); p != nil {
|
|
|
|
fmt.Println(p)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Wait for "f" to return.
|
|
|
|
f()
|
|
|
|
|
|
|
|
// Make a new screen and redraw.
|
|
|
|
a.Lock()
|
|
|
|
var err error
|
|
|
|
a.screen, err = tcell.NewScreen()
|
|
|
|
if err != nil {
|
|
|
|
a.Unlock()
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if err = a.screen.Init(); err != nil {
|
|
|
|
a.Unlock()
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
a.Unlock()
|
|
|
|
a.Draw()
|
|
|
|
|
|
|
|
// Continue application loop.
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-12-15 14:29:21 +00: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 {
|
2017-12-26 16:34:58 +00:00
|
|
|
a.RLock()
|
2018-02-20 16:15:17 +00:00
|
|
|
screen := a.screen
|
|
|
|
root := a.root
|
2018-03-05 15:37:10 +00:00
|
|
|
fullscreen := a.rootFullscreen
|
2018-02-20 16:15:17 +00:00
|
|
|
before := a.beforeDraw
|
|
|
|
after := a.afterDraw
|
|
|
|
a.RUnlock()
|
2017-12-15 14:29:21 +00:00
|
|
|
|
2017-12-18 19:04:52 +00:00
|
|
|
// Maybe we're not ready yet or not anymore.
|
2018-02-20 16:15:17 +00:00
|
|
|
if screen == nil || root == nil {
|
2017-12-15 14:29:21 +00:00
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2018-01-01 20:50:20 +00:00
|
|
|
// Resize if requested.
|
2018-02-20 16:15:17 +00:00
|
|
|
if fullscreen && root != nil {
|
|
|
|
width, height := screen.Size()
|
|
|
|
root.SetRect(0, 0, width, height)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call before handler if there is one.
|
|
|
|
if before != nil {
|
|
|
|
if before(screen) {
|
|
|
|
screen.Show()
|
|
|
|
return a
|
|
|
|
}
|
2018-01-01 20:50:20 +00:00
|
|
|
}
|
|
|
|
|
2017-12-15 14:29:21 +00:00
|
|
|
// Draw all primitives.
|
2018-02-20 16:15:17 +00:00
|
|
|
root.Draw(screen)
|
|
|
|
|
|
|
|
// Call after handler if there is one.
|
|
|
|
if after != nil {
|
|
|
|
after(screen)
|
|
|
|
}
|
2017-12-15 14:29:21 +00:00
|
|
|
|
|
|
|
// Sync screen.
|
2018-02-20 16:15:17 +00:00
|
|
|
screen.Show()
|
|
|
|
|
|
|
|
return a
|
|
|
|
}
|
2017-12-15 14:29:21 +00:00
|
|
|
|
2018-02-20 16:15:17 +00:00
|
|
|
// SetBeforeDrawFunc installs a callback function which is invoked just before
|
|
|
|
// the root primitive is drawn during screen updates. If the function returns
|
|
|
|
// true, drawing will not continue, i.e. the root primitive will not be drawn
|
|
|
|
// (and an after-draw-handler will not be called).
|
|
|
|
//
|
|
|
|
// Note that the screen is not cleared by the application. To clear the screen,
|
|
|
|
// you may call screen.Clear().
|
|
|
|
//
|
|
|
|
// Provide nil to uninstall the callback function.
|
|
|
|
func (a *Application) SetBeforeDrawFunc(handler func(screen tcell.Screen) bool) *Application {
|
|
|
|
a.beforeDraw = handler
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2018-03-10 22:30:42 +00:00
|
|
|
// GetBeforeDrawFunc returns the callback function installed with
|
|
|
|
// SetBeforeDrawFunc() or nil if none has been installed.
|
|
|
|
func (a *Application) GetBeforeDrawFunc() func(screen tcell.Screen) bool {
|
|
|
|
return a.beforeDraw
|
|
|
|
}
|
|
|
|
|
2018-02-20 16:15:17 +00:00
|
|
|
// SetAfterDrawFunc installs a callback function which is invoked after the root
|
|
|
|
// primitive was drawn during screen updates.
|
|
|
|
//
|
|
|
|
// Provide nil to uninstall the callback function.
|
|
|
|
func (a *Application) SetAfterDrawFunc(handler func(screen tcell.Screen)) *Application {
|
|
|
|
a.afterDraw = handler
|
2017-12-15 14:29:21 +00:00
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2018-03-10 22:30:42 +00:00
|
|
|
// GetAfterDrawFunc returns the callback function installed with
|
|
|
|
// SetAfterDrawFunc() or nil if none has been installed.
|
|
|
|
func (a *Application) GetAfterDrawFunc() func(screen tcell.Screen) {
|
|
|
|
return a.afterDraw
|
|
|
|
}
|
|
|
|
|
2018-03-05 15:37:10 +00:00
|
|
|
// SetRoot sets the root primitive for this application. If "fullscreen" is set
|
|
|
|
// to true, the root primitive's position will be changed to fill the screen.
|
|
|
|
//
|
|
|
|
// This function must be called at least once or nothing will be displayed when
|
|
|
|
// the application starts.
|
2018-01-01 16:16:36 +00:00
|
|
|
//
|
|
|
|
// It also calls SetFocus() on the primitive.
|
2018-03-05 15:37:10 +00:00
|
|
|
func (a *Application) SetRoot(root Primitive, fullscreen bool) *Application {
|
2018-01-01 16:16:36 +00:00
|
|
|
a.Lock()
|
2017-12-15 14:29:21 +00:00
|
|
|
a.root = root
|
2018-03-05 15:37:10 +00:00
|
|
|
a.rootFullscreen = fullscreen
|
2018-01-01 20:50:20 +00:00
|
|
|
if a.screen != nil {
|
|
|
|
a.screen.Clear()
|
|
|
|
}
|
2018-01-01 16:16:36 +00:00
|
|
|
a.Unlock()
|
|
|
|
|
|
|
|
a.SetFocus(root)
|
2017-12-15 14:29:21 +00:00
|
|
|
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2017-12-27 15:04:21 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2017-12-15 14:29:21 +00:00
|
|
|
// 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
|
2017-12-28 21:19:36 +00:00
|
|
|
if a.screen != nil {
|
|
|
|
a.screen.HideCursor()
|
|
|
|
}
|
2017-12-15 14:29:21 +00:00
|
|
|
a.Unlock()
|
2018-03-10 11:59:42 +00:00
|
|
|
if p != nil {
|
|
|
|
p.Focus(func(p Primitive) {
|
|
|
|
a.SetFocus(p)
|
|
|
|
})
|
|
|
|
}
|
2017-12-15 14:29:21 +00:00
|
|
|
|
|
|
|
return a
|
|
|
|
}
|
2017-12-20 19:54:49 +00:00
|
|
|
|
|
|
|
// GetFocus returns the primitive which has the current focus. If none has it,
|
|
|
|
// nil is returned.
|
|
|
|
func (a *Application) GetFocus() Primitive {
|
2017-12-26 16:34:58 +00:00
|
|
|
a.RLock()
|
|
|
|
defer a.RUnlock()
|
2017-12-20 19:54:49 +00:00
|
|
|
return a.focus
|
|
|
|
}
|