-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
textrestrictedentry.go
52 lines (41 loc) · 1.26 KB
/
textrestrictedentry.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
package widgets
import (
"strings"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
)
// A widget.Entry that allows restrictions on the text
// that can be typed into it, based on a charAllowed callback.
type TextRestrictedEntry struct {
widget.Entry
charAllowed CharAllowedFunc
minWidth float32
}
type CharAllowedFunc func(curText string, selectedText string, r rune) bool
func NewTextRestrictedEntry(charAllowed CharAllowedFunc) *TextRestrictedEntry {
e := &TextRestrictedEntry{charAllowed: charAllowed}
e.ExtendBaseWidget(e)
return e
}
func (e *TextRestrictedEntry) TypedRune(r rune) {
if e.charAllowed == nil || e.charAllowed(e.Text, e.SelectedText(), r) {
e.Entry.TypedRune(r)
}
}
func (e *TextRestrictedEntry) TypedShortcut(s fyne.Shortcut) {
if s.ShortcutName() == (&fyne.ShortcutPaste{}).ShortcutName() {
return
}
e.Entry.TypedShortcut(s)
}
func (e *TextRestrictedEntry) SetMinCharWidth(numChars int) {
e.minWidth = theme.Padding()*2 + fyne.MeasureText(strings.Repeat("W", numChars),
fyne.CurrentApp().Settings().Theme().Size(theme.SizeNameText), e.TextStyle).Width
}
func (e *TextRestrictedEntry) MinSize() fyne.Size {
if e.minWidth < 0.001 {
return e.Entry.MinSize()
}
return fyne.NewSize(e.minWidth, e.Entry.MinSize().Height)
}