This repository has been archived by the owner on Apr 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
avatar.go
145 lines (116 loc) · 2.76 KB
/
avatar.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
package roundimage
/*
import (
"context"
"github.com/diamondburned/gotk4-handy"
"github.com/diamondburned/gotk4/pkg/gdk/v3"
"github.com/diamondburned/gotk4/pkg/gtk/v3"
)
// TODO: GIF support
// TextSetter is an interface for setting texts.
type TextSetter interface {
SetText(text string)
}
func TrySetText(imager Imager, text string) {
if setter, ok := imager.(TextSetter); ok {
setter.SetText(text)
}
}
// Avatar is a static HdyAvatar container.
type Avatar struct {
handy.Avatar
pixbuf *gdk.Pixbuf
url string
size int
cancel context.CancelFunc
}
// Make a better API that allows scaling.
var (
_ Imager = (*Avatar)(nil)
_ TextSetter = (*Avatar)(nil)
_ httputil.ImageContainer = (*Avatar)(nil)
)
func NewAvatar(size int) *Avatar {
avatar := Avatar{
Avatar: *handy.AvatarNew(size, "", true),
size: size,
}
// Set the load function. This should hopefully trigger a reload.
avatar.SetImageLoadFunc(avatar.loadFunc)
return &avatar
}
// GetSizeRequest returns the virtual size.
func (a *Avatar) GetSizeRequest() (int, int) {
return a.size, a.size
}
// SetSizeRequest sets the avatar size. The actual size is min(w, h).
func (a *Avatar) SetSizeRequest(w, h int) {
var min = w
if w > h {
min = h
}
a.size = min
a.Avatar.SetSize(min)
a.Avatar.SetSizeRequest(w, h)
}
func (a *Avatar) loadFunc(size int) *gdk.Pixbuf {
if a.url == "" {
return nil
}
if a.pixbuf != nil && a.size == size {
return a.pixbuf
}
a.size = size
a.refetch()
return nil
}
func (a *Avatar) refetch() {
a.cancelCtx()
ctx, cancel := context.WithCancel(context.Background())
a.cancel = cancel
httputil.AsyncImage(ctx, a, a.url)
}
func (a *Avatar) cancelCtx() {
if a.cancel != nil {
a.cancel()
a.cancel = nil
}
}
// SetRadius is a no-op.
func (a *Avatar) SetRadius(float64) {}
// SetImageURL sets the avatar's source URL and reloads it asynchronously.
func (a *Avatar) SetImageURL(url string) {
a.url = url
a.refetch()
}
// SetFromPixbuf sets the pixbuf.
func (a *Avatar) SetFromPixbuf(pb *gdk.Pixbuf) {
a.cancelCtx()
a.pixbuf = pb
// a.Avatar.SetImageLoadFunc(a.loadFunc)
a.Avatar.QueueDraw()
}
// SetFromAnimation sets the first frame of the animation.
func (a *Avatar) SetFromAnimation(pa *gdk.PixbufAnimation) {
a.cancelCtx()
a.pixbuf = pa.GetStaticImage()
// a.Avatar.SetImageLoadFunc(a.loadFunc)
a.Avatar.QueueDraw()
}
// GetPixbuf returns the underlying pixbuf.
func (a *Avatar) GetPixbuf() *gdk.Pixbuf {
return a.pixbuf
}
// GetAnimation returns nil.
func (a *Avatar) GetAnimation() *gdk.PixbufAnimation {
return nil
}
// GetImage returns nil.
func (a *Avatar) GetImage() *gtk.Image {
return nil
}
// GetStorageType always returns IMAGE_PIXBUF.
func (a *Avatar) GetStorageType() gtk.ImageType {
return gtk.IMAGE_PIXBUF
}
*/