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.4 KiB
Go
85 lines
1.4 KiB
Go
package ui
|
|
|
|
import (
|
|
"github.com/gdamore/tcell"
|
|
"github.com/mattn/go-runewidth"
|
|
)
|
|
|
|
const (
|
|
TEXT_LEFT = iota
|
|
TEXT_CENTER = iota
|
|
TEXT_RIGHT = iota
|
|
)
|
|
|
|
type Text struct {
|
|
Invalidatable
|
|
text string
|
|
strategy uint
|
|
fg tcell.Color
|
|
bg tcell.Color
|
|
bold bool
|
|
reverse bool
|
|
}
|
|
|
|
func NewText(text string) *Text {
|
|
return &Text{
|
|
bg: tcell.ColorDefault,
|
|
fg: tcell.ColorDefault,
|
|
text: text,
|
|
}
|
|
}
|
|
|
|
func (t *Text) Text(text string) *Text {
|
|
t.text = text
|
|
t.Invalidate()
|
|
return t
|
|
}
|
|
|
|
func (t *Text) Strategy(strategy uint) *Text {
|
|
t.strategy = strategy
|
|
t.Invalidate()
|
|
return t
|
|
}
|
|
|
|
func (t *Text) Bold(bold bool) *Text {
|
|
t.bold = bold
|
|
t.Invalidate()
|
|
return t
|
|
}
|
|
|
|
func (t *Text) Color(fg tcell.Color, bg tcell.Color) *Text {
|
|
t.fg = fg
|
|
t.bg = bg
|
|
t.Invalidate()
|
|
return t
|
|
}
|
|
|
|
func (t *Text) Reverse(reverse bool) *Text {
|
|
t.reverse = reverse
|
|
t.Invalidate()
|
|
return t
|
|
}
|
|
|
|
func (t *Text) Draw(ctx *Context) {
|
|
size := runewidth.StringWidth(t.text)
|
|
x := 0
|
|
if t.strategy == TEXT_CENTER {
|
|
x = (ctx.Width() - size) / 2
|
|
}
|
|
if t.strategy == TEXT_RIGHT {
|
|
x = ctx.Width() - size
|
|
}
|
|
style := tcell.StyleDefault.Background(t.bg).Foreground(t.fg)
|
|
if t.bold {
|
|
style = style.Bold(true)
|
|
}
|
|
if t.reverse {
|
|
style = style.Reverse(true)
|
|
}
|
|
ctx.Fill(0, 0, ctx.Width(), ctx.Height(), ' ', style)
|
|
ctx.Printf(x, 0, style, t.text)
|
|
}
|
|
|
|
func (t *Text) Invalidate() {
|
|
t.DoInvalidate(t)
|
|
}
|