-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
tappablewrappers.go
117 lines (90 loc) · 2.37 KB
/
tappablewrappers.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
package widgets
import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/driver/desktop"
"fyne.io/fyne/v2/widget"
)
// TappableIcon is a tappable wrapper of widget.Icon
type TappableIcon struct {
widget.Icon
NoPointerCursor bool
OnTapped func()
}
func NewTappableIcon(res fyne.Resource) *TappableIcon {
icon := &TappableIcon{}
icon.ExtendBaseWidget(icon)
icon.SetResource(res)
return icon
}
var _ fyne.Tappable = (*TappableIcon)(nil)
func (t *TappableIcon) Tapped(_ *fyne.PointEvent) {
if t.OnTapped != nil {
t.OnTapped()
}
}
func (t *TappableIcon) TappedSecondary(_ *fyne.PointEvent) {
}
var _ desktop.Hoverable = (*TappableIcon)(nil)
func (t *TappableIcon) MouseIn(*desktop.MouseEvent) {}
func (t *TappableIcon) MouseOut() {}
func (t *TappableIcon) MouseMoved(*desktop.MouseEvent) {}
func (t *TappableIcon) Cursor() desktop.Cursor {
if t.NoPointerCursor {
return desktop.DefaultCursor
}
return desktop.PointerCursor
}
// TappableImage is a tappable wrapper of canvas.Image
type TappableImage struct {
widget.BaseWidget
canvas.Image
DisableTapping bool
OnTapped func(*fyne.PointEvent)
OnTappedSecondary func(*fyne.PointEvent)
}
func NewTappableImage(onTapped func(*fyne.PointEvent)) *TappableImage {
t := &TappableImage{OnTapped: onTapped}
t.ExtendBaseWidget(t)
return t
}
func (t *TappableImage) Hide() {
t.BaseWidget.Hide()
}
func (t *TappableImage) Show() {
t.BaseWidget.Show()
}
func (t *TappableImage) Move(pos fyne.Position) {
t.BaseWidget.Move(pos)
}
func (t *TappableImage) Refresh() {
t.BaseWidget.Refresh()
}
func (t *TappableImage) MinSize() fyne.Size {
return t.Image.MinSize()
}
func (t *TappableImage) Resize(size fyne.Size) {
t.BaseWidget.Resize(size)
}
func (t *TappableImage) Cursor() desktop.Cursor {
if !t.DisableTapping && t.haveImage() {
return desktop.PointerCursor
}
return desktop.DefaultCursor
}
func (t *TappableImage) Tapped(e *fyne.PointEvent) {
if !t.DisableTapping && t.haveImage() && t.OnTapped != nil {
t.OnTapped(e)
}
}
func (t *TappableImage) TappedSecondary(e *fyne.PointEvent) {
if !t.DisableTapping && t.haveImage() && t.OnTappedSecondary != nil {
t.OnTappedSecondary(e)
}
}
func (t *TappableImage) CreateRenderer() fyne.WidgetRenderer {
return widget.NewSimpleRenderer(&t.Image)
}
func (t *TappableImage) haveImage() bool {
return t.Image.Resource != nil || t.Image.Image != nil
}