-
Notifications
You must be signed in to change notification settings - Fork 0
/
field.go
46 lines (40 loc) · 923 Bytes
/
field.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
package main
import (
"fyne.io/fyne/widget"
"strings"
)
// field.go handles numeric entry fields (used for entry of hours/mins/secs)
type (
NumericField struct {
*widget.Entry
width int
valid func(int) bool // indicates if the values is valid
}
)
func NewNumericFieldRange(min, max int) *NumericField {
if min < 0 || min >= max {
panic("Invalid range for NewNumericFieldRange")
}
entry := widget.NewEntry()
digits := countDigits(max)
entry.Text = strings.Repeat("0", digits)
return &NumericField{
Entry: entry,
width: digits,
valid: func(v int) bool {
return v >= min && v < max
},
}
}
func NewNumericField(width int, valid func(int) bool) *NumericField {
entry := widget.NewEntry()
entry.Text = strings.Repeat("0", width)
return &NumericField{Entry: entry, width: width, valid: valid}
}
func countDigits(v int) int {
count := 0
for ; v != 0; v /= 10 {
count++
}
return count
}