zntg/ui/ui.go
Sander Schobers 3c89748eac Added drag & drop addon.
- Drop addon is based on WM_DROPFILES, dragdrop addon is based on the OLE IDragDrop interface and thus can registerer more interactions.
- The allg5ui implementation will try to fall back on the drop addon (because the dragdrop addon wouldn't work properly).
- Drop addon is refactored to use the same interface as the dragdrop addon.
2021-06-04 17:17:22 +02:00

81 lines
1.6 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 {
if s == nil {
s = DefaultStyle()
}
ctx := newContext(r, s, view)
defer ctx.Destroy()
root, ok := view.(RootControl)
if ok {
err := root.Init(ctx)
if err != nil {
return err
}
}
dragDropTarget := &dragDropEventTarget{renderer: r}
dragDrop := DefaultDragDropProvider
if dragDrop != nil {
dragDrop.Register(r.WindowHandle(), dragDropTarget)
}
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, float32(size.X), float32(size.Y))
overlays.Arrange(ctx, bounds, geom.ZeroPtF32, nil)
overlays.Render(ctx)
if ctx.HasQuit() {
return nil
}
tooltip := ctx.tooltip.Text
ctx.tooltip.Text = ""
if r.PushEvents(ctx, wait) {
if ctx.tooltip.Text != tooltip {
ctx.overlays.SetVisibility(uiDefaultTooltipOverlay, ctx.tooltip.Text != "")
}
} else {
ctx.tooltip.Text = tooltip
}
dragDropEvents := dragDropTarget.events
dragDropTarget.events = nil
for _, e := range dragDropEvents {
ctx.Handle(e)
}
}
return nil
}