Added Area.RepaintAll() (requested by aybabtme on the Gopher Academy Slack) and implemented it on Mac OS X.

This commit is contained in:
Pietro Gagliardi 2014-06-09 22:11:08 -04:00
parent 6b860779e3
commit fa4094adeb
4 changed files with 41 additions and 4 deletions

12
area.go
View File

@ -308,6 +308,18 @@ func (a *Area) SetSize(width int, height int) {
a.initheight = height
}
// RepaintAll signals the entirety of the Area for redraw.
// If called before the Window containing the Area is created, RepaintAll does nothing.
func (a *Area) RepaintAll() {
a.lock.Lock()
defer a.lock.Unlock()
if !a.created {
return
}
a.sysData.repaintAll()
}
func (a *Area) make(window *sysData) error {
a.lock.Lock()
defer a.lock.Unlock()

View File

@ -40,6 +40,7 @@ var _xSysData interface {
setProgress(int)
len() int
setAreaSize(int, int)
repaintAll()
} = &sysData{} // this line will error if there's an inconsistency
// signal sends the event signal. This raise is done asynchronously to avoid deadlocking the UI task.

View File

@ -402,3 +402,13 @@ func (s *sysData) setAreaSize(width int, height int) {
}
<-ret
}
func (s *sysData) repaintAll() {
ret := make(chan struct{})
defer close(ret)
uitask <- func() {
C.display(s.id)
ret <- struct{}{}
}
<-ret
}

View File

@ -11,6 +11,7 @@ import (
"bytes"
"time"
"strconv"
"sync"
. "github.com/andlabs/ui"
)
@ -116,7 +117,9 @@ func invalidTest(c *Combobox, l *Listbox, s *Stack, g *Grid) {
defer x("Grid.SetStretchy y > len"); g.SetStretchy(0, 5555); panic(nil)
}()
}
ah := &areaHandler{nil}
ah := &areaHandler{
img: nil,
}
type at struct {
msg string
x, y int
@ -150,6 +153,7 @@ var invalidBefore = flag.Bool("invalid", false, "run invalid test before opening
type areaHandler struct {
img *image.RGBA
lock sync.Mutex
}
func (a *areaHandler) Paint(rect image.Rectangle) *image.RGBA {
//fmt.Println(rect)
@ -160,6 +164,7 @@ func (a *areaHandler) Paint(rect image.Rectangle) *image.RGBA {
i = 1 - i
}
*/
a.lock.Lock(); defer a.lock.Unlock()
return a.img.SubImage(rect).(*image.RGBA)
}
func (a *areaHandler) Mouse(e MouseEvent) bool {
@ -170,6 +175,10 @@ func (a *areaHandler) Key(e KeyEvent) bool {
fmt.Printf("%#v %q\n", e, e.Key)
return false
}
func (a *areaHandler) mutate() {
a.lock.Lock(); defer a.lock.Unlock()
draw.Draw(a.img, a.img.Rect, &image.Uniform{color.RGBA{255,255,0,128}}, image.ZP, draw.Over)
}
var doArea = flag.Bool("area", false, "run area test instead (overrides -kb and -areabounds)")
func areaTest() {
@ -191,9 +200,10 @@ func areaTest() {
img := image.NewRGBA(ximg.Bounds())
draw.Draw(img, img.Rect, ximg, image.ZP, draw.Over)
w := NewWindow("Area Test", 100, 100)
a := NewArea(320, 240, &areaHandler{
areahandler := &areaHandler{
img: img,
})
}
a := NewArea(320, 240, areahandler)
timedisp := NewLabel("")
timechan := time.Tick(time.Second)
widthbox := NewLineEdit("320")
@ -204,7 +214,8 @@ func areaTest() {
sizeStack.SetStretchy(1)
sizeStack.SetStretchy(2)
modaltest := NewButton("Modal")
sizeStack = NewHorizontalStack(sizeStack, Space(), modaltest)
repainttest := NewButton("Repaint All")
sizeStack = NewHorizontalStack(sizeStack, repainttest, modaltest)
sizeStack.SetStretchy(0)
sizeStack.SetStretchy(1)
sizeStack.SetStretchy(2)
@ -227,6 +238,9 @@ func areaTest() {
a.SetSize(width, height)
case <-modaltest.Clicked:
MsgBox("Modal Test", "")
case <-repainttest.Clicked:
areahandler.mutate()
a.RepaintAll()
}
}
}