zntg/ui/ui.go
Sander Schobers dbc017507c Added Parent to Controls.
Fixed issue where events of partially and invisible controls where incorrectly handled.
2019-04-11 23:38:32 +02:00

44 lines
907 B
Go

package ui
import (
"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{r: r, style: s, view: view, ims: NewImages(r)}
root, ok := view.(RootControl)
if ok {
root.Init(ctx)
}
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()
for !ctx.quit {
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.quit {
return nil
}
r.PushEvents(ctx, wait)
}
anim.Stop()
return nil
}