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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
package main
import (
"fmt"
"math/bits"
"strconv"
"strings"
"gioui.org/layout"
"gioui.org/text"
"gioui.org/unit"
"gioui.org/widget"
"gioui.org/widget/material"
)
type Float struct {
val float32
editor widget.Editor
th *material.Theme
}
func NewFloat(th *material.Theme) Float {
f := Float{th: th}
f.editor.SetText(fmt.Sprintf("%.2f", f.val))
f.editor.SingleLine = true
f.editor.Alignment = text.Middle
return f
}
func (f *Float) Set(v float32) {
f.val = v
f.editor.SetText(fmt.Sprintf("%.2f", f.val))
}
func (f *Float) Layout(gtx layout.Context) layout.Dimensions {
inputString := f.editor.Text()
inputString = strings.TrimSpace(inputString)
inputFloat, err := strconv.ParseFloat(inputString, 32)
if err != nil {
f.editor.SetText("")
} else {
f.val = float32(inputFloat)
}
ed := material.Editor(f.th, &f.editor, "")
border := widget.Border{
Color: black,
CornerRadius: unit.Dp(3),
Width: unit.Dp(2),
}
inset := layout.Inset{
Top: unit.Dp(1),
Bottom: unit.Dp(1),
Left: unit.Dp(3),
Right: unit.Dp(3),
}
return border.Layout(gtx,
func(gtx layout.Context) layout.Dimensions {
return inset.Layout(gtx, ed.Layout)
},
)
}
type Int struct {
val int
editor widget.Editor
th *material.Theme
}
func NewInt(th *material.Theme) Int {
i := Int{th: th}
i.editor.SetText(fmt.Sprintf("%d", i.val))
i.editor.SingleLine = true
i.editor.Alignment = text.Middle
return i
}
func (i *Int) Set(v int) {
i.val = v
i.editor.SetText(fmt.Sprintf("%d", i.val))
}
func (i *Int) Layout(gtx layout.Context) layout.Dimensions {
inputString := i.editor.Text()
inputString = strings.TrimSpace(inputString)
inputInt, err := strconv.ParseInt(inputString, 10, bits.UintSize)
if err != nil {
i.editor.SetText("")
} else {
i.val = int(inputInt)
}
ed := material.Editor(i.th, &i.editor, "")
border := widget.Border{
Color: black,
CornerRadius: unit.Dp(3),
Width: unit.Dp(2),
}
inset := layout.Inset{
Top: unit.Dp(1),
Bottom: unit.Dp(1),
Left: unit.Dp(3),
Right: unit.Dp(3),
}
return border.Layout(gtx,
func(gtx layout.Context) layout.Dimensions {
return inset.Layout(gtx, ed.Layout)
},
)
}
|