Path: blob/main/internal/sidebar/directbutton/channel.go
366 views
package directbutton12import (3"context"4"log/slog"56"github.com/diamondburned/arikawa/v3/discord"7"github.com/diamondburned/gotk4/pkg/gtk/v4"8"github.com/diamondburned/gotkit/gtkutil/cssutil"9"github.com/diamondburned/ningen/v3"10"libdb.so/dissent/internal/gtkcord"11"libdb.so/dissent/internal/sidebar/sidebutton"12)1314type ChannelButton struct {15*sidebutton.Button16id discord.ChannelID17}1819var channelCSS = cssutil.Applier("dmbutton-channel", `20`)2122func NewChannelButton(ctx context.Context, id discord.ChannelID) *ChannelButton {23ch := ChannelButton{id: id}24ch.Button = sidebutton.NewButton(ctx, func() {25parent := gtk.BaseWidget(ch.Button.Parent())26parent.ActivateAction("win.open-channel", gtkcord.NewChannelIDVariant(id))27})28channelCSS(ch)29return &ch30}3132// ID returns the channel ID.33func (c *ChannelButton) ID() discord.ChannelID { return c.id }3435// Invalidate invalidates and updates the state of the channel.36func (c *ChannelButton) Invalidate() {37state := gtkcord.FromContext(c.Context())3839ch, err := state.Cabinet.Channel(c.id)40if err != nil {41slog.Error(42"cannot fetch channel for DMButton",43"channel_id", c.id,44"err", err)45return46}4748c.Update(ch)49c.InvalidateUnread()50}5152// Update updates the channel with the given Discord object.53func (c *ChannelButton) Update(ch *discord.Channel) {54name := gtkcord.ChannelName(ch)5556var iconURL string57if ch.Icon != "" {58iconURL = ch.IconURL()59} else if len(ch.DMRecipients) == 1 {60iconURL = ch.DMRecipients[0].AvatarURL()61}6263c.Button.SetTooltipText(name)64c.Icon.SetText(name)65c.Icon.SetFromURL(iconURL)66}6768// InvalidateUnread invalidates the guild's unread state.69func (c *ChannelButton) InvalidateUnread() {70state := gtkcord.FromContext(c.Context())71unreads := state.ChannelCountUnreads(c.id, ningen.UnreadOpts{})7273indicator := state.ChannelIsUnread(c.id, ningen.UnreadOpts{})74if indicator != ningen.ChannelRead && unreads == 0 {75unreads = 176}7778c.SetIndicator(indicator)79c.Mentions.SetCount(unreads)80}818283