zntg/ui/ui.go

53 lines
1.0 KiB
Go
Raw Normal View History

package ui
import (
2019-04-10 19:20:39 +00:00
"time"
"opslag.de/schobers/geom"
)
// Run runs the application loop.
func Run(r Renderer, s *Style, view Control) error {
return RunWait(r, s, view, false)
}
// RunWait runs the application loop and conditionally waits on events before rendering.
func RunWait(r Renderer, s *Style, view Control, wait bool) error {
ctx := &context{quit: make(chan struct{}), r: r, style: s, view: view, ims: NewImages(r)}
root, ok := view.(RootControl)
if ok {
2019-05-01 07:40:59 +00:00
err := root.Init(ctx)
if err != nil {
return err
}
}
2019-04-10 19:20:39 +00:00
anim := time.NewTicker(30 * time.Millisecond)
go func() {
for {
select {
case <-anim.C:
case <-ctx.quit:
return
}
if ctx.animate {
2019-04-10 19:20:39 +00:00
r.Refresh()
}
ctx.animate = false
}
}()
defer anim.Stop()
2019-04-10 19:20:39 +00:00
ctx.Renderer().Refresh()
for !ctx.HasQuit() {
var size = r.Size()
var bounds = geom.RectF32(0, 0, size.X, size.Y)
view.Arrange(ctx, bounds, geom.ZeroPtF32, nil)
view.Render(ctx)
if ctx.HasQuit() {
return nil
}
r.PushEvents(ctx, wait)
}
return nil
}