2023-07-02 00:52:14 -06:00
|
|
|
package x
|
|
|
|
|
2023-07-03 22:04:00 -06:00
|
|
|
import "image"
|
2023-07-02 00:52:14 -06:00
|
|
|
import "git.tebibyte.media/tomo/tomo"
|
2023-07-03 22:04:00 -06:00
|
|
|
import "git.tebibyte.media/tomo/tomo/canvas"
|
|
|
|
|
|
|
|
type boxSet map[anyBox] struct { }
|
|
|
|
|
|
|
|
func (set boxSet) Empty () bool {
|
|
|
|
return set == nil || len(set) == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (set boxSet) Has (box anyBox) bool {
|
|
|
|
if set == nil { return false }
|
|
|
|
_, ok := set[box]
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func (set *boxSet) Add (box anyBox) {
|
|
|
|
if *set == nil {
|
|
|
|
*set = make(boxSet)
|
|
|
|
}
|
|
|
|
(*set)[box] = struct { } { }
|
|
|
|
}
|
|
|
|
|
|
|
|
func (set *boxSet) Pop () anyBox {
|
|
|
|
for box := range *set {
|
|
|
|
delete(*set, box)
|
|
|
|
return box
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type parent interface {
|
|
|
|
window () *window
|
|
|
|
canvas () canvas.Canvas
|
|
|
|
}
|
2023-07-02 00:52:14 -06:00
|
|
|
|
|
|
|
type anyBox interface {
|
|
|
|
tomo.Box
|
2023-07-03 22:04:00 -06:00
|
|
|
doDraw ()
|
|
|
|
doLayout ()
|
|
|
|
setParent (parent)
|
|
|
|
recursiveRedo ()
|
2023-07-02 00:52:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (window *window) SetRoot (root tomo.Object) {
|
2023-07-04 22:44:56 -06:00
|
|
|
if window.root != nil {
|
|
|
|
window.root.setParent(nil)
|
|
|
|
}
|
|
|
|
if root == nil {
|
|
|
|
window.root = nil
|
|
|
|
} else {
|
|
|
|
box := root.Box().(anyBox)
|
|
|
|
box.setParent(window)
|
|
|
|
window.invalidateLayout(box)
|
|
|
|
window.root = box
|
|
|
|
}
|
2023-07-02 00:52:14 -06:00
|
|
|
}
|
|
|
|
|
2023-07-03 22:04:00 -06:00
|
|
|
func (window *window) window () *window {
|
|
|
|
return window
|
|
|
|
}
|
|
|
|
|
|
|
|
func (window *window) canvas () canvas.Canvas {
|
|
|
|
return window.xCanvas
|
|
|
|
}
|
|
|
|
|
|
|
|
func (window *window) invalidateDraw (box anyBox) {
|
|
|
|
window.needDraw.Add(box)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (window *window) invalidateLayout (box anyBox) {
|
|
|
|
window.needLayout.Add(box)
|
|
|
|
window.invalidateDraw(box)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (window *window) focus (box anyBox) {
|
|
|
|
if window.focused == box { return }
|
|
|
|
window.invalidateDraw(window.focused)
|
|
|
|
window.focused = box
|
|
|
|
window.invalidateDraw(box)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (window *window) afterEvent () {
|
2023-07-04 22:44:56 -06:00
|
|
|
if window.xCanvas == nil { return }
|
|
|
|
|
2023-07-03 22:04:00 -06:00
|
|
|
if window.needRedo {
|
2023-07-04 22:44:56 -06:00
|
|
|
// set child bounds
|
|
|
|
childBounds := window.metrics.bounds
|
|
|
|
childBounds = childBounds.Sub(childBounds.Min)
|
|
|
|
window.root.SetBounds(childBounds)
|
|
|
|
|
|
|
|
// full relayout/redraw
|
2023-07-03 22:04:00 -06:00
|
|
|
if window.root != nil {
|
|
|
|
window.root.recursiveRedo()
|
|
|
|
}
|
2023-07-04 22:44:56 -06:00
|
|
|
window.pushAll()
|
|
|
|
window.needRedo = false
|
2023-07-03 22:04:00 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for len(window.needLayout) > 0 {
|
|
|
|
window.needLayout.Pop().doLayout()
|
|
|
|
}
|
|
|
|
var toPush image.Rectangle
|
|
|
|
for len(window.needDraw) > 0 {
|
|
|
|
box := window.needDraw.Pop()
|
|
|
|
box.doDraw()
|
|
|
|
toPush = toPush.Union(box.Bounds())
|
|
|
|
}
|
2023-07-04 22:44:56 -06:00
|
|
|
if !toPush.Empty() {
|
|
|
|
window.pushRegion(toPush)
|
|
|
|
}
|
2023-07-02 00:52:14 -06:00
|
|
|
}
|