Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
diamondburned
GitHub Repository: diamondburned/gtkcord4
Path: blob/main/internal/sidebar/sidebutton/mention.go
366 views
1
package sidebutton
2
3
import (
4
"strconv"
5
6
"github.com/diamondburned/gotk4/pkg/gtk/v4"
7
"github.com/diamondburned/gotkit/gtkutil/cssutil"
8
)
9
10
// MentionsIndicator is a small indicator that shows the mention count.
11
type MentionsIndicator struct {
12
*gtk.Revealer
13
Label *gtk.Label
14
15
count int
16
reveal bool
17
}
18
19
var mentionCSS = cssutil.Applier("sidebar-mention", `
20
.sidebar-mention {
21
background: none;
22
}
23
.sidebar-mention.sidebar-mention-active,
24
.sidebar-mention.sidebar-mention-active label {
25
border-radius: 100px;
26
background-color: @theme_bg_color;
27
}
28
.sidebar-mention.sidebar-mention-active label {
29
color: white;
30
background-color: @mentioned;
31
min-width: 12pt;
32
min-height: 12pt;
33
padding: 0;
34
margin: 2px;
35
font-size: 8pt;
36
font-weight: bold;
37
}
38
`)
39
40
// NewMentionsIndicator creates a new mention indicator.
41
func NewMentionsIndicator() *MentionsIndicator {
42
m := &MentionsIndicator{
43
Revealer: gtk.NewRevealer(),
44
Label: gtk.NewLabel(""),
45
reveal: true,
46
}
47
48
m.SetChild(m.Label)
49
m.SetHAlign(gtk.AlignEnd)
50
m.SetVAlign(gtk.AlignEnd)
51
m.SetTransitionType(gtk.RevealerTransitionTypeCrossfade)
52
m.SetTransitionDuration(100)
53
54
m.update()
55
mentionCSS(m)
56
return m
57
}
58
59
// SetCount sets the mention count.
60
func (m *MentionsIndicator) SetCount(count int) {
61
if count == m.count {
62
return
63
}
64
65
m.count = count
66
m.update()
67
}
68
69
// Count returns the mention count.
70
func (m *MentionsIndicator) Count() int {
71
return m.count
72
}
73
74
// SetRevealChild sets whether the indicator should be revealed.
75
// This lets the user hide the indicator even if there are mentions.
76
func (m *MentionsIndicator) SetRevealChild(reveal bool) {
77
m.reveal = reveal
78
m.update()
79
}
80
81
func (m *MentionsIndicator) update() {
82
if m.count == 0 {
83
m.RemoveCSSClass("sidebar-mention-active")
84
m.Revealer.SetRevealChild(false)
85
return
86
}
87
88
m.AddCSSClass("sidebar-mention-active")
89
m.Label.SetText(strconv.Itoa(m.count))
90
m.Revealer.SetRevealChild(m.reveal && m.count > 0)
91
}
92
93