1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
|
// Copyright 2015 The Tcell Authors
//
// 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 (
"sync"
"github.com/zyedidia/tcell"
)
// 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
}
// SetRootWidget sets the primary (root, main) Widget to be displayed.
func (app *Application) SetRootWidget(widget Widget) {
app.widget = widget
}
// 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
}
app.screen.SetStyle(app.style)
}
return nil
}
// Quit causes the application to shutdown gracefully. It does not wait
// for the application to exit, but returns immediately.
func (app *Application) Quit() {
ev := &eventAppQuit{}
ev.SetEventNow()
if scr := app.screen; scr != nil {
go func() { scr.PostEventWait(ev) }()
}
}
// Refresh causes the application forcibly redraw everything. Use this
// to clear up screen corruption, etc.
func (app *Application) Refresh() {
ev := &eventAppRefresh{}
ev.SetEventNow()
if scr := app.screen; scr != nil {
go func() { scr.PostEventWait(ev) }()
}
}
// Update asks the application to draw any screen updates that have not
// been drawn yet.
func (app *Application) Update() {
ev := &eventAppUpdate{}
ev.SetEventNow()
if scr := app.screen; scr != nil {
go func() { scr.PostEventWait(ev) }()
}
}
// PostFunc posts a function to be executed in the context of the
// application's event loop. Functions that need to update displayed
// state, etc. can do this to avoid holding locks.
func (app *Application) PostFunc(fn func()) {
ev := &eventAppFunc{fn: fn}
ev.SetEventNow()
if scr := app.screen; scr != nil {
go func() { scr.PostEventWait(ev) }()
}
}
// 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
}
}
// 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)
}
}
func (app *Application) run() {
screen := app.screen
widget := app.widget
if widget == nil {
app.wg.Done()
return
}
if screen == nil {
if e := app.initialize(); e != nil {
app.wg.Done()
return
}
screen = app.screen
}
screen.Init()
screen.Clear()
widget.SetView(screen)
loop:
for {
if widget = app.widget; widget == nil {
break
}
widget.Draw()
screen.Show()
ev := screen.PollEvent()
switch nev := ev.(type) {
case *eventAppQuit:
break loop
case *eventAppUpdate:
screen.Show()
case *eventAppRefresh:
screen.Sync()
case *eventAppFunc:
nev.fn()
case *tcell.EventResize:
screen.Sync()
widget.Resize()
default:
widget.HandleEvent(ev)
}
}
screen.Fini()
app.wg.Done()
}
// 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()
}
type eventAppUpdate struct {
tcell.EventTime
}
type eventAppQuit struct {
tcell.EventTime
}
type eventAppRefresh struct {
tcell.EventTime
}
type eventAppFunc struct {
tcell.EventTime
fn func()
}
|