/
music.go
130 lines (113 loc) · 2.4 KB
/
music.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
package module
import (
"github.com/shimmerglass/bar3x/lib/spotify"
"github.com/shimmerglass/bar3x/ui"
"github.com/shimmerglass/bar3x/ui/base"
"github.com/shimmerglass/bar3x/ui/markup"
)
type Music struct {
Img *base.Image
Placeholder *base.Rect
TextRow *base.Row
Progress *base.Bar
Artist *base.Text
Sep *base.Sizer
Title *base.Text
mk *markup.Markup
keyID string
keySecret string
moduleBase
}
func NewMusic(p ui.ParentDrawable, mk *markup.Markup) *Music {
return &Music{
mk: mk,
moduleBase: newBase(p),
}
}
func (m *Music) Init() error {
_, err := m.mk.Parse(m, m, `
<Row ref="Root">
<Sizer PaddingRight="{h_padding}">
<Layers>
<Rect
ref="Placeholder"
Width="{bar_height - v_padding * 2}"
Height="{bar_height - v_padding * 2}"
Color="{neutral_color}"
Visible="{!$Img.Visible}"
/>
<Image
ref="Img"
Width="{bar_height - v_padding * 2}"
Height="{bar_height - v_padding * 2}"
/>
</Layers>
</Sizer>
<Col>
<Row ref="TextRow">
<Text ref="Title" MaxWidth="120" />
<Sizer ref="Sep" PaddingLeft="{h_padding}" PaddingRight="{h_padding}">
<Icon>{icons.dot}</Icon>
</Sizer>
<Text ref="Artist" MaxWidth="120" />
</Row>
<Sizer PaddingTop="3">
<Bar
ref="Progress"
FgColor="{accent_color}"
BgColor="{neutral_color}"
Height="2"
Width="{$TextRow.Width}"
Direction="left-right"
/>
</Sizer>
</Col>
</Row>
`)
if err != nil {
return err
}
go func() {
spot := spotify.New(m.keyID, m.keySecret)
for s := range spot.Up {
if !s.Playing {
m.SetVisible(false)
m.Notify()
continue
} else {
m.SetVisible(true)
}
m.Artist.SetText(s.Artist)
m.Title.SetText(s.Title)
if s.Artist == "" {
m.Artist.SetVisible(false)
m.Sep.SetVisible(false)
} else {
m.Artist.SetVisible(true)
m.Sep.SetVisible(true)
}
m.Img.SetImage(s.Image)
if s.Image == nil {
m.Img.SetVisible(false)
} else {
m.Img.SetVisible(true)
}
m.Progress.SetValue(s.Progress)
m.Notify()
}
}()
return nil
}
// parameters
func (m *Music) SpotifyKeyID() string {
return m.keyID
}
func (m *Music) SetSpotifyKeyID(v string) {
m.keyID = v
}
func (m *Music) SpotifyKeySecret() string {
return m.keySecret
}
func (m *Music) SetSpotifyKeySecret(v string) {
m.keySecret = v
}