-
Notifications
You must be signed in to change notification settings - Fork 38
/
pill.go
140 lines (116 loc) · 2.83 KB
/
pill.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package sidebutton
import (
"github.com/diamondburned/gotk4/pkg/gtk/v4"
"github.com/diamondburned/gotkit/gtkutil/cssutil"
"github.com/diamondburned/ningen/v3"
)
type PillState uint8
// Pill state enums.
const (
PillInactive PillState = iota
PillDisabled
PillActive
PillOpened
)
// CSSClass returns the CSS class for the current state.
func (s PillState) CSSClass() string {
switch s {
case PillInactive, PillDisabled:
return ""
case PillActive:
return "guilds-pill-active"
case PillOpened:
return "guilds-pill-opened"
default:
return ""
}
}
type PillAttributes uint8
// Additional pill attributes.
const (
PillUnread PillAttributes = 1 << iota
PillMentioned
)
// PillAttrsFromUnread creates a new PillAttributes reflecting the correct
// unread indication state.
func PillAttrsFromUnread(state ningen.UnreadIndication) PillAttributes {
var attrs PillAttributes
switch state {
case ningen.ChannelUnread:
attrs |= PillUnread
case ningen.ChannelMentioned:
attrs |= PillUnread | PillMentioned
}
return attrs
}
// CSSClasses returns the CSS classes that corresponds to the current state of
// the pill.
func (a PillAttributes) CSSClasses() []string {
var classes []string
if a.Has(PillUnread) {
classes = append(classes, "guilds-pill-unread")
}
if a.Has(PillMentioned) {
classes = append(classes, "guilds-pill-mentioned")
}
return classes
}
// Has returns true if a has these in its bits.
func (a PillAttributes) Has(these PillAttributes) bool {
return a&these == these
}
type Pill struct {
*gtk.Box // width IconPadding
State PillState
Attrs PillAttributes
oldState PillState
oldAttrs PillAttributes
}
var stripCSS = cssutil.Applier("guilds-pill", `
@define-color mentioned rgb(240, 71, 71);
.guilds-pill {
padding: 0;
transition: 100ms linear;
border-radius: 0 99px 99px 0;
background-color: @theme_fg_color;
}
.guilds-pill.guilds-pill-active {
padding: 20px 3px;
}
.guilds-pill.guilds-pill-unread:not(.guilds-pill-opened):not(.guilds-pill-active) {
padding: 6px 3px;
}
.guilds-pill.guilds-pill-mentioned {
background-color: @mentioned;
}
`)
func NewPill() *Pill {
p := Pill{}
p.Box = gtk.NewBox(gtk.OrientationHorizontal, 0)
p.Box.SetVExpand(true)
p.Box.SetVAlign(gtk.AlignCenter)
p.Box.SetHAlign(gtk.AlignStart)
stripCSS(p)
return &p
}
// Invalidate reads the 2 fields inside Pill and updates the widget accordingly.
func (r *Pill) Invalidate() {
if r.State != r.oldState {
if class := r.oldState.CSSClass(); class != "" {
r.RemoveCSSClass(r.oldState.CSSClass())
}
r.oldState = r.State
if class := r.oldState.CSSClass(); class != "" {
r.AddCSSClass(class)
}
}
if r.Attrs != r.oldAttrs {
for _, class := range r.oldAttrs.CSSClasses() {
r.RemoveCSSClass(class)
}
r.oldAttrs = r.Attrs
for _, class := range r.oldAttrs.CSSClasses() {
r.AddCSSClass(class)
}
}
}