Skip to content

Commit

Permalink
fixes #682 views/app uses deprecated PostEventWait
Browse files Browse the repository at this point in the history
  • Loading branch information
gdamore committed Feb 16, 2024
1 parent c3711de commit cc24c71
Showing 1 changed file with 25 additions and 38 deletions.
63 changes: 25 additions & 38 deletions views/app.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2018 The Tcell Authors
// Copyright 2024 The Tcell Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use file except in compliance with the License.
Expand All @@ -22,12 +22,15 @@ import (

// 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
paste bool
widget Widget
screen tcell.Screen
style tcell.Style
err error
wg sync.WaitGroup
paste bool
stopQ chan struct{}
eventQ chan tcell.Event
stopOnce sync.Once
}

// SetRootWidget sets the primary (root, main) Widget to be displayed.
Expand Down Expand Up @@ -56,30 +59,23 @@ func (app *Application) initialize() error {
// 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) }()
}
app.stopOnce.Do(func() { close(app.stopQ) })

Check warning on line 62 in views/app.go

View check run for this annotation

Codecov / codecov/patch

views/app.go#L62

Added line #L62 was not covered by tests
}

// 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) }()
scr.Sync()

Check warning on line 69 in views/app.go

View check run for this annotation

Codecov / codecov/patch

views/app.go#L69

Added line #L69 was not covered by tests
}
}

// Update asks the application to draw any screen updates that have not
// been drawn yet.
// been drawn yet. It is not necessary to call this from inside an
// event handler, as a draw will be done implicitly after handling events.
func (app *Application) Update() {
ev := &eventAppUpdate{}
ev.SetEventNow()
if scr := app.screen; scr != nil {
go func() { scr.PostEventWait(ev) }()
scr.Show()

Check warning on line 78 in views/app.go

View check run for this annotation

Codecov / codecov/patch

views/app.go#L78

Added line #L78 was not covered by tests
}
}

Expand Down Expand Up @@ -140,6 +136,10 @@ func (app *Application) run() {
screen.Clear()
widget.SetView(screen)

app.eventQ = make(chan tcell.Event, 16)
app.stopQ = make(chan struct{})
go screen.ChannelEvents(app.eventQ, app.stopQ)

Check warning on line 142 in views/app.go

View check run for this annotation

Codecov / codecov/patch

views/app.go#L139-L142

Added lines #L139 - L142 were not covered by tests
loop:
for {
if widget = app.widget; widget == nil {
Expand All @@ -148,14 +148,13 @@ loop:
widget.Draw()
screen.Show()

ev := screen.PollEvent()
switch nev := ev.(type) {
case *eventAppQuit:
ev := <-app.eventQ
// ev := screen.PollEvent()
if ev == nil {
screen.Fini()

Check warning on line 154 in views/app.go

View check run for this annotation

Codecov / codecov/patch

views/app.go#L151-L154

Added lines #L151 - L154 were not covered by tests
break loop
case *eventAppUpdate:
screen.Show()
case *eventAppRefresh:
screen.Sync()
}
switch nev := ev.(type) {

Check warning on line 157 in views/app.go

View check run for this annotation

Codecov / codecov/patch

views/app.go#L157

Added line #L157 was not covered by tests
case *eventAppFunc:
nev.fn()
case *tcell.EventResize:
Expand Down Expand Up @@ -188,18 +187,6 @@ func (app *Application) Run() error {
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()
Expand Down

0 comments on commit cc24c71

Please sign in to comment.