-
Notifications
You must be signed in to change notification settings - Fork 1
/
home.go
364 lines (338 loc) · 10.6 KB
/
home.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
package main
import (
"bytes"
"encoding/base64"
"gioui.org/font"
"gioui.org/gesture"
"gioui.org/io/key"
"gioui.org/layout"
"gioui.org/op/clip"
"gioui.org/op/paint"
"gioui.org/unit"
"gioui.org/widget"
"gioui.org/widget/material"
"github.com/hako/durafmt"
"github.com/katzenpost/katzen/assets"
"github.com/katzenpost/katzenpost/catshadow"
"golang.org/x/exp/shiny/materialdesign/icons"
"image"
"image/png"
"sort"
"strings"
"sync"
"time"
)
var (
contactList = &layout.List{Axis: layout.Vertical, ScrollToEnd: false}
selectedIdx = 0
connectIcon, _ = widget.NewIcon(icons.DeviceSignalWiFi4Bar)
disconnectIcon, _ = widget.NewIcon(icons.DeviceSignalWiFiOff)
settingsIcon, _ = widget.NewIcon(icons.ActionSettings)
addContactIcon, _ = widget.NewIcon(icons.SocialPersonAdd)
logo = getLogo()
units, _ = durafmt.UnitsCoder{PluralSep: ":", UnitsSep: ","}.Decode("y:y,w:w,d:d,h:h,m:m,s:s,ms:ms,us:us")
avatars = make(map[string]layout.Widget)
)
type HomePage struct {
l *sync.Mutex
a *App
updateCh chan interface{}
contacts []*catshadow.Contact
addContact *widget.Clickable
connect *widget.Clickable
showSettings *widget.Clickable
av map[string]*widget.Image
contactClicks map[string]*gesture.Click
}
type AddContactClick struct{}
type ShowSettingsClick struct{}
func (p *HomePage) Layout(gtx layout.Context) layout.Dimensions {
contacts := p.contacts
// xxx do not request this every frame...
bg := Background{
Color: th.Bg,
Inset: layout.Inset{},
}
if len(contacts) == 0 {
selectedIdx = 0
} else if selectedIdx < 0 {
selectedIdx = len(contacts) - 1
} else {
selectedIdx = selectedIdx % len(contacts)
}
// re-center list view for keyboard contact selection
if selectedIdx < contactList.Position.First || selectedIdx >= contactList.Position.First+contactList.Position.Count {
// list doesn't wrap around view to end, so do not give negative value for First
if selectedIdx < contactList.Position.Count-1 {
contactList.Position.First = 0
} else {
contactList.Position.First = (selectedIdx - contactList.Position.Count + 1)
}
}
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.showSettings, settingsIcon).Layout),
layout.Rigid(button(th, p.addContact, addContactIcon).Layout),
)
}),
// show list of conversations
layout.Flexed(1, func(gtx C) D {
gtx.Constraints.Min.X = gtx.Dp(unit.Dp(300))
// the contactList
return contactList.Layout(gtx, len(contacts), func(gtx C, i int) layout.Dimensions {
lastMsg := contacts[i].LastMessage
// inset each contact Flex
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
}
return bg.Layout(gtx, func(gtx C) D {
// returns Flex of contact icon, contact name, and last message received or sent
if _, ok := p.contactClicks[contacts[i].Nickname]; !ok {
c := new(gesture.Click)
p.contactClicks[contacts[i].Nickname] = c
}
dims := layout.Flex{Axis: layout.Horizontal, Spacing: layout.SpaceEvenly}.Layout(gtx,
// contact avatar
layout.Rigid(func(gtx C) D {
return layoutAvatar(gtx, p.a.c, contacts[i].Nickname)
}),
// contact name and last message
layout.Flexed(1, func(gtx C) D {
gtx.Constraints.Max.Y = gtx.Dp(unit.Dp(96))
return layout.Flex{Axis: layout.Vertical, Alignment: layout.Start, Spacing: layout.SpaceBetween}.Layout(gtx,
layout.Rigid(func(gtx C) D {
return layout.Flex{Axis: layout.Horizontal, Alignment: layout.Start, Spacing: layout.SpaceBetween}.Layout(gtx,
// contact name
layout.Rigid(func(gtx C) D {
in := layout.Inset{Top: unit.Dp(4), Bottom: unit.Dp(4), Left: unit.Dp(12), Right: unit.Dp(12)}
return in.Layout(gtx, ContactStyle(th, contacts[i].Nickname).Layout)
}),
layout.Rigid(func(gtx C) D {
return layout.Flex{Axis: layout.Vertical, Alignment: layout.Start, Spacing: layout.SpaceEnd}.Layout(gtx,
layout.Rigid(func(gtx C) D {
if contacts[i].IsPending {
return pandaIcon.Layout(gtx, th.Palette.ContrastBg)
}
return fill{th.Bg}.Layout(gtx)
}),
layout.Rigid(func(gtx C) D {
// timestamp
if lastMsg != nil {
messageAge := strings.Replace(durafmt.ParseShort(time.Now().Round(0).Sub(lastMsg.Timestamp).Truncate(time.Minute)).Format(units), "0 s", "now", 1)
return material.Caption(th, messageAge).Layout(gtx)
}
return fill{th.Bg}.Layout(gtx)
}),
)
}),
)
}),
// last message
layout.Rigid(func(gtx C) D {
in := layout.Inset{Top: unit.Dp(4), Bottom: unit.Dp(4), Left: unit.Dp(12), Right: unit.Dp(12)}
if lastMsg != nil {
return in.Layout(gtx, func(gtx C) D {
// TODO: set the color based on sent or received
return material.Body2(th, string(lastMsg.Plaintext)).Layout(gtx)
})
} else {
return fill{th.Bg}.Layout(gtx)
}
}),
)
}),
)
a := clip.Rect(image.Rectangle{Max: dims.Size})
t := a.Push(gtx.Ops)
p.contactClicks[contacts[i].Nickname].Add(gtx.Ops)
t.Pop()
return dims
})
})
}),
)
})
}
func getLogo() *widget.Image {
d, err := base64.StdEncoding.DecodeString(assets.Logob64)
if err != nil {
return nil
}
if m, _, err := image.Decode(bytes.NewReader(d)); err == nil {
return &widget.Image{Src: paint.NewImageOp(m)}
}
return nil
}
func layoutLogo(gtx C) D {
in := layout.Inset{Left: unit.Dp(12), Right: unit.Dp(12)}
return in.Layout(gtx, func(gtx C) D {
sz := gtx.Constraints.Max.X
logo.Scale = float32(sz) / float32(gtx.Dp(unit.Dp(float32(sz))))
return logo.Layout(gtx)
})
}
func layoutAvatar(gtx C, c *catshadow.Client, nickname string) D {
return layout.Center.Layout(gtx, func(gtx C) D {
cc := clipCircle{}
return cc.Layout(gtx, func(gtx C) D {
sz := image.Point{X: gtx.Dp(42), Y: gtx.Dp(42)}
gtx.Constraints = layout.Exact(gtx.Constraints.Constrain(sz))
if w, ok := avatars[nickname]; ok {
return w(gtx)
} else {
if b, err := c.GetBlob("avatar://" + nickname); err == nil {
if m, _, err := image.Decode(bytes.NewReader(b)); err == nil {
w = func(gtx C) D {
return widget.Image{Fit: widget.Contain, Src: paint.NewImageOp(m)}.Layout(gtx)
}
}
} else {
co := Contactal{SharedSecret: nickname}
i := co.Render(sz)
b := &bytes.Buffer{}
if err := png.Encode(b, i); err == nil {
c.AddBlob("avatar://"+nickname, b.Bytes())
}
w = func(gtx C) D {
return widget.Image{Fit: widget.Contain, Src: paint.NewImageOp(i)}.Layout(gtx)
}
}
avatars[nickname] = w
return w(gtx)
}
})
})
}
// ChooseContactClick is the event that indicates which contact was selected
type ChooseContactClick struct {
nickname string
}
// Connect is the event that indicates client online mode is requested
type OnlineClick struct {
}
// OfflineClick is the event that indicates client offline mode is requested
type OfflineClick struct {
Err error
}
// Event returns a ChooseContactClick event when a contact is chosen
func (p *HomePage) Event(gtx layout.Context) interface{} {
if p.connect.Clicked(gtx) {
if !isConnected && !isConnecting {
return OnlineClick{}
}
return OfflineClick{}
}
// listen for pointer right click events on the addContact widget
if p.addContact.Clicked(gtx) {
return AddContactClick{}
}
if p.showSettings.Clicked(gtx) {
return ShowSettingsClick{}
}
for nickname, click := range p.contactClicks {
if e, ok := click.Update(gtx.Source); ok {
if e.Kind == gesture.KindClick {
return ChooseContactClick{nickname: nickname}
}
}
}
// check for keypress events
if e, ok := shortcutEvents(gtx); ok {
if e.Name == key.NameF2 {
return AddContactClick{}
}
if e.Name == key.NameF3 {
return ShowSettingsClick{}
}
if e.Name == key.NameF4 {
if !isConnected {
return OnlineClick{}
}
return OfflineClick{}
}
if e.Name == key.NameUpArrow {
selectedIdx = selectedIdx - 1
}
if e.Name == key.NameDownArrow {
selectedIdx = selectedIdx + 1
}
if e.Name == key.NameReturn {
p.l.Lock()
defer p.l.Unlock()
if len(p.contacts) < selectedIdx+1 {
return nil
}
return ChooseContactClick{nickname: p.contacts[selectedIdx].Nickname}
}
}
return nil
}
func (p *HomePage) Start(stop <-chan struct{}) {
// receive commands to update the contact list, e.g. from KeyExchangeCompleted events
go func() {
for {
select {
case <-stop:
return
case <-p.updateCh:
p.UpdateContacts()
}
}
}()
}
func (h *HomePage) UpdateContacts() {
h.l.Lock()
defer h.l.Unlock()
if h.a == nil {
return
}
if h.a.c == nil {
return
}
contacts := make(sortedContacts, 0)
// GetContacts() returns map[string]*Contact
for _, contact := range h.a.c.GetContacts() {
contacts = append(contacts, contact)
}
sort.Sort(contacts)
h.contacts = contacts
h.a.w.Invalidate()
}
func newHomePage(a *App) *HomePage {
updateCh := make(chan interface{}, 1)
updateCh <- struct{}{} // prod page worker to fetch contacts from catshadow
return &HomePage{
a: a,
l: new(sync.Mutex),
updateCh: updateCh,
contacts: []*catshadow.Contact{},
addContact: &widget.Clickable{},
connect: &widget.Clickable{},
showSettings: &widget.Clickable{},
contactClicks: make(map[string]*gesture.Click),
av: make(map[string]*widget.Image),
}
}
func ContactStyle(th *material.Theme, txt string) material.LabelStyle {
l := material.Label(th, th.TextSize, txt)
l.Font.Weight = font.Bold
return l
}