-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathentry.go
189 lines (165 loc) · 3.86 KB
/
entry.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package wx
import (
"strings"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/driver/desktop"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
var (
EntryExCaseUpper = strings.ToUpper
EntryExCaseLower = strings.ToLower
EntryExCaseTitle = func(s string) string {
return cases.Title(language.French).String(strings.ToLower(s))
}
)
type EntryEx struct {
widget.Entry
// widget configuration
AcceptTab bool
RuneModifier func(rune) rune
CaseModifier func(string) string // only for non multiline entries ; use RuneModifier for multiline
ToolTipable
// custom callbacks
OnChanged func(string)
OnFocusGained func()
OnFocusLost func()
OnTypedRune func(r rune) (block bool)
OnTypedKey func(k *fyne.KeyEvent) (block bool)
OnTypedShortcut func(s fyne.Shortcut) (block bool)
//
Menu, DisabledMenu *fyne.Menu
readOnly bool
minCols int
}
func NewEntryEx(minRows int) *EntryEx {
e := &EntryEx{}
e.ExtendBaseWidget(e)
e.ToolTipable.parent = e
e.Wrapping = fyne.TextTruncate
if minRows > 1 {
e.MultiLine = true
e.Wrapping = fyne.TextWrapWord
e.SetMinRowsVisible(minRows)
} else {
e.Wrapping = fyne.TextTruncate
}
return e
}
func (e *EntryEx) ExtendBaseWidget(wid fyne.Widget) {
e.Entry.OnChanged = e.onChanged
e.Entry.ExtendBaseWidget(wid)
}
func (e *EntryEx) onChanged(s string) {
if !e.MultiLine && e.CaseModifier != nil {
s = e.CaseModifier(s)
e.Text = s
e.Refresh()
}
if e.OnChanged != nil {
e.OnChanged(s)
}
}
func (e *EntryEx) AcceptsTab() bool {
return e.AcceptTab
}
func (e *EntryEx) ReadOnly() bool { return e.readOnly }
func (e *EntryEx) SetReadOnly(b bool) {
e.readOnly = b
if cnv := fyne.CurrentApp().Driver().CanvasForObject(e); b && cnv != nil && cnv.Focused() == e {
cnv.Focus(nil)
}
e.Entry.Refresh()
}
func (e *EntryEx) SetMinColsVisible(c int) {
e.minCols = c
e.Refresh()
}
func (e *EntryEx) MinSize() fyne.Size {
sz := e.Entry.MinSize()
if e.minCols > 0 {
calc := fyne.MeasureText(strings.Repeat("M", e.minCols), theme.TextSize(), e.TextStyle).Width + 2*theme.InnerPadding() + 2*theme.InputBorderSize()
if calc > sz.Width {
sz.Width = calc
}
}
return sz
}
func (e *EntryEx) SetText(s string) {
e.Entry.CursorColumn = 0
e.Entry.CursorRow = 0
e.Entry.SetText(s)
}
func (e *EntryEx) FocusGained() {
if e.readOnly {
return
}
e.Entry.FocusGained()
if e.OnFocusGained != nil {
e.OnFocusGained()
}
}
func (e *EntryEx) FocusLost() {
e.Entry.FocusLost()
if e.OnFocusLost != nil {
e.OnFocusLost()
}
}
func (e *EntryEx) TypedRune(r rune) {
if e.readOnly {
return
}
if e.RuneModifier != nil {
r = e.RuneModifier(r)
}
if e.OnTypedRune != nil && e.OnTypedRune(r) {
return
}
e.Entry.TypedRune(r)
}
func (e *EntryEx) TypedKey(k *fyne.KeyEvent) {
if e.readOnly {
switch k.Name {
case fyne.KeyEnter, fyne.KeyReturn, fyne.KeyBackspace, fyne.KeyDelete:
return
case fyne.KeyTab:
if e.AcceptTab {
return
}
}
}
if e.OnTypedKey != nil && e.OnTypedKey(k) {
return
}
e.Entry.TypedKey(k)
}
func (e *EntryEx) TypedShortcut(s fyne.Shortcut) {
if e.readOnly {
switch s.(type) {
case *fyne.ShortcutPaste:
return
case *fyne.ShortcutCut:
s = &fyne.ShortcutCopy{Clipboard: s.(*fyne.ShortcutCut).Clipboard}
}
}
if e.OnTypedShortcut != nil && e.OnTypedShortcut(s) {
return
}
e.Entry.TypedShortcut(s)
}
func (e *EntryEx) TappedSecondary(p *fyne.PointEvent) {
menu := e.Menu
if e.Disabled() {
menu = e.DisabledMenu
}
if menu == nil {
e.Entry.TappedSecondary(p)
return
}
widget.ShowPopUpMenuAtPosition(menu, fyne.CurrentApp().Driver().CanvasForObject(e), p.AbsolutePosition)
}
func (e *EntryEx) MouseIn(me *desktop.MouseEvent) { e.ToolTipable.MouseIn(me) }
func (e *EntryEx) MouseMoved(me *desktop.MouseEvent) { e.ToolTipable.MouseMoved(me) }
func (e *EntryEx) MouseOut() { e.ToolTipable.MouseOut() }