mirror of
https://git.sr.ht/~sircmpwn/aerc
synced 2026-01-03 05:01:40 +01:00
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.
69 lines
1.4 KiB
Go
69 lines
1.4 KiB
Go
package ui
|
|
|
|
import (
|
|
"github.com/gdamore/tcell"
|
|
)
|
|
|
|
const (
|
|
BORDER_LEFT = 1 << iota
|
|
BORDER_TOP = 1 << iota
|
|
BORDER_RIGHT = 1 << iota
|
|
BORDER_BOTTOM = 1 << iota
|
|
)
|
|
|
|
type Bordered struct {
|
|
Invalidatable
|
|
borders uint
|
|
content Drawable
|
|
onInvalidate func(d Drawable)
|
|
}
|
|
|
|
func NewBordered(content Drawable, borders uint) *Bordered {
|
|
b := &Bordered{
|
|
borders: borders,
|
|
content: content,
|
|
}
|
|
content.OnInvalidate(b.contentInvalidated)
|
|
return b
|
|
}
|
|
|
|
func (bordered *Bordered) contentInvalidated(d Drawable) {
|
|
bordered.Invalidate()
|
|
}
|
|
|
|
func (bordered *Bordered) Children() []Drawable {
|
|
return []Drawable{bordered.content}
|
|
}
|
|
|
|
func (bordered *Bordered) Invalidate() {
|
|
bordered.DoInvalidate(bordered)
|
|
}
|
|
|
|
func (bordered *Bordered) Draw(ctx *Context) {
|
|
x := 0
|
|
y := 0
|
|
width := ctx.Width()
|
|
height := ctx.Height()
|
|
style := tcell.StyleDefault.Reverse(true)
|
|
if bordered.borders&BORDER_LEFT != 0 {
|
|
ctx.Fill(0, 0, 1, ctx.Height(), ' ', style)
|
|
x += 1
|
|
width -= 1
|
|
}
|
|
if bordered.borders&BORDER_TOP != 0 {
|
|
ctx.Fill(0, 0, ctx.Width(), 1, ' ', style)
|
|
y += 1
|
|
height -= 1
|
|
}
|
|
if bordered.borders&BORDER_RIGHT != 0 {
|
|
ctx.Fill(ctx.Width()-1, 0, 1, ctx.Height(), ' ', style)
|
|
width -= 1
|
|
}
|
|
if bordered.borders&BORDER_BOTTOM != 0 {
|
|
ctx.Fill(0, ctx.Height()-1, ctx.Width(), 1, ' ', style)
|
|
height -= 1
|
|
}
|
|
subctx := ctx.Subcontext(x, y, width, height)
|
|
bordered.content.Draw(subctx)
|
|
}
|