-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathselectentry.go
121 lines (106 loc) · 2.76 KB
/
selectentry.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
package wx
import (
"strings"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
)
type SelectEntryEx struct {
widget.SelectEntry
// custom callbacks
OnFocusGained func()
OnFocusLost func()
OnTypedRune func(rune) (block bool)
OnTypedKey func(*fyne.KeyEvent) (block bool)
OnTypedShortcut func(fyne.Shortcut) (block bool)
readOnly bool
minCols int
}
func NewSelectEntryEx() *SelectEntryEx {
sel := &SelectEntryEx{}
sel.Wrapping = fyne.TextTruncate
sel.ExtendBaseWidget(sel)
return sel
}
func (sel *SelectEntryEx) ReadOnly() bool { return sel.readOnly }
func (sel *SelectEntryEx) SetReadOnly(b bool) {
sel.readOnly = b
if b {
sel.SelectEntry.Entry.ActionItem.(fyne.Disableable).Disable()
} else {
sel.SelectEntry.Entry.ActionItem.(fyne.Disableable).Enable()
}
}
func (e *SelectEntryEx) SetMinColsVisible(c int) {
e.minCols = c
e.Refresh()
}
func (e *SelectEntryEx) MinSize() fyne.Size {
sz := e.SelectEntry.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 (sel *SelectEntryEx) SetText(s string) {
sel.SelectEntry.CursorColumn = 0
sel.SelectEntry.CursorRow = 0
sel.SelectEntry.SetText(s)
}
// FocusGained is a hook called by the focus handling logic after this object gained the focus.
func (sel *SelectEntryEx) FocusGained() {
if sel.readOnly {
return
}
sel.SelectEntry.FocusGained()
if sel.OnFocusGained != nil {
sel.OnFocusGained()
}
}
// FocusLost is a hook called by the focus handling logic after this object lost the focus.
func (sel *SelectEntryEx) FocusLost() {
sel.SelectEntry.FocusLost()
if sel.OnFocusLost != nil {
sel.OnFocusLost()
}
}
// TypedRune is a hook called by the input handling logic on text input events if this object is focused.
func (sel *SelectEntryEx) TypedRune(r rune) {
if sel.readOnly {
return
}
if sel.OnTypedRune != nil && sel.OnTypedRune(r) {
return
}
sel.SelectEntry.TypedRune(r)
}
// TypedKey is a hook called by the input handling logic on key events if this object is focused.
func (sel *SelectEntryEx) TypedKey(e *fyne.KeyEvent) {
if sel.readOnly {
switch e.Name {
case fyne.KeyEnter, fyne.KeyReturn, fyne.KeyBackspace, fyne.KeyDelete:
return
}
}
if sel.OnTypedKey != nil && sel.OnTypedKey(e) {
return
}
sel.SelectEntry.TypedKey(e)
}
func (sel *SelectEntryEx) TypedShortcut(s fyne.Shortcut) {
if sel.readOnly {
switch s.(type) {
case *fyne.ShortcutPaste:
return
case *fyne.ShortcutCut:
s = &fyne.ShortcutCopy{Clipboard: s.(*fyne.ShortcutCut).Clipboard}
}
}
if sel.OnTypedShortcut != nil && sel.OnTypedShortcut(s) {
return
}
sel.SelectEntry.TypedShortcut(s)
}