Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
diamondburned
GitHub Repository: diamondburned/gtkcord4
Path: blob/main/internal/messages/prefs.go
366 views
1
package messages
2
3
import (
4
"context"
5
"fmt"
6
7
"github.com/diamondburned/gotk4/pkg/gtk/v4"
8
"github.com/diamondburned/gotkit/app"
9
"github.com/diamondburned/gotkit/app/locale"
10
"github.com/diamondburned/gotkit/app/prefs"
11
"github.com/diamondburned/gotkit/gtkutil"
12
"github.com/diamondburned/gotkit/gtkutil/cssutil"
13
"github.com/pkg/errors"
14
"libdb.so/dissent/internal/gtkcord"
15
)
16
17
var askBeforeDelete = prefs.NewBool(true, prefs.PropMeta{
18
Name: "Ask Before Deleting",
19
Section: "Messages",
20
Description: "Ask before deleting a message.",
21
})
22
23
var showBlockedMessages = prefs.NewBool(false, prefs.PropMeta{
24
Name: "Show Blocked Messages",
25
Section: "Messages",
26
Description: "Show messages from blocked users as dimmed instead of completely hidden.",
27
})
28
29
var messagesWidth = prefs.NewInt(800, prefs.IntMeta{
30
Name: "Messages Width",
31
Section: "Messages",
32
Description: "The width of the messages column.",
33
Min: 400,
34
Max: 12000,
35
})
36
37
var maxEmbedWidth = prefs.NewInt(gtkcord.EmbedMaxWidth, prefs.IntMeta{
38
Name: "Max Embed Width",
39
Section: "Messages",
40
Description: "The maximum width of an embed or image. May impact minimum window width.",
41
Min: 100,
42
Max: 2000,
43
})
44
45
var maxImageHeight = prefs.NewInt(gtkcord.EmbedImgHeight, prefs.IntMeta{
46
Name: "Max Image Height",
47
Section: "Messages",
48
Description: "The maximum height of an image.",
49
Min: 100,
50
Max: 2000,
51
})
52
53
var redactMessages = prefs.NewBool(false, prefs.PropMeta{
54
Name: "Redact Messages",
55
Section: "Messages",
56
Description: "Redact instead of deleting messages. This will replace the message content with a redacted message.",
57
})
58
59
var showSummaries = prefs.NewBool(true, prefs.PropMeta{
60
Name: "Show Summaries",
61
Section: "Messages",
62
Description: "Show message summaries as they come.",
63
})
64
65
func init() {
66
prefs.RegisterProp((*blockedUsersPrefs)(nil))
67
prefs.Order((*blockedUsersPrefs)(nil), showBlockedMessages)
68
}
69
70
type collapsedMessageTimestampStyle string
71
72
const (
73
hiddenTimestampStyle collapsedMessageTimestampStyle = "Hidden"
74
// relativeTimestampStyle collapsedMessageTimestampStyle = "Relative" // e.g. "+2s"
75
compactTimestampStyle collapsedMessageTimestampStyle = "Timestamp" // e.g. "21:16"
76
)
77
78
// defaultCollapsedMessageTimestampStyle is the default style of the timestamp
79
// for compacted messages.
80
const defaultCollapsedMessageTimestampStyle = hiddenTimestampStyle
81
82
var collapsedMessageTimestamp = prefs.NewEnumList(
83
defaultCollapsedMessageTimestampStyle,
84
prefs.EnumListMeta[collapsedMessageTimestampStyle]{
85
PropMeta: prefs.PropMeta{
86
Name: "Collapsed Message Timestamp",
87
Section: "Messages",
88
Description: "The style of the timestamp in compacted messages. " +
89
"Timestamp will show the time only, while Relative will show the time relative to the last message.",
90
},
91
Options: []collapsedMessageTimestampStyle{
92
hiddenTimestampStyle,
93
// relativeTimestampStyle,
94
compactTimestampStyle,
95
},
96
},
97
)
98
99
var _ = cssutil.WriteCSS(`
100
.message-blockedusers-expander {
101
margin-top: 4px;
102
}
103
.message-blockedusers-expander expander {
104
min-width: 16px;
105
min-height: 16px;
106
padding: 4px;
107
}
108
.message-blockedusers {
109
font-size: 0.95em;
110
margin-left: 24px;
111
}
112
.message-blockedusers > *:not(:first-child) {
113
margin-top: 4px;
114
}
115
.message-blockedusers button {
116
padding: 4px 8px;
117
}
118
`)
119
120
type blockedUsersPrefs struct{}
121
122
func (*blockedUsersPrefs) MarshalJSON() ([]byte, error) {
123
return []byte("null"), nil
124
}
125
126
func (*blockedUsersPrefs) UnmarshalJSON(b []byte) error {
127
if string(b) != "null" {
128
return fmt.Errorf("unexpected %q, expecting null", b)
129
}
130
return nil
131
}
132
133
func (*blockedUsersPrefs) Meta() prefs.PropMeta {
134
return prefs.PropMeta{
135
Name: "Blocked Users",
136
Section: "Messages",
137
Description: "List of users whose messages you won't see.",
138
}
139
}
140
141
// Pubsubber panics. Do not call this method.
142
func (*blockedUsersPrefs) Pubsubber() *prefs.Pubsub {
143
panic("BUG: accidental call to Pubsubber")
144
}
145
146
func (*blockedUsersPrefs) CreateWidget(ctx context.Context, _ func()) gtk.Widgetter {
147
state := gtkcord.FromContext(ctx)
148
if state == nil {
149
panic("BUG: context not passed down properly to prefui")
150
}
151
152
var blockedList *gtk.Box
153
154
expander := gtk.NewExpander(locale.Get("Show"))
155
expander.AddCSSClass("message-blockedusers-expander")
156
expander.SetResizeToplevel(true)
157
expander.SetExpanded(false)
158
expander.NotifyProperty("expanded", func() {
159
if !expander.Expanded() {
160
expander.SetLabel(locale.Get("Show"))
161
expander.SetChild(nil)
162
blockedList = nil
163
return
164
}
165
166
blockedList = gtk.NewBox(gtk.OrientationVertical, 0)
167
blockedList.AddCSSClass("message-blockedusers")
168
169
for _, userID := range state.RelationshipState.BlockedUserIDs() {
170
tag := userID.Mention()
171
172
presence, _ := state.Presence(0, userID)
173
if presence != nil {
174
tag = presence.User.Tag()
175
}
176
177
unblock := gtk.NewButtonWithLabel(locale.Get("Unblock"))
178
name := gtk.NewLabel(tag)
179
name.SetHExpand(true)
180
name.SetXAlign(0)
181
name.SetSelectable(true)
182
183
box := gtk.NewBox(gtk.OrientationHorizontal, 0)
184
box.Append(name)
185
box.Append(unblock)
186
187
blockedList.Append(box)
188
189
unblock.ConnectClicked(func() {
190
// Ensure that the user is still blocked.
191
if !state.RelationshipState.IsBlocked(userID) {
192
return
193
}
194
195
box.SetSensitive(false)
196
gtkutil.Async(ctx, func() func() {
197
err := state.DeleteRelationship(userID)
198
199
return func() {
200
if err != nil {
201
box.SetSensitive(true)
202
app.Error(ctx, errors.Wrapf(err,
203
"cannot unblock user %s (%s)",
204
tag, userID.Mention(),
205
))
206
} else {
207
blockedList.Remove(box)
208
}
209
}
210
})
211
})
212
}
213
214
expander.SetLabel(locale.Get("Hide"))
215
expander.SetChild(blockedList)
216
})
217
218
return expander
219
}
220
221
func (*blockedUsersPrefs) WidgetIsLarge() bool {
222
return true
223
}
224
225