-
Notifications
You must be signed in to change notification settings - Fork 6
/
reply.go
275 lines (230 loc) · 5.83 KB
/
reply.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
package message
import (
"context"
"fmt"
"html"
"github.com/diamondburned/gotk4/pkg/core/glib"
"github.com/diamondburned/gotk4/pkg/gtk/v4"
"github.com/diamondburned/gotk4/pkg/pango"
"github.com/diamondburned/gotkit/app/locale"
"github.com/diamondburned/gotkit/gtkutil"
"github.com/diamondburned/gotkit/gtkutil/cssutil"
"github.com/diamondburned/gotkit/gtkutil/textutil"
"github.com/diamondburned/gotktrix/internal/app/messageview/message/mauthor"
"github.com/diamondburned/gotktrix/internal/gotktrix"
"github.com/diamondburned/gotrix/event"
"github.com/diamondburned/gotrix/matrix"
)
type Reply struct {
*gtk.Box
ctx context.Context
view MessageViewer
box struct {
header *gtk.Box
info *gtk.Label
content *gtk.Label
}
event event.RoomEvent
replyID matrix.EventID
roomID matrix.RoomID
mentionURL string
link bool
done bool
}
var replyInfoCSS = cssutil.Applier("message-reply-info", `
.message-reply-info {
color: alpha(@theme_fg_color, 0.85);
}
`)
var replyContentCSS = cssutil.Applier("message-reply-content", `
.message-reply-content {
caret-color: transparent;
}
`)
var replyCSS = cssutil.Applier("message-reply", `
.message-reply {
margin-bottom: 2px;
border-left: 3px solid alpha(@theme_fg_color, 0.5);
padding: 0 5px;
}
`)
// NewReply creates a new Reply widget.
func NewReply(ctx context.Context, v MessageViewer, roomID matrix.RoomID, eventID matrix.EventID) *Reply {
r := Reply{
ctx: ctx,
view: v,
replyID: eventID,
roomID: roomID,
mentionURL: mentionURL(roomID, eventID),
}
r.box.info = gtk.NewLabel(locale.S(ctx, "In reply to "))
replyInfoCSS(r.box.info)
r.box.header = gtk.NewBox(gtk.OrientationHorizontal, 0)
r.box.header.Append(r.box.info)
r.box.content = gtk.NewLabel("")
r.box.content.SetXAlign(0)
r.box.content.SetEllipsize(pango.EllipsizeEnd)
r.box.content.SetSingleLineMode(true)
r.box.content.SetSelectable(true)
replyContentCSS(r.box.content)
r.Box = gtk.NewBox(gtk.OrientationVertical, 0)
r.Box.Append(r.box.header)
r.Box.Append(r.box.content)
replyCSS(r.Box)
return &r
}
// MentionURL returns the URL to matrix.to for the message that this is replying
// to.
func (r *Reply) MentionURL() string {
return r.mentionURL
}
func mentionURL(roomID matrix.RoomID, replyID matrix.EventID) string {
return fmt.Sprintf(`https://matrix.to/#/%s/%s`, roomID, replyID)
}
// ShowContent opens a new Popover with the message content.
func (r *Reply) ShowContent() {
if r.event == nil {
return
}
msg := NewCozyMessage(r.ctx, r.view, r.event, nil)
msg.LoadMore()
gtk.BaseWidget(msg).SetVAlign(gtk.AlignStart)
view := gtk.NewViewport(nil, nil)
view.SetChild(msg)
scroll := gtk.NewScrolledWindow()
scroll.SetPolicy(gtk.PolicyNever, gtk.PolicyAutomatic)
scroll.SetMinContentWidth(250)
scroll.SetMaxContentWidth(650)
scroll.SetMinContentHeight(10)
scroll.SetMaxContentHeight(600)
scroll.SetPropagateNaturalWidth(true)
scroll.SetPropagateNaturalHeight(true)
scroll.SetChild(view)
p := gtk.NewPopover()
p.SetParent(r.box.info)
p.SetPosition(gtk.PosTop)
p.SetChild(scroll)
p.Popup()
p.ConnectHide(func() {
glib.TimeoutSecondsAdd(2, p.Unparent)
})
}
// InvalidateContent invalidates the Reply's content. For now, this function
// does nothing after being called more than once.
func (r *Reply) InvalidateContent() {
if r.done {
return
}
r.done = true
gtkutil.Async(r.ctx, func() func() {
client := gotktrix.FromContext(r.ctx)
ev, err := client.RoomTimelineEvent(r.roomID, r.replyID)
if err != nil {
return func() {
r.useError(err)
r.done = false
}
}
return func() {
author := mauthor.NewChip(r.ctx, r.roomID, ev.RoomInfo().Sender)
author.Unpad()
r.box.header.Append(author)
r.event = ev
r.bindLink()
message, ok := ev.(*event.RoomMessageEvent)
if !ok {
r.setContent(RenderEvent(r.ctx, ev))
} else {
// TODO: handle message.FormattedBody.
r.setContent(message.Body)
}
}
})
}
func (r *Reply) bindLink() {
if r.link {
return
}
r.link = true
r.box.info.SetMarkup(fmt.Sprintf(
`<a href="%s">%s</a>`,
html.EscapeString(r.mentionURL), locale.S(r.ctx, "In reply to "),
))
r.box.info.ConnectActivateLink(func(link string) bool {
if link == r.mentionURL {
if !r.view.ScrollTo(r.replyID) {
r.ShowContent()
}
return true
}
return false
})
}
func (r *Reply) useError(err error) {
r.box.content.SetMarkup(textutil.ErrorMarkup(err.Error()))
}
func (r *Reply) setContent(content string) {
r.box.content.SetText(content)
}
/*
type replyWalker struct {
*Reply
state *renderState
reply bool
render bool
content bool
}
func (r *replyWalker) isReplyURL(url string) bool {
return strings.Contains(url,
fmt.Sprintf("https://matrix.to/%s/%s", r.RoomID, r.ReplyID),
)
}
func (r *replyWalker) walkChildren(n *html.Node) traverseStatus {
for n := n.FirstChild; n != nil; n = n.NextSibling {
switch r.walkNode(n) {
case traverseOK:
// traverseChildren never returns traverseSkipChildren.
if r.walkChildren(n) == traverseFailed {
return traverseFailed
}
case traverseSkipChildren:
continue
case traverseFailed:
return traverseFailed
}
}
return traverseOK
}
func (r *replyWalker) walkNode(n *html.Node) traverseStatus {
switch n.Type {
case html.ElementNode:
switch n.Data {
case "mx-reply":
r.reply = true
r.walkChildren(n)
r.reply = false
return traverseSkipChildren
case "blockquote":
r.render = true
r.walkChildren(n)
r.render = false
return traverseSkipChildren
case "a":
if !r.render || !r.reply || !r.isReplyURL(nodeAttr(n, "href")) {
return traverseOK
}
r.useNodes(n)
return traverseSkipChildren
}
}
return traverseOK
}
func (r *replyWalker) useNodes(from *html.Node) {
parent := *from.Parent
parent.FirstChild = from
w, ok := renderHTMLNode(r.ctx, r.RoomID, &parent)
if ok {
r.setContent(w)
}
}
*/