/
statusbar.go
138 lines (118 loc) · 2.73 KB
/
statusbar.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package app
import (
"github.com/gogo/protobuf/proto"
"gomatcha.io/matcha"
"gomatcha.io/matcha/internal"
"gomatcha.io/matcha/internal/radix"
"gomatcha.io/matcha/pb/app"
"gomatcha.io/matcha/view"
)
func init() {
internal.RegisterMiddleware(func() interface{} {
return &statusBarMiddleware{
radix: radix.NewRadix(),
}
})
}
type StatusBarStyle int
const (
StatusBarStyleDefault StatusBarStyle = iota
StatusBarStyleLight
StatusBarStyleDark
)
type StatusBar struct {
Hidden bool
Style StatusBarStyle
}
func (s StatusBar) OptionsKey() string {
return "gomatcha.io/matcha/app statusbar"
}
type statusBarMiddleware struct {
radix *radix.Radix
}
func (m *statusBarMiddleware) Build(ctx *view.Context, model *view.Model) {
path := idSliceToIntSlice(ctx.Path())
add := false
statusBar := StatusBar{}
for _, i := range model.Options {
var ok bool
if statusBar, ok = i.(StatusBar); ok {
add = true
}
}
if add {
n := m.radix.Insert(path)
n.Value = statusBar
} else {
m.radix.Delete(path)
}
}
func (m *statusBarMiddleware) MarshalProtobuf() proto.Message {
var statusBar StatusBar
maxId := int64(-1)
m.radix.Range(func(path []int64, node *radix.Node) {
if len(path) > 0 && path[len(path)-1] > maxId {
maxId = path[len(path)-1]
statusBar, _ = node.Value.(StatusBar)
}
})
return &app.StatusBar{
Hidden: statusBar.Hidden,
Style: app.StatusBarStyle(statusBar.Style),
}
}
func (m *statusBarMiddleware) Key() string {
return "gomatcha.io/matcha/app statusbar"
}
func init() {
internal.RegisterMiddleware(func() interface{} {
return &activityIndicatorMiddleware{
radix: radix.NewRadix(),
}
})
}
// If any view has an ActivityIndicator option, the spinner will be visible.
// return view.Model{
// Options: []view.Option{app.ActivityIndicator{}}
// }
type ActivityIndicator struct {
}
func (a ActivityIndicator) OptionsKey() string {
return "gomatcha.io/matcha/app activity"
}
type activityIndicatorMiddleware struct {
radix *radix.Radix
}
func (m *activityIndicatorMiddleware) Build(ctx *view.Context, model *view.Model) {
path := idSliceToIntSlice(ctx.Path())
add := false
for _, i := range model.Options {
if _, ok := i.(ActivityIndicator); ok {
add = true
}
}
if add {
m.radix.Insert(path)
} else {
m.radix.Delete(path)
}
}
func (m *activityIndicatorMiddleware) MarshalProtobuf() proto.Message {
visible := false
m.radix.Range(func(path []int64, node *radix.Node) {
visible = true
})
return &app.ActivityIndicator{
Visible: visible,
}
}
func (m *activityIndicatorMiddleware) Key() string {
return "gomatcha.io/matcha/app activity"
}
func idSliceToIntSlice(ids []matcha.Id) []int64 {
ints := make([]int64, len(ids))
for idx, i := range ids {
ints[idx] = int64(i)
}
return ints
}