43 lines
806 B
Go
43 lines
806 B
Go
package numeric
|
|
|
|
import (
|
|
"strconv"
|
|
|
|
"fyne.io/fyne/v2"
|
|
"fyne.io/fyne/v2/driver/mobile"
|
|
"fyne.io/fyne/v2/widget"
|
|
)
|
|
|
|
type numericalEntry struct {
|
|
widget.Entry
|
|
}
|
|
|
|
func NewNumericalEntry() *numericalEntry {
|
|
entry := &numericalEntry{}
|
|
entry.ExtendBaseWidget(entry)
|
|
return entry
|
|
}
|
|
|
|
func (e *numericalEntry) TypedRune(r rune) {
|
|
if (r >= '0' && r <= '9') || r == '.' || r == ',' {
|
|
e.Entry.TypedRune(r)
|
|
}
|
|
}
|
|
|
|
func (e *numericalEntry) 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 (e *numericalEntry) Keyboard() mobile.KeyboardType {
|
|
return mobile.NumberKeyboard
|
|
}
|