forked from google/gxui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
draw_paint.go
73 lines (61 loc) · 1.46 KB
/
draw_paint.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package parts
import (
"fmt"
"runtime"
"github.com/google/gxui"
"github.com/google/gxui/mixins/outer"
)
const debugVerifyDetachOnGC = false
type DrawPaintOuter interface {
outer.Attachable
outer.Painter
outer.Parenter
outer.Sized
}
type DrawPaint struct {
outer DrawPaintOuter
driver gxui.Driver
canvas gxui.Canvas
dirty bool
redrawRequested bool
}
func verifyDetach(o DrawPaintOuter) {
if o.Attached() {
panic(fmt.Errorf("%T garbage collected while still attached", o))
}
}
func (d *DrawPaint) Init(outer DrawPaintOuter, theme gxui.Theme) {
d.outer = outer
d.driver = theme.Driver()
if debugVerifyDetachOnGC {
runtime.SetFinalizer(d.outer, verifyDetach)
}
}
func (d *DrawPaint) Redraw() {
d.driver.AssertUIGoroutine()
if !d.redrawRequested {
if p := d.outer.Parent(); p != nil {
d.redrawRequested = true
p.Redraw()
}
}
}
func (d *DrawPaint) Draw() gxui.Canvas {
if !d.outer.Attached() {
panic(fmt.Errorf("Attempting to draw a non-attached control %T", d.outer))
}
s := d.outer.Size()
if s.Area() == 0 {
return nil // No area to draw in
}
if d.canvas == nil || d.canvas.Size() != s || d.redrawRequested {
d.canvas = d.driver.CreateCanvas(s)
d.redrawRequested = false
d.outer.Paint(d.canvas)
d.canvas.Complete()
}
return d.canvas
}