Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
diamondburned
GitHub Repository: diamondburned/gtkcord4
Path: blob/main/internal/window/login/loading/pulsatingbar.go
452 views
1
package loading
2
3
import (
4
"github.com/diamondburned/gotk4/pkg/glib/v2"
5
"github.com/diamondburned/gotk4/pkg/gtk/v4"
6
"github.com/diamondburned/gotkit/gtkutil/cssutil"
7
)
8
9
type PulsatingFlags uint8
10
11
const (
12
// PulseFast pulses at double speed.
13
PulseFast PulsatingFlags = 1 << iota
14
// PulseBarOSD adds the .osd class.
15
PulseBarOSD
16
)
17
18
func (f PulsatingFlags) has(these PulsatingFlags) bool {
19
return f&these == these
20
}
21
22
// PulsatingBar is a bar that moves back and forth asynchronously for as long as
23
// it is visible (mapped).
24
type PulsatingBar struct {
25
*gtk.Box
26
Bar *gtk.ProgressBar
27
}
28
29
const pulsateRate = 1000 / 30 // 30Hz update
30
31
var pulsatingBarCSS = cssutil.Applier("loading-pulsatingbar", `
32
.loading-pulsatingbar {
33
opacity: 0;
34
transition: all 0.15s ease-in-out;
35
}
36
.loading-pulsatingbar.loading {
37
opacity: 1;
38
}
39
.loading-pulsatingbar,
40
.loading-pulsatingbar progerssbar trough {
41
min-height: 4px;
42
}
43
.loading-pulsatingbar progressbar.osd {
44
margin: 0;
45
padding: 0;
46
}
47
`)
48
49
// NewPulsatingBar creates a new PulsatingBar.
50
func NewPulsatingBar(flags PulsatingFlags) *PulsatingBar {
51
p := PulsatingBar{}
52
p.Bar = gtk.NewProgressBar()
53
p.Bar.Hide()
54
55
p.Box = gtk.NewBox(gtk.OrientationVertical, 0)
56
p.Box.Append(p.Bar)
57
pulsatingBarCSS(p)
58
59
if flags.has(PulseFast) {
60
p.Bar.SetPulseStep(0.1)
61
} else {
62
p.Bar.SetPulseStep(0.05)
63
}
64
65
if flags.has(PulseBarOSD) {
66
p.Box.AddCSSClass("osd")
67
p.Bar.AddCSSClass("osd")
68
}
69
70
var source glib.SourceHandle
71
p.Bar.ConnectShow(func() {
72
p.Bar.SetFraction(0)
73
source = glib.TimeoutAdd(pulsateRate, func() bool {
74
p.Bar.Pulse()
75
return true
76
})
77
})
78
p.Bar.ConnectHide(func() {
79
if source > 0 {
80
glib.SourceRemove(source)
81
source = 0
82
}
83
})
84
85
return &p
86
}
87
88
func (p *PulsatingBar) Show() {
89
p.Bar.Show()
90
p.AddCSSClass("loading")
91
}
92
93
func (p *PulsatingBar) Hide() {
94
p.Bar.Hide()
95
p.RemoveCSSClass("loading")
96
}
97
98