2019-03-05 20:52:18 +00:00
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
2019-04-10 19:20:39 +00:00
|
|
|
"time"
|
|
|
|
|
2019-03-05 20:52:18 +00:00
|
|
|
"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 {
|
2019-04-10 20:41:28 +00:00
|
|
|
ctx := &context{r: r, style: s, view: view, ims: NewImages(r)}
|
2019-03-05 20:52:18 +00:00
|
|
|
root, ok := view.(RootControl)
|
|
|
|
if ok {
|
|
|
|
root.Init(ctx)
|
|
|
|
}
|
2019-04-10 19:20:39 +00:00
|
|
|
anim := time.NewTicker(30 * time.Millisecond)
|
|
|
|
go func() {
|
|
|
|
for range anim.C {
|
|
|
|
if ctx.animate && !ctx.quit {
|
|
|
|
r.Refresh()
|
|
|
|
}
|
|
|
|
ctx.animate = false
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
ctx.Renderer().Refresh()
|
2019-03-05 20:52:18 +00:00
|
|
|
for !ctx.quit {
|
|
|
|
var size = r.Size()
|
|
|
|
var bounds = geom.RectF32(0, 0, size.X, size.Y)
|
|
|
|
view.Arrange(ctx, bounds, geom.ZeroPtF32)
|
|
|
|
view.Render(ctx)
|
|
|
|
if ctx.quit {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
r.PushEvents(ctx, wait)
|
|
|
|
}
|
2019-04-10 19:20:39 +00:00
|
|
|
anim.Stop()
|
2019-03-05 20:52:18 +00:00
|
|
|
return nil
|
|
|
|
}
|