-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspool.go
172 lines (160 loc) · 4.44 KB
/
spool.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package main
import (
"gioui.org/gesture"
"gioui.org/layout"
"gioui.org/op/clip"
"gioui.org/unit"
"gioui.org/widget"
"gioui.org/widget/material"
"gioui.org/x/notify"
"github.com/katzenpost/katzenpost/catshadow"
"image"
"sync"
"time"
//"gioui.org/widget/material"
)
type SpoolPage struct {
a *App
provider *layout.List
providerClicks map[string]*gesture.Click
connect *widget.Clickable
settings *widget.Clickable
back *widget.Clickable
submit *widget.Clickable
once *sync.Once
errCh chan error
}
func (p *SpoolPage) Start(stop <-chan struct{}) {
// start a goroutine that redraws the page every second
go func() {
for {
select {
case <-stop:
return
case <-time.After(time.Second):
p.a.w.Invalidate()
}
}
}()
}
func (p *SpoolPage) Layout(gtx layout.Context) layout.Dimensions {
bg := Background{
Color: th.Bg,
Inset: layout.Inset{},
}
providers, err := p.a.c.GetSpoolProviders()
return bg.Layout(gtx, func(gtx C) D {
// returns a flex consisting of the contacts list and add contact button
return layout.Flex{Axis: layout.Vertical, Alignment: layout.End}.Layout(gtx,
// topbar: Name, Add Contact, Settings
layout.Rigid(func(gtx C) D {
return layout.Flex{Axis: layout.Horizontal, Spacing: layout.SpaceBetween, Alignment: layout.Middle}.Layout(
gtx,
layout.Rigid(layoutLogo),
layout.Flexed(1, fill{th.Bg}.Layout),
func() layout.FlexChild {
if isConnected {
return layout.Rigid(button(th, p.connect, connectIcon).Layout)
}
return layout.Rigid(button(th, p.connect, disconnectIcon).Layout)
}(),
layout.Rigid(button(th, p.settings, settingsIcon).Layout),
//layout.Rigid(button(th, p.addContact, addContactIcon).Layout),
)
}),
// Add a caption
layout.Rigid(func(gtx C) D {
if err == catshadow.ErrNotOnline {
return material.Body2(th, "Welcome to Katzen. Please connect to choose a message storage provider").Layout(gtx)
}
if isConnecting {
return material.Body2(th, "Connecting...").Layout(gtx)
}
return material.Body2(th, "Please choose a message storage provider").Layout(gtx)
}),
// show list of providers
layout.Flexed(1, func(gtx C) D {
gtx.Constraints.Min.X = gtx.Dp(unit.Dp(300))
// skip the provider list if there was an error
if err != nil {
return layout.Dimensions{}
}
return p.provider.Layout(gtx, len(providers), func(gtx C, i int) layout.Dimensions {
in := layout.Inset{Top: unit.Dp(8), Bottom: unit.Dp(8), Left: unit.Dp(12), Right: unit.Dp(12)}
// if the layout is selected, change background color
bg := Background{Inset: in}
if i == selectedIdx {
bg.Color = th.ContrastBg
} else {
bg.Color = th.Bg
}
// create a click handler for this provider
if _, ok := p.providerClicks[providers[i]]; !ok {
c := new(gesture.Click)
p.providerClicks[providers[i]] = c
}
// attach click handler to this element
dims := bg.Layout(gtx, func(gtx C) D {
return material.Body2(th, providers[i]).Layout(gtx)
})
a := clip.Rect(image.Rectangle{Max: dims.Size})
t := a.Push(gtx.Ops)
p.providerClicks[providers[i]].Add(gtx.Ops)
t.Pop()
return dims
})
}),
)
})
}
func (p *SpoolPage) Event(gtx layout.Context) interface{} {
if p.back.Clicked(gtx) {
return BackEvent{}
}
if p.connect.Clicked(gtx) {
if !isConnected && !isConnecting {
return OnlineClick{}
}
return OfflineClick{}
}
if p.settings.Clicked(gtx) {
return ShowSettingsClick{}
}
for provider, click := range p.providerClicks {
if _, ok := click.Update(gtx.Source); ok {
provider := provider // copy reference to provider
go p.once.Do(func() {
select {
case p.errCh <- p.a.c.CreateRemoteSpoolOn(provider):
case <-p.a.c.HaltCh():
return
}
})
}
}
select {
case e := <-p.errCh:
if e == nil {
notify.Push("Success", "Katzen created a spool")
return BackEvent{}
} else {
notify.Push("Failure", e.Error())
p.once = new(sync.Once)
}
default:
}
return nil
}
func newSpoolPage(a *App) *SpoolPage {
p := &SpoolPage{}
p.provider = &layout.List{Axis: layout.Vertical}
p.back = &widget.Clickable{}
p.connect = &widget.Clickable{}
p.settings = &widget.Clickable{}
p.once = new(sync.Once)
p.errCh = make(chan error)
p.submit = &widget.Clickable{}
p.providerClicks = make(map[string]*gesture.Click)
p.a = a
return p
}