-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathblock.go
154 lines (121 loc) · 3.02 KB
/
block.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
Copyright 2012 the go.uik authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package uik
import (
"code.google.com/p/draw2d/draw2d"
"github.com/skelterjohn/geom"
"image/draw"
)
type BlockID int
var blockIDs = make(chan BlockID)
func init() {
go func() {
var counter BlockID
for {
counter++
blockIDs <- counter
}
}()
}
type Drawer interface {
Draw(buffer draw.Image, invalidRects RectSet)
}
// The Block type is a basic unit that can receive events and draw itself.
//
// This struct essentially defines an interface, except a synchronous interface
// based on channels rather than an asynchronous interface based on method
// calls.
type Block struct {
ID BlockID
Parent *Foundation
UserEventsIn DropChan
UserEvents <-chan interface{}
ResizeEvents ResizeChan
Subscribe chan<- Subscription
Drawer
buffer draw.Image
Paint func(gc draw2d.GraphicContext)
Invalidations InvalidationChan
SizeHints SizeHintChan
setSizeHint SizeHintChan
placementNotifications placementNotificationChan
HasKeyFocus bool
// size of block
Size geom.Coord
}
func (b *Block) Initialize() {
b.ID = <-blockIDs
b.Drawer = b
b.UserEventsIn, b.UserEvents, b.Subscribe = SubscriptionQueue(20)
b.ResizeEvents = make(ResizeChan, 1)
b.placementNotifications = make(placementNotificationChan, 1)
b.setSizeHint = make(SizeHintChan, 1)
go b.handleSizeHints()
}
func (b *Block) Draw(buffer draw.Image, invalidRects RectSet) {
// Report(b.ID, "Block.Draw()", buffer.Bounds())
gc := draw2d.NewGraphicContext(buffer)
b.DoPaint(gc)
}
func (b *Block) Invalidate(areas ...geom.Rect) {
// Report(b.ID, "invalidation")
if len(areas) == 0 {
b.Invalidations.Stack(Invalidation{
Bounds: []geom.Rect{b.Bounds()},
})
} else {
b.Invalidations.Stack(Invalidation{
Bounds: areas,
})
}
}
func (b *Block) HandleEvent(e interface{}) {
switch e := e.(type) {
case KeyFocusEvent:
b.HasKeyFocus = e.Focus
}
}
func (b *Block) SetSizeHint(sh SizeHint) {
b.setSizeHint <- sh
}
func (b *Block) handleSizeHints() {
sh := <-b.setSizeHint
b.SizeHints.Stack(sh)
for {
select {
case sh = <-b.setSizeHint:
case pn := <-b.placementNotifications:
b.Parent = pn.Foundation
b.SizeHints = pn.SizeHints
}
b.SizeHints.Stack(sh)
}
}
func (b *Block) Bounds() geom.Rect {
return geom.Rect{
geom.Coord{0, 0},
b.Size,
}
}
func (b *Block) DoPaint(gc draw2d.GraphicContext) {
if b.Paint != nil {
b.Paint(gc)
}
}
func (b *Block) DoResizeEvent(e ResizeEvent) {
if e.Size == b.Size {
return
}
b.Size = e.Size
b.Invalidate()
}