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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
|
local custom = require "assets.custom"
local gdebug = require "gears.debug"
local phosphor = require "assets.phosphor"
local playerctl = require "services.playerctl"
local qcolor = require "quarrel.color"
local qpersistent = require "quarrel.persistent"
local qui = require "quarrel.ui"
local qvars = require "quarrel.vars"
local simpleicons = require "assets.simpleicons"
local wibox = require "wibox"
local M = {}
local client_icons = { -- this is used to map the mpris clients to icons
firefox = simpleicons.librewolf, -- librewolf uses the same player id as firefox
spotify = simpleicons.spotify,
mpd = custom.vinyl_record_fill,
__generic = phosphor.waveform_fill, -- used for any client not in the map
}
local DEFAULTS = {
---@type string
title = "Nothing's playing",
artist = {},
---@type string
album = "",
progresstext = {
---@type string
position = "~",
---@type string
length = "~",
},
---@type number
position = 0,
---@type number
length = math.huge,
client_icon = client_icons.__generic,
art = phosphor.music_notes_fill,
}
local bg_icon = qui.icon {
icon = DEFAULTS.client_icon,
widget = {
forced_width = 0,
forced_height = 0,
},
color = qcolor.palette.bg.highest,
}
local function to_hms(time)
local format = "%i:%02i"
local h = math.floor(time / 60 ^ 2)
local m = math.floor((time / 60) % 60)
local s = math.floor(time % 60)
if h > 0 then
format = "%i:" .. format
return string.format(format, h, m, s)
end
return string.format(format, m, s)
end
local function is_empty(str)
return (str == nil or #str == 0)
end
--- mirror of playerctl:list()
--- why bother mirroring? simple: we need to track changes to the player list
--- but vanished and appeared simply don't give us an old version to compare with
---@type Playerctl.data[]
local players = {}
---@type number
M.active_player_index = qpersistent.get "active_player_index" --[[@as number]]
if not M.active_player_index then
M.active_player_index = 1
gdebug.print_error "failed to get active_player_index from qpersistent, falling back..."
qpersistent.store("active_player_index", M.active_player_index)
end
--- the reason we do this instead of some other hack is this is the only way to draw the icons *without* resizing the mpris container
--- this happens because in the panel, height is unlimited, and imagebox grows until a hard limit
---@class wibox.widget.base
local client_background = wibox.container.background()
function client_background:before_draw_children(_, cr, width, height)
cr:save()
cr:translate(width - (height / 1.25), -(height * 0.125))
---@diagnostic disable-next-line: missing-parameter
wibox.widget.draw_to_cairo_context(bg_icon, cr, height * 1.25, height * 1.25)
cr:restore()
end
M.widget = wibox.widget(qui.styled {
widget = wibox.container.background,
bg = qcolor.palette.bg.high,
{
widget = client_background,
{
widget = wibox.container.margin,
margins = qui.BIG_PADDING,
{
nil,
{
{
{
widget = wibox.container.background,
bg = qcolor.palette.bg(),
shape = qui.shape,
{
widget = wibox.widget.imagebox,
image = DEFAULTS.art,
forced_height = qui.CHAR_HEIGHT * 5,
forced_width = qui.CHAR_HEIGHT * 5,
valign = "center",
halign = "center",
stylesheet = qui.recolor(qcolor.palette.bg.highest),
id = "cover",
},
},
{
widget = wibox.container.margin,
right = qui.BIG_PADDING,
left = qui.BIG_PADDING,
{
{
widget = wibox.container.constraint,
height = qui.CHAR_HEIGHT * 2.5,
strategy = "max",
{
widget = wibox.widget.textbox,
text = DEFAULTS.title, -- Song
id = "song",
valign = "top",
},
},
{
widget = wibox.container.constraint,
height = qui.CHAR_HEIGHT * 2.5,
strategy = "max",
{
widget = wibox.container.background,
fg = qcolor.palette.fg.low,
{
widget = wibox.widget.textbox,
text = DEFAULTS.artist_album, -- Artist - Album Name
id = "artist_album",
valign = "top",
},
},
},
layout = wibox.layout.fixed.vertical,
},
},
layout = wibox.layout.fixed.horizontal,
},
nil,
{
widget = wibox.container.margin,
top = qui.BIG_PADDING,
right = qui.BIG_PADDING,
{
{
widget = wibox.widget.textbox,
text = DEFAULTS.progresstext.position .. " / " .. DEFAULTS.progresstext.length, -- position / length
id = "progresstext",
},
{
widget = wibox.container.place,
{
widget = wibox.widget.progressbar,
forced_height = qui.PADDING,
color = qcolor.palette.yellow(),
value = DEFAULTS.position,
max_value = DEFAULTS.length,
background_color = qcolor.palette.bg.lowest,
bar_shape = qui.shape,
shape = qui.shape,
id = "progressbar",
},
},
layout = wibox.layout.fixed.horizontal,
spacing = qui.BIG_PADDING,
},
},
layout = wibox.layout.align.vertical,
},
{
layout = wibox.layout.flex.vertical,
spacing = qui.BIG_PADDING,
id = "client_list",
},
layout = wibox.layout.align.horizontal,
},
},
},
})
local layout = M.widget:get_children_by_id("client_list")[1] --[[@as wibox.layout.flex]]
local progressbar = M.widget:get_children_by_id("progressbar")[1] --[[@as wibox.widget.progressbar]]
local progresstext = M.widget:get_children_by_id("progresstext")[1] --[[@as wibox.widget.textbox]]
local song = M.widget:get_children_by_id("song")[1] --[[@as wibox.widget.textbox]]
local artist_album = M.widget:get_children_by_id("artist_album")[1] --[[@as wibox.widget.textbox]]
local cover = M.widget:get_children_by_id("cover")[1] --[[@as wibox.widget.imagebox]]
local function mirror_player_list()
players = playerctl:list()
end
mirror_player_list()
local function handle_metadata(_, player)
local title, album, artist, art
if player ~= playerctl:list()[M.active_player_index] then
return
end
if player then
if player.metadata.title then
title = player.metadata.title
else
title = DEFAULTS.title
end
if player.metadata.album then
album = player.metadata.album
else
album = DEFAULTS.album
end
if player.metadata.artist then
artist = player.metadata.artist
else
artist = DEFAULTS.artist
end
if player.metadata.art and player.metadata.art:match "^file://" then
art = player.metadata.art:gsub("^file://", "")
else
art = DEFAULTS.art
end
else
title = DEFAULTS.title
album = DEFAULTS.album
artist = DEFAULTS.artist
art = DEFAULTS.art
end
artist_album.text = table.concat(artist, ", ") .. ((is_empty(artist) or is_empty(album)) and "" or " - ") .. album
song.text = title
---@diagnostic disable-next-line:inject-field
cover.image = art
end
local function handle_position(_, player)
if player ~= playerctl:list()[M.active_player_index] then
return
end
local position, length
local content = ""
if player then
if player.position then
position = player.position / playerctl.unit
content = content .. to_hms(position)
else
position = DEFAULTS.position
content = content .. DEFAULTS.progresstext.position
end
content = content .. " / "
if player.metadata.length then
length = player.metadata.length / playerctl.unit
content = content .. to_hms(length)
else
length = DEFAULTS.length
content = content .. DEFAULTS.progresstext.length
end
else
position = DEFAULTS.position
length = DEFAULTS.length
content = DEFAULTS.progresstext.position .. " / " .. DEFAULTS.progresstext.length
end
progresstext.text = content
---@diagnostic disable-next-line:inject-field
progressbar.value = position
---@diagnostic disable-next-line:inject-field
progressbar.max_value = length
end
playerctl:connect_signal("player::metadata", handle_metadata)
playerctl:connect_signal("player::position", handle_position)
local function update_player(player)
handle_metadata(nil, player)
handle_position(nil, player)
qpersistent.store("active_player_index", M.active_player_index)
local client_icon = client_icons[(player or { name = "__generic" }).name]
bg_icon.image = client_icon or client_icons.__generic
client_background:emit_signal "widget::redraw_needed"
for i, child in ipairs(layout.children) do
---@diagnostic disable-next-line:undefined-field
child:index_handler(i)
end
end
function M.next_player()
local players_length = #layout.children
if players_length == 0 then
return
end
if M.active_player_index + 1 > players_length then
M.active_player_index = 1
else
M.active_player_index = M.active_player_index + 1
end
update_player(playerctl:list()[M.active_player_index])
end
function M.previous_player()
local players_length = #layout.children
if players_length == 0 then
return
end
if M.active_player_index - 1 < 1 then
M.active_player_index = players_length
else
M.active_player_index = M.active_player_index - 1
end
update_player(playerctl:list()[M.active_player_index])
end
---@param diff_player Playerctl.data
local function recalculate_active_player(diff_player, vanished)
if type(diff_player) ~= "table" then
return
end
if #layout.children == 0 then
M.active_player_index = 1
update_player()
return
end
local active_player = players[M.active_player_index]
if not active_player then -- we're recovering from a state with no players
update_player(diff_player)
return
end
if diff_player.instance == active_player.instance and vanished then -- active player vanished; fall back to previous player
M.previous_player()
else -- non-active player appeared/vanished; try to find active player
for i, p in ipairs(playerctl:list()) do
if p.instance == active_player.instance then
M.active_player_index = i
update_player(p)
return
end
end
gdebug.print_warning(
"failed to find active player:\n " .. gdebug.dump_return(active_player, nil, 2):gsub("\n", "\n ")
)
M.active_player_index = 1
update_player(playerctl:list()[M.active_player_index])
end
end
local function register_player(player)
local widget = wibox.widget {
widget = wibox.container.constraint,
width = qui.PADDING,
strategy = "min",
{
widget = wibox.container.background,
shape = qui.shape,
bg = qcolor.palette.bg.lowest,
},
index_handler = function(self, index)
if M.active_player_index == index then
self.widget.bg = qcolor.palette.yellow()
else
self.widget.bg = qcolor.palette.bg.lowest
end
end,
}
---@diagnostic disable-next-line:undefined-field
layout:add(widget)
recalculate_active_player(player, false)
mirror_player_list()
end
local function unregister_player(player)
---@diagnostic disable-next-line:undefined-field
layout:remove(#layout.children)
recalculate_active_player(player, true)
mirror_player_list()
end
for _, player in ipairs(playerctl:list()) do
register_player(player)
end
-- recover state
---@diagnostic disable-next-line:undefined-field
local last_active_player = playerctl:list()[M.active_player_index]
update_player(last_active_player)
playerctl:connect_signal("player::appeared", function(_, player)
register_player(player)
end)
playerctl:connect_signal("player::vanished", function(_, player)
unregister_player(player)
end)
return M
|