Many Drawable implementations have their own Invalidate and OnInvalidate functions, with an unexported onInvalidate field. However OnInvalidate and Invalidate are usually not called in the same goroutine. This results in a race on this field, e.g.: Read at 0x00c000094748 by goroutine 7: git.sr.ht/~sircmpwn/aerc2/widgets.NewDirectoryList.func1() /home/simon/src/aerc2/widgets/dirlist.go:85 +0x56 git.sr.ht/~sircmpwn/aerc2/widgets.(*Spinner).Start.func1() /home/simon/src/aerc2/widgets/spinner.go:93 +0x1bb Previous write at 0x00c000094748 by main goroutine: [failed to restore the stack] Goroutine 7 (running) created at: git.sr.ht/~sircmpwn/aerc2/widgets.(*Spinner).Start() /home/simon/src/aerc2/widgets/spinner.go:46 +0x8f git.sr.ht/~sircmpwn/aerc2/widgets.NewDirectoryList() /home/simon/src/aerc2/widgets/dirlist.go:37 +0x286 git.sr.ht/~sircmpwn/aerc2/widgets.NewAccountView() /home/simon/src/aerc2/widgets/account.go:50 +0x5ca git.sr.ht/~sircmpwn/aerc2/widgets.NewAerc() /home/simon/src/aerc2/widgets/aerc.go:60 +0x800 main.main() /home/simon/src/aerc2/aerc.go:65 +0x33e To fix this, introduce a new type, Invalidatable, which protects the field. Unfortunately the Drawable must be passed to the callback function in Invalidate, so we still need to re-implement this in each Invalidatable user.
85 lines
1.7 KiB
Go
85 lines
1.7 KiB
Go
package widgets
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/gdamore/tcell"
|
|
|
|
"git.sr.ht/~sircmpwn/aerc2/lib/ui"
|
|
)
|
|
|
|
type StatusLine struct {
|
|
ui.Invalidatable
|
|
stack []*StatusMessage
|
|
fallback StatusMessage
|
|
}
|
|
|
|
type StatusMessage struct {
|
|
bg tcell.Color
|
|
fg tcell.Color
|
|
message string
|
|
}
|
|
|
|
func NewStatusLine() *StatusLine {
|
|
return &StatusLine{
|
|
fallback: StatusMessage{
|
|
bg: tcell.ColorDefault,
|
|
fg: tcell.ColorDefault,
|
|
message: "Idle",
|
|
},
|
|
}
|
|
}
|
|
|
|
func (status *StatusLine) Invalidate() {
|
|
status.DoInvalidate(status)
|
|
}
|
|
|
|
func (status *StatusLine) Draw(ctx *ui.Context) {
|
|
line := &status.fallback
|
|
if len(status.stack) != 0 {
|
|
line = status.stack[len(status.stack)-1]
|
|
}
|
|
style := tcell.StyleDefault.
|
|
Background(line.bg).Foreground(line.fg).Reverse(true)
|
|
ctx.Fill(0, 0, ctx.Width(), ctx.Height(), ' ', style)
|
|
ctx.Printf(0, 0, style, "%s", line.message)
|
|
}
|
|
|
|
func (status *StatusLine) Set(text string) *StatusMessage {
|
|
status.fallback = StatusMessage{
|
|
bg: tcell.ColorDefault,
|
|
fg: tcell.ColorDefault,
|
|
message: text,
|
|
}
|
|
status.Invalidate()
|
|
return &status.fallback
|
|
}
|
|
|
|
func (status *StatusLine) Push(text string, expiry time.Duration) *StatusMessage {
|
|
msg := &StatusMessage{
|
|
bg: tcell.ColorDefault,
|
|
fg: tcell.ColorDefault,
|
|
message: text,
|
|
}
|
|
status.stack = append(status.stack, msg)
|
|
go (func() {
|
|
time.Sleep(expiry)
|
|
for i, m := range status.stack {
|
|
if m == msg {
|
|
status.stack = append(status.stack[:i], status.stack[i+1:]...)
|
|
break
|
|
}
|
|
}
|
|
status.Invalidate()
|
|
})()
|
|
return msg
|
|
}
|
|
|
|
func (status *StatusLine) Expire() {
|
|
status.stack = nil
|
|
}
|
|
|
|
func (msg *StatusMessage) Color(bg tcell.Color, fg tcell.Color) {
|
|
msg.bg = bg
|
|
msg.fg = fg
|
|
}
|