diff --git a/ztm_vehicles_app.go b/ztm_vehicles_app.go index 1093776..5ae8a8c 100644 --- a/ztm_vehicles_app.go +++ b/ztm_vehicles_app.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "strings" "fyne.io/fyne/v2/app" @@ -10,10 +11,46 @@ import ( "github.com/gocolly/colly/v2" "fyne.io/fyne/v2" - "fyne.io/fyne/v2/layout" + + "strconv" + + "fyne.io/fyne/v2/driver/mobile" ) +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 +} + type vehicle struct { producer string model string @@ -97,7 +134,7 @@ func main() { w := a.NewWindow("ZTM vehicles") entryLabel := widget.NewLabel("Podaj numer taborowy pojazdu:") - entry := widget.NewEntry() + entry := newNumericalEntry() output := widget.NewLabel("") output.Wrapping = fyne.TextWrapWord