2018-01-09 01:49:58 +08:00
|
|
|
// Copyright 2018 The Tcell Authors
|
2015-10-31 00:33:45 +08:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the license at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package views
|
|
|
|
|
|
|
|
import (
|
2015-11-05 03:24:00 +08:00
|
|
|
"sync"
|
|
|
|
|
2020-08-26 07:20:58 +08:00
|
|
|
"github.com/gdamore/tcell/v2"
|
2015-10-31 00:33:45 +08:00
|
|
|
)
|
|
|
|
|
2015-11-05 03:24:00 +08:00
|
|
|
// Application represents an event-driven application running on a screen.
|
|
|
|
type Application struct {
|
|
|
|
widget Widget
|
|
|
|
screen tcell.Screen
|
|
|
|
style tcell.Style
|
|
|
|
err error
|
|
|
|
wg sync.WaitGroup
|
|
|
|
}
|
2015-10-31 00:33:45 +08:00
|
|
|
|
2015-11-05 03:24:00 +08:00
|
|
|
// SetRootWidget sets the primary (root, main) Widget to be displayed.
|
|
|
|
func (app *Application) SetRootWidget(widget Widget) {
|
|
|
|
app.widget = widget
|
2015-10-31 00:33:45 +08:00
|
|
|
}
|
|
|
|
|
2015-11-05 03:24:00 +08:00
|
|
|
// initialize initializes the application. It will normally attempt to
|
|
|
|
// allocate a default screen if one is not already established.
|
|
|
|
func (app *Application) initialize() error {
|
|
|
|
if app.screen == nil {
|
|
|
|
if app.screen, app.err = tcell.NewScreen(); app.err != nil {
|
|
|
|
return app.err
|
2015-10-31 00:33:45 +08:00
|
|
|
}
|
2015-11-05 03:24:00 +08:00
|
|
|
app.screen.SetStyle(app.style)
|
2015-10-31 00:33:45 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-11-05 03:24:00 +08:00
|
|
|
// Quit causes the application to shutdown gracefully. It does not wait
|
|
|
|
// for the application to exit, but returns immediately.
|
|
|
|
func (app *Application) Quit() {
|
2015-10-31 00:33:45 +08:00
|
|
|
ev := &eventAppQuit{}
|
|
|
|
ev.SetEventNow()
|
2015-11-05 03:24:00 +08:00
|
|
|
if scr := app.screen; scr != nil {
|
|
|
|
go func() { scr.PostEventWait(ev) }()
|
2015-10-31 00:33:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-05 03:24:00 +08:00
|
|
|
// Refresh causes the application forcibly redraw everything. Use this
|
2015-10-31 00:33:45 +08:00
|
|
|
// to clear up screen corruption, etc.
|
2015-11-05 03:24:00 +08:00
|
|
|
func (app *Application) Refresh() {
|
|
|
|
ev := &eventAppRefresh{}
|
2015-10-31 00:33:45 +08:00
|
|
|
ev.SetEventNow()
|
2015-11-05 03:24:00 +08:00
|
|
|
if scr := app.screen; scr != nil {
|
|
|
|
go func() { scr.PostEventWait(ev) }()
|
2015-10-31 00:33:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-05 03:24:00 +08:00
|
|
|
// Update asks the application to draw any screen updates that have not
|
|
|
|
// been drawn yet.
|
|
|
|
func (app *Application) Update() {
|
|
|
|
ev := &eventAppUpdate{}
|
2015-10-31 00:33:45 +08:00
|
|
|
ev.SetEventNow()
|
2015-11-05 03:24:00 +08:00
|
|
|
if scr := app.screen; scr != nil {
|
|
|
|
go func() { scr.PostEventWait(ev) }()
|
2015-10-31 00:33:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-05 03:24:00 +08:00
|
|
|
// PostFunc posts a function to be executed in the context of the
|
2015-10-31 00:33:45 +08:00
|
|
|
// application's event loop. Functions that need to update displayed
|
|
|
|
// state, etc. can do this to avoid holding locks.
|
2015-11-05 03:24:00 +08:00
|
|
|
func (app *Application) PostFunc(fn func()) {
|
2015-10-31 00:33:45 +08:00
|
|
|
ev := &eventAppFunc{fn: fn}
|
|
|
|
ev.SetEventNow()
|
2015-11-05 03:24:00 +08:00
|
|
|
if scr := app.screen; scr != nil {
|
|
|
|
go func() { scr.PostEventWait(ev) }()
|
2015-10-31 00:33:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-05 03:24:00 +08:00
|
|
|
// SetScreen sets the screen to use for the application. This must be
|
|
|
|
// done before the application starts to run or is initialized.
|
|
|
|
func (app *Application) SetScreen(scr tcell.Screen) {
|
|
|
|
if app.screen == nil {
|
|
|
|
app.screen = scr
|
|
|
|
app.err = nil
|
|
|
|
}
|
2015-10-31 00:33:45 +08:00
|
|
|
}
|
|
|
|
|
2015-11-05 03:24:00 +08:00
|
|
|
// SetStyle sets the default style (background) to be used for Widgets
|
|
|
|
// that have not specified any other style.
|
|
|
|
func (app *Application) SetStyle(style tcell.Style) {
|
|
|
|
app.style = style
|
|
|
|
if app.screen != nil {
|
|
|
|
app.screen.SetStyle(style)
|
2015-11-02 06:44:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-05 03:24:00 +08:00
|
|
|
func (app *Application) run() {
|
|
|
|
|
|
|
|
screen := app.screen
|
|
|
|
widget := app.widget
|
2015-10-31 00:33:45 +08:00
|
|
|
|
2015-11-05 03:24:00 +08:00
|
|
|
if widget == nil {
|
|
|
|
app.wg.Done()
|
2015-10-31 00:33:45 +08:00
|
|
|
return
|
|
|
|
}
|
2015-11-05 03:24:00 +08:00
|
|
|
if screen == nil {
|
|
|
|
if e := app.initialize(); e != nil {
|
|
|
|
app.wg.Done()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
screen = app.screen
|
2015-10-31 00:33:45 +08:00
|
|
|
}
|
2018-01-09 01:49:58 +08:00
|
|
|
defer func() {
|
|
|
|
screen.Fini()
|
|
|
|
app.wg.Done()
|
|
|
|
}()
|
2015-11-05 03:24:00 +08:00
|
|
|
screen.Init()
|
2021-10-11 13:41:26 +08:00
|
|
|
screen.EnableMouse()
|
2015-11-05 03:24:00 +08:00
|
|
|
screen.Clear()
|
|
|
|
widget.SetView(screen)
|
2015-10-31 00:33:45 +08:00
|
|
|
|
|
|
|
loop:
|
|
|
|
for {
|
2015-11-05 03:24:00 +08:00
|
|
|
if widget = app.widget; widget == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
widget.Draw()
|
|
|
|
screen.Show()
|
2015-10-31 00:33:45 +08:00
|
|
|
|
2015-11-05 03:24:00 +08:00
|
|
|
ev := screen.PollEvent()
|
2015-10-31 00:33:45 +08:00
|
|
|
switch nev := ev.(type) {
|
|
|
|
case *eventAppQuit:
|
|
|
|
break loop
|
2015-11-05 03:24:00 +08:00
|
|
|
case *eventAppUpdate:
|
|
|
|
screen.Show()
|
|
|
|
case *eventAppRefresh:
|
|
|
|
screen.Sync()
|
2015-10-31 00:33:45 +08:00
|
|
|
case *eventAppFunc:
|
|
|
|
nev.fn()
|
|
|
|
case *tcell.EventResize:
|
2015-11-05 03:24:00 +08:00
|
|
|
screen.Sync()
|
|
|
|
widget.Resize()
|
2015-10-31 00:33:45 +08:00
|
|
|
default:
|
2015-11-05 03:24:00 +08:00
|
|
|
widget.HandleEvent(ev)
|
2015-10-31 00:33:45 +08:00
|
|
|
}
|
|
|
|
}
|
2015-11-05 03:24:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start starts the application loop, initializing the screen
|
|
|
|
// and starting the Event loop. The application will run in a goroutine
|
|
|
|
// until Quit is called.
|
|
|
|
func (app *Application) Start() {
|
|
|
|
app.wg.Add(1)
|
|
|
|
go app.run()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait waits until the application finishes.
|
|
|
|
func (app *Application) Wait() error {
|
|
|
|
app.wg.Wait()
|
|
|
|
return app.err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run runs the application, waiting until the application loop exits.
|
|
|
|
// It is equivalent to app.Start() followed by app.Wait()
|
|
|
|
func (app *Application) Run() error {
|
|
|
|
app.Start()
|
|
|
|
return app.Wait()
|
2015-10-31 00:33:45 +08:00
|
|
|
}
|
|
|
|
|
2015-11-05 03:24:00 +08:00
|
|
|
type eventAppUpdate struct {
|
2015-10-31 00:33:45 +08:00
|
|
|
tcell.EventTime
|
|
|
|
}
|
|
|
|
|
|
|
|
type eventAppQuit struct {
|
|
|
|
tcell.EventTime
|
|
|
|
}
|
|
|
|
|
2015-11-05 03:24:00 +08:00
|
|
|
type eventAppRefresh struct {
|
2015-10-31 00:33:45 +08:00
|
|
|
tcell.EventTime
|
|
|
|
}
|
|
|
|
|
|
|
|
type eventAppFunc struct {
|
|
|
|
tcell.EventTime
|
|
|
|
fn func()
|
|
|
|
}
|