-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconversation.go
414 lines (386 loc) · 12.8 KB
/
conversation.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
package main
import (
"github.com/hako/durafmt"
"github.com/katzenpost/katzenpost/catshadow"
"golang.org/x/exp/shiny/materialdesign/icons"
"image"
"io"
"runtime"
"strings"
"time"
"gioui.org/gesture"
"gioui.org/io/clipboard"
"gioui.org/io/event"
"gioui.org/io/key"
"gioui.org/io/pointer"
"gioui.org/io/transfer"
"gioui.org/layout"
"gioui.org/op/clip"
"gioui.org/unit"
"gioui.org/widget"
"gioui.org/widget/material"
)
var (
messageList = &layout.List{Axis: layout.Vertical, ScrollToEnd: true}
messageField = &widget.Editor{SingleLine: true}
backIcon, _ = widget.NewIcon(icons.NavigationChevronLeft)
sendIcon, _ = widget.NewIcon(icons.NavigationChevronRight)
queuedIcon, _ = widget.NewIcon(icons.NotificationSync)
sentIcon, _ = widget.NewIcon(icons.ActionDone)
deliveredIcon, _ = widget.NewIcon(icons.ActionDoneAll)
pandaIcon, _ = widget.NewIcon(icons.ActionPets)
)
type conversationPage struct {
a *App
nickname string
avatar *widget.Image
edit *gesture.Click
compose *widget.Editor
send *widget.Clickable
back *widget.Clickable
cancel *gesture.Click
msgcopy *widget.Clickable
msgpaste *LongPress
msgdetails *widget.Clickable
messageClicked *catshadow.Message
messageClicks map[*catshadow.Message]*gesture.Click
}
func (c *conversationPage) Start(stop <-chan struct{}) {
}
type MessageSent struct {
nickname string
msgId catshadow.MessageID
}
type EditContact struct {
nickname string
}
func (c *conversationPage) Event(gtx layout.Context) interface{} {
// check for editor SubmitEvents
if e, ok := c.compose.Update(gtx); ok {
switch e.(type) {
case widget.SubmitEvent:
c.send.Click()
case widget.ChangeEvent:
}
}
// catch clicks on send button, update list view position to bottom
if c.send.Clicked(gtx) {
messageList.ScrollToEnd = true
// XXX: could do this in Layout where we know the # of messages
messageList.ScrollTo(0x1 << 32)
msg := []byte(c.compose.Text())
c.compose.SetText("")
if len(msg) == 0 {
return nil
}
// truncate messages
// TODO: this should split messages and return the set of message IDs sent
if len(msg)+4 > c.a.c.DoubleRatchetPayloadLength() {
msg = msg[:c.a.c.DoubleRatchetPayloadLength()-4]
}
msgId := c.a.c.SendMessage(c.nickname, msg)
return MessageSent{nickname: c.nickname, msgId: msgId}
}
// check for long press
if e, ok := c.msgpaste.Update(gtx); ok {
if e.Type == LongPressed {
gtx.Source.Execute(clipboard.ReadCmd{Tag: c.msgpaste})
} else { // LongPressCancelled
gtx.Execute(key.FocusCmd{Tag: c.compose})
}
}
// catch clipboard transfer triggered by long press and update composition
if ev, ok := gtx.Event(transfer.TargetFilter{Target: c.msgpaste, Type: "application/text"}); ok {
switch e := ev.(type) {
case transfer.DataEvent:
f := e.Open()
defer f.Close()
if b, err := io.ReadAll(f); err == nil {
if c.compose.SelectionLen() > 0 {
c.compose.Delete(1) // deletes the selection as a single rune
}
start, _ := c.compose.Selection()
txt := c.compose.Text()
c.compose.SetText(txt[:start] + string(b) + txt[start:])
gtx.Execute(key.FocusCmd{Tag: c.compose})
}
}
}
if e, ok := c.edit.Update(gtx.Source); ok {
if e.Kind == gesture.KindClick {
return EditContact{nickname: c.nickname}
}
}
if c.back.Clicked(gtx) {
return BackEvent{}
}
if c.msgcopy.Clicked(gtx) {
gtx.Source.Execute(clipboard.WriteCmd{
Data: io.NopCloser(strings.NewReader(string(c.messageClicked.Plaintext))),
})
c.messageClicked = nil
}
if c.msgdetails.Clicked(gtx) {
c.messageClicked = nil // not implemented
}
for msg, click := range c.messageClicks {
if _, ok := click.Update(gtx.Source); ok {
c.messageClicked = msg
}
}
if _, ok := c.cancel.Update(gtx.Source); ok {
c.messageClicked = nil
}
/* XXX: doesn't seem to work yet
if e, ok := readMenuKeys(c, gtx); ok {
if e.Name == key.NameEscape && e.State == key.Release {
return BackEvent{}
}
if e.Name == key.NameF5 && e.State == key.Release {
return EditContact{nickname: c.nickname}
}
if e.Name == key.NameUpArrow && e.State == key.Release {
messageList.ScrollToEnd = false
if messageList.Position.First > 0 {
messageList.Position.First = messageList.Position.First - 1
}
}
if e.Name == key.NameDownArrow && e.State == key.Release {
messageList.ScrollToEnd = true
messageList.Position.First = messageList.Position.First + 1
}
if e.Name == key.NamePageUp && e.State == key.Release {
messageList.ScrollToEnd = false
if messageList.Position.First-messageList.Position.Count > 0 {
messageList.Position.First = messageList.Position.First - messageList.Position.Count
}
}
if e.Name == key.NamePageDown && e.State == key.Release {
messageList.ScrollToEnd = true
messageList.Position.First = messageList.Position.First + messageList.Position.Count
}
return RedrawEvent{}
}
*/
return nil
}
func layoutMessage(gtx C, msg *catshadow.Message, isSelected bool, expires time.Duration) D {
var statusIcon *widget.Icon
if msg.Outbound == true {
statusIcon = queuedIcon
switch {
case !msg.Sent:
statusIcon = queuedIcon
case msg.Sent && !msg.Delivered:
statusIcon = sentIcon
case msg.Delivered:
statusIcon = deliveredIcon
default:
}
}
return layout.Flex{Axis: layout.Vertical, Alignment: layout.End, Spacing: layout.SpaceBetween}.Layout(gtx,
layout.Rigid(material.Body1(th, string(msg.Plaintext)).Layout),
layout.Rigid(func(gtx C) D {
in := layout.Inset{Top: unit.Dp(8), Bottom: unit.Dp(0), Left: unit.Dp(8), Right: unit.Dp(8)}
return in.Layout(gtx, func(gtx C) D {
timeLabel := strings.Replace(durafmt.ParseShort(time.Now().Round(0).Sub(msg.Timestamp).Truncate(time.Minute)).Format(units), "0 s", "now", 1)
var whenExpires string
if expires == 0 {
whenExpires = ""
} else {
whenExpires = durafmt.ParseShort(msg.Timestamp.Add(expires).Sub(time.Now().Round(0).Truncate(time.Minute))).Format(units) + " remaining"
}
if isSelected {
timeLabel = msg.Timestamp.Truncate(time.Minute).Format(time.RFC822)
if msg.Outbound {
timeLabel = "Sent: " + timeLabel
} else {
timeLabel = "Received: " + timeLabel
}
}
if msg.Outbound {
return layout.Flex{Axis: layout.Horizontal, Alignment: layout.End, Spacing: layout.SpaceBetween}.Layout(gtx,
layout.Rigid(material.Caption(th, timeLabel).Layout),
layout.Rigid(material.Caption(th, whenExpires).Layout),
layout.Rigid(func(gtx C) D {
return statusIcon.Layout(gtx, th.Palette.ContrastFg)
}),
)
// do not show delivery status for received messages, instead show received timestamp
} else {
return layout.Flex{Axis: layout.Horizontal, Alignment: layout.End, Spacing: layout.SpaceBetween}.Layout(gtx,
layout.Rigid(material.Caption(th, timeLabel).Layout),
layout.Rigid(material.Caption(th, whenExpires).Layout),
)
}
})
}),
)
}
func (c *conversationPage) Layout(gtx layout.Context) layout.Dimensions {
// set focus on composition
gtx.Execute(key.FocusCmd{Tag: c.compose})
contact := c.a.c.GetContacts()[c.nickname]
if n, ok := notifications[c.nickname]; ok {
n.Cancel()
delete(notifications, c.nickname)
}
messages := c.a.c.GetSortedConversation(c.nickname)
expires, _ := c.a.c.GetExpiration(c.nickname)
bgl := Background{
Color: th.Bg,
Inset: layout.Inset{Top: unit.Dp(0), Bottom: unit.Dp(0), Left: unit.Dp(0), Right: unit.Dp(0)},
}
return layout.Flex{Axis: layout.Vertical, Spacing: layout.SpaceBetween, Alignment: layout.Middle}.Layout(gtx,
layout.Rigid(func(gtx C) D {
return bgl.Layout(gtx, func(gtx C) D {
return layout.Flex{Axis: layout.Horizontal, Spacing: layout.SpaceBetween, Alignment: layout.Middle}.Layout(gtx,
layout.Rigid(button(th, c.back, backIcon).Layout),
layout.Rigid(func(gtx C) D {
dims := layoutAvatar(gtx, c.a.c, c.nickname)
a := clip.Rect(image.Rectangle{Max: dims.Size})
t := a.Push(gtx.Ops)
c.edit.Add(gtx.Ops)
t.Pop()
return dims
}),
layout.Rigid(material.Caption(th, c.nickname).Layout),
layout.Rigid(func(gtx C) D {
if contact.IsPending {
return pandaIcon.Layout(gtx, th.Palette.ContrastFg)
}
return layout.Dimensions{}
}),
layout.Flexed(1, fill{th.Bg}.Layout),
)
},
)
}),
layout.Flexed(2, func(gtx C) D {
return bgl.Layout(gtx, func(ctx C) D {
if len(messages) == 0 {
return fill{th.Bg}.Layout(ctx)
}
dims := messageList.Layout(gtx, len(messages), func(gtx C, i int) layout.Dimensions {
if _, ok := c.messageClicks[messages[i]]; !ok {
c.messageClicks[messages[i]] = new(gesture.Click)
}
bgSender := Background{
Color: th.ContrastBg,
Inset: layout.Inset{Top: unit.Dp(8), Bottom: unit.Dp(8), Left: unit.Dp(8), Right: unit.Dp(12)},
Radius: unit.Dp(10),
}
bgReceiver := Background{
Color: th.ContrastFg,
Inset: layout.Inset{Top: unit.Dp(8), Bottom: unit.Dp(8), Left: unit.Dp(12), Right: unit.Dp(8)},
Radius: unit.Dp(10),
}
inbetween := layout.Inset{Top: unit.Dp(2)}
if i > 0 {
if messages[i-1].Outbound != messages[i].Outbound {
inbetween = layout.Inset{Top: unit.Dp(8)}
}
}
var dims D
isSelected := messages[i] == c.messageClicked
if messages[i].Outbound {
dims = layout.Flex{Axis: layout.Horizontal, Alignment: layout.Baseline, Spacing: layout.SpaceAround}.Layout(gtx,
layout.Flexed(1, fill{th.Bg}.Layout),
layout.Flexed(5, func(gtx C) D {
return inbetween.Layout(gtx, func(gtx C) D {
return bgSender.Layout(gtx, func(gtx C) D {
return layoutMessage(gtx, messages[i], isSelected, expires)
})
})
}),
)
} else {
dims = layout.Flex{Axis: layout.Horizontal, Alignment: layout.Baseline, Spacing: layout.SpaceAround}.Layout(gtx,
layout.Flexed(5, func(gtx C) D {
return inbetween.Layout(gtx, func(gtx C) D {
return bgReceiver.Layout(gtx, func(gtx C) D {
return layoutMessage(gtx, messages[i], isSelected, expires)
})
})
}),
layout.Flexed(1, fill{th.Bg}.Layout),
)
}
a := clip.Rect(image.Rectangle{Max: dims.Size})
t := a.Push(gtx.Ops)
c.messageClicks[messages[i]].Add(gtx.Ops)
t.Pop()
return dims
})
if c.messageClicked != nil {
a := clip.Rect(image.Rectangle{Max: dims.Size})
t := a.Push(gtx.Ops)
c.cancel.Add(gtx.Ops)
t.Pop()
}
return dims
})
}),
layout.Rigid(func(gtx C) D {
bg := Background{
Color: th.ContrastBg,
Inset: layout.Inset{Top: unit.Dp(8), Bottom: unit.Dp(0), Left: unit.Dp(12), Right: unit.Dp(12)},
}
// return the menu laid out for message actions
if c.messageClicked != nil {
return bg.Layout(gtx, func(gtx C) D {
return layout.Flex{Axis: layout.Horizontal, Spacing: layout.SpaceBetween, Alignment: layout.Baseline}.Layout(gtx,
layout.Rigid(material.Button(th, c.msgcopy, "copy").Layout),
layout.Flexed(1, fill{th.Bg}.Layout),
layout.Rigid(material.Button(th, c.msgdetails, "details").Layout),
)
})
}
bgSender := Background{
Color: th.ContrastBg,
Inset: layout.Inset{Top: unit.Dp(8), Bottom: unit.Dp(8), Left: unit.Dp(12), Right: unit.Dp(12)},
Radius: unit.Dp(10),
}
bgl := Background{
Color: th.Bg,
Inset: layout.Inset{Top: unit.Dp(8), Bottom: unit.Dp(0), Left: unit.Dp(0), Right: unit.Dp(0)},
}
return bgl.Layout(gtx, func(gtx C) D {
return layout.Flex{Axis: layout.Horizontal, Spacing: layout.SpaceBetween, Alignment: layout.Middle}.Layout(gtx,
layout.Flexed(1, fill{th.Bg}.Layout),
layout.Flexed(5, func(gtx C) D {
dims := bgSender.Layout(gtx, material.Editor(th, c.compose, "").Layout)
t := pointer.PassOp{}.Push(gtx.Ops)
defer t.Pop()
a := clip.Rect(image.Rectangle{Max: dims.Size})
x := a.Push(gtx.Ops)
defer x.Pop()
event.Op(gtx.Ops, c.msgpaste)
return dims
}),
layout.Rigid(func(gtx C) D {
return layout.Inset{Left: unit.Dp(8), Right: unit.Dp(8)}.Layout(gtx, button(th, c.send, sendIcon).Layout)
}),
)
})
}),
)
}
func newConversationPage(a *App, nickname string) *conversationPage {
ed := &widget.Editor{SingleLine: false, Submit: true}
if runtime.GOOS == "android" {
ed.Submit = false
}
p := &conversationPage{a: a, nickname: nickname,
compose: ed,
messageClicks: make(map[*catshadow.Message]*gesture.Click),
back: &widget.Clickable{},
msgcopy: &widget.Clickable{},
msgpaste: NewLongPress(a.w.Invalidate, 800*time.Millisecond),
msgdetails: &widget.Clickable{},
cancel: new(gesture.Click),
send: &widget.Clickable{},
edit: new(gesture.Click),
}
return p
}