forked from diamondburned/gtkcord3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
member.go
114 lines (90 loc) · 2.14 KB
/
member.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package members
import (
"fmt"
"github.com/diamondburned/arikawa/discord"
"github.com/TheBlueOompaLoompa/gtkcord3/gtkcord/components/user"
"github.com/TheBlueOompaLoompa/gtkcord3/gtkcord/gtkutils"
"github.com/TheBlueOompaLoompa/gtkcord3/gtkcord/semaphore"
"github.com/gotk3/gotk3/gtk"
)
const MemberAvatarSize = 32
type Section struct {
*gtk.ListBoxRow
Name string
Count uint64
Label *gtk.Label
}
func NewSection(name string, count uint64) *Section {
r, _ := gtk.ListBoxRowNew()
r.Show()
r.SetSelectable(false)
r.SetActivatable(false)
gtkutils.InjectCSSUnsafe(r, "section", "")
l, _ := gtk.LabelNew("")
l.Show()
l.SetHAlign(gtk.ALIGN_START)
gtkutils.Margin4(l, 16, 2, 8, 8)
r.Add(l)
s := &Section{
ListBoxRow: r,
Label: l,
}
s.UpdateUnsafe(name, count)
return s
}
func (s *Section) shouldUpdate(name string, count uint64) bool {
if (name != "" && s.Name == name) && (count > 0 && s.Count == count) {
return false
}
if name != "" {
s.Name = name
}
if count > 0 {
s.Count = count
}
return true
}
func (s *Section) Update(name string, count uint64) {
if !s.shouldUpdate(name, count) {
return
}
semaphore.IdleMust(s.Label.SetMarkup,
fmt.Sprintf(`<span size="smaller" weight="bold">%s—%d</span>`, s.Name, s.Count))
}
func (s *Section) UpdateUnsafe(name string, count uint64) {
if !s.shouldUpdate(name, count) {
return
}
s.Label.SetMarkup(
fmt.Sprintf(`<span size="smaller" weight="bold">%s—%d</span>`, s.Name, s.Count))
}
type Member struct {
*gtk.ListBoxRow
User *user.Container
// user
ID discord.UserID
}
func NewMember(m discord.Member, p discord.Presence, guild discord.Guild) *Member {
body := user.New()
body.UpdateMember(m, guild)
body.UpdateActivity(p.Game)
body.UpdateStatus(p.Status)
if m.User.Avatar != "" {
body.UpdateAvatar(m.User.AvatarURL() + "?size=32")
}
return newMember(body, m.User.ID)
}
func NewMemberUnavailable() *Member {
body := user.New()
return newMember(body, 0)
}
func newMember(body *user.Container, uID discord.UserID) *Member {
r, _ := gtk.ListBoxRowNew()
r.Show()
r.Add(body)
return &Member{
ListBoxRow: r,
User: body,
ID: uID,
}
}