Path: blob/main/internal/sidebar/sidebutton/button.go
366 views
package sidebutton12import (3"context"45"github.com/diamondburned/gotk4/pkg/gtk/v4"6"github.com/diamondburned/gotkit/components/onlineimage"7"github.com/diamondburned/gotkit/gtkutil/cssutil"8"github.com/diamondburned/gotkit/gtkutil/imgutil"9"github.com/diamondburned/ningen/v3"10"libdb.so/dissent/internal/gtkcord"11)1213// Button is a widget showing a single guild icon.14type Button struct {15*gtk.Overlay16Button *gtk.Button1718IconOverlay *gtk.Overlay19Icon *onlineimage.Avatar20Mentions *MentionsIndicator2122Pill *Pill2324ctx context.Context25mentions int26indicator ningen.UnreadIndication27}2829var buttonCSS = cssutil.Applier("sidebar-button", `30.sidebar-button > button {31padding: 4px 12px;32border: none;33border-radius: 0;34background: none;35}36.sidebar-button image {37background-color: @theme_bg_color;38}39.sidebar-button > button avatar {40border-radius: calc({$guild_icon_size} / 2);41outline: 0px solid transparent;42outline-offset: 0;43}44.sidebar-button > button:hover avatar {45border-radius: calc({$guild_icon_size} / 4);46outline: 2px solid @theme_selected_bg_color;47background-color: alpha(@theme_selected_bg_color, 0.35);48}49.sidebar-button > button image,50.sidebar-button > button avatar {51transition: 200ms ease;52transition-property: all;53}54`)5556// NewButton creates a new button.57func NewButton(ctx context.Context, open func()) *Button {58g := Button{59ctx: ctx,60}6162g.Icon = onlineimage.NewAvatar(ctx, imgutil.HTTPProvider, gtkcord.GuildIconSize)63g.Mentions = NewMentionsIndicator()6465g.IconOverlay = gtk.NewOverlay()66g.IconOverlay.AddCSSClass("sidebar-button-overlay")67g.IconOverlay.SetChild(g.Icon.Avatar)68g.IconOverlay.AddOverlay(g.Mentions)6970g.Button = gtk.NewButton()71g.Button.SetHasFrame(false)72g.Button.SetHAlign(gtk.AlignCenter)73g.Button.SetChild(g.IconOverlay)74g.Button.ConnectClicked(func() {75g.SetSelected(true)76open()77})7879iconAnimation := g.Icon.EnableAnimation()80iconAnimation.ConnectMotion(g.Button)8182g.Pill = NewPill()8384g.Overlay = gtk.NewOverlay()85g.Overlay.SetChild(g.Button)86g.Overlay.AddOverlay(g.Pill)87buttonCSS(g)8889return &g90}9192// Context returns the context of the button that was passed in during93// construction.94func (g *Button) Context() context.Context {95return g.ctx96}9798// Activate activates the button.99func (g *Button) Activate() bool {100return g.Button.Activate()101}102103// Unselect unselects the guild visually. This is mostly used by the parent104// widget for list-keeping.105func (g *Button) Unselect() {106g.SetSelected(false)107}108109// SetSelected sets the button's selected state.110func (g *Button) SetSelected(selected bool) {111if selected {112g.Pill.State = PillActive113} else {114g.Pill.State = PillDisabled115}116g.Pill.Invalidate()117}118119// IsSelected returns true if the guild is selected.120func (g *Button) IsSelected() bool {121return g.Pill.State == PillActive122}123124// SetIndicator sets the button's unread indicator.125func (g *Button) SetIndicator(indicator ningen.UnreadIndication) {126if g.indicator == indicator {127return128}129130g.indicator = indicator131g.Pill.Attrs = PillAttrsFromUnread(g.indicator)132g.Pill.Invalidate()133}134135// SetMentions sets the button's mention indicator.136func (g *Button) SetMentions(mentions int) {137if g.mentions == mentions {138return139}140141g.mentions = mentions142}143144145