-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumEntry.go
44 lines (36 loc) · 847 Bytes
/
numEntry.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
package main
import (
"strconv"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/data/validation"
"fyne.io/fyne/v2/driver/mobile"
"fyne.io/fyne/v2/widget"
)
type NumEntry struct {
widget.Entry
}
func NewNumEntry() *NumEntry {
e := &NumEntry{}
e.ExtendBaseWidget(e)
e.Validator = validation.NewRegexp(`\d`, "Must contain a number")
return e
}
func (e *NumEntry) TypedRune(r rune) {
if (r >= '0' && r <= '9') || r == '.' || r == ',' {
e.Entry.TypedRune(r)
}
}
func (e *NumEntry) TypedShortcut(shortcut fyne.Shortcut) {
paste, ok := shortcut.(*fyne.ShortcutPaste)
if !ok {
e.Entry.TypedShortcut(shortcut)
return
}
content := paste.Clipboard.Content()
if _, err := strconv.ParseFloat(content, 64); err == nil {
e.Entry.TypedShortcut(shortcut)
}
}
func (n *NumEntry) Keyboard() mobile.KeyboardType {
return mobile.NumberKeyboard
}