/
duration.go
92 lines (75 loc) · 2.1 KB
/
duration.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
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
package tui
import (
"image/color"
"time"
"gioui.org/layout"
"gioui.org/text"
"gioui.org/widget"
"gioui.org/widget/material"
)
type Duration struct {
Value time.Duration
valid bool
editor widget.Editor
spin Spin
}
func (dur *Duration) SetValue(value time.Duration) {
if dur.Value == value {
return
}
dur.Value = value
dur.valid = true
dur.editor.SetText(value.String())
}
type DurationEditorStyle struct {
Caption material.LabelStyle
Value *Duration
Editor material.EditorStyle
Min time.Duration
Max time.Duration
}
func DurationEditor(theme *material.Theme, value *Duration, caption string, min, max time.Duration) DurationEditorStyle {
cap := material.Body2(theme, caption)
cap.Color = color.NRGBA{R: 0xE0, G: 0xE0, B: 0xE0, A: 0xFF}
cap.Alignment = text.End
editor := material.Editor(theme, &value.editor, "")
editor.Color = color.NRGBA{R: 0xFF, G: 0xFF, B: 0xFF, A: 0xFF}
editor.Font = cap.Font
editor.TextSize = cap.TextSize * 0.8
cap.TextSize = cap.TextSize * 0.8
return DurationEditorStyle{
Caption: cap,
Value: value,
Editor: editor,
Min: min,
Max: max,
}
}
func (edit DurationEditorStyle) setValue(value time.Duration) {
if value < edit.Min {
value = edit.Min
}
if value > edit.Max {
value = edit.Max
}
edit.Value.SetValue(value)
}
func (edit DurationEditorStyle) Layout(gtx layout.Context) layout.Dimensions {
if edit.Value.spin.Dragging() && edit.Value.spin.Delta != 0 {
changed := float64(edit.Value.Value) + float64(edit.Value.spin.Delta)*float64(time.Second/10)
edit.setValue(time.Duration(changed))
}
dim := layout.Flex{
Alignment: layout.Middle,
}.Layout(gtx,
layout.Flexed(1, edit.Caption.Layout),
layout.Rigid(layout.Spacer{Width: Small}.Layout),
layout.Flexed(2, func(gtx layout.Context) layout.Dimensions {
return RoundBox(color.NRGBA{0x40, 0x40, 0x40, 0xFF}).Layout(gtx.Disabled(), edit.Editor.Layout)
}),
layout.Rigid(layout.Spacer{Width: Small}.Layout),
layout.Rigid(Spinner(color.NRGBA{0x60, 0x60, 0x60, 0xFF}, color.NRGBA{0x80, 0x80, 0x80, 0xFF}, &edit.Value.spin).Layout),
)
edit.setValue(edit.Value.Value)
return dim
}