/
icon.go
115 lines (86 loc) · 2.36 KB
/
icon.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
package widget
import (
"image/color"
"fyne.io/fyne"
"fyne.io/fyne/canvas"
"fyne.io/fyne/theme"
)
type iconRenderer struct {
objects []fyne.CanvasObject
image *Icon
}
func (i *iconRenderer) MinSize() fyne.Size {
size := theme.IconInlineSize()
return fyne.NewSize(size, size)
}
func (i *iconRenderer) Layout(size fyne.Size) {
if len(i.objects) == 0 {
return
}
i.objects[0].Resize(size)
}
func (i *iconRenderer) Objects() []fyne.CanvasObject {
return i.objects
}
func (i *iconRenderer) ApplyTheme() {
i.Refresh()
}
func (i *iconRenderer) BackgroundColor() color.Color {
return color.Transparent
}
func (i *iconRenderer) Refresh() {
i.objects = nil
if i.image.Resource != nil {
raster := canvas.NewImageFromResource(i.image.Resource)
raster.FillMode = canvas.ImageFillContain
i.objects = append(i.objects, raster)
}
i.Layout(i.image.Size())
canvas.Refresh(i.image)
}
func (i *iconRenderer) Destroy() {
}
// Icon widget is a basic image component that load's its resource to match the theme.
type Icon struct {
baseWidget
Resource fyne.Resource // The resource for this icon
}
// Resize sets a new size for a widget.
// Note this should not be used if the widget is being managed by a Layout within a Container.
func (i *Icon) Resize(size fyne.Size) {
i.resize(size, i)
}
// Move the widget to a new position, relative to it's parent.
// Note this should not be used if the widget is being managed by a Layout within a Container.
func (i *Icon) Move(pos fyne.Position) {
i.move(pos, i)
}
// MinSize returns the smallest size this widget can shrink to
func (i *Icon) MinSize() fyne.Size {
return i.minSize(i)
}
// Show this widget, if it was previously hidden
func (i *Icon) Show() {
i.show(i)
}
// Hide this widget, if it was previously visible
func (i *Icon) Hide() {
i.hide(i)
}
// SetResource updates the resource rendered in this icon widget
func (i *Icon) SetResource(res fyne.Resource) {
i.Resource = res
Refresh(i)
}
// CreateRenderer is a private method to Fyne which links this widget to it's renderer
func (i *Icon) CreateRenderer() fyne.WidgetRenderer {
render := &iconRenderer{image: i}
render.objects = []fyne.CanvasObject{}
return render
}
// NewIcon returns a new icon widget that displays a themed icon resource
func NewIcon(res fyne.Resource) *Icon {
icon := &Icon{}
icon.SetResource(res) // force the image conversion
return icon
}