Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
diamondburned
GitHub Repository: diamondburned/gtkcord4
Path: blob/main/internal/sidebar/directbutton/channel.go
366 views
1
package directbutton
2
3
import (
4
"context"
5
"log/slog"
6
7
"github.com/diamondburned/arikawa/v3/discord"
8
"github.com/diamondburned/gotk4/pkg/gtk/v4"
9
"github.com/diamondburned/gotkit/gtkutil/cssutil"
10
"github.com/diamondburned/ningen/v3"
11
"libdb.so/dissent/internal/gtkcord"
12
"libdb.so/dissent/internal/sidebar/sidebutton"
13
)
14
15
type ChannelButton struct {
16
*sidebutton.Button
17
id discord.ChannelID
18
}
19
20
var channelCSS = cssutil.Applier("dmbutton-channel", `
21
`)
22
23
func NewChannelButton(ctx context.Context, id discord.ChannelID) *ChannelButton {
24
ch := ChannelButton{id: id}
25
ch.Button = sidebutton.NewButton(ctx, func() {
26
parent := gtk.BaseWidget(ch.Button.Parent())
27
parent.ActivateAction("win.open-channel", gtkcord.NewChannelIDVariant(id))
28
})
29
channelCSS(ch)
30
return &ch
31
}
32
33
// ID returns the channel ID.
34
func (c *ChannelButton) ID() discord.ChannelID { return c.id }
35
36
// Invalidate invalidates and updates the state of the channel.
37
func (c *ChannelButton) Invalidate() {
38
state := gtkcord.FromContext(c.Context())
39
40
ch, err := state.Cabinet.Channel(c.id)
41
if err != nil {
42
slog.Error(
43
"cannot fetch channel for DMButton",
44
"channel_id", c.id,
45
"err", err)
46
return
47
}
48
49
c.Update(ch)
50
c.InvalidateUnread()
51
}
52
53
// Update updates the channel with the given Discord object.
54
func (c *ChannelButton) Update(ch *discord.Channel) {
55
name := gtkcord.ChannelName(ch)
56
57
var iconURL string
58
if ch.Icon != "" {
59
iconURL = ch.IconURL()
60
} else if len(ch.DMRecipients) == 1 {
61
iconURL = ch.DMRecipients[0].AvatarURL()
62
}
63
64
c.Button.SetTooltipText(name)
65
c.Icon.SetText(name)
66
c.Icon.SetFromURL(iconURL)
67
}
68
69
// InvalidateUnread invalidates the guild's unread state.
70
func (c *ChannelButton) InvalidateUnread() {
71
state := gtkcord.FromContext(c.Context())
72
unreads := state.ChannelCountUnreads(c.id, ningen.UnreadOpts{})
73
74
indicator := state.ChannelIsUnread(c.id, ningen.UnreadOpts{})
75
if indicator != ningen.ChannelRead && unreads == 0 {
76
unreads = 1
77
}
78
79
c.SetIndicator(indicator)
80
c.Mentions.SetCount(unreads)
81
}
82
83