From fa4094adeb122ea23cc898da75421230d7b7eda1 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Mon, 9 Jun 2014 22:11:08 -0400 Subject: [PATCH] Added Area.RepaintAll() (requested by aybabtme on the Gopher Academy Slack) and implemented it on Mac OS X. --- area.go | 12 ++++++++++++ sysdata.go | 1 + sysdata_darwin.go | 10 ++++++++++ test/main.go | 22 ++++++++++++++++++---- 4 files changed, 41 insertions(+), 4 deletions(-) diff --git a/area.go b/area.go index 8bf1d60..6558db8 100644 --- a/area.go +++ b/area.go @@ -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() diff --git a/sysdata.go b/sysdata.go index 1507912..074ef64 100644 --- a/sysdata.go +++ b/sysdata.go @@ -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. diff --git a/sysdata_darwin.go b/sysdata_darwin.go index 72a5519..d0e9b16 100644 --- a/sysdata_darwin.go +++ b/sysdata_darwin.go @@ -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 +} diff --git a/test/main.go b/test/main.go index 59a5791..b39829b 100644 --- a/test/main.go +++ b/test/main.go @@ -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() } } }