forked from fyne-io/fyne
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcanvas.go
560 lines (489 loc) · 13.7 KB
/
canvas.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
package common
import (
"image/color"
"reflect"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/internal"
"fyne.io/fyne/v2/internal/app"
"fyne.io/fyne/v2/internal/async"
"fyne.io/fyne/v2/internal/cache"
"fyne.io/fyne/v2/internal/driver"
"fyne.io/fyne/v2/internal/painter/gl"
)
// SizeableCanvas defines a canvas with size related functions.
type SizeableCanvas interface {
fyne.Canvas
Resize(fyne.Size)
MinSize() fyne.Size
}
// Canvas defines common canvas implementation.
type Canvas struct {
OnFocus func(obj fyne.Focusable)
OnUnfocus func()
impl SizeableCanvas
contentFocusMgr *app.FocusManager
menuFocusMgr *app.FocusManager
overlays *overlayStack
shortcut fyne.ShortcutHandler
painter gl.Painter
// Any object that requests to enter to the refresh queue should
// not be omitted as it is always a rendering task's decision
// for skipping frames or drawing calls.
//
// If an object failed to ender the refresh queue, the object may
// disappear or blink from the view at any frames. As of this reason,
// the refreshQueue is an unbounded queue which is able to cache
// arbitrary number of fyne.CanvasObject for the rendering.
refreshQueue *async.CanvasObjectQueue
dirty bool
mWindowHeadTree, contentTree, menuTree *renderCacheTree
}
// AddShortcut adds a shortcut to the canvas.
func (c *Canvas) AddShortcut(shortcut fyne.Shortcut, handler func(shortcut fyne.Shortcut)) {
c.shortcut.AddShortcut(shortcut, handler)
}
func (c *Canvas) DrawDebugOverlay(obj fyne.CanvasObject, pos fyne.Position, size fyne.Size) {
switch obj.(type) {
case fyne.Widget:
r := canvas.NewRectangle(color.Transparent)
r.StrokeColor = color.NRGBA{R: 0xcc, G: 0x33, B: 0x33, A: 0xff}
r.StrokeWidth = 1
r.Resize(obj.Size())
c.Painter().Paint(r, pos, size)
t := canvas.NewText(reflect.ValueOf(obj).Elem().Type().Name(), r.StrokeColor)
t.TextSize = 10
c.Painter().Paint(t, pos.AddXY(2, 2), size)
case *fyne.Container:
r := canvas.NewRectangle(color.Transparent)
r.StrokeColor = color.NRGBA{R: 0x33, G: 0x33, B: 0xcc, A: 0xff}
r.StrokeWidth = 1
r.Resize(obj.Size())
c.Painter().Paint(r, pos, size)
}
}
// EnsureMinSize ensure canvas min size.
//
// This function uses lock.
func (c *Canvas) EnsureMinSize() bool {
if c.impl.Content() == nil {
return false
}
windowNeedsMinSizeUpdate := false
csize := c.impl.Size()
min := c.impl.MinSize()
var parentNeedingUpdate *RenderCacheNode
ensureMinSize := func(node *RenderCacheNode, pos fyne.Position) {
obj := node.obj
cache.SetCanvasForObject(obj, c.impl, func() {
if img, ok := obj.(*canvas.Image); ok {
img.Refresh() // this may now have a different texScale
}
})
if parentNeedingUpdate == node {
c.updateLayout(obj)
parentNeedingUpdate = nil
}
if !obj.Visible() {
return
}
minSize := obj.MinSize()
minSizeChanged := node.minSize != minSize
if minSizeChanged {
node.minSize = minSize
if node.parent != nil {
parentNeedingUpdate = node.parent
} else {
windowNeedsMinSizeUpdate = true
size := obj.Size()
expectedSize := minSize.Max(size)
if expectedSize != size && size != csize {
obj.Resize(expectedSize)
} else {
c.updateLayout(obj)
}
}
}
}
c.WalkTrees(nil, ensureMinSize)
shouldResize := windowNeedsMinSizeUpdate && (csize.Width < min.Width || csize.Height < min.Height)
if shouldResize {
c.impl.Resize(csize.Max(min))
}
return windowNeedsMinSizeUpdate
}
// Focus makes the provided item focused.
func (c *Canvas) Focus(obj fyne.Focusable) {
focusMgr := c.focusManager()
if focusMgr != nil && focusMgr.Focus(obj) { // fast path – probably >99.9% of all cases
if c.OnFocus != nil {
c.OnFocus(obj)
}
return
}
focusMgrs := append([]*app.FocusManager{c.contentFocusMgr, c.menuFocusMgr}, c.overlays.ListFocusManagers()...)
for _, mgr := range focusMgrs {
if mgr == nil {
continue
}
if focusMgr != mgr {
if mgr.Focus(obj) {
if c.OnFocus != nil {
c.OnFocus(obj)
}
return
}
}
}
fyne.LogError("Failed to focus object which is not part of the canvas’ content, menu or overlays.", nil)
}
// Focused returns the current focused object.
func (c *Canvas) Focused() fyne.Focusable {
mgr := c.focusManager()
if mgr == nil {
return nil
}
return mgr.Focused()
}
// FocusGained signals to the manager that its content got focus.
// Valid only on Desktop.
func (c *Canvas) FocusGained() {
mgr := c.focusManager()
if mgr == nil {
return
}
mgr.FocusGained()
}
// FocusLost signals to the manager that its content lost focus.
// Valid only on Desktop.
func (c *Canvas) FocusLost() {
mgr := c.focusManager()
if mgr == nil {
return
}
mgr.FocusLost()
}
// FocusNext focuses the next focusable item.
func (c *Canvas) FocusNext() {
mgr := c.focusManager()
if mgr == nil {
return
}
mgr.FocusNext()
}
// FocusPrevious focuses the previous focusable item.
func (c *Canvas) FocusPrevious() {
mgr := c.focusManager()
if mgr == nil {
return
}
mgr.FocusPrevious()
}
// FreeDirtyTextures frees dirty textures and returns the number of freed textures.
func (c *Canvas) FreeDirtyTextures() (freed uint64) {
freeObject := func(object fyne.CanvasObject) {
freeWalked := func(obj fyne.CanvasObject, _ fyne.Position, _ fyne.Position, _ fyne.Size) bool {
// No image refresh while recursing to avoid double texture upload.
if _, ok := obj.(*canvas.Image); ok {
return false
}
if c.painter != nil {
c.painter.Free(obj)
}
return false
}
// Image.Refresh will trigger a refresh specific to the object, while recursing on parent widget would just lead to
// a double texture upload.
if img, ok := object.(*canvas.Image); ok {
if c.painter != nil {
c.painter.Free(img)
}
} else {
driver.WalkCompleteObjectTree(object, freeWalked, nil)
}
}
// Within a frame, refresh tasks are requested from the Refresh method,
// and we desire to clear out all requested operations within a frame.
// See https://github.com/fyne-io/fyne/issues/2548.
tasksToDo := c.refreshQueue.Len()
shouldFilterDuplicates := (tasksToDo > 200) // filtering has overhead, not worth enabling for few tasks
var refreshSet map[fyne.CanvasObject]struct{}
if shouldFilterDuplicates {
refreshSet = make(map[fyne.CanvasObject]struct{})
}
for c.refreshQueue.Len() > 0 {
object := c.refreshQueue.Out()
if !shouldFilterDuplicates {
freed++
freeObject(object)
} else {
refreshSet[object] = struct{}{}
tasksToDo--
if tasksToDo == 0 {
shouldFilterDuplicates = false // stop collecting messages to avoid starvation
for object := range refreshSet {
freed++
freeObject(object)
}
}
}
}
if c.painter != nil {
cache.RangeExpiredTexturesFor(c.impl, c.painter.Free)
}
return
}
// Initialize initializes the canvas.
func (c *Canvas) Initialize(impl SizeableCanvas, onOverlayChanged func()) {
c.impl = impl
c.refreshQueue = async.NewCanvasObjectQueue()
c.overlays = &overlayStack{
OverlayStack: internal.OverlayStack{
OnChange: onOverlayChanged,
Canvas: impl,
},
}
}
// ObjectTrees return canvas object trees.
//
// This function uses lock.
func (c *Canvas) ObjectTrees() []fyne.CanvasObject {
var content, menu fyne.CanvasObject
if c.contentTree != nil && c.contentTree.root != nil {
content = c.contentTree.root.obj
}
if c.menuTree != nil && c.menuTree.root != nil {
menu = c.menuTree.root.obj
}
trees := make([]fyne.CanvasObject, 0, len(c.Overlays().List())+2)
trees = append(trees, content)
if menu != nil {
trees = append(trees, menu)
}
trees = append(trees, c.Overlays().List()...)
return trees
}
// Overlays returns the overlay stack.
func (c *Canvas) Overlays() fyne.OverlayStack {
// we don't need to lock here, because overlays never changes
return c.overlays
}
// Painter returns the canvas painter.
func (c *Canvas) Painter() gl.Painter {
return c.painter
}
// Refresh refreshes a canvas object.
func (c *Canvas) Refresh(obj fyne.CanvasObject) {
c.refreshQueue.In(obj)
async.EnsureMain(c.SetDirty)
}
// RemoveShortcut removes a shortcut from the canvas.
func (c *Canvas) RemoveShortcut(shortcut fyne.Shortcut) {
c.shortcut.RemoveShortcut(shortcut)
}
// SetContentTreeAndFocusMgr sets content tree and focus manager.
//
// This function does not use the canvas lock.
func (c *Canvas) SetContentTreeAndFocusMgr(content fyne.CanvasObject) {
c.contentTree = &renderCacheTree{root: &RenderCacheNode{obj: content}}
var focused fyne.Focusable
if c.contentFocusMgr != nil {
focused = c.contentFocusMgr.Focused() // keep old focus if possible
}
c.contentFocusMgr = app.NewFocusManager(content)
if focused != nil {
c.contentFocusMgr.Focus(focused)
}
}
// CheckDirtyAndClear returns true if the canvas is dirty and
// clears the dirty state atomically.
func (c *Canvas) CheckDirtyAndClear() bool {
wasDirty := c.dirty
c.dirty = false
return wasDirty
}
// SetDirty sets canvas dirty flag atomically.
func (c *Canvas) SetDirty() {
c.dirty = true
}
// SetMenuTreeAndFocusMgr sets menu tree and focus manager.
//
// This function does not use the canvas lock.
func (c *Canvas) SetMenuTreeAndFocusMgr(menu fyne.CanvasObject) {
c.menuTree = &renderCacheTree{root: &RenderCacheNode{obj: menu}}
if menu != nil {
c.menuFocusMgr = app.NewFocusManager(menu)
} else {
c.menuFocusMgr = nil
}
}
// SetMobileWindowHeadTree sets window head tree.
//
// This function does not use the canvas lock.
func (c *Canvas) SetMobileWindowHeadTree(head fyne.CanvasObject) {
c.mWindowHeadTree = &renderCacheTree{root: &RenderCacheNode{obj: head}}
}
// SetPainter sets the canvas painter.
func (c *Canvas) SetPainter(p gl.Painter) {
c.painter = p
}
// TypedShortcut handle the registered shortcut.
func (c *Canvas) TypedShortcut(shortcut fyne.Shortcut) {
c.shortcut.TypedShortcut(shortcut)
}
// Unfocus unfocuses all the objects in the canvas.
func (c *Canvas) Unfocus() {
mgr := c.focusManager()
if mgr == nil {
return
}
if mgr.Focus(nil) && c.OnUnfocus != nil {
c.OnUnfocus()
}
}
// WalkTrees walks over the trees.
func (c *Canvas) WalkTrees(
beforeChildren func(*RenderCacheNode, fyne.Position),
afterChildren func(*RenderCacheNode, fyne.Position),
) {
c.walkTree(c.contentTree, beforeChildren, afterChildren)
if c.mWindowHeadTree != nil && c.mWindowHeadTree.root.obj != nil {
c.walkTree(c.mWindowHeadTree, beforeChildren, afterChildren)
}
if c.menuTree != nil && c.menuTree.root.obj != nil {
c.walkTree(c.menuTree, beforeChildren, afterChildren)
}
for _, tree := range c.overlays.renderCaches {
if tree != nil {
c.walkTree(tree, beforeChildren, afterChildren)
}
}
}
func (c *Canvas) focusManager() *app.FocusManager {
if focusMgr := c.overlays.TopFocusManager(); focusMgr != nil {
return focusMgr
}
if c.isMenuActive() {
return c.menuFocusMgr
}
return c.contentFocusMgr
}
func (c *Canvas) isMenuActive() bool {
if c.menuTree == nil || c.menuTree.root == nil || c.menuTree.root.obj == nil {
return false
}
menu := c.menuTree.root.obj
if am, ok := menu.(activatableMenu); ok {
return am.IsActive()
}
return true
}
func (c *Canvas) walkTree(
tree *renderCacheTree,
beforeChildren func(*RenderCacheNode, fyne.Position),
afterChildren func(*RenderCacheNode, fyne.Position),
) {
var node, parent, prev *RenderCacheNode
node = tree.root
bc := func(obj fyne.CanvasObject, pos fyne.Position, _ fyne.Position, _ fyne.Size) bool {
if node != nil && node.obj != obj {
if parent.firstChild == node {
parent.firstChild = nil
}
node = nil
}
if node == nil {
node = &RenderCacheNode{parent: parent, obj: obj}
if parent.firstChild == nil {
parent.firstChild = node
} else {
prev.nextSibling = node
}
}
if prev != nil && prev.parent != parent {
prev = nil
}
if beforeChildren != nil {
beforeChildren(node, pos)
}
parent = node
node = parent.firstChild
return false
}
ac := func(obj fyne.CanvasObject, pos fyne.Position, _ fyne.CanvasObject) {
node = parent
parent = node.parent
if prev != nil && prev.parent != parent {
prev.nextSibling = nil
}
if afterChildren != nil {
afterChildren(node, pos)
}
prev = node
node = node.nextSibling
}
driver.WalkVisibleObjectTree(tree.root.obj, bc, ac)
}
// RenderCacheNode represents a node in a render cache tree.
type RenderCacheNode struct {
// structural data
firstChild *RenderCacheNode
nextSibling *RenderCacheNode
obj fyne.CanvasObject
parent *RenderCacheNode
// cache data
minSize fyne.Size
}
// Obj returns the node object.
func (r *RenderCacheNode) Obj() fyne.CanvasObject {
return r.obj
}
type activatableMenu interface {
IsActive() bool
}
type overlayStack struct {
internal.OverlayStack
renderCaches []*renderCacheTree
}
func (o *overlayStack) Add(overlay fyne.CanvasObject) {
if overlay == nil {
return
}
o.add(overlay)
}
func (o *overlayStack) Remove(overlay fyne.CanvasObject) {
if overlay == nil || len(o.List()) == 0 {
return
}
o.remove(overlay)
}
func (o *overlayStack) add(overlay fyne.CanvasObject) {
o.renderCaches = append(o.renderCaches, &renderCacheTree{root: &RenderCacheNode{obj: overlay}})
o.OverlayStack.Add(overlay)
}
func (o *overlayStack) remove(overlay fyne.CanvasObject) {
o.OverlayStack.Remove(overlay)
overlayCount := len(o.List())
// it is possible that overlays are removed implicitly and render caches already cleared out
if overlayCount >= len(o.renderCaches) {
return
}
o.renderCaches[overlayCount] = nil // release memory reference to removed element
o.renderCaches = o.renderCaches[:overlayCount]
}
type renderCacheTree struct {
root *RenderCacheNode
}
func (c *Canvas) updateLayout(objToLayout fyne.CanvasObject) {
switch cont := objToLayout.(type) {
case *fyne.Container:
if cont.Layout != nil {
layout := cont.Layout
objects := cont.Objects
layout.Layout(objects, cont.Size())
}
case fyne.Widget:
renderer := cache.Renderer(cont)
renderer.Layout(cont.Size())
}
}