Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
diamondburned
GitHub Repository: diamondburned/gtkcord4
Path: blob/main/internal/sidebar/sidebutton/button.go
366 views
1
package sidebutton
2
3
import (
4
"context"
5
6
"github.com/diamondburned/gotk4/pkg/gtk/v4"
7
"github.com/diamondburned/gotkit/components/onlineimage"
8
"github.com/diamondburned/gotkit/gtkutil/cssutil"
9
"github.com/diamondburned/gotkit/gtkutil/imgutil"
10
"github.com/diamondburned/ningen/v3"
11
"libdb.so/dissent/internal/gtkcord"
12
)
13
14
// Button is a widget showing a single guild icon.
15
type Button struct {
16
*gtk.Overlay
17
Button *gtk.Button
18
19
IconOverlay *gtk.Overlay
20
Icon *onlineimage.Avatar
21
Mentions *MentionsIndicator
22
23
Pill *Pill
24
25
ctx context.Context
26
mentions int
27
indicator ningen.UnreadIndication
28
}
29
30
var buttonCSS = cssutil.Applier("sidebar-button", `
31
.sidebar-button > button {
32
padding: 4px 12px;
33
border: none;
34
border-radius: 0;
35
background: none;
36
}
37
.sidebar-button image {
38
background-color: @theme_bg_color;
39
}
40
.sidebar-button > button avatar {
41
border-radius: calc({$guild_icon_size} / 2);
42
outline: 0px solid transparent;
43
outline-offset: 0;
44
}
45
.sidebar-button > button:hover avatar {
46
border-radius: calc({$guild_icon_size} / 4);
47
outline: 2px solid @theme_selected_bg_color;
48
background-color: alpha(@theme_selected_bg_color, 0.35);
49
}
50
.sidebar-button > button image,
51
.sidebar-button > button avatar {
52
transition: 200ms ease;
53
transition-property: all;
54
}
55
`)
56
57
// NewButton creates a new button.
58
func NewButton(ctx context.Context, open func()) *Button {
59
g := Button{
60
ctx: ctx,
61
}
62
63
g.Icon = onlineimage.NewAvatar(ctx, imgutil.HTTPProvider, gtkcord.GuildIconSize)
64
g.Mentions = NewMentionsIndicator()
65
66
g.IconOverlay = gtk.NewOverlay()
67
g.IconOverlay.AddCSSClass("sidebar-button-overlay")
68
g.IconOverlay.SetChild(g.Icon.Avatar)
69
g.IconOverlay.AddOverlay(g.Mentions)
70
71
g.Button = gtk.NewButton()
72
g.Button.SetHasFrame(false)
73
g.Button.SetHAlign(gtk.AlignCenter)
74
g.Button.SetChild(g.IconOverlay)
75
g.Button.ConnectClicked(func() {
76
g.SetSelected(true)
77
open()
78
})
79
80
iconAnimation := g.Icon.EnableAnimation()
81
iconAnimation.ConnectMotion(g.Button)
82
83
g.Pill = NewPill()
84
85
g.Overlay = gtk.NewOverlay()
86
g.Overlay.SetChild(g.Button)
87
g.Overlay.AddOverlay(g.Pill)
88
buttonCSS(g)
89
90
return &g
91
}
92
93
// Context returns the context of the button that was passed in during
94
// construction.
95
func (g *Button) Context() context.Context {
96
return g.ctx
97
}
98
99
// Activate activates the button.
100
func (g *Button) Activate() bool {
101
return g.Button.Activate()
102
}
103
104
// Unselect unselects the guild visually. This is mostly used by the parent
105
// widget for list-keeping.
106
func (g *Button) Unselect() {
107
g.SetSelected(false)
108
}
109
110
// SetSelected sets the button's selected state.
111
func (g *Button) SetSelected(selected bool) {
112
if selected {
113
g.Pill.State = PillActive
114
} else {
115
g.Pill.State = PillDisabled
116
}
117
g.Pill.Invalidate()
118
}
119
120
// IsSelected returns true if the guild is selected.
121
func (g *Button) IsSelected() bool {
122
return g.Pill.State == PillActive
123
}
124
125
// SetIndicator sets the button's unread indicator.
126
func (g *Button) SetIndicator(indicator ningen.UnreadIndication) {
127
if g.indicator == indicator {
128
return
129
}
130
131
g.indicator = indicator
132
g.Pill.Attrs = PillAttrsFromUnread(g.indicator)
133
g.Pill.Invalidate()
134
}
135
136
// SetMentions sets the button's mention indicator.
137
func (g *Button) SetMentions(mentions int) {
138
if g.mentions == mentions {
139
return
140
}
141
142
g.mentions = mentions
143
}
144
145