diff --git a/application.go b/application.go index 6e6666b..7e47860 100644 --- a/application.go +++ b/application.go @@ -97,6 +97,14 @@ type Application struct { // be forwarded). inputCapture func(event *tcell.EventKey) *tcell.EventKey + // An optional callback function which is invoked before the application's + // focus changes. + beforeFocus func(p Primitive) bool + + // An optional callback function which is invoked after the application's + // focus changes. + afterFocus func(p Primitive) + // An optional callback function which is invoked just before the root // primitive is drawn. beforeDraw func(screen tcell.Screen) bool @@ -812,14 +820,34 @@ func (a *Application) ResizeToFullScreen(p Primitive) *Application { // called on the new primitive. func (a *Application) SetFocus(p Primitive) *Application { a.Lock() + + if a.beforeFocus != nil { + a.Unlock() + ok := a.beforeFocus(p) + if !ok { + return a + } + a.Lock() + } + if a.focus != nil { a.focus.Blur() } + a.focus = p + if a.screen != nil { a.screen.HideCursor() } - a.Unlock() + + if a.afterFocus != nil { + a.Unlock() + + a.afterFocus(p) + } else { + a.Unlock() + } + if p != nil { p.Focus(func(p Primitive) { a.SetFocus(p) @@ -837,6 +865,30 @@ func (a *Application) GetFocus() Primitive { return a.focus } +// SetBeforeFocusFunc installs a callback function which is invoked before the +// application's focus changes. Return false to maintain the current focus. +// +// Provide nil to uninstall the callback function. +func (a *Application) SetBeforeFocusFunc(handler func(p Primitive) bool) *Application { + a.Lock() + defer a.Unlock() + + a.beforeFocus = handler + return a +} + +// SetAfterFocusFunc installs a callback function which is invoked after the +// application's focus changes. +// +// Provide nil to uninstall the callback function. +func (a *Application) SetAfterFocusFunc(handler func(p Primitive)) *Application { + a.Lock() + defer a.Unlock() + + a.afterFocus = handler + return a +} + // 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