-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbutton.go
162 lines (139 loc) · 3.34 KB
/
button.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
package wx
import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/driver/desktop"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
)
type Button struct {
widget.Button
Unfocusable bool
Menu *fyne.Menu
OnMenuRequest func() // if set, called just before showing popup menu
minWidth float32
ToolTipable
}
func NewButton(text string, icon fyne.Resource, importance widget.Importance, action func(), menuItems ...*fyne.MenuItem) *Button {
b := &Button{
Button: widget.Button{
Text: text, Icon: icon, Importance: importance, OnTapped: action,
},
}
for _, item := range menuItems {
if item == nil {
panic("wx.NewButton got passed a nil menu item")
}
}
if len(menuItems) > 0 {
b.Menu = fyne.NewMenu("", menuItems...)
}
b.ToolTipable.parent = b
b.ExtendBaseWidget(b)
return b
}
func NewTBButton(text string, icon fyne.Resource, action func(), menuItems ...*fyne.MenuItem) *Button {
b := &Button{
Button: widget.Button{
Text: text, Icon: icon, OnTapped: action,
Importance: widget.LowImportance,
},
}
for _, item := range menuItems {
if item == nil {
panic("wx.NewTBButton got passed a nil menu item")
}
}
if len(menuItems) > 0 {
b.Menu = fyne.NewMenu("", menuItems...)
}
b.ToolTipable.parent = b
b.ExtendBaseWidget(b)
return b
}
func (b *Button) ToolbarObject() fyne.CanvasObject {
return b
}
func (b *Button) MinSize() fyne.Size {
sz := b.Button.MinSize()
if sz.Width < b.minWidth {
sz.Width = b.minWidth
}
return sz
}
func (b *Button) SetMinWidth(w float32) {
b.minWidth = w
}
func (b *Button) Tap() {
pos := fyne.CurrentApp().Driver().AbsolutePositionForObject(b)
pos.Y += b.Size().Height + theme.Padding()
b.Tapped(&fyne.PointEvent{
Position: fyne.NewPos(0, b.Size().Height+theme.Padding()),
AbsolutePosition: pos,
})
}
func (b *Button) TapSecondary() {
pos := fyne.CurrentApp().Driver().AbsolutePositionForObject(b)
pos.Y += b.Size().Height + theme.Padding()
b.TappedSecondary(&fyne.PointEvent{
Position: fyne.NewPos(0, b.Size().Height+theme.Padding()),
AbsolutePosition: pos,
})
}
func (b *Button) Tapped(e *fyne.PointEvent) {
if b.OnTapped == nil {
b.TappedSecondary(e)
} else {
b.Button.Tapped(e)
}
}
func (b *Button) TappedSecondary(e *fyne.PointEvent) {
if b.Disabled() {
return
}
if b.OnMenuRequest != nil {
if b.Menu == nil {
b.Menu = &fyne.Menu{}
}
b.OnMenuRequest()
}
if b.Menu != nil && len(b.Menu.Items) > 0 {
widget.ShowPopUpMenuAtPosition(b.Menu, fyne.CurrentApp().Driver().CanvasForObject(b), e.AbsolutePosition)
}
}
func (b *Button) FocusGained() {
if !b.Unfocusable {
b.Button.FocusGained()
} else {
drv := fyne.CurrentApp().Driver()
if dd, ok := drv.(desktop.Driver); ok {
if dd.CurrentKeyModifiers()&fyne.KeyModifierShift != 0 {
go drv.CanvasForObject(b).FocusPrevious()
} else {
go drv.CanvasForObject(b).FocusNext()
}
}
}
}
func (b *Button) FocusLost() {
if !b.Unfocusable {
b.Button.FocusLost()
}
}
func (b *Button) TypedRune(r rune) {
b.Button.TypedRune(r)
}
func (b *Button) TypedKey(e *fyne.KeyEvent) {
b.Button.TypedKey(e)
}
func (b *Button) MouseIn(e *desktop.MouseEvent) {
b.ToolTipable.MouseIn(e)
b.Button.MouseIn(e)
}
func (b *Button) MouseMoved(e *desktop.MouseEvent) {
b.ToolTipable.MouseMoved(e)
b.Button.MouseMoved(e)
}
func (b *Button) MouseOut() {
b.ToolTipable.MouseOut()
b.Button.MouseOut()
}