-
Notifications
You must be signed in to change notification settings - Fork 162
/
progressview.go
78 lines (69 loc) · 1.87 KB
/
progressview.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
// Package ios implements a native ios views.
package ios
import (
"image/color"
"gomatcha.io/matcha/comm"
"gomatcha.io/matcha/internal"
"gomatcha.io/matcha/layout/constraint"
"gomatcha.io/matcha/paint"
pb "gomatcha.io/matcha/proto"
pbios "gomatcha.io/matcha/proto/view/ios"
"gomatcha.io/matcha/view"
)
// ProgressView implements a progess view.
type ProgressView struct {
view.Embed
Progress float64
ProgressNotifier comm.Float64Notifier
ProgressColor color.Color
PaintStyle *paint.Style
progressNotifier comm.Float64Notifier
}
// NewProgressView returns a new view.
func NewProgressView() *ProgressView {
return &ProgressView{}
}
// Lifecycle implements the view.View interface.
func (v *ProgressView) Lifecycle(from, to view.Stage) {
if view.ExitsStage(from, to, view.StageMounted) {
if v.progressNotifier != nil {
v.Unsubscribe(v.progressNotifier)
}
}
}
// Build implements the view.View interface.
func (v *ProgressView) Build(ctx view.Context) view.Model {
l := &constraint.Layouter{}
l.Solve(func(s *constraint.Solver) {
s.Height(2) // 2.5 if its a bar
s.WidthEqual(l.MinGuide().Width())
s.TopEqual(l.MaxGuide().Top())
s.LeftEqual(l.MaxGuide().Left())
})
if v.ProgressNotifier != v.progressNotifier {
if v.progressNotifier != nil {
v.Unsubscribe(v.progressNotifier)
}
if v.ProgressNotifier != nil {
v.Subscribe(v.ProgressNotifier)
}
v.progressNotifier = v.ProgressNotifier
}
val := v.Progress
if v.ProgressNotifier != nil {
val = v.ProgressNotifier.Value()
}
painter := paint.Painter(nil)
if v.PaintStyle != nil {
painter = v.PaintStyle
}
return view.Model{
Painter: painter,
Layouter: l,
NativeViewName: "gomatcha.io/matcha/view/progressview",
NativeViewState: internal.MarshalProtobuf(&pbios.ProgressView{
Progress: val,
ProgressColor: pb.ColorEncode(v.ProgressColor),
}),
}
}