~gioverse/chat

ref: ff42a2f8b59287707042461aa1ca1347fb8250eb chat/widget/image.go -rw-r--r-- 978 bytes
ff42a2f8Chris Waldon list: update Loader to return if more elements 10 months ago
                                                                                
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
package widget

import (
	"image"

	"gioui.org/op/paint"
)

// CachedImage is a cacheable image operation.
type CachedImage struct {
	op paint.ImageOp
	ch bool
}

// Reload tells the CachedImage to repopulate the cache.
func (img *CachedImage) Reload() {
	img.ch = true
}

// Cache the image if it is not already.
// First call will compute the image operation, subsequent calls will noop.
// When reloaded, cache will re-populated on next invocation.
func (img *CachedImage) Cache(src image.Image) *CachedImage {
	if img == nil || src == nil {
		return img
	}
	if img.op == (paint.ImageOp{}) || img.changed() {
		img.op = paint.NewImageOp(src)
	}
	return img
}

// Op returns the concrete image operation.
func (img CachedImage) Op() paint.ImageOp {
	return paint.ImageOp(img.op)
}

// changed reports whether the underlying image has changed and therefore
// should be cached again.
func (img *CachedImage) changed() bool {
	defer func() { img.ch = false }()
	return img.ch
}