-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcanvasView.go
114 lines (97 loc) · 2.81 KB
/
canvasView.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package rui
// DrawFunction is the constant for "draw-function" property tag.
//
// Used by `CanvasView`.
// Property sets the draw function of `CanvasView`.
//
// Supported types: `func(Canvas)`.
const DrawFunction PropertyName = "draw-function"
// CanvasView interface of a custom draw view
type CanvasView interface {
View
// Redraw forces CanvasView to redraw its content
Redraw()
}
type canvasViewData struct {
viewData
}
// NewCanvasView creates the new custom draw view
func NewCanvasView(session Session, params Params) CanvasView {
view := new(canvasViewData)
view.init(session)
setInitParams(view, params)
return view
}
func newCanvasView(session Session) View {
return new(canvasViewData)
}
// Init initialize fields of ViewsContainer by default values
func (canvasView *canvasViewData) init(session Session) {
canvasView.viewData.init(session)
canvasView.tag = "CanvasView"
canvasView.normalize = normalizeCanvasViewTag
canvasView.set = canvasView.setFunc
canvasView.remove = canvasView.removeFunc
canvasView.changed = canvasView.propertyChanged
}
func normalizeCanvasViewTag(tag PropertyName) PropertyName {
tag = defaultNormalize(tag)
switch tag {
case "draw-func":
tag = DrawFunction
}
return tag
}
func (canvasView *canvasViewData) removeFunc(tag PropertyName) []PropertyName {
if tag == DrawFunction {
if canvasView.getRaw(DrawFunction) != nil {
canvasView.setRaw(DrawFunction, nil)
canvasView.Redraw()
return []PropertyName{DrawFunction}
}
return []PropertyName{}
}
return canvasView.viewData.removeFunc(tag)
}
func (canvasView *canvasViewData) setFunc(tag PropertyName, value any) []PropertyName {
if tag == DrawFunction {
if fn, ok := value.(func(Canvas)); ok {
canvasView.setRaw(DrawFunction, fn)
} else {
notCompatibleType(tag, value)
return nil
}
return []PropertyName{DrawFunction}
}
return canvasView.viewData.setFunc(tag, value)
}
func (canvasView *canvasViewData) propertyChanged(tag PropertyName) {
if tag == DrawFunction {
canvasView.Redraw()
} else {
canvasView.viewData.propertyChanged(tag)
}
}
func (canvasView *canvasViewData) htmlTag() string {
return "canvas"
}
func (canvasView *canvasViewData) Redraw() {
canvas := newCanvas(canvasView)
canvas.ClearRect(0, 0, canvasView.frame.Width, canvasView.frame.Height)
if value := canvasView.getRaw(DrawFunction); value != nil {
if drawer, ok := value.(func(Canvas)); ok {
drawer(canvas)
}
}
canvas.finishDraw()
}
func (canvasView *canvasViewData) onResize(self View, x, y, width, height float64) {
canvasView.viewData.onResize(self, x, y, width, height)
canvasView.Redraw()
}
// RedrawCanvasView finds CanvasView with canvasViewID and redraws it
func RedrawCanvasView(rootView View, canvasViewID string) {
if canvas := CanvasViewByID(rootView, canvasViewID); canvas != nil {
canvas.Redraw()
}
}