|
|
@ -41,8 +41,8 @@ type Application struct {
|
|
|
|
// was drawn.
|
|
|
|
// was drawn.
|
|
|
|
afterDraw func(screen tcell.Screen)
|
|
|
|
afterDraw func(screen tcell.Screen)
|
|
|
|
|
|
|
|
|
|
|
|
// If this value is true, the application has entered suspended mode.
|
|
|
|
// Used for events loop lock during Suspend()
|
|
|
|
suspended bool
|
|
|
|
suspendMutex sync.Mutex
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// NewApplication creates and returns a new application.
|
|
|
|
// NewApplication creates and returns a new application.
|
|
|
@ -103,28 +103,20 @@ func (a *Application) Run() error {
|
|
|
|
|
|
|
|
|
|
|
|
// Start event loop.
|
|
|
|
// Start event loop.
|
|
|
|
for {
|
|
|
|
for {
|
|
|
|
|
|
|
|
// Do not poll events during suspend mode
|
|
|
|
|
|
|
|
a.suspendMutex.Lock()
|
|
|
|
a.Lock()
|
|
|
|
a.Lock()
|
|
|
|
screen := a.screen
|
|
|
|
screen := a.screen
|
|
|
|
if a.suspended {
|
|
|
|
|
|
|
|
a.suspended = false // Clear previous suspended flag.
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
a.Unlock()
|
|
|
|
a.Unlock()
|
|
|
|
if screen == nil {
|
|
|
|
if screen == nil {
|
|
|
|
|
|
|
|
a.suspendMutex.Unlock()
|
|
|
|
break
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Wait for next event.
|
|
|
|
// Wait for next event.
|
|
|
|
event := a.screen.PollEvent()
|
|
|
|
event := a.screen.PollEvent()
|
|
|
|
|
|
|
|
a.suspendMutex.Unlock()
|
|
|
|
if event == nil {
|
|
|
|
if event == nil {
|
|
|
|
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.
|
|
|
|
// The screen was finalized. Exit the loop.
|
|
|
|
break
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -190,14 +182,14 @@ func (a *Application) Stop() {
|
|
|
|
func (a *Application) Suspend(f func()) bool {
|
|
|
|
func (a *Application) Suspend(f func()) bool {
|
|
|
|
a.Lock()
|
|
|
|
a.Lock()
|
|
|
|
|
|
|
|
|
|
|
|
if a.suspended || a.screen == nil {
|
|
|
|
if a.screen == nil {
|
|
|
|
// Application is already suspended.
|
|
|
|
// Application is already suspended.
|
|
|
|
a.Unlock()
|
|
|
|
a.Unlock()
|
|
|
|
return false
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Enter suspended mode.
|
|
|
|
// Enter suspended mode.
|
|
|
|
a.suspended = true
|
|
|
|
a.suspendMutex.Lock()
|
|
|
|
a.Unlock()
|
|
|
|
a.Unlock()
|
|
|
|
a.Stop()
|
|
|
|
a.Stop()
|
|
|
|
|
|
|
|
|
|
|
@ -217,10 +209,12 @@ func (a *Application) Suspend(f func()) bool {
|
|
|
|
var err error
|
|
|
|
var err error
|
|
|
|
a.screen, err = tcell.NewScreen()
|
|
|
|
a.screen, err = tcell.NewScreen()
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
a.suspendMutex.Unlock()
|
|
|
|
a.Unlock()
|
|
|
|
a.Unlock()
|
|
|
|
panic(err)
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err = a.screen.Init(); err != nil {
|
|
|
|
if err = a.screen.Init(); err != nil {
|
|
|
|
|
|
|
|
a.suspendMutex.Unlock()
|
|
|
|
a.Unlock()
|
|
|
|
a.Unlock()
|
|
|
|
panic(err)
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -228,6 +222,7 @@ func (a *Application) Suspend(f func()) bool {
|
|
|
|
a.Draw()
|
|
|
|
a.Draw()
|
|
|
|
|
|
|
|
|
|
|
|
// Continue application loop.
|
|
|
|
// Continue application loop.
|
|
|
|
|
|
|
|
a.suspendMutex.Unlock()
|
|
|
|
return true
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|