-
Notifications
You must be signed in to change notification settings - Fork 7
/
mauthor.go
282 lines (235 loc) · 6.21 KB
/
mauthor.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
// Package mauthor handles rendering Matrix room members' names.
package mauthor
import (
"fmt"
"html"
"strings"
"github.com/chanbakjsd/gotrix/event"
"github.com/chanbakjsd/gotrix/matrix"
"github.com/diamondburned/gotk4/pkg/gtk/v4"
"github.com/diamondburned/gotkit/gtkutil/textutil"
"github.com/diamondburned/gotktrix/internal/gotktrix"
"github.com/diamondburned/gotktrix/internal/gotktrix/events/m"
"github.com/diamondburned/gotktrix/internal/gotktrix/events/pronouns"
)
type markupOpts struct {
textTag textutil.TextTag
hasher ColorHasher
name string
at bool
shade bool
minimal bool
}
// MarkupMod is a function type that Markup can take multiples of. It
// changes subtle behaviors of the Markup function, such as the color hasher
// used.
type MarkupMod func(opts *markupOpts)
// WithMinimal renders the markup without additional information, such as
// pronouns.
func WithMinimal() MarkupMod {
return func(opts *markupOpts) {
opts.minimal = true
}
}
// WithShade renders the markup with a background shade.
func WithShade() MarkupMod {
return func(opts *markupOpts) {
opts.shade = true
}
}
// WithColorHasher uses the given color hasher.
func WithColorHasher(hasher ColorHasher) MarkupMod {
return func(opts *markupOpts) {
opts.hasher = hasher
}
}
// WithMention makes the renderer prefix an at ("@") symbol.
func WithMention() MarkupMod {
return func(opts *markupOpts) {
opts.at = true
}
}
// WithWidgetColor determines the best hasher from the given widget. The caller
// should beware to call this function in the main thread to not cause a race
// condition.
func WithWidgetColor() MarkupMod {
if textutil.IsDarkTheme() {
return WithColorHasher(LightColorHasher)
} else {
return WithColorHasher(DarkColorHasher)
}
}
// WithTextTagAttr sets the given attribute into the text tag used for the
// author. It is only useful for Text.
func WithTextTagAttr(attr textutil.TextTag) MarkupMod {
return func(opts *markupOpts) {
opts.textTag = attr
}
}
// WithName overrides the name in the generated author string.
func WithName(name string) MarkupMod {
return func(opts *markupOpts) {
opts.name = name
}
}
func mkopts(mods []MarkupMod) markupOpts {
opts := markupOpts{
hasher: DefaultColorHasher(),
}
for _, mod := range mods {
mod(&opts)
}
return opts
}
// UserColor returns the color in hexadecimal with the # prefix.
func UserColor(uID matrix.UserID, mods ...MarkupMod) string {
return userColor(uID, mkopts(mods))
}
func userColor(uID matrix.UserID, opts markupOpts) string {
name, _, err := uID.Parse()
if err != nil {
name = string(uID)
}
return RGBHex(opts.hasher.Hash(name))
}
// Name returns the raw name string with no markup.
func Name(c *gotktrix.Client, rID matrix.RoomID, uID matrix.UserID, mods ...MarkupMod) string {
c = c.Offline()
opts := mkopts(mods)
name, _, _ := uID.Parse()
if name == "" {
name = string(uID)
}
if rID != "" {
n, err := c.MemberName(rID, uID, !opts.minimal)
if err == nil && n.Name != "" {
name = n.Name
}
}
if opts.name != "" {
name = opts.name
}
if opts.at && !strings.HasPrefix(name, "@") {
name = "@" + name
}
return name
}
// Markup renders the markup string for the given user inside the given room.
// The markup format follows the Pango markup format.
//
// If the given room ID string is empty, then certain information are skipped.
// If it's non-empty, then the state will be used to fetch additional
// information.
func Markup(c *gotktrix.Client, rID matrix.RoomID, uID matrix.UserID, mods ...MarkupMod) string {
// TODO: maybe bridge role colors?
c = c.Offline()
opts := mkopts(mods)
name, _, _ := uID.Parse()
if name == "" {
name = string(uID)
}
var ambiguous bool
if rID != "" {
n, err := c.MemberName(rID, uID, !opts.minimal)
if err == nil && n.Name != "" {
name = n.Name
ambiguous = n.Ambiguous
}
}
if opts.name != "" {
name = opts.name
}
if opts.at && !strings.HasPrefix(name, "@") {
name = "@" + name
}
b := strings.Builder{}
b.Grow(512)
if opts.shade {
b.WriteString(fmt.Sprintf(
`<span color="%s" bgcolor="%[1]s33">%s</span>`,
userColor(uID, opts), html.EscapeString(name),
))
} else {
b.WriteString(fmt.Sprintf(
`<span color="%s">%s</span>`,
userColor(uID, opts), html.EscapeString(name),
))
}
if opts.minimal {
return b.String()
}
if pronoun := pronouns.UserPronouns(c, rID, uID).Pronoun(); pronoun != "" {
b.WriteByte(' ')
b.WriteString(fmt.Sprintf(
`<span fgalpha="90%%" size="small">(%s)</span>`,
html.EscapeString(string(pronoun)),
))
}
if ev, _ := c.RoomState(rID, event.TypeRoomMember, string(uID)); ev != nil {
member := m.DiscordMemberFromMatrix(ev.(*event.RoomMemberEvent))
if member != nil {
b.WriteByte(' ')
b.WriteString(fmt.Sprintf(
`<span fgalpha="75%%" size="small">(%s)</span>`,
member.Username,
))
// We can show the username directly instead of the Matrix ID for
// Discord users.
ambiguous = false
}
}
if ambiguous {
b.WriteByte(' ')
b.WriteString(fmt.Sprintf(
`<span fgalpha="75%%" size="small">(%s)</span>`,
html.EscapeString(string(uID)),
))
}
return b.String()
}
// Text renders the author's name into a rich text buffer. Tje written string is
// always minimal. The inserted tags have the "_mauthor" prefix.
func Text(c *gotktrix.Client, iter *gtk.TextIter, rID matrix.RoomID, uID matrix.UserID, mods ...MarkupMod) {
opts := mkopts(mods)
name, _, _ := uID.Parse()
if name == "" {
name = string(uID)
}
if rID != "" {
n, err := c.MemberName(rID, uID, !opts.minimal)
if err == nil {
name = n.Name
}
}
if opts.name != "" {
name = opts.name
}
if opts.at && !strings.HasPrefix(name, "@") {
name = "@" + name
} else if name == "" {
return
}
start := iter.Offset()
buf := iter.Buffer()
buf.Insert(iter, name)
startIter := buf.IterAtOffset(start)
tags := buf.TagTable()
tag := tags.Lookup("_mauthor_" + string(uID))
if tag == nil {
color := userColor(uID, opts)
attrs := textutil.TextTag{
"foreground": color,
}
if opts.shade {
attrs["background"] = color + "33" // alpha
}
if opts.textTag != nil {
for k, v := range opts.textTag {
attrs[k] = v
}
}
tag = attrs.Tag("_mauthor_" + string(uID))
tags.Add(tag)
}
buf.ApplyTag(tag, startIter, iter)
}