-
Notifications
You must be signed in to change notification settings - Fork 887
/
gradientcomposite.go
282 lines (216 loc) · 4.69 KB
/
gradientcomposite.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
// 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 (
"unsafe"
"github.com/lxn/win"
)
type GradientComposite struct {
*Composite
vertical bool
color1 Color
color2 Color
verticalChangedPublisher EventPublisher
color1ChangedPublisher EventPublisher
color2ChangedPublisher EventPublisher
brush *BitmapBrush
}
func NewGradientComposite(parent Container) (*GradientComposite, error) {
return NewGradientCompositeWithStyle(parent, 0)
}
func NewGradientCompositeWithStyle(parent Container, style uint32) (*GradientComposite, error) {
composite, err := NewCompositeWithStyle(parent, style)
if err != nil {
return nil, err
}
gc := &GradientComposite{Composite: composite}
succeeded := false
defer func() {
if !succeeded {
gc.Dispose()
}
}()
if err := InitWrapperWindow(gc); err != nil {
return nil, err
}
gc.MustRegisterProperty("Vertical", NewBoolProperty(
func() bool {
return gc.Vertical()
},
func(b bool) error {
gc.SetVertical(b)
return nil
},
gc.verticalChangedPublisher.Event()))
gc.MustRegisterProperty("Color1", NewProperty(
func() interface{} {
return float64(uint32(gc.Color1()))
},
func(v interface{}) error {
var c Color
switch v := v.(type) {
case Color:
c = v
case uint32:
c = Color(v)
case float64:
c = Color(uint32(v))
default:
return ErrInvalidType
}
return gc.SetColor1(c)
},
gc.color1ChangedPublisher.Event()))
gc.MustRegisterProperty("Color2", NewProperty(
func() interface{} {
return float64(uint32(gc.Color2()))
},
func(v interface{}) error {
var c Color
switch v := v.(type) {
case Color:
c = v
case uint32:
c = Color(v)
case float64:
c = Color(uint32(v))
default:
return ErrInvalidType
}
return gc.SetColor2(c)
},
gc.color2ChangedPublisher.Event()))
succeeded = true
return gc, nil
}
func (gc *GradientComposite) Vertical() bool {
return gc.vertical
}
func (gc *GradientComposite) SetVertical(vertical bool) (err error) {
if vertical == gc.vertical {
return nil
}
old := gc.vertical
defer func() {
if err != nil {
gc.vertical = old
}
}()
gc.vertical = vertical
if err = gc.updateBackground(); err != nil {
return
}
gc.verticalChangedPublisher.Publish()
return
}
func (gc *GradientComposite) Color1() Color {
return gc.color1
}
func (gc *GradientComposite) SetColor1(c Color) (err error) {
if c == gc.color1 {
return nil
}
old := gc.color1
defer func() {
if err != nil {
gc.color1 = old
}
}()
gc.color1 = c
if err = gc.updateBackground(); err != nil {
return
}
gc.color1ChangedPublisher.Publish()
return
}
func (gc *GradientComposite) Color2() Color {
return gc.color2
}
func (gc *GradientComposite) SetColor2(c Color) (err error) {
if c == gc.color2 {
return nil
}
old := gc.color2
defer func() {
if err != nil {
gc.color2 = old
}
}()
gc.color2 = c
if err = gc.updateBackground(); err != nil {
return
}
gc.color2ChangedPublisher.Publish()
return
}
func (gc *GradientComposite) updateBackground() error {
bounds := gc.ClientBoundsPixels()
if bounds.Width < 1 || bounds.Height < 1 {
return nil
}
if gc.brush != nil {
gc.brush.Dispose()
gc.brush.Bitmap().Dispose()
gc.brush = nil
}
if gc.vertical {
bounds.Width = 1
} else {
bounds.Height = 1
}
bmp, err := NewBitmapForDPI(bounds.Size(), gc.DPI())
if err != nil {
return err
}
defer func() {
if gc.brush == nil {
bmp.Dispose()
}
}()
canvas, err := NewCanvasFromImage(bmp)
if err != nil {
return err
}
defer canvas.Dispose()
var orientation Orientation
if gc.vertical {
orientation = Vertical
} else {
orientation = Horizontal
}
if err := canvas.GradientFillRectanglePixels(gc.color1, gc.color2, orientation, bounds); err != nil {
return err
}
gc.brush, err = NewBitmapBrush(bmp)
if err != nil {
return err
}
gc.SetBackground(gc.brush)
return nil
}
func (gc *GradientComposite) Dispose() {
if gc.brush != nil {
gc.SetBackground(nil)
gc.brush.Dispose()
gc.brush.Bitmap().Dispose()
gc.brush = nil
}
gc.Composite.Dispose()
}
func (gc *GradientComposite) WndProc(hwnd win.HWND, msg uint32, wParam, lParam uintptr) uintptr {
switch msg {
case win.WM_WINDOWPOSCHANGED:
wp := (*win.WINDOWPOS)(unsafe.Pointer(lParam))
if wp.Flags&win.SWP_NOSIZE != 0 {
break
}
size := gc.ClientBoundsPixels().Size()
if gc.brush != nil && gc.brush.bitmap.size == size {
break
}
gc.updateBackground()
}
return gc.Composite.WndProc(hwnd, msg, wParam, lParam)
}