wip: ship calculator
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package validator
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strconv"
|
||||
|
||||
"fyne.io/fyne/v2"
|
||||
)
|
||||
|
||||
func NewStackValidator(first fyne.StringValidator, rest ...fyne.StringValidator) fyne.StringValidator {
|
||||
if first == nil {
|
||||
panic("first validator cannot be nil")
|
||||
}
|
||||
return func(s string) error {
|
||||
if err := first(s); err != nil {
|
||||
return err
|
||||
}
|
||||
for i := range rest {
|
||||
if err := rest[i](s); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func NewMutualValidator(other func() float64, valid func(float64) bool) fyne.StringValidator {
|
||||
if other == nil {
|
||||
panic("other value getter cannot be nil")
|
||||
}
|
||||
return func(s string) error {
|
||||
myValue, err := ParseFloat(s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !valid(myValue) {
|
||||
return errors.New("invalid value")
|
||||
}
|
||||
if !valid(other()) {
|
||||
return errors.New("invalid other value")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func FloatValueValidator(s string) error {
|
||||
if _, err := ParseFloat(s); err != nil {
|
||||
return errors.New("not a float value")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func ParseFloat(s string) (float64, error) {
|
||||
return strconv.ParseFloat(s, 64)
|
||||
}
|
||||
Reference in New Issue
Block a user