-
Notifications
You must be signed in to change notification settings - Fork 885
/
graphicseffects.go
292 lines (224 loc) · 7.6 KB
/
graphicseffects.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// Copyright 2017 The Walk Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build windows
package walk
import "math"
var (
borderGlowAlpha = []float64{0.2, 0.1, 0.075, 0.05, 0.075}
InteractionEffect WidgetGraphicsEffect
FocusEffect WidgetGraphicsEffect
)
type WidgetGraphicsEffect interface {
Draw(widget Widget, canvas *Canvas) error
}
type widgetGraphicsEffectBase struct {
color Color
dpi2Bitmap map[int]*Bitmap
}
func (wgeb *widgetGraphicsEffectBase) create(color Color) error {
wgeb.color = color
return nil
}
func (wgeb *widgetGraphicsEffectBase) Dispose() {
if len(wgeb.dpi2Bitmap) == 0 {
return
}
for dpi, bitmap := range wgeb.dpi2Bitmap {
bitmap.Dispose()
delete(wgeb.dpi2Bitmap, dpi)
}
}
func (wgeb *widgetGraphicsEffectBase) bitmapForDPI(dpi int) (*Bitmap, error) {
if wgeb.dpi2Bitmap == nil {
wgeb.dpi2Bitmap = make(map[int]*Bitmap)
} else if bitmap, ok := wgeb.dpi2Bitmap[dpi]; ok {
return bitmap, nil
}
var disposables Disposables
defer disposables.Treat()
bitmap, err := NewBitmapWithTransparentPixelsForDPI(SizeFrom96DPI(Size{12, 12}, dpi), dpi)
if err != nil {
return nil, err
}
disposables.Add(bitmap)
canvas, err := NewCanvasFromImage(bitmap)
if err != nil {
return nil, err
}
defer canvas.Dispose()
for i := 1; i <= 5; i++ {
size := SizeFrom96DPI(Size{i*2 + 2, i*2 + 2}, dpi)
bmp, err := NewBitmapWithTransparentPixelsForDPI(size, dpi)
if err != nil {
return nil, err
}
defer bmp.Dispose()
bmpCanvas, err := NewCanvasFromImage(bmp)
if err != nil {
return nil, err
}
defer bmpCanvas.Dispose()
color := RGB(
byte(math.Min(1.0, float64(wgeb.color.R())/255.0-0.1+0.1*float64(i))*255.0),
byte(math.Min(1.0, float64(wgeb.color.G())/255.0-0.1+0.1*float64(i))*255.0),
byte(math.Min(1.0, float64(wgeb.color.B())/255.0-0.1+0.1*float64(i))*255.0),
)
brush, err := NewSolidColorBrush(color)
if err != nil {
return nil, err
}
defer brush.Dispose()
ellipseSize := SizeFrom96DPI(Size{i * 2, i * 2}, dpi)
if err := bmpCanvas.FillRoundedRectanglePixels(brush, Rectangle{0, 0, size.Width, size.Height}, ellipseSize); err != nil {
return nil, err
}
bmpCanvas.Dispose()
opacity := byte(borderGlowAlpha[i-1] * 255.0)
offset := PointFrom96DPI(Point{5 - i, 5 - i}, dpi)
canvas.DrawBitmapWithOpacityPixels(bmp, Rectangle{offset.X, offset.Y, size.Width, size.Height}, opacity)
}
disposables.Spare()
wgeb.dpi2Bitmap[dpi] = bitmap
return bitmap, nil
}
type BorderGlowEffect struct {
widgetGraphicsEffectBase
}
func NewBorderGlowEffect(color Color) (*BorderGlowEffect, error) {
bge := new(BorderGlowEffect)
if err := bge.create(color); err != nil {
return nil, err
}
return bge, nil
}
func (bge *BorderGlowEffect) Draw(widget Widget, canvas *Canvas) error {
b := widget.BoundsPixels()
dpi := canvas.DPI()
bitmap, err := bge.bitmapForDPI(dpi)
if err != nil {
return err
}
off1 := IntFrom96DPI(1, dpi)
off2 := IntFrom96DPI(2, dpi)
off5 := IntFrom96DPI(5, dpi)
canvas.DrawBitmapPart(bitmap, Rectangle{b.X - off5, b.Y - off5, off5, off5}, Rectangle{0, 0, off5, off5})
canvas.DrawBitmapPart(bitmap, Rectangle{b.X, b.Y - off5, b.Width, off5}, Rectangle{off5 + off1, 0, off1, off5})
canvas.DrawBitmapPart(bitmap, Rectangle{b.X + b.Width, b.Y - off5, off5, off5}, Rectangle{off5 + off2, 0, off5, off5})
canvas.DrawBitmapPart(bitmap, Rectangle{b.X + b.Width, b.Y, off5, b.Height}, Rectangle{off5 + off2, off5 + off1, off5, off1})
canvas.DrawBitmapPart(bitmap, Rectangle{b.X + b.Width, b.Y + b.Height, off5, off5}, Rectangle{off5 + off2, off5 + off2, off5, off5})
canvas.DrawBitmapPart(bitmap, Rectangle{b.X, b.Y + b.Height, b.Width, off5}, Rectangle{off5 + off1, off5 + off2, off1, off5})
canvas.DrawBitmapPart(bitmap, Rectangle{b.X - off5, b.Y + b.Height, off5, off5}, Rectangle{0, off5 + off2, off5, off5})
canvas.DrawBitmapPart(bitmap, Rectangle{b.X - off5, b.Y, off5, b.Height}, Rectangle{0, off5 + off1, off5, off1})
return nil
}
type DropShadowEffect struct {
widgetGraphicsEffectBase
}
func NewDropShadowEffect(color Color) (*DropShadowEffect, error) {
dse := new(DropShadowEffect)
if err := dse.create(color); err != nil {
return nil, err
}
return dse, nil
}
func (dse *DropShadowEffect) Draw(widget Widget, canvas *Canvas) error {
b := widget.BoundsPixels()
dpi := canvas.DPI()
bitmap, err := dse.bitmapForDPI(dpi)
if err != nil {
return err
}
off1 := IntFrom96DPI(1, dpi)
off2 := IntFrom96DPI(2, dpi)
off5 := IntFrom96DPI(5, dpi)
off10 := IntFrom96DPI(10, dpi)
canvas.DrawBitmapPart(bitmap, Rectangle{b.X + b.Width, b.Y + off10 - off5, off5, off5}, Rectangle{off5 + off2, 0, off5, off5})
canvas.DrawBitmapPart(bitmap, Rectangle{b.X + b.Width, b.Y + off10, off5, b.Height - off10}, Rectangle{off5 + off2, off5 + off1, off5, off1})
canvas.DrawBitmapPart(bitmap, Rectangle{b.X + b.Width, b.Y + b.Height, off5, off5}, Rectangle{off5 + off2, off5 + off2, off5, off5})
canvas.DrawBitmapPart(bitmap, Rectangle{b.X + off10, b.Y + b.Height, b.Width - off10, off5}, Rectangle{off5 + off1, off5 + off2, off1, off5})
canvas.DrawBitmapPart(bitmap, Rectangle{b.X + off10 - off5, b.Y + b.Height, off5, off5}, Rectangle{0, off5 + off2, off5, off5})
return nil
}
type widgetGraphicsEffectListObserver interface {
onInsertedGraphicsEffect(index int, effect WidgetGraphicsEffect) error
onRemovedGraphicsEffect(index int, effect WidgetGraphicsEffect) error
onClearedGraphicsEffects() error
}
type WidgetGraphicsEffectList struct {
items []WidgetGraphicsEffect
observer widgetGraphicsEffectListObserver
}
func newWidgetGraphicsEffectList(observer widgetGraphicsEffectListObserver) *WidgetGraphicsEffectList {
return &WidgetGraphicsEffectList{observer: observer}
}
func (l *WidgetGraphicsEffectList) Add(effect WidgetGraphicsEffect) error {
if effect == nil {
return newError("effect == nil")
}
return l.Insert(len(l.items), effect)
}
func (l *WidgetGraphicsEffectList) At(index int) WidgetGraphicsEffect {
return l.items[index]
}
func (l *WidgetGraphicsEffectList) Clear() error {
observer := l.observer
oldItems := l.items
l.items = l.items[:0]
if observer != nil {
if err := observer.onClearedGraphicsEffects(); err != nil {
l.items = oldItems
return err
}
}
return nil
}
func (l *WidgetGraphicsEffectList) Index(effect WidgetGraphicsEffect) int {
for i, item := range l.items {
if item == effect {
return i
}
}
return -1
}
func (l *WidgetGraphicsEffectList) Contains(effect WidgetGraphicsEffect) bool {
return l.Index(effect) > -1
}
func (l *WidgetGraphicsEffectList) insertIntoSlice(index int, effect WidgetGraphicsEffect) {
l.items = append(l.items, nil)
copy(l.items[index+1:], l.items[index:])
l.items[index] = effect
}
func (l *WidgetGraphicsEffectList) Insert(index int, effect WidgetGraphicsEffect) error {
observer := l.observer
l.insertIntoSlice(index, effect)
if observer != nil {
if err := observer.onInsertedGraphicsEffect(index, effect); err != nil {
l.items = append(l.items[:index], l.items[index+1:]...)
return err
}
}
return nil
}
func (l *WidgetGraphicsEffectList) Len() int {
return len(l.items)
}
func (l *WidgetGraphicsEffectList) Remove(effect WidgetGraphicsEffect) error {
index := l.Index(effect)
if index == -1 {
return nil
}
return l.RemoveAt(index)
}
func (l *WidgetGraphicsEffectList) RemoveAt(index int) error {
observer := l.observer
item := l.items[index]
l.items = append(l.items[:index], l.items[index+1:]...)
if observer != nil {
if err := observer.onRemovedGraphicsEffect(index, item); err != nil {
l.insertIntoSlice(index, item)
return err
}
}
return nil
}