Path: blob/main/internal/sidebar/channels/channel_icon.go
366 views
package channels12import (3"log/slog"45"github.com/diamondburned/arikawa/v3/discord"6"github.com/diamondburned/gotk4/pkg/gtk/v4"7"github.com/diamondburned/gotkit/gtkutil/cssutil"8)910var channelIndicatorCSS = cssutil.Applier("channel-item-indicator", `11@define-color channel_item_indicator_color @theme_bg_color;1213.channel-item-indicator {14font-family: monospace;15font-size: 0.75em;16font-weight: bold;1718margin-right: -12px;19min-width: 12px; /* prevent (size >= 0) warnings */2021/* Replicate a text outline for the indicators. */22text-shadow:230 0 2px @channel_item_indicator_color,240 0 2px @channel_item_indicator_color,250 0 2px @channel_item_indicator_color,260 0 2px @channel_item_indicator_color,270 0 2px @channel_item_indicator_color,280 0 2px @channel_item_indicator_color,290 0 2px @channel_item_indicator_color,300 0 2px @channel_item_indicator_color,310 0 2px @channel_item_indicator_color,320 0 2px @channel_item_indicator_color,330 0 2px @channel_item_indicator_color,340 0 2px @channel_item_indicator_color,350 0 2px @channel_item_indicator_color,360 0 2px @channel_item_indicator_color,370 0 2px @channel_item_indicator_color;38}39`)4041var channelIconImageCSS = cssutil.Applier("channel-icon-image", `42.channel-icon-image {43min-width: 0px;44}45`)4647type ChannelIconOverrideFunc func(t discord.ChannelType) (string, bool)4849var _ ChannelIconOverrideFunc = channelIconBase5051func channelIconBase(chType discord.ChannelType) (string, bool) {52switch chType {53case discord.GuildText:54return "channel-symbolic", true55case discord.GuildAnnouncement:56return "channel-broadcast-symbolic", true57case discord.GuildPublicThread, discord.GuildPrivateThread, discord.GuildAnnouncementThread:58return "thread-branch-symbolic", true59case discord.GuildVoice, discord.GuildStageVoice:60return "channel-voice-symbolic", true61case discord.DirectMessage:62return "person-symbolic", true63case discord.GroupDM:64return "group-symbolic", true65default:66return "channel-symbolic", false67}68}6970// ChannelIcon is a widget that displays a channel's icon.71// This does not handle DM channels, as it does not display avatars. It is only72// for displaying the channel icon.73type ChannelIcon struct {74*gtk.Widget75Icon *gtk.Image76}7778const unknownChannelType discord.ChannelType = 99997980// NewChannelIcon creates a new ChannelIcon.81// If ch is nil, the icon will be a special unknown icon.82func NewChannelIcon(ch *discord.Channel, overrides ...ChannelIconOverrideFunc) *ChannelIcon {83chType := unknownChannelType84var nsfw bool85if ch != nil {86chType = ch.Type87nsfw = ch.NSFW88}8990var iconName string91var found bool92for _, override := range append(overrides, channelIconBase) {93iconName, found = override(chType)94if found {95break96}97}9899if !found && chType != unknownChannelType {100slog.Debug(101"channel icon called with unknown channel type, using fallback icon",102"channel_type", chType)103}104105icon := gtk.NewImageFromIconName(iconName)106channelIconImageCSS(icon)107108if found && !nsfw {109return &ChannelIcon{110Widget: &icon.Widget,111Icon: icon,112}113}114115var indicatorStr string116if !found {117indicatorStr = "?"118} else {119indicatorStr = "!"120}121122indicator := gtk.NewLabel(indicatorStr)123indicator.SetXAlign(1)124indicator.SetHAlign(gtk.AlignCenter)125indicator.SetVAlign(gtk.AlignEnd)126indicator.SetHExpand(false)127indicator.SetVExpand(false)128channelIndicatorCSS(indicator)129130iconFrame := gtk.NewOverlay()131iconFrame.SetHAlign(gtk.AlignCenter)132iconFrame.SetVAlign(gtk.AlignCenter)133iconFrame.SetChild(icon)134iconFrame.AddOverlay(indicator)135136return &ChannelIcon{137Widget: &iconFrame.Widget,138Icon: icon,139}140}141142143