2017-12-15 14:29:21 +00:00
|
|
|
package tview
|
|
|
|
|
|
|
|
import (
|
2022-06-29 07:24:37 +00:00
|
|
|
"context"
|
2017-12-15 14:29:21 +00:00
|
|
|
"sync"
|
2020-01-27 14:20:36 +00:00
|
|
|
"time"
|
2017-12-15 14:29:21 +00:00
|
|
|
|
2020-10-18 12:15:57 +00:00
|
|
|
"github.com/gdamore/tcell/v2"
|
2017-12-15 14:29:21 +00:00
|
|
|
)
|
|
|
|
|
2020-01-27 14:20:36 +00:00
|
|
|
const (
|
|
|
|
// The size of the event/update/redraw channels.
|
|
|
|
queueSize = 100
|
|
|
|
|
|
|
|
// The minimum time between two consecutive redraws.
|
|
|
|
redrawPause = 50 * time.Millisecond
|
|
|
|
)
|
2018-10-28 12:42:49 +00:00
|
|
|
|
2020-03-27 17:41:44 +00:00
|
|
|
// DoubleClickInterval specifies the maximum time between clicks to register a
|
|
|
|
// double click rather than click.
|
|
|
|
var DoubleClickInterval = 500 * time.Millisecond
|
|
|
|
|
|
|
|
// MouseAction indicates one of the actions the mouse is logically doing.
|
|
|
|
type MouseAction int16
|
|
|
|
|
|
|
|
// Available mouse actions.
|
|
|
|
const (
|
|
|
|
MouseMove MouseAction = iota
|
|
|
|
MouseLeftDown
|
|
|
|
MouseLeftUp
|
|
|
|
MouseLeftClick
|
|
|
|
MouseLeftDoubleClick
|
|
|
|
MouseMiddleDown
|
|
|
|
MouseMiddleUp
|
|
|
|
MouseMiddleClick
|
|
|
|
MouseMiddleDoubleClick
|
|
|
|
MouseRightDown
|
|
|
|
MouseRightUp
|
|
|
|
MouseRightClick
|
|
|
|
MouseRightDoubleClick
|
2020-03-29 19:36:06 +00:00
|
|
|
MouseScrollUp
|
|
|
|
MouseScrollDown
|
|
|
|
MouseScrollLeft
|
|
|
|
MouseScrollRight
|
2020-03-27 17:41:44 +00:00
|
|
|
)
|
|
|
|
|
2020-02-19 15:59:33 +00:00
|
|
|
// queuedUpdate represented the execution of f queued by
|
2021-05-28 18:52:09 +00:00
|
|
|
// Application.QueueUpdate(). If "done" is not nil, it receives exactly one
|
|
|
|
// element after f has executed.
|
2020-02-19 15:59:33 +00:00
|
|
|
type queuedUpdate struct {
|
2022-07-24 21:39:17 +00:00
|
|
|
f func()
|
|
|
|
//done chan struct{}
|
2020-02-19 15:59:33 +00:00
|
|
|
}
|
|
|
|
|
2017-12-15 14:29:21 +00:00
|
|
|
// 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.
|
2018-11-05 09:53:57 +00:00
|
|
|
//
|
|
|
|
// The following command displays a primitive p on the screen until Ctrl-C is
|
|
|
|
// pressed:
|
|
|
|
//
|
|
|
|
// if err := tview.NewApplication().SetRoot(p, true).Run(); err != nil {
|
|
|
|
// panic(err)
|
|
|
|
// }
|
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
|
|
|
|
2022-06-29 07:24:37 +00:00
|
|
|
runContext context.Context
|
|
|
|
runCancelFunc context.CancelFunc
|
|
|
|
|
2018-11-26 11:04:06 +00:00
|
|
|
// The application's screen. Apart from Run(), this variable should never be
|
|
|
|
// set directly. Always use the screenReplacement channel after calling
|
|
|
|
// Fini(), to set a new screen (or nil to stop the application).
|
2017-12-15 14:29:21 +00:00
|
|
|
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
|
|
|
|
2020-03-27 17:41:44 +00:00
|
|
|
// Set to true if mouse events are enabled.
|
2019-11-04 05:34:46 +00:00
|
|
|
enableMouse bool
|
|
|
|
|
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
|
|
|
|
2018-10-09 04:02:02 +00:00
|
|
|
// Used to send screen events from separate goroutine to main event loop
|
|
|
|
events chan tcell.Event
|
|
|
|
|
2018-10-28 12:42:49 +00:00
|
|
|
// Functions queued from goroutines, used to serialize updates to primitives.
|
2020-02-19 15:59:33 +00:00
|
|
|
updates chan queuedUpdate
|
2018-10-28 12:42:49 +00:00
|
|
|
|
2018-11-26 11:04:06 +00:00
|
|
|
// An object that the screen variable will be set to after Fini() was called.
|
|
|
|
// Use this channel to set a new screen object for the application
|
|
|
|
// (screen.Init() and draw() will be called implicitly). A value of nil will
|
|
|
|
// stop the application.
|
|
|
|
screenReplacement chan tcell.Screen
|
2019-11-04 05:34:46 +00:00
|
|
|
|
|
|
|
// An optional capture function which receives a mouse event and returns the
|
|
|
|
// event to be forwarded to the default mouse handler (nil if nothing should
|
|
|
|
// be forwarded).
|
2020-01-24 20:40:34 +00:00
|
|
|
mouseCapture func(event *tcell.EventMouse, action MouseAction) (*tcell.EventMouse, MouseAction)
|
2019-11-05 06:03:04 +00:00
|
|
|
|
2020-03-27 17:41:44 +00:00
|
|
|
mouseCapturingPrimitive Primitive // A Primitive returned by a MouseHandler which will capture future mouse events.
|
|
|
|
lastMouseX, lastMouseY int // The last position of the mouse.
|
|
|
|
mouseDownX, mouseDownY int // The position of the mouse when its button was last pressed.
|
|
|
|
lastMouseClick time.Time // The time when a mouse button was last clicked.
|
|
|
|
lastMouseButtons tcell.ButtonMask // The last mouse button state.
|
2017-12-15 14:29:21 +00:00
|
|
|
}
|
|
|
|
|
2022-06-29 07:24:37 +00:00
|
|
|
func (a *Application) Close() error {
|
|
|
|
a.runCancelFunc()
|
|
|
|
close(a.events)
|
|
|
|
close(a.screenReplacement)
|
|
|
|
close(a.updates)
|
|
|
|
|
|
|
|
// flush events channel
|
|
|
|
go func() {
|
|
|
|
for range a.events {
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
// flush screenReplacement channel
|
|
|
|
go func() {
|
|
|
|
for range a.screenReplacement {
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
// flush updates channel
|
|
|
|
go func() {
|
2022-07-24 21:39:17 +00:00
|
|
|
for range a.updates {
|
2022-06-29 07:24:37 +00:00
|
|
|
// important to set done for calling channel to be able to return
|
2022-07-24 21:39:17 +00:00
|
|
|
//up.done <- struct{}{}
|
2022-06-29 07:24:37 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-15 14:29:21 +00:00
|
|
|
// NewApplication creates and returns a new application.
|
|
|
|
func NewApplication() *Application {
|
2022-06-29 07:24:37 +00:00
|
|
|
cancelContext, cancelFunc := context.WithCancel(context.Background())
|
2018-10-09 04:02:02 +00:00
|
|
|
return &Application{
|
2022-06-29 07:24:37 +00:00
|
|
|
runContext: cancelContext,
|
|
|
|
runCancelFunc: cancelFunc,
|
2018-11-26 11:04:06 +00:00
|
|
|
events: make(chan tcell.Event, queueSize),
|
2020-02-19 15:59:33 +00:00
|
|
|
updates: make(chan queuedUpdate, queueSize),
|
2018-11-26 11:04:06 +00:00
|
|
|
screenReplacement: make(chan tcell.Screen, 1),
|
2018-10-09 04:02:02 +00:00
|
|
|
}
|
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
|
2019-07-11 10:30:32 +00:00
|
|
|
// application.
|
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
|
|
|
|
}
|
|
|
|
|
2020-03-27 17:41:44 +00:00
|
|
|
// SetMouseCapture sets a function which captures mouse events (consisting of
|
|
|
|
// the original tcell mouse event and the semantic mouse action) before they are
|
|
|
|
// forwarded to the appropriate mouse event handler. This function can then
|
|
|
|
// choose to forward that event (or a different one) by returning it or stop
|
|
|
|
// the event processing by returning a nil mouse event.
|
2020-01-24 20:40:34 +00:00
|
|
|
func (a *Application) SetMouseCapture(capture func(event *tcell.EventMouse, action MouseAction) (*tcell.EventMouse, MouseAction)) *Application {
|
2019-11-04 05:34:46 +00:00
|
|
|
a.mouseCapture = capture
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetMouseCapture returns the function installed with SetMouseCapture() or nil
|
|
|
|
// if no such function has been installed.
|
2020-01-24 20:40:34 +00:00
|
|
|
func (a *Application) GetMouseCapture() func(event *tcell.EventMouse, action MouseAction) (*tcell.EventMouse, MouseAction) {
|
2019-11-04 05:34:46 +00:00
|
|
|
return a.mouseCapture
|
|
|
|
}
|
|
|
|
|
2018-09-05 11:26:22 +00:00
|
|
|
// SetScreen allows you to provide your own tcell.Screen object. For most
|
|
|
|
// applications, this is not needed and you should be familiar with
|
2018-11-26 11:04:06 +00:00
|
|
|
// tcell.Screen when using this function.
|
2018-09-05 11:26:22 +00:00
|
|
|
//
|
2018-11-26 11:04:06 +00:00
|
|
|
// This function is typically called before the first call to Run(). Init() need
|
|
|
|
// not be called on the screen.
|
2018-09-05 11:26:22 +00:00
|
|
|
func (a *Application) SetScreen(screen tcell.Screen) *Application {
|
2018-11-26 11:04:06 +00:00
|
|
|
if screen == nil {
|
|
|
|
return a // Invalid input. Do nothing.
|
2018-09-05 11:26:22 +00:00
|
|
|
}
|
2018-11-26 11:04:06 +00:00
|
|
|
|
|
|
|
a.Lock()
|
|
|
|
if a.screen == nil {
|
|
|
|
// Run() has not been called yet.
|
|
|
|
a.screen = screen
|
|
|
|
a.Unlock()
|
|
|
|
return a
|
2018-09-05 11:26:22 +00:00
|
|
|
}
|
2018-11-26 11:04:06 +00:00
|
|
|
|
|
|
|
// Run() is already in progress. Exchange screen.
|
|
|
|
oldScreen := a.screen
|
|
|
|
a.Unlock()
|
|
|
|
oldScreen.Fini()
|
2022-06-29 07:24:37 +00:00
|
|
|
// check to see if the Application.Run is still valid
|
|
|
|
if a.runContext.Err() == nil {
|
|
|
|
a.screenReplacement <- screen
|
|
|
|
}
|
2018-11-26 11:04:06 +00:00
|
|
|
|
2018-09-05 11:26:22 +00:00
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2021-01-11 17:44:53 +00:00
|
|
|
// EnableMouse enables mouse events or disables them (if "false" is provided).
|
2020-03-03 16:24:50 +00:00
|
|
|
func (a *Application) EnableMouse(enable bool) *Application {
|
2019-11-04 05:34:46 +00:00
|
|
|
a.Lock()
|
2020-03-03 16:24:50 +00:00
|
|
|
defer a.Unlock()
|
|
|
|
if enable != a.enableMouse && a.screen != nil {
|
|
|
|
if enable {
|
|
|
|
a.screen.EnableMouse()
|
|
|
|
} else {
|
|
|
|
a.screen.DisableMouse()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
a.enableMouse = enable
|
2019-11-04 05:42:46 +00:00
|
|
|
return a
|
2019-11-04 05:34:46 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2020-01-27 14:20:36 +00:00
|
|
|
var (
|
2021-06-24 16:27:19 +00:00
|
|
|
err, appErr error
|
2020-08-18 10:21:46 +00:00
|
|
|
lastRedraw time.Time // The time the screen was last redrawn.
|
|
|
|
redrawTimer *time.Timer // A timer to schedule the next redraw.
|
2020-01-27 14:20:36 +00:00
|
|
|
)
|
2017-12-23 23:08:52 +00:00
|
|
|
a.Lock()
|
2017-12-15 14:29:21 +00:00
|
|
|
|
2018-09-05 11:26:22 +00:00
|
|
|
// Make a screen if there is none yet.
|
|
|
|
if a.screen == nil {
|
|
|
|
a.screen, err = tcell.NewScreen()
|
|
|
|
if err != nil {
|
|
|
|
a.Unlock()
|
|
|
|
return err
|
|
|
|
}
|
2018-11-26 11:04:06 +00:00
|
|
|
if err = a.screen.Init(); err != nil {
|
|
|
|
a.Unlock()
|
|
|
|
return err
|
|
|
|
}
|
2019-11-04 05:34:46 +00:00
|
|
|
if a.enableMouse {
|
|
|
|
a.screen.EnableMouse()
|
|
|
|
}
|
2017-12-15 14:29:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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()
|
2018-10-28 12:42:49 +00:00
|
|
|
a.draw()
|
2017-12-15 14:29:21 +00:00
|
|
|
|
2018-10-28 12:42:49 +00:00
|
|
|
// Separate loop to wait for screen events.
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
2018-10-09 04:02:02 +00:00
|
|
|
go func() {
|
2022-06-29 07:24:37 +00:00
|
|
|
defer func() {
|
|
|
|
// call the runCancelFunc when exiting this function.
|
|
|
|
//This will stop the channels accepting any more events
|
|
|
|
a.runCancelFunc()
|
|
|
|
}()
|
2018-10-28 12:42:49 +00:00
|
|
|
defer wg.Done()
|
2022-06-29 07:24:37 +00:00
|
|
|
|
|
|
|
// check to see if the Application.Run is still valid
|
2022-07-07 20:15:12 +00:00
|
|
|
for {
|
2018-11-26 11:04:06 +00:00
|
|
|
a.RLock()
|
|
|
|
screen := a.screen
|
|
|
|
a.RUnlock()
|
|
|
|
if screen == nil {
|
|
|
|
// We have no screen. Let's stop.
|
|
|
|
a.QueueEvent(nil)
|
|
|
|
break
|
|
|
|
}
|
2018-10-28 12:42:49 +00:00
|
|
|
|
2018-11-26 11:04:06 +00:00
|
|
|
// Wait for next event and queue it.
|
|
|
|
event := screen.PollEvent()
|
|
|
|
if event != nil {
|
|
|
|
// Regular event. Queue.
|
|
|
|
a.QueueEvent(event)
|
|
|
|
continue
|
|
|
|
}
|
2018-10-28 12:42:49 +00:00
|
|
|
|
2022-06-29 07:24:37 +00:00
|
|
|
// A screen was finalized (event is nil). Wait for a new screen.
|
|
|
|
var ok bool
|
|
|
|
select {
|
|
|
|
// exit when runContext complete
|
|
|
|
case <-a.runContext.Done():
|
2018-11-26 11:04:06 +00:00
|
|
|
return
|
2022-06-29 07:24:37 +00:00
|
|
|
case screen, ok = <-a.screenReplacement:
|
|
|
|
if !ok || screen == nil {
|
|
|
|
// No new screen. We're done.
|
|
|
|
a.QueueEvent(nil)
|
|
|
|
return
|
|
|
|
}
|
2018-10-28 12:42:49 +00:00
|
|
|
|
2022-06-29 07:24:37 +00:00
|
|
|
// We have a new screen. Keep going.
|
|
|
|
a.Lock()
|
|
|
|
a.screen = screen
|
|
|
|
enableMouse := a.enableMouse
|
|
|
|
a.Unlock()
|
2018-11-26 11:04:06 +00:00
|
|
|
|
2022-06-29 07:24:37 +00:00
|
|
|
// Initialize and draw this screen.
|
|
|
|
if err := screen.Init(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if enableMouse {
|
|
|
|
screen.EnableMouse()
|
|
|
|
}
|
|
|
|
a.draw()
|
2021-01-11 17:44:53 +00:00
|
|
|
}
|
2017-12-15 14:29:21 +00:00
|
|
|
}
|
2018-10-09 04:02:02 +00:00
|
|
|
}()
|
2018-01-11 10:52:27 +00:00
|
|
|
|
2018-10-09 04:02:02 +00:00
|
|
|
// Start event loop.
|
2018-10-28 12:42:49 +00:00
|
|
|
EventLoop:
|
2022-06-29 07:24:37 +00:00
|
|
|
// check to see if the Application.Run is still valid
|
2022-07-07 20:15:12 +00:00
|
|
|
for {
|
2018-10-09 04:02:02 +00:00
|
|
|
select {
|
2022-06-29 07:24:37 +00:00
|
|
|
// break loop when runContext complete
|
|
|
|
case <-a.runContext.Done():
|
|
|
|
break EventLoop
|
|
|
|
case event, ok := <-a.events:
|
|
|
|
if !ok || event == nil {
|
2018-10-28 12:42:49 +00:00
|
|
|
break EventLoop
|
2018-10-09 04:02:02 +00:00
|
|
|
}
|
2017-12-26 16:34:58 +00:00
|
|
|
|
2018-10-09 04:02:02 +00:00
|
|
|
switch event := event.(type) {
|
|
|
|
case *tcell.EventKey:
|
|
|
|
a.RLock()
|
2020-08-18 10:05:43 +00:00
|
|
|
root := a.root
|
2018-10-28 12:42:49 +00:00
|
|
|
inputCapture := a.inputCapture
|
2018-10-09 04:02:02 +00:00
|
|
|
a.RUnlock()
|
|
|
|
|
|
|
|
// Intercept keys.
|
2020-08-18 10:05:43 +00:00
|
|
|
var draw bool
|
2018-10-28 12:42:49 +00:00
|
|
|
if inputCapture != nil {
|
|
|
|
event = inputCapture(event)
|
2018-10-09 04:02:02 +00:00
|
|
|
if event == nil {
|
2018-12-26 20:24:39 +00:00
|
|
|
a.draw()
|
2018-11-10 10:10:01 +00:00
|
|
|
continue // Don't forward event.
|
2018-10-09 04:02:02 +00:00
|
|
|
}
|
2020-08-18 10:05:43 +00:00
|
|
|
draw = true
|
2017-12-26 16:34:58 +00:00
|
|
|
}
|
|
|
|
|
2018-10-09 04:02:02 +00:00
|
|
|
// Ctrl-C closes the application.
|
|
|
|
if event.Key() == tcell.KeyCtrlC {
|
|
|
|
a.Stop()
|
2021-06-24 16:27:19 +00:00
|
|
|
break
|
2018-10-09 04:02:02 +00:00
|
|
|
}
|
2017-12-26 16:34:58 +00:00
|
|
|
|
2020-08-18 10:05:43 +00:00
|
|
|
// Pass other key events to the root primitive.
|
2020-11-17 18:33:25 +00:00
|
|
|
if root != nil && root.HasFocus() {
|
2020-08-18 10:05:43 +00:00
|
|
|
if handler := root.InputHandler(); handler != nil {
|
2018-10-09 04:02:02 +00:00
|
|
|
handler(event, func(p Primitive) {
|
|
|
|
a.SetFocus(p)
|
|
|
|
})
|
2020-08-18 10:05:43 +00:00
|
|
|
draw = true
|
2018-10-09 04:02:02 +00:00
|
|
|
}
|
2017-12-15 14:29:21 +00:00
|
|
|
}
|
2020-08-18 10:05:43 +00:00
|
|
|
|
|
|
|
// Redraw.
|
|
|
|
if draw {
|
|
|
|
a.draw()
|
|
|
|
}
|
2018-10-09 04:02:02 +00:00
|
|
|
case *tcell.EventResize:
|
2020-01-27 14:20:36 +00:00
|
|
|
if time.Since(lastRedraw) < redrawPause {
|
|
|
|
if redrawTimer != nil {
|
|
|
|
redrawTimer.Stop()
|
|
|
|
}
|
2022-06-29 07:24:37 +00:00
|
|
|
redrawTimer = time.AfterFunc(redrawPause,
|
|
|
|
func() {
|
|
|
|
// check to see if the Application.Run is still valid
|
|
|
|
if a.runContext.Err() == nil {
|
|
|
|
a.events <- event
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2020-01-27 14:20:36 +00:00
|
|
|
}
|
2018-10-09 04:02:02 +00:00
|
|
|
a.RLock()
|
|
|
|
screen := a.screen
|
|
|
|
a.RUnlock()
|
2018-11-19 09:35:28 +00:00
|
|
|
if screen == nil {
|
|
|
|
continue
|
|
|
|
}
|
2020-01-29 06:55:29 +00:00
|
|
|
lastRedraw = time.Now()
|
2018-10-09 04:02:02 +00:00
|
|
|
screen.Clear()
|
2018-10-28 12:42:49 +00:00
|
|
|
a.draw()
|
2019-11-04 05:34:46 +00:00
|
|
|
case *tcell.EventMouse:
|
2020-03-03 18:07:39 +00:00
|
|
|
consumed, isMouseDownAction := a.fireMouseActions(event)
|
|
|
|
if consumed {
|
|
|
|
a.draw()
|
2020-02-28 01:19:36 +00:00
|
|
|
}
|
2020-03-27 17:41:44 +00:00
|
|
|
a.lastMouseButtons = event.Buttons()
|
2020-03-03 16:16:56 +00:00
|
|
|
if isMouseDownAction {
|
2020-02-28 01:19:36 +00:00
|
|
|
a.mouseDownX, a.mouseDownY = event.Position()
|
2019-11-04 05:34:46 +00:00
|
|
|
}
|
2021-06-24 16:27:19 +00:00
|
|
|
case *tcell.EventError:
|
|
|
|
appErr = event
|
|
|
|
a.Stop()
|
2017-12-15 14:29:21 +00:00
|
|
|
}
|
2018-10-09 04:02:02 +00:00
|
|
|
|
2018-10-28 12:42:49 +00:00
|
|
|
// If we have updates, now is the time to execute them.
|
2022-06-29 07:24:37 +00:00
|
|
|
case update, ok := <-a.updates:
|
|
|
|
if !ok {
|
|
|
|
break EventLoop
|
|
|
|
}
|
2020-02-19 15:59:33 +00:00
|
|
|
update.f()
|
2022-07-24 21:39:17 +00:00
|
|
|
//if update.done != nil {
|
|
|
|
// update.done <- struct{}{}
|
|
|
|
//}
|
2018-10-28 12:42:49 +00:00
|
|
|
}
|
2017-12-15 14:29:21 +00:00
|
|
|
}
|
2022-06-29 07:24:37 +00:00
|
|
|
// call the runCancelFunc when exiting eventLoop.
|
|
|
|
//This will stop the channels accepting any more events
|
|
|
|
a.runCancelFunc()
|
2017-12-15 14:29:21 +00:00
|
|
|
|
2018-11-26 11:04:06 +00:00
|
|
|
// Wait for the event loop to finish.
|
2018-10-28 12:42:49 +00:00
|
|
|
wg.Wait()
|
2018-11-26 11:04:06 +00:00
|
|
|
a.screen = nil
|
2018-10-28 12:42:49 +00:00
|
|
|
|
2021-06-24 16:27:19 +00:00
|
|
|
return appErr
|
2017-12-15 14:29:21 +00:00
|
|
|
}
|
|
|
|
|
2020-03-27 17:41:44 +00:00
|
|
|
// fireMouseActions analyzes the provided mouse event, derives mouse actions
|
|
|
|
// from it and then forwards them to the corresponding primitives.
|
2020-03-03 18:07:39 +00:00
|
|
|
func (a *Application) fireMouseActions(event *tcell.EventMouse) (consumed, isMouseDownAction bool) {
|
2020-03-27 17:41:44 +00:00
|
|
|
// We want to relay follow-up events to the same target primitive.
|
|
|
|
var targetPrimitive Primitive
|
2020-02-28 01:19:36 +00:00
|
|
|
|
2020-03-27 17:41:44 +00:00
|
|
|
// Helper function to fire a mouse action.
|
|
|
|
fire := func(action MouseAction) {
|
2020-03-03 18:07:39 +00:00
|
|
|
switch action {
|
|
|
|
case MouseLeftDown, MouseMiddleDown, MouseRightDown:
|
|
|
|
isMouseDownAction = true
|
|
|
|
}
|
2020-02-28 01:19:36 +00:00
|
|
|
|
2020-03-03 18:07:39 +00:00
|
|
|
// Intercept event.
|
2020-03-27 17:41:44 +00:00
|
|
|
if a.mouseCapture != nil {
|
|
|
|
event, action = a.mouseCapture(event, action)
|
2020-03-03 18:07:39 +00:00
|
|
|
if event == nil {
|
|
|
|
consumed = true
|
|
|
|
return // Don't forward event.
|
2020-02-28 01:19:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-27 17:41:44 +00:00
|
|
|
// Determine the target primitive.
|
|
|
|
var primitive, capturingPrimitive Primitive
|
|
|
|
if a.mouseCapturingPrimitive != nil {
|
|
|
|
primitive = a.mouseCapturingPrimitive
|
|
|
|
targetPrimitive = a.mouseCapturingPrimitive
|
|
|
|
} else if targetPrimitive != nil {
|
|
|
|
primitive = targetPrimitive
|
2020-02-28 01:19:36 +00:00
|
|
|
} else {
|
2020-03-27 17:41:44 +00:00
|
|
|
primitive = a.root
|
2020-03-03 18:07:39 +00:00
|
|
|
}
|
2020-03-27 17:41:44 +00:00
|
|
|
if primitive != nil {
|
|
|
|
if handler := primitive.MouseHandler(); handler != nil {
|
|
|
|
var wasConsumed bool
|
|
|
|
wasConsumed, capturingPrimitive = handler(action, event, func(p Primitive) {
|
2020-03-03 18:07:39 +00:00
|
|
|
a.SetFocus(p)
|
|
|
|
})
|
2020-03-27 17:41:44 +00:00
|
|
|
if wasConsumed {
|
2020-03-03 18:07:39 +00:00
|
|
|
consumed = true
|
2020-02-28 01:19:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-27 17:41:44 +00:00
|
|
|
a.mouseCapturingPrimitive = capturingPrimitive
|
2020-02-28 01:19:36 +00:00
|
|
|
}
|
|
|
|
|
2020-03-27 17:41:44 +00:00
|
|
|
x, y := event.Position()
|
|
|
|
buttons := event.Buttons()
|
|
|
|
clickMoved := x != a.mouseDownX || y != a.mouseDownY
|
|
|
|
buttonChanges := buttons ^ a.lastMouseButtons
|
2020-03-03 18:07:39 +00:00
|
|
|
|
2020-03-27 17:41:44 +00:00
|
|
|
if x != a.lastMouseX || y != a.lastMouseY {
|
|
|
|
fire(MouseMove)
|
|
|
|
a.lastMouseX = x
|
|
|
|
a.lastMouseY = y
|
2020-03-03 18:07:39 +00:00
|
|
|
}
|
|
|
|
|
2020-03-27 17:41:44 +00:00
|
|
|
for _, buttonEvent := range []struct {
|
2020-03-03 18:07:39 +00:00
|
|
|
button tcell.ButtonMask
|
|
|
|
down, up, click, dclick MouseAction
|
|
|
|
}{
|
2021-02-14 22:43:27 +00:00
|
|
|
{tcell.ButtonPrimary, MouseLeftDown, MouseLeftUp, MouseLeftClick, MouseLeftDoubleClick},
|
|
|
|
{tcell.ButtonMiddle, MouseMiddleDown, MouseMiddleUp, MouseMiddleClick, MouseMiddleDoubleClick},
|
|
|
|
{tcell.ButtonSecondary, MouseRightDown, MouseRightUp, MouseRightClick, MouseRightDoubleClick},
|
2020-03-03 18:07:39 +00:00
|
|
|
} {
|
2020-03-27 17:41:44 +00:00
|
|
|
if buttonChanges&buttonEvent.button != 0 {
|
|
|
|
if buttons&buttonEvent.button != 0 {
|
|
|
|
fire(buttonEvent.down)
|
2020-03-03 18:07:39 +00:00
|
|
|
} else {
|
2020-03-27 17:41:44 +00:00
|
|
|
fire(buttonEvent.up)
|
2020-03-03 18:07:39 +00:00
|
|
|
if !clickMoved {
|
2020-03-27 17:41:44 +00:00
|
|
|
if a.lastMouseClick.Add(DoubleClickInterval).Before(time.Now()) {
|
|
|
|
fire(buttonEvent.click)
|
|
|
|
a.lastMouseClick = time.Now()
|
2020-03-03 18:07:39 +00:00
|
|
|
} else {
|
2020-03-27 17:41:44 +00:00
|
|
|
fire(buttonEvent.dclick)
|
|
|
|
a.lastMouseClick = time.Time{} // reset
|
2020-03-03 18:07:39 +00:00
|
|
|
}
|
2020-02-28 01:19:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-27 17:41:44 +00:00
|
|
|
for _, wheelEvent := range []struct {
|
2020-03-03 18:07:39 +00:00
|
|
|
button tcell.ButtonMask
|
|
|
|
action MouseAction
|
|
|
|
}{
|
2020-03-29 19:36:06 +00:00
|
|
|
{tcell.WheelUp, MouseScrollUp},
|
|
|
|
{tcell.WheelDown, MouseScrollDown},
|
|
|
|
{tcell.WheelLeft, MouseScrollLeft},
|
|
|
|
{tcell.WheelRight, MouseScrollRight}} {
|
2020-03-27 17:41:44 +00:00
|
|
|
if buttons&wheelEvent.button != 0 {
|
|
|
|
fire(wheelEvent.action)
|
2020-03-03 18:07:39 +00:00
|
|
|
}
|
2020-02-28 01:19:36 +00:00
|
|
|
}
|
2020-03-03 18:07:39 +00:00
|
|
|
|
|
|
|
return consumed, isMouseDownAction
|
2020-02-28 01:19:36 +00:00
|
|
|
}
|
|
|
|
|
2017-12-15 14:29:21 +00:00
|
|
|
// Stop stops the application, causing Run() to return.
|
|
|
|
func (a *Application) Stop() {
|
2018-06-28 12:49:42 +00:00
|
|
|
a.Lock()
|
|
|
|
defer a.Unlock()
|
2018-10-28 12:42:49 +00:00
|
|
|
screen := a.screen
|
|
|
|
if screen == nil {
|
2017-12-18 19:04:52 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
a.screen = nil
|
2018-10-28 12:42:49 +00:00
|
|
|
screen.Fini()
|
2022-06-29 07:24:37 +00:00
|
|
|
|
|
|
|
// check to see if the Application.Run is still valid
|
|
|
|
if a.runContext.Err() == nil {
|
|
|
|
a.screenReplacement <- 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 {
|
2018-11-26 11:04:06 +00:00
|
|
|
a.RLock()
|
2018-10-28 12:42:49 +00:00
|
|
|
screen := a.screen
|
2018-11-26 11:04:06 +00:00
|
|
|
a.RUnlock()
|
2018-10-28 12:42:49 +00:00
|
|
|
if screen == nil {
|
2018-11-26 11:04:06 +00:00
|
|
|
return false // Screen has not yet been initialized.
|
2018-03-13 07:16:09 +00:00
|
|
|
}
|
|
|
|
|
2018-11-26 11:04:06 +00:00
|
|
|
// Enter suspended mode.
|
2021-03-11 19:14:19 +00:00
|
|
|
if err := screen.Suspend(); err != nil {
|
|
|
|
return false // Suspension failed.
|
|
|
|
}
|
2018-03-13 07:16:09 +00:00
|
|
|
|
|
|
|
// Wait for "f" to return.
|
|
|
|
f()
|
|
|
|
|
2021-03-11 19:14:19 +00:00
|
|
|
// If the screen object has changed in the meantime, we need to do more.
|
2020-12-04 16:13:33 +00:00
|
|
|
a.RLock()
|
2021-03-11 19:14:19 +00:00
|
|
|
defer a.RUnlock()
|
|
|
|
if a.screen != screen {
|
|
|
|
// Calling Stop() while in suspend mode currently still leads to a
|
|
|
|
// panic, see https://github.com/gdamore/tcell/issues/440.
|
|
|
|
screen.Fini()
|
|
|
|
if a.screen == nil {
|
|
|
|
return true // If stop was called (a.screen is nil), we're done already.
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// It hasn't changed. Resume.
|
|
|
|
screen.Resume() // Not much we can do in case of an error.
|
2018-03-13 07:16:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Continue application loop.
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-11-05 09:53:57 +00:00
|
|
|
// Draw refreshes the screen (during the next update cycle). It calls the Draw()
|
|
|
|
// function of the application's root primitive and then syncs the screen
|
2021-03-11 17:07:04 +00:00
|
|
|
// buffer. It is almost never necessary to call this function. It can actually
|
|
|
|
// deadlock your application if you call it from the main thread (e.g. in a
|
|
|
|
// callback function of a widget). Please see
|
2020-02-19 15:59:33 +00:00
|
|
|
// https://github.com/rivo/tview/wiki/Concurrency for details.
|
2017-12-15 14:29:21 +00:00
|
|
|
func (a *Application) Draw() *Application {
|
2018-11-05 09:53:57 +00:00
|
|
|
a.QueueUpdate(func() {
|
|
|
|
a.draw()
|
|
|
|
})
|
2018-10-28 12:42:49 +00:00
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2018-12-03 09:50:38 +00:00
|
|
|
// ForceDraw refreshes the screen immediately. Use this function with caution as
|
|
|
|
// it may lead to race conditions with updates to primitives in other
|
2018-12-25 17:55:57 +00:00
|
|
|
// goroutines. It is always preferrable to use Draw() instead. Never call this
|
|
|
|
// function from a goroutine.
|
2018-12-03 09:50:38 +00:00
|
|
|
//
|
|
|
|
// It is safe to call this function during queued updates and direct event
|
|
|
|
// handling.
|
|
|
|
func (a *Application) ForceDraw() *Application {
|
|
|
|
return a.draw()
|
|
|
|
}
|
|
|
|
|
2018-10-28 12:42:49 +00:00
|
|
|
// draw actually does what Draw() promises to do.
|
|
|
|
func (a *Application) draw() *Application {
|
2018-07-27 14:30:50 +00:00
|
|
|
a.Lock()
|
|
|
|
defer a.Unlock()
|
|
|
|
|
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
|
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
|
|
|
|
2021-05-28 18:52:09 +00:00
|
|
|
// Sync forces a full re-sync of the screen buffer with the actual screen during
|
|
|
|
// the next event cycle. This is useful for when the terminal screen is
|
|
|
|
// corrupted so you may want to offer your users a keyboard shortcut to refresh
|
|
|
|
// the screen.
|
|
|
|
func (a *Application) Sync() *Application {
|
2022-06-29 07:24:37 +00:00
|
|
|
// check to see if the Application.Run is still valid
|
2022-06-30 09:40:22 +00:00
|
|
|
msg := queuedUpdate{
|
|
|
|
f: func() {
|
|
|
|
a.RLock()
|
|
|
|
screen := a.screen
|
|
|
|
a.RUnlock()
|
|
|
|
if screen == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
screen.Sync()
|
|
|
|
},
|
|
|
|
}
|
2022-06-29 07:24:37 +00:00
|
|
|
if a.runContext.Err() == nil {
|
2022-06-30 09:40:22 +00:00
|
|
|
a.updates <- msg
|
2022-06-29 07:24:37 +00:00
|
|
|
}
|
2021-05-28 18:52:09 +00:00
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-06-10 16:30:03 +00:00
|
|
|
// SetFocus sets the focus to a new primitive. All key events will be directed
|
|
|
|
// down the hierarchy (starting at the root) until a primitive handles them,
|
|
|
|
// which per default goes towards the focused primitive.
|
2017-12-15 14:29:21 +00:00
|
|
|
//
|
|
|
|
// 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
|
|
|
|
}
|
2018-10-09 04:02:02 +00:00
|
|
|
|
2018-10-28 12:42:49 +00:00
|
|
|
// QueueUpdate is used to synchronize access to primitives from non-main
|
|
|
|
// goroutines. The provided function will be executed as part of the event loop
|
|
|
|
// and thus will not cause race conditions with other such update functions or
|
|
|
|
// the Draw() function.
|
|
|
|
//
|
|
|
|
// Note that Draw() is not implicitly called after the execution of f as that
|
|
|
|
// may not be desirable. You can call Draw() from f if the screen should be
|
2018-11-05 09:53:57 +00:00
|
|
|
// refreshed after each update. Alternatively, use QueueUpdateDraw() to follow
|
|
|
|
// up with an immediate refresh of the screen.
|
2020-02-19 15:59:33 +00:00
|
|
|
//
|
|
|
|
// This function returns after f has executed.
|
2022-07-24 21:39:17 +00:00
|
|
|
func (a *Application) QueueUpdate(
|
|
|
|
f func(),
|
|
|
|
) *Application {
|
2022-06-30 09:40:22 +00:00
|
|
|
defer func() {
|
|
|
|
if err := recover(); err != nil {
|
2022-07-07 20:15:12 +00:00
|
|
|
// dealing with panic, as we can still get a closed channel
|
2022-06-30 09:40:22 +00:00
|
|
|
}
|
|
|
|
}()
|
2022-06-29 07:24:37 +00:00
|
|
|
// check to see if the Application.Run is still valid
|
2022-07-24 21:39:17 +00:00
|
|
|
//ch := make(chan struct{})
|
2022-06-30 09:40:22 +00:00
|
|
|
msg := queuedUpdate{
|
2022-07-24 21:39:17 +00:00
|
|
|
f: f,
|
|
|
|
//done: ch,
|
2022-06-30 09:40:22 +00:00
|
|
|
}
|
2022-06-29 07:24:37 +00:00
|
|
|
if a.runContext.Err() == nil {
|
2022-07-24 21:39:17 +00:00
|
|
|
select {
|
|
|
|
case a.updates <- msg:
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2022-06-29 07:24:37 +00:00
|
|
|
}
|
2018-10-09 04:02:02 +00:00
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2018-11-05 09:53:57 +00:00
|
|
|
// QueueUpdateDraw works like QueueUpdate() except it refreshes the screen
|
|
|
|
// immediately after executing f.
|
2022-07-24 21:39:17 +00:00
|
|
|
func (a *Application) QueueUpdateDraw(
|
|
|
|
f func(),
|
|
|
|
) *Application {
|
|
|
|
a.QueueUpdate(
|
|
|
|
func() {
|
|
|
|
f()
|
|
|
|
a.draw()
|
|
|
|
},
|
|
|
|
)
|
2018-11-05 09:53:57 +00:00
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2018-10-28 12:42:49 +00:00
|
|
|
// QueueEvent sends an event to the Application event loop.
|
|
|
|
//
|
|
|
|
// It is not recommended for event to be nil.
|
|
|
|
func (a *Application) QueueEvent(event tcell.Event) *Application {
|
2022-06-29 07:24:37 +00:00
|
|
|
// check to see if the Application.Run is still valid
|
|
|
|
if a.runContext.Err() == nil {
|
|
|
|
a.events <- event
|
|
|
|
}
|
2018-10-09 04:02:02 +00:00
|
|
|
return a
|
|
|
|
}
|