Improved synchronization with a quit signal.
This commit is contained in:
parent
4e6d089efe
commit
c709d906d0
@ -14,7 +14,7 @@ var _ EventTarget = &context{}
|
|||||||
|
|
||||||
type context struct {
|
type context struct {
|
||||||
animate bool
|
animate bool
|
||||||
quit bool
|
quit chan struct{}
|
||||||
r Renderer
|
r Renderer
|
||||||
view Control
|
view Control
|
||||||
ims *Images
|
ims *Images
|
||||||
@ -23,11 +23,22 @@ type context struct {
|
|||||||
|
|
||||||
func (c *context) Animate() { c.animate = true }
|
func (c *context) Animate() { c.animate = true }
|
||||||
|
|
||||||
func (c *context) HasQuit() bool { return c.quit }
|
func (c *context) HasQuit() bool {
|
||||||
|
select {
|
||||||
|
case <-c.quit:
|
||||||
|
return true
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (c *context) Images() *Images { return c.ims }
|
func (c *context) Images() *Images { return c.ims }
|
||||||
|
|
||||||
func (c *context) Quit() { c.quit = true }
|
func (c *context) Quit() {
|
||||||
|
if !c.HasQuit() {
|
||||||
|
close(c.quit)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (c *context) Renderer() Renderer { return c.r }
|
func (c *context) Renderer() Renderer { return c.r }
|
||||||
|
|
||||||
|
18
ui/ui.go
18
ui/ui.go
@ -13,7 +13,7 @@ func Run(r Renderer, s *Style, view Control) error {
|
|||||||
|
|
||||||
// RunWait runs the application loop and conditionally waits on events before rendering.
|
// RunWait runs the application loop and conditionally waits on events before rendering.
|
||||||
func RunWait(r Renderer, s *Style, view Control, wait bool) error {
|
func RunWait(r Renderer, s *Style, view Control, wait bool) error {
|
||||||
ctx := &context{r: r, style: s, view: view, ims: NewImages(r)}
|
ctx := &context{quit: make(chan struct{}), r: r, style: s, view: view, ims: NewImages(r)}
|
||||||
root, ok := view.(RootControl)
|
root, ok := view.(RootControl)
|
||||||
if ok {
|
if ok {
|
||||||
err := root.Init(ctx)
|
err := root.Init(ctx)
|
||||||
@ -23,24 +23,30 @@ func RunWait(r Renderer, s *Style, view Control, wait bool) error {
|
|||||||
}
|
}
|
||||||
anim := time.NewTicker(30 * time.Millisecond)
|
anim := time.NewTicker(30 * time.Millisecond)
|
||||||
go func() {
|
go func() {
|
||||||
for range anim.C {
|
for {
|
||||||
if ctx.animate && !ctx.quit {
|
select {
|
||||||
|
case <-anim.C:
|
||||||
|
case <-ctx.quit:
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if ctx.animate {
|
||||||
r.Refresh()
|
r.Refresh()
|
||||||
}
|
}
|
||||||
ctx.animate = false
|
ctx.animate = false
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
defer anim.Stop()
|
||||||
|
|
||||||
ctx.Renderer().Refresh()
|
ctx.Renderer().Refresh()
|
||||||
for !ctx.quit {
|
for !ctx.HasQuit() {
|
||||||
var size = r.Size()
|
var size = r.Size()
|
||||||
var bounds = geom.RectF32(0, 0, size.X, size.Y)
|
var bounds = geom.RectF32(0, 0, size.X, size.Y)
|
||||||
view.Arrange(ctx, bounds, geom.ZeroPtF32, nil)
|
view.Arrange(ctx, bounds, geom.ZeroPtF32, nil)
|
||||||
view.Render(ctx)
|
view.Render(ctx)
|
||||||
if ctx.quit {
|
if ctx.HasQuit() {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
r.PushEvents(ctx, wait)
|
r.PushEvents(ctx, wait)
|
||||||
}
|
}
|
||||||
anim.Stop()
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user