This repository has been archived by the owner on Apr 5, 2022. It is now read-only.
/
gtkcord.go
425 lines (347 loc) · 10.1 KB
/
gtkcord.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
415
416
417
418
419
420
421
422
423
424
425
package gtkcord
import (
"math/rand"
"net/http"
"time"
_ "embed"
"github.com/diamondburned/arikawa/v2/api"
"github.com/diamondburned/arikawa/v2/discord"
"github.com/diamondburned/arikawa/v2/gateway"
"github.com/diamondburned/gotk4-handy/pkg/handy"
"github.com/diamondburned/gotk4/pkg/glib/v2"
"github.com/diamondburned/gotk4/pkg/gtk/v3"
"github.com/diamondburned/gtkcord3/gtkcord/components/channel"
"github.com/diamondburned/gtkcord3/gtkcord/components/greet"
"github.com/diamondburned/gtkcord3/gtkcord/components/guild"
"github.com/diamondburned/gtkcord3/gtkcord/components/hamburger"
"github.com/diamondburned/gtkcord3/gtkcord/components/header"
"github.com/diamondburned/gtkcord3/gtkcord/components/login"
"github.com/diamondburned/gtkcord3/gtkcord/components/logo"
"github.com/diamondburned/gtkcord3/gtkcord/components/message"
"github.com/diamondburned/gtkcord3/gtkcord/components/quickswitcher"
"github.com/diamondburned/gtkcord3/gtkcord/components/singlebox"
"github.com/diamondburned/gtkcord3/gtkcord/components/window"
"github.com/diamondburned/gtkcord3/gtkcord/gtkutils/gdbus"
"github.com/diamondburned/ningen/v2"
"github.com/diamondburned/gtkcord3/internal/keyring"
"github.com/diamondburned/gtkcord3/internal/log"
)
//go:embed style.css
var styleCSS string
func init() {
window.ApplicationCSS = styleCSS
}
var HTTPClient = http.Client{
Timeout: 10 * time.Second,
}
func discordSettings() {
discord.DefaultEmbedColor = 0x808080
api.UserAgent = "" +
"Mozilla/5.0 (X11; Linux x86_64) " +
"AppleWebKit/537.36 (KHTML, like Gecko) " +
"Chrome/79.0.3945.130 " +
"Safari/537.36"
gateway.DefaultIdentity = gateway.IdentifyProperties{
OS: "linux",
}
}
func init() {
rand.Seed(time.Now().UnixNano())
discordSettings()
}
const (
SpinnerSize = 56
ChannelWidth = 240
)
type Application struct {
*gtk.Application
Window *window.Container
Notifier *gdbus.Notifier
MPRIS *gdbus.MPRISWatcher
mprisState *mprisState
Plugins []*Plugin
State *ningen.State
// Preferences window, hidden by default
Settings *Settings
// Main Grid, left is always LeftGrid - *gtk.Grid
Main *handy.Leaflet // LeftGrid -- Right
Right *singlebox.Box // Stack of Messages or full screen server details TODO
// <grid> <item> <item>
// | Left | Right |
// | Left Grid | Messages |
// Left Grid
LeftGrid *gtk.Grid
leftCols [maxLeftGridColumn]gtk.Widgetter
// <item> <item>
// | Guilds | Channels
// Application states
Header *header.Header
Guilds *guild.Guilds
Privates *channel.PrivateChannels
Channels *channel.Channels
Messages *message.Messages
}
// New is not thread-safe.
func New(app *gtk.Application) *Application {
plugins, err := loadPlugins()
if err != nil {
log.Fatalln("Failed to load plugins:", err)
}
return &Application{
Application: app,
Plugins: plugins,
}
}
func (a *Application) Close() {
// Mark application as exited:
a.Application = nil
// Close session on exit:
if a.State != nil {
a.State.Close()
}
}
func (a *Application) Activate() {
// Register the dbus connection:
conn := a.DBusConnection()
a.Notifier = gdbus.NewNotifier(conn)
a.MPRIS = gdbus.NewMPRISWatcher(conn) // notify.go
a.mprisState = newMPRISState()
// Activate the window singleton:
if err := window.WithApplication(a.Application); err != nil {
log.Fatalln("Failed to initialize the window:", err)
}
a.Window = window.Window
// Set the window specs:
window.Resize(1000, 800)
window.SetTitle("gtkcord")
// Create the preferences/settings window, which applies settings as a side
// effect:
a.Settings = a.makeSettings()
}
func (a *Application) init() {
// Pre-make the leaflet but don't use it:
l := handy.NewLeaflet()
l.SetModeTransitionDuration(150)
l.SetTransitionType(handy.LeafletTransitionTypeSlide)
l.SetInterpolateSize(true)
l.SetCanSwipeBack(true)
l.Container.Show()
a.Main = l
l.Connect("notify::folded", func() {
// If we're not ready:
if a.State == nil {
return
}
// Avoid repeating:
folded := l.Folded()
// Fold the header too:
a.Header.Fold(folded)
// If folded, we expand those panels:
a.Channels.SetHExpand(folded)
a.Privates.SetHExpand(folded)
})
// Create a new Header:
a.Header = header.NewHeader()
}
func (a *Application) displayMain() {
window.Display(&a.Main.Container)
}
func (a *Application) Ready(s *ningen.State) error {
a.State = s
// When the websocket closes, the screen must be changed to a busy one. The
// websocket may close if it's disconnected unexpectedly.
s.Gateway.AfterClose = func(error) {
// Is the application already dead?
if a.Application == nil {
return
}
// Run this asynchronously. This guarantees that the UI thread would
// never be hardlocked.
glib.IdleAdd(func() { window.NowLoading() })
}
// Show the main screen once everything is resumed. See above NowLoading
// call.
s.AddHandler(func(c *ningen.Connected) {
glib.IdleAdd(func() { a.displayMain() })
})
// Store the token:
go func() {
keyring.Set(s.Token)
}()
// Set gateway error functions to our own:
s.Gateway.ErrorLog = func(err error) {
log.Errorln(err)
}
// Make the main widgets:
a.init()
window.SetHeader(a.Header.HeaderBar) // restore header post login.
window.Resize(1200, 900)
if ready := s.Ready(); ready.UserSettings != nil {
switch ready.UserSettings.Theme {
case "dark":
window.PreferDarkTheme(true)
case "light":
window.PreferDarkTheme(false)
}
}
// Bind the hamburger:
hamburger.BindToButton(a.Header.Hamburger.Button, hamburger.Opts{
State: s,
Settings: a.Settings.Show,
LogOut: a.LogOut,
})
// Bind stuff
a.bindActions()
a.bindNotifier()
// Guilds
a.Guilds = guild.NewGuilds(s)
a.Guilds.OnSelect = a.SwitchGuild
a.Guilds.DMButton.OnClick = a.SwitchDM
a.Channels = channel.NewChannels(s, func(ch *channel.Channel) {
a.SwitchChannel(ch)
a.FocusMessages()
})
a.Privates = channel.NewPrivateChannels(s, func(ch *channel.PrivateChannel) {
a.SwitchChannel(ch)
a.FocusMessages()
})
a.Messages = message.NewMessages(s, message.Opts{
InputZeroWidth: a.Settings.General.Behavior.ZeroWidth,
InputOnTyping: a.Settings.General.Behavior.OnTyping,
MessageWidth: a.Settings.General.Customization.MessageWidth,
})
greeter := greet.NewGreeter()
greeter.SetSurface(logo.Surface(greet.IconSize, 2))
a.Messages.SetPlaceholder(greeter)
// Bind OnClick to trigger below callback:
a.Header.Back.OnClick = func() {
a.Main.SetFocusChild(a.LeftGrid)
}
// Bind the channel menu button:
a.Header.ChMenuBtn.SetSpawner(func(p *gtk.Popover) gtk.Widgetter {
guID := a.Messages.GuildID()
chID := a.Messages.ChannelID()
if !chID.IsValid() || !guID.IsValid() {
// guarded, shouldn't happen.
return nil
}
return header.NewChMenuBody(p, s, guID, chID)
})
// // Bind to set-focus-child so swiping left works too.
// a.Main.Connect("set-focus-child", func(w gtk.Widgetter) {
// if w == nil {
// return
// }
// switch gtk.BaseWidget(w).Name() {
// case "left":
// a.Main.SetVisibleChild(a.LeftGrid)
// a.Header.Body.SetVisibleChild(a.Header.LeftSide)
// case "right":
// a.Main.SetVisibleChild(a.Right)
// a.Header.Body.SetVisibleChild(a.Header.RightSide)
// }
// })
// Set widths:
a.Channels.SetSizeRequest(ChannelWidth, -1)
a.Privates.SetSizeRequest(ChannelWidth, -1)
// Guilds and Channels grid:
a.LeftGrid = gtk.NewGrid()
a.LeftGrid.SetHAlign(gtk.AlignStart)
a.LeftGrid.SetName("left")
a.LeftGrid.SetOrientation(gtk.OrientationHorizontal)
a.LeftGrid.SetRowHomogeneous(true)
a.LeftGrid.Show()
leftGroup := gtk.NewSizeGroup(gtk.SizeGroupHorizontal)
leftGroup.AddWidget(a.LeftGrid)
leftGroup.AddWidget(a.Header.LeftSide)
// Add the guilds and the separator right and left of channels:
a.setLeftGridCol(a.Guilds, guildsColumn)
a.setLeftGridCol(newSeparator(), separatorColumn1)
a.setLeftGridCol(newSeparator(), separatorColumn2)
// Set the left grid to the main leaflet:
a.Main.Add(a.LeftGrid)
// Make left grid the default view:
a.Main.SetVisibleChild(a.LeftGrid)
// Message widget container, which will hold *Messages:
a.Right = singlebox.NewBox(gtk.OrientationHorizontal, 0)
a.Right.SetName("right")
a.Right.SetHExpand(true)
a.Right.SetVExpand(true)
a.Right.Show()
rightGroup := gtk.NewSizeGroup(gtk.SizeGroupHorizontal)
rightGroup.AddWidget(a.Right)
rightGroup.AddWidget(a.Header.RightSide)
// Set the message container to the main container:
a.Main.Add(a.Right)
// Display the grid and header
a.displayMain()
window.Show()
// Make a Quick switcher
quickswitcher.Bind(quickswitcher.Spawner{
State: s,
OnGuild: func(id discord.GuildID) {
if g, _ := a.Guilds.FindByID(id); g != nil {
g.Activate()
}
},
OnChannel: func(chID discord.ChannelID, gID discord.GuildID) {
a.SwitchToID(chID, gID)
},
})
// Finally, mark plugins as ready:
a.readyPlugins()
return nil
}
func (a *Application) LogOut() {
// Disable the entire application:
window.Window.SetSensitive(false)
state := a.State
a.State = nil
go func() {
// First we need to close the session:
if err := state.CloseGracefully(); err != nil {
log.Errorln("failed to close gracefully:", err)
}
// Then we delete the keyrings:
keyring.Delete()
// Then we call the login dialog and exit without waiting:
glib.IdleAdd(func() {
window.Window.SetSensitive(true)
a.ShowLogin("")
})
}()
}
// ShowLogin shows the login screen.
func (a *Application) ShowLogin(lastToken string) {
l := login.NewLogin(func(s *ningen.State) {
if err := a.Ready(s); err != nil {
log.Fatalln("failed to login:", err)
}
a.Connect("shutdown", func() {
log.Infoln("app shutting down")
s.Close()
})
})
l.LastToken = lastToken
l.Run()
}
func newSeparator() *gtk.Separator {
s := gtk.NewSeparator(gtk.OrientationVertical)
s.Show()
return s
}
type leftGridColPosition uint8
const (
guildsColumn leftGridColPosition = iota
separatorColumn1
channelsColumn
separatorColumn2
maxLeftGridColumn
)
func (a *Application) setLeftGridCol(w gtk.Widgetter, n leftGridColPosition) {
if w := a.leftCols[n]; w != nil {
a.LeftGrid.Remove(w)
}
a.leftCols[n] = w
a.LeftGrid.Attach(w, int(n), 0, 1, 1)
}