zntg/ui/ui.go
Sander Schobers b9534ee255 Made tooltip overlay switch visibility only when changed.
Added Tooltip.SetVisibility.
Fixed bug where no events where triggered for change in visibility.
2020-05-17 07:21:58 +02:00

62 lines
1.2 KiB
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 := newContext(r, s, view)
defer ctx.Destroy()
root, ok := view.(RootControl)
if ok {
err := root.Init(ctx)
if err != nil {
return err
}
}
anim := time.NewTicker(30 * time.Millisecond)
go func() {
for {
select {
case <-anim.C:
case <-ctx.quit:
return
}
if ctx.animate {
r.Refresh()
}
ctx.animate = false
}
}()
defer anim.Stop()
overlays := ctx.Overlays()
ctx.Renderer().Refresh()
for !ctx.HasQuit() {
var size = r.Size()
var bounds = geom.RectF32(0, 0, size.X, size.Y)
overlays.Arrange(ctx, bounds, geom.ZeroPtF32, nil)
overlays.Render(ctx)
if ctx.HasQuit() {
return nil
}
tooltip := ctx.tooltip.Text
ctx.tooltip.Text = ""
r.PushEvents(ctx, wait)
if ctx.tooltip.Text != tooltip {
ctx.overlays.SetVisibility(uiDefaultTooltipOverlay, ctx.tooltip.Text != "")
}
}
return nil
}