From a11ee2ec8aea7d14bcfe58573782497d1a35ec62 Mon Sep 17 00:00:00 2001 From: Artem Tarasov Date: Sun, 19 May 2024 12:42:49 +0200 Subject: [PATCH 01/15] working progressbar --- mpris-widget/init.lua | 90 +++++++++++++++++++++++++------------------ 1 file changed, 53 insertions(+), 37 deletions(-) diff --git a/mpris-widget/init.lua b/mpris-widget/init.lua index eb2dda6..d8a7e7e 100644 --- a/mpris-widget/init.lua +++ b/mpris-widget/init.lua @@ -11,60 +11,70 @@ local watch = require("awful.widget.watch") local wibox = require("wibox") local gears = require("gears") -local GET_MPD_CMD = "playerctl -p %s -f '{{status}};{{xesam:artist}};{{xesam:title}}' metadata" +local GET_MPD_CMD = "playerctl -p '%s' -f '{{status}};{{xesam:artist}};{{xesam:title}};{{mpris:artUrl}};{{position}};{{mpris:length}}' metadata" local TOGGLE_MPD_CMD = "playerctl play-pause" local NEXT_MPD_CMD = "playerctl next" local PREV_MPD_CMD = "playerctl previous" local LIST_PLAYERS_CMD = "playerctl -l" -local PATH_TO_ICONS = "/usr/share/icons/Arc" -local PAUSE_ICON_NAME = PATH_TO_ICONS .. "/actions/24/player_pause.png" -local PLAY_ICON_NAME = PATH_TO_ICONS .. "/actions/24/player_play.png" -local STOP_ICON_NAME = PATH_TO_ICONS .. "/actions/24/player_stop.png" -local LIBRARY_ICON_NAME = PATH_TO_ICONS .. "/actions/24/music-library.png" +local PATH_TO_ICONS = "/usr/share/icons/Adwaita" +local PAUSE_ICON_NAME = PATH_TO_ICONS .. "/symbolic/actions/media-playback-pause-symbolic.svg" +local PLAY_ICON_NAME = PATH_TO_ICONS .. "/symbolic/actions/media-playback-start-symbolic.svg" +local STOP_ICON_NAME = PATH_TO_ICONS .. "/symbolic/actions/media-playback-stop-symbolic.svg" +local LIBRARY_ICON_NAME = PATH_TO_ICONS .. "/symbolic/places/folder-music-symbolic.svg" + +local FONT = 'Roboto Mono 18px' local default_player = '' -local icon = wibox.widget { +local icon = wibox.widget { id = "icon", widget = wibox.widget.imagebox, image = PLAY_ICON_NAME } -local mpris_widget = wibox.widget { - { - id = 'artist', - widget = wibox.widget.textbox - }, - { - icon, - max_value = 1, - value = 0, - thickness = 2, - start_angle = 4.71238898, -- 2pi*3/4 - forced_height = 24, - forced_width = 24, - rounded_edge = true, - bg = "#ffffff11", - paddings = 0, - widget = wibox.container.arcchart - }, - { - id = 'title', - widget = wibox.widget.textbox - }, +local progress_widget = wibox.widget { + id = 'progress', + widget = wibox.container.arcchart, + icon, + min_value = 0, + max_value = 1, + value = 0, + thickness = 2, + start_angle = 4.71238898, -- 2pi*3/4 + forced_height = 24, + forced_width = 24, + rounded_edge = true, + bg = "#ffffff11", + paddings = 2, +} + +local artist_widget = wibox.widget { + id = 'artist', + font = FONT, + widget = wibox.widget.textbox +} + +local title_widget = wibox.widget { + id = 'title', + font = FONT, + widget = wibox.widget.textbox +} + +local mpris_widget = wibox.widget { + artist_widget, + progress_widget, + title_widget, + spacing = 4, layout = wibox.layout.fixed.horizontal, - set_text = function(self, artist, title) - self:get_children_by_id('artist')[1]:set_text(artist) - self:get_children_by_id('title')[1]:set_text(title) - end } local rows = { layout = wibox.layout.fixed.vertical } local popup = awful.popup { bg = beautiful.bg_normal, + fg = beautiful.fg_normal, ontop = true, visible = false, shape = gears.shape.rounded_rect, @@ -88,7 +98,7 @@ local function rebuild_popup() shape = gears.shape.circle, forced_width = 20, forced_height = 20, - check_color = beautiful.fg_urgent, + check_color = beautiful.fg_normal, widget = wibox.widget.checkbox }, valign = 'center', @@ -120,6 +130,7 @@ local function rebuild_popup() layout = wibox.container.margin }, bg = beautiful.bg_normal, + fg = beautiful.fg_normal, widget = wibox.container.background }) end @@ -138,6 +149,7 @@ local function worker() player_status = words[1] artist = words[2] current_song = words[3] + art_url = words[4] if current_song ~= nil then if string.len(current_song) > 18 then current_song = string.sub(current_song, 0, 9) .. ".." @@ -147,11 +159,15 @@ local function worker() if player_status == "Playing" then icon.image = PLAY_ICON_NAME widget.colors = { beautiful.widget_main_color } - widget:set_text(artist, current_song) + artist_widget:set_text(artist) + title_widget:set_text(current_song) + progress_widget.value = tonumber(words[5]) / tonumber(words[6]) elseif player_status == "Paused" then icon.image = PAUSE_ICON_NAME widget.colors = { beautiful.widget_main_color } - widget:set_text(artist, current_song) + artist_widget:set_text(artist) + title_widget:set_text(current_song) + progress_widget.value = tonumber(words[5]) / tonumber(words[6]) elseif player_status == "Stopped" then icon.image = STOP_ICON_NAME else -- no player is running @@ -176,7 +192,7 @@ local function worker() ) ) - watch(string.format(GET_MPD_CMD, "'" .. default_player .. "'"), 1, update_graphic, mpris_widget) + watch(string.format(GET_MPD_CMD, default_player), 1, update_graphic, mpris_widget) local mpris_popup = awful.widget.watch( "playerctl metadata --format '{{ status }}: {{ artist }} - {{ title }}\n" From 484eb68671d24f4f74d3f5bd964185df79d3c601 Mon Sep 17 00:00:00 2001 From: Artem Tarasov Date: Sun, 19 May 2024 14:38:46 +0200 Subject: [PATCH 02/15] show cover art in the popup --- mpris-widget/init.lua | 59 ++++++++++++++++++++++++++++++------------- 1 file changed, 42 insertions(+), 17 deletions(-) diff --git a/mpris-widget/init.lua b/mpris-widget/init.lua index d8a7e7e..cf37f98 100644 --- a/mpris-widget/init.lua +++ b/mpris-widget/init.lua @@ -11,7 +11,7 @@ local watch = require("awful.widget.watch") local wibox = require("wibox") local gears = require("gears") -local GET_MPD_CMD = "playerctl -p '%s' -f '{{status}};{{xesam:artist}};{{xesam:title}};{{mpris:artUrl}};{{position}};{{mpris:length}}' metadata" +local GET_MPD_CMD = "playerctl -f '{{status}};{{xesam:artist}};{{xesam:title}};{{mpris:artUrl}};{{position}};{{mpris:length}}' metadata" local TOGGLE_MPD_CMD = "playerctl play-pause" local NEXT_MPD_CMD = "playerctl next" @@ -26,7 +26,7 @@ local LIBRARY_ICON_NAME = PATH_TO_ICONS .. "/symbolic/places/folder-music-symbol local FONT = 'Roboto Mono 18px' -local default_player = '' +local default_player = 'mpv' local icon = wibox.widget { id = "icon", @@ -70,6 +70,20 @@ local mpris_widget = wibox.widget { layout = wibox.layout.fixed.horizontal, } +local cover_art_widget = wibox.widget { + widget = wibox.widget.imagebox, + forced_height = 300, + forced_width = 300, + resize_allowed = true, +} + +local metadata_widget = wibox.widget { + widget = wibox.widget.textbox, + forced_height = 100, + forced_width = 300, +} + + local rows = { layout = wibox.layout.fixed.vertical } local popup = awful.popup { @@ -140,34 +154,45 @@ local function rebuild_popup() popup:setup(rows) end +local function update_metadata(artist, current_song, progress, art_url) + artist_widget:set_text(artist) + title_widget:set_text(current_song) + progress_widget.value = progress + + -- poor man's urldecode + art_url = art_url:gsub("file://", "/") + art_url = art_url:gsub("%%(%x%x)", function(x) return string.char(tonumber(x, 16)) end) + cover_art_widget:set_image(art_url) +end + local function worker() -- retrieve song info - local current_song, artist, player_status + local current_song, artist, player_status, art_url local update_graphic = function(widget, stdout, _, _, _) local words = gears.string.split(stdout, ';') player_status = words[1] artist = words[2] current_song = words[3] + art_url = words[4] + if current_song ~= nil then - if string.len(current_song) > 18 then - current_song = string.sub(current_song, 0, 9) .. ".." + if string.len(current_song) > 30 then + current_song = string.sub(current_song, 0, 28) .. ".." end end if player_status == "Playing" then icon.image = PLAY_ICON_NAME widget.colors = { beautiful.widget_main_color } - artist_widget:set_text(artist) - title_widget:set_text(current_song) - progress_widget.value = tonumber(words[5]) / tonumber(words[6]) + progress = tonumber(words[5]) / tonumber(words[6]) + update_metadata(artist, current_song, progress, art_url) elseif player_status == "Paused" then icon.image = PAUSE_ICON_NAME widget.colors = { beautiful.widget_main_color } - artist_widget:set_text(artist) - title_widget:set_text(current_song) - progress_widget.value = tonumber(words[5]) / tonumber(words[6]) + progress = tonumber(words[5]) / tonumber(words[6]) + update_metadata(artist, current_song, progress, art_url) elseif player_status == "Stopped" then icon.image = STOP_ICON_NAME else -- no player is running @@ -192,7 +217,7 @@ local function worker() ) ) - watch(string.format(GET_MPD_CMD, default_player), 1, update_graphic, mpris_widget) + watch(GET_MPD_CMD, 1, update_graphic, mpris_widget) local mpris_popup = awful.widget.watch( "playerctl metadata --format '{{ status }}: {{ artist }} - {{ title }}\n" @@ -201,7 +226,7 @@ local function worker() function(callback_popup, stdout) local metadata = stdout if callback_popup.visible then - callback_popup:get_widget().text = metadata + metadata_widget:set_text(metadata) callback_popup:move_next_to(mouse.current_widget_geometry) end end, @@ -209,10 +234,10 @@ local function worker() border_color = beautiful.border_color, ontop = true, visible = false, - widget = wibox.widget { - widget = wibox.widget.textbox, - forced_height = 100, - forced_width = 200, + widget = wibox.widget { + cover_art_widget, + metadata_widget, + layout = wibox.layout.fixed.vertical, } } ) From d8376f7bec2d016766e5ee990bbd2269ef5fc2c3 Mon Sep 17 00:00:00 2001 From: Artem Tarasov Date: Sun, 19 May 2024 18:34:45 +0200 Subject: [PATCH 03/15] minor improvements --- mpris-widget/init.lua | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/mpris-widget/init.lua b/mpris-widget/init.lua index cf37f98..11e42f9 100644 --- a/mpris-widget/init.lua +++ b/mpris-widget/init.lua @@ -24,7 +24,7 @@ local PLAY_ICON_NAME = PATH_TO_ICONS .. "/symbolic/actions/media-playback-sta local STOP_ICON_NAME = PATH_TO_ICONS .. "/symbolic/actions/media-playback-stop-symbolic.svg" local LIBRARY_ICON_NAME = PATH_TO_ICONS .. "/symbolic/places/folder-music-symbolic.svg" -local FONT = 'Roboto Mono 18px' +local FONT = 'Roboto Condensed 16px' local default_player = 'mpv' @@ -72,13 +72,14 @@ local mpris_widget = wibox.widget { local cover_art_widget = wibox.widget { widget = wibox.widget.imagebox, - forced_height = 300, + forced_height = 0, forced_width = 300, resize_allowed = true, } local metadata_widget = wibox.widget { widget = wibox.widget.textbox, + font = FONT, forced_height = 100, forced_width = 300, } @@ -162,12 +163,19 @@ local function update_metadata(artist, current_song, progress, art_url) -- poor man's urldecode art_url = art_url:gsub("file://", "/") art_url = art_url:gsub("%%(%x%x)", function(x) return string.char(tonumber(x, 16)) end) - cover_art_widget:set_image(art_url) + + if art_url ~= nil and art_url ~= "" then + cover_art_widget.image = art_url + cover_art_widget.forced_height = 300 + else + cover_art_widget.image = nil + cover_art_widget.forced_height = 0 + end end local function worker() -- retrieve song info - local current_song, artist, player_status, art_url + local current_song, artist, player_status, art_url, progress local update_graphic = function(widget, stdout, _, _, _) local words = gears.string.split(stdout, ';') @@ -178,20 +186,24 @@ local function worker() art_url = words[4] if current_song ~= nil then - if string.len(current_song) > 30 then - current_song = string.sub(current_song, 0, 28) .. ".." + if string.len(current_song) > 40 then + current_song = string.sub(current_song, 0, 38) .. "…" end end if player_status == "Playing" then icon.image = PLAY_ICON_NAME widget.colors = { beautiful.widget_main_color } - progress = tonumber(words[5]) / tonumber(words[6]) + if words[5] ~= nil and words[6] ~= nil then + progress = tonumber(words[5]) / tonumber(words[6]) + end update_metadata(artist, current_song, progress, art_url) elseif player_status == "Paused" then icon.image = PAUSE_ICON_NAME widget.colors = { beautiful.widget_main_color } - progress = tonumber(words[5]) / tonumber(words[6]) + if words[5] ~= nil and words[6] ~= nil then + progress = tonumber(words[5]) / tonumber(words[6]) + end update_metadata(artist, current_song, progress, art_url) elseif player_status == "Stopped" then icon.image = STOP_ICON_NAME From f944270602e624debe62b5466c23c4ef1acc4b99 Mon Sep 17 00:00:00 2001 From: Artem Tarasov Date: Mon, 20 May 2024 19:05:59 +0200 Subject: [PATCH 04/15] refactoring + fix player selection --- mpris-widget/init.lua | 320 ++++++++++++++++++++++++------------------ 1 file changed, 182 insertions(+), 138 deletions(-) diff --git a/mpris-widget/init.lua b/mpris-widget/init.lua index 11e42f9..da9d5ec 100644 --- a/mpris-widget/init.lua +++ b/mpris-widget/init.lua @@ -11,13 +11,6 @@ local watch = require("awful.widget.watch") local wibox = require("wibox") local gears = require("gears") -local GET_MPD_CMD = "playerctl -f '{{status}};{{xesam:artist}};{{xesam:title}};{{mpris:artUrl}};{{position}};{{mpris:length}}' metadata" - -local TOGGLE_MPD_CMD = "playerctl play-pause" -local NEXT_MPD_CMD = "playerctl next" -local PREV_MPD_CMD = "playerctl previous" -local LIST_PLAYERS_CMD = "playerctl -l" - local PATH_TO_ICONS = "/usr/share/icons/Adwaita" local PAUSE_ICON_NAME = PATH_TO_ICONS .. "/symbolic/actions/media-playback-pause-symbolic.svg" local PLAY_ICON_NAME = PATH_TO_ICONS .. "/symbolic/actions/media-playback-start-symbolic.svg" @@ -26,7 +19,64 @@ local LIBRARY_ICON_NAME = PATH_TO_ICONS .. "/symbolic/places/folder-music-symbol local FONT = 'Roboto Condensed 16px' -local default_player = 'mpv' +local playerctl = { + player_name = 'mpv', +} + +function playerctl:set_player(name) + self.player_name = name + + if self.timer ~= nil then + self.timer:stop() + playerctl:watch(self.watch_params.timeout, self.watch_params.callback, self.watch_params.widget) + end +end + +function playerctl:cmd(cmd) + return "playerctl -p '" .. self.player_name .. "' " .. cmd +end + +function playerctl:watch(timeout, callback, widget) + local cmd = self:cmd("-f '{{status}};{{xesam:artist}};{{xesam:title}};{{mpris:artUrl}};{{position}};{{mpris:length}};{{album}}' metadata") + + self.watch_params = {timeout = timeout, callback = callback, widget = widget} + + local cb = function(widget, stdout, _, _, _) + local words = gears.string.split(stdout, ';') + + local progress + if words[5] ~= nil and words[6] ~= nil then + progress = tonumber(words[5]) / tonumber(words[6]) + end + + local metadata = { + status = words[1], + artist = words[2], + current_song = words[3], + art_url = words[4], + position = words[5], + length = words[6], + album = words[7], + progress = progress, + } + + callback(widget, metadata) + end + + _, self.timer = awful.widget.watch(cmd, timeout, cb, widget) +end + +function playerctl:toggle() + awful.spawn(self:cmd("play-pause"), false) +end + +function playerctl:next() + awful.spawn(self:cmd("next"), false) +end + +function playerctl:prev() + awful.spawn(self:cmd("previous"), false) +end local icon = wibox.widget { id = "icon", @@ -84,128 +134,140 @@ local metadata_widget = wibox.widget { forced_width = 300, } +local player_selector_popup = { + popup = awful.popup { + bg = beautiful.bg_normal, + fg = beautiful.fg_normal, + ontop = true, + visible = false, + shape = gears.shape.rounded_rect, + border_width = 1, + border_color = beautiful.bg_focus, + maximum_width = 400, + offset = { y = 5 }, + widget = {} + }, -local rows = { layout = wibox.layout.fixed.vertical } - -local popup = awful.popup { - bg = beautiful.bg_normal, - fg = beautiful.fg_normal, - ontop = true, - visible = false, - shape = gears.shape.rounded_rect, - border_width = 1, - border_color = beautiful.bg_focus, - maximum_width = 400, - offset = { y = 5 }, - widget = {} + rows = { layout = wibox.layout.fixed.vertical }, } -local function rebuild_popup() - awful.spawn.easy_async(LIST_PLAYERS_CMD, function(stdout, _, _, _) - for i = 0, #rows do rows[i] = nil end - for player_name in stdout:gmatch("[^\r\n]+") do - if player_name ~= '' and player_name ~= nil then - local checkbox = wibox.widget { - { - checked = player_name == default_player, - color = beautiful.bg_normal, - paddings = 2, - shape = gears.shape.circle, - forced_width = 20, - forced_height = 20, - check_color = beautiful.fg_normal, - widget = wibox.widget.checkbox - }, - valign = 'center', - layout = wibox.container.place, - } +function player_selector_popup:add_radio_button(player_name) + local checkbox = wibox.widget { + { + checked = player_name == playerctl.player_name, + color = beautiful.bg_normal, + paddings = 2, + shape = gears.shape.circle, + forced_width = 20, + forced_height = 20, + check_color = beautiful.fg_normal, + widget = wibox.widget.checkbox + }, + valign = 'center', + layout = wibox.container.place, + } - checkbox:connect_signal("button::press", function() - default_player = player_name - rebuild_popup() - end) - - table.insert(rows, wibox.widget { - { - { - checkbox, - { - { - text = player_name, - align = 'left', - widget = wibox.widget.textbox - }, - left = 10, - layout = wibox.container.margin - }, - spacing = 8, - layout = wibox.layout.align.horizontal - }, - margins = 4, - layout = wibox.container.margin - }, - bg = beautiful.bg_normal, - fg = beautiful.fg_normal, - widget = wibox.container.background - }) - end - end + checkbox:connect_signal("button::press", function() + playerctl:set_player(player_name) + self:toggle() end) - popup:setup(rows) + table.insert(self.rows, wibox.widget { + { + { + checkbox, + { + { + text = player_name, + align = 'left', + widget = wibox.widget.textbox + }, + left = 10, + layout = wibox.container.margin + }, + spacing = 8, + layout = wibox.layout.align.horizontal + }, + margins = 4, + layout = wibox.container.margin + }, + bg = beautiful.bg_normal, + fg = beautiful.fg_normal, + widget = wibox.container.background + }) end -local function update_metadata(artist, current_song, progress, art_url) - artist_widget:set_text(artist) - title_widget:set_text(current_song) - progress_widget.value = progress +function player_selector_popup:rebuild() + self.rows = { layout = wibox.layout.fixed.vertical } + awful.spawn.easy_async("playerctl -l", function(stdout, _, _, _) + for name in stdout:gmatch("[^\r\n]+") do + if name ~= '' and name ~= nil then + self:add_radio_button(name) + end + end - -- poor man's urldecode - art_url = art_url:gsub("file://", "/") - art_url = art_url:gsub("%%(%x%x)", function(x) return string.char(tonumber(x, 16)) end) + self.popup:setup(self.rows) + self.popup.visible = true + self.popup:move_next_to(mouse.current_widget_geometry) + end) +end - if art_url ~= nil and art_url ~= "" then - cover_art_widget.image = art_url - cover_art_widget.forced_height = 300 +function player_selector_popup:toggle() + if self.popup.visible then + self.popup.visible = false else - cover_art_widget.image = nil - cover_art_widget.forced_height = 0 + self:rebuild() end end +local function duration(microseconds) + local seconds = microseconds / 1000000 + local minutes = seconds / 60 + seconds = seconds % 60 + local hours = minutes / 60 + minutes = minutes % 60 + if hours >= 1 then + return string.format("%.f:%02.f:%02.f", hours, minutes, seconds) + end + return string.format("%.f:%02.f", minutes, seconds) +end + local function worker() - -- retrieve song info - local current_song, artist, player_status, art_url, progress + local update_metadata = function(meta) + artist_widget:set_text(meta.artist) + title_widget:set_text(meta.current_song) + metadata_widget:set_text(string.format('%s - %s (%s/%s)', meta.album, meta.current_song, duration(meta.position), duration(meta.length))) + progress_widget.value = meta.progress - local update_graphic = function(widget, stdout, _, _, _) - local words = gears.string.split(stdout, ';') - player_status = words[1] - artist = words[2] - current_song = words[3] + -- poor man's urldecode + local art_url = meta.art_url:gsub("file://", "/") + art_url = art_url:gsub("%%(%x%x)", function(x) return string.char(tonumber(x, 16)) end) - art_url = words[4] + if art_url ~= nil and art_url ~= "" then + cover_art_widget.image = art_url + cover_art_widget.forced_height = 300 + else + cover_art_widget.image = nil + cover_art_widget.forced_height = 0 + end + end - if current_song ~= nil then - if string.len(current_song) > 40 then - current_song = string.sub(current_song, 0, 38) .. "…" + local update_graphic = function(widget, metadata) + if metadata.current_song ~= nil then + if string.len(metadata.current_song) > 40 then + metadata.current_song = string.sub(metadata.current_song, 0, 38) .. "…" end end - if player_status == "Playing" then + if metadata.status == "Playing" then icon.image = PLAY_ICON_NAME widget.colors = { beautiful.widget_main_color } - if words[5] ~= nil and words[6] ~= nil then - progress = tonumber(words[5]) / tonumber(words[6]) - end - update_metadata(artist, current_song, progress, art_url) - elseif player_status == "Paused" then + update_metadata(metadata) + elseif metadata.status == "Paused" then icon.image = PAUSE_ICON_NAME widget.colors = { beautiful.widget_main_color } - if words[5] ~= nil and words[6] ~= nil then - progress = tonumber(words[5]) / tonumber(words[6]) - end - update_metadata(artist, current_song, progress, art_url) - elseif player_status == "Stopped" then + update_metadata(metadata) + elseif metadata.status == "Stopped" then icon.image = STOP_ICON_NAME else -- no player is running icon.image = LIBRARY_ICON_NAME @@ -215,48 +277,30 @@ local function worker() mpris_widget:buttons( awful.util.table.join( - awful.button({}, 3, function() - if popup.visible then - popup.visible = not popup.visible - else - rebuild_popup() - popup:move_next_to(mouse.current_widget_geometry) - end - end), - awful.button({}, 4, function() awful.spawn(NEXT_MPD_CMD, false) end), - awful.button({}, 5, function() awful.spawn(PREV_MPD_CMD, false) end), - awful.button({}, 1, function() awful.spawn(TOGGLE_MPD_CMD, false) end) + awful.button({}, 3, function() player_selector_popup:toggle() end), + awful.button({}, 4, function() playerctl:next() end), + awful.button({}, 5, function() playerctl:prev() end), + awful.button({}, 1, function() playerctl:toggle() end) ) ) - watch(GET_MPD_CMD, 1, update_graphic, mpris_widget) + playerctl:watch(1, update_graphic, mpris_widget) - local mpris_popup = awful.widget.watch( - "playerctl metadata --format '{{ status }}: {{ artist }} - {{ title }}\n" - .. "Duration: {{ duration(position) }}/{{ duration(mpris:length) }}'", - 1, - function(callback_popup, stdout) - local metadata = stdout - if callback_popup.visible then - metadata_widget:set_text(metadata) - callback_popup:move_next_to(mouse.current_widget_geometry) - end - end, - awful.popup { - border_color = beautiful.border_color, - ontop = true, - visible = false, - widget = wibox.widget { - cover_art_widget, - metadata_widget, - layout = wibox.layout.fixed.vertical, - } + local mpris_popup = awful.popup { + border_color = beautiful.border_color, + ontop = true, + visible = false, + widget = wibox.widget { + cover_art_widget, + metadata_widget, + layout = wibox.layout.fixed.vertical, } - ) + } mpris_widget:connect_signal('mouse::enter', function() mpris_popup.visible = true + mpris_popup:move_next_to(mouse.current_widget_geometry) end) mpris_widget:connect_signal('mouse::leave', function() From 2e422691d2e27f400364a7d8a9321df34a9b1fdd Mon Sep 17 00:00:00 2001 From: Artem Tarasov Date: Mon, 20 May 2024 19:48:43 +0200 Subject: [PATCH 05/15] fix duration function, allow to override icons and font --- mpris-widget/init.lua | 172 ++++++++++++++++++++++-------------------- 1 file changed, 90 insertions(+), 82 deletions(-) diff --git a/mpris-widget/init.lua b/mpris-widget/init.lua index da9d5ec..4dc7692 100644 --- a/mpris-widget/init.lua +++ b/mpris-widget/init.lua @@ -11,14 +11,6 @@ local watch = require("awful.widget.watch") local wibox = require("wibox") local gears = require("gears") -local PATH_TO_ICONS = "/usr/share/icons/Adwaita" -local PAUSE_ICON_NAME = PATH_TO_ICONS .. "/symbolic/actions/media-playback-pause-symbolic.svg" -local PLAY_ICON_NAME = PATH_TO_ICONS .. "/symbolic/actions/media-playback-start-symbolic.svg" -local STOP_ICON_NAME = PATH_TO_ICONS .. "/symbolic/actions/media-playback-stop-symbolic.svg" -local LIBRARY_ICON_NAME = PATH_TO_ICONS .. "/symbolic/places/folder-music-symbolic.svg" - -local FONT = 'Roboto Condensed 16px' - local playerctl = { player_name = 'mpv', } @@ -44,9 +36,10 @@ function playerctl:watch(timeout, callback, widget) local cb = function(widget, stdout, _, _, _) local words = gears.string.split(stdout, ';') - local progress - if words[5] ~= nil and words[6] ~= nil then - progress = tonumber(words[5]) / tonumber(words[6]) + local position, length, progress = tonumber(words[5]), tonumber(words[6]) + + if position ~= nil and length ~= nil then + progress = position / length end local metadata = { @@ -54,8 +47,8 @@ function playerctl:watch(timeout, callback, widget) artist = words[2], current_song = words[3], art_url = words[4], - position = words[5], - length = words[6], + position = position, + length = length, album = words[7], progress = progress, } @@ -78,61 +71,6 @@ function playerctl:prev() awful.spawn(self:cmd("previous"), false) end -local icon = wibox.widget { - id = "icon", - widget = wibox.widget.imagebox, - image = PLAY_ICON_NAME -} - -local progress_widget = wibox.widget { - id = 'progress', - widget = wibox.container.arcchart, - icon, - min_value = 0, - max_value = 1, - value = 0, - thickness = 2, - start_angle = 4.71238898, -- 2pi*3/4 - forced_height = 24, - forced_width = 24, - rounded_edge = true, - bg = "#ffffff11", - paddings = 2, -} - -local artist_widget = wibox.widget { - id = 'artist', - font = FONT, - widget = wibox.widget.textbox -} - -local title_widget = wibox.widget { - id = 'title', - font = FONT, - widget = wibox.widget.textbox -} - -local mpris_widget = wibox.widget { - artist_widget, - progress_widget, - title_widget, - spacing = 4, - layout = wibox.layout.fixed.horizontal, -} - -local cover_art_widget = wibox.widget { - widget = wibox.widget.imagebox, - forced_height = 0, - forced_width = 300, - resize_allowed = true, -} - -local metadata_widget = wibox.widget { - widget = wibox.widget.textbox, - font = FONT, - forced_height = 100, - forced_width = 300, -} local player_selector_popup = { popup = awful.popup { @@ -198,8 +136,9 @@ function player_selector_popup:add_radio_button(player_name) end function player_selector_popup:rebuild() - self.rows = { layout = wibox.layout.fixed.vertical } awful.spawn.easy_async("playerctl -l", function(stdout, _, _, _) + for i = 0, #self.rows do self.rows[i] = nil end + for name in stdout:gmatch("[^\r\n]+") do if name ~= '' and name ~= nil then self:add_radio_button(name) @@ -221,18 +160,87 @@ function player_selector_popup:toggle() end local function duration(microseconds) - local seconds = microseconds / 1000000 - local minutes = seconds / 60 - seconds = seconds % 60 - local hours = minutes / 60 - minutes = minutes % 60 - if hours >= 1 then - return string.format("%.f:%02.f:%02.f", hours, minutes, seconds) + if microseconds == nil then + return "--:--" end - return string.format("%.f:%02.f", minutes, seconds) + + local seconds = math.floor(microseconds / 1000000) + local minutes = math.floor(seconds / 60) + seconds = seconds - minutes * 60 + local hours = math.floor(minutes / 60) + minutes = minutes - hours * 60 + if hours >= 1 then + return string.format("%d:%02d:%02d", hours, minutes, seconds) + end + return string.format("%d:%02d", minutes, seconds) end -local function worker() +local mpris_widget = {} + +local function worker(user_args) + local args = user_args or {} + + local font = args.font or 'Roboto Condensed 16px' + + local path_to_icons = "/usr/share/icons/Adwaita" + + local pause_icon = args.pause_icon or path_to_icons .. "/symbolic/actions/media-playback-pause-symbolic.svg" + local play_icon = args.play_icon or path_to_icons .. "/symbolic/actions/media-playback-start-symbolic.svg" + local stop_icon = args.stop_icon or path_to_icons .. "/symbolic/actions/media-playback-stop-symbolic.svg" + local library_icon = args.library_icon or path_to_icons .. "/symbolic/places/folder-music-symbolic.svg" + + local icon = wibox.widget { + widget = wibox.widget.imagebox, + image = play_icon + } + + local progress_widget = wibox.widget { + widget = wibox.container.arcchart, + icon, + min_value = 0, + max_value = 1, + value = 0, + thickness = 2, + start_angle = 4.71238898, -- 2pi*3/4 + forced_height = 24, + forced_width = 24, + rounded_edge = true, + bg = "#ffffff11", + paddings = 2, + } + + local artist_widget = wibox.widget { + font = font, + widget = wibox.widget.textbox + } + + local title_widget = wibox.widget { + font = font, + widget = wibox.widget.textbox + } + + mpris_widget = wibox.widget { + artist_widget, + progress_widget, + title_widget, + spacing = 4, + layout = wibox.layout.fixed.horizontal, + } + + local cover_art_widget = wibox.widget { + widget = wibox.widget.imagebox, + forced_height = 0, + forced_width = 300, + resize_allowed = true, + } + + local metadata_widget = wibox.widget { + widget = wibox.widget.textbox, + font = font, + forced_height = 100, + forced_width = 300, + } + local update_metadata = function(meta) artist_widget:set_text(meta.artist) title_widget:set_text(meta.current_song) @@ -260,17 +268,17 @@ local function worker() end if metadata.status == "Playing" then - icon.image = PLAY_ICON_NAME + icon.image = play_icon widget.colors = { beautiful.widget_main_color } update_metadata(metadata) elseif metadata.status == "Paused" then - icon.image = PAUSE_ICON_NAME + icon.image = pause_icon widget.colors = { beautiful.widget_main_color } update_metadata(metadata) elseif metadata.status == "Stopped" then - icon.image = STOP_ICON_NAME + icon.image = stop_icon else -- no player is running - icon.image = LIBRARY_ICON_NAME + icon.image = library_icon widget.colors = { beautiful.widget_red } end end From 182f80349d2a5e3a31c8360137b0f72c9e4e384d Mon Sep 17 00:00:00 2001 From: Artem Tarasov Date: Mon, 20 May 2024 19:58:40 +0200 Subject: [PATCH 06/15] customizable popup width --- mpris-widget/init.lua | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/mpris-widget/init.lua b/mpris-widget/init.lua index 4dc7692..57518d5 100644 --- a/mpris-widget/init.lua +++ b/mpris-widget/init.lua @@ -188,6 +188,7 @@ local function worker(user_args) local play_icon = args.play_icon or path_to_icons .. "/symbolic/actions/media-playback-start-symbolic.svg" local stop_icon = args.stop_icon or path_to_icons .. "/symbolic/actions/media-playback-stop-symbolic.svg" local library_icon = args.library_icon or path_to_icons .. "/symbolic/places/folder-music-symbolic.svg" + local popup_width = args.popup_width or 300 local icon = wibox.widget { widget = wibox.widget.imagebox, @@ -230,7 +231,7 @@ local function worker(user_args) local cover_art_widget = wibox.widget { widget = wibox.widget.imagebox, forced_height = 0, - forced_width = 300, + forced_width = popup_width, resize_allowed = true, } @@ -238,7 +239,7 @@ local function worker(user_args) widget = wibox.widget.textbox, font = font, forced_height = 100, - forced_width = 300, + forced_width = popup_width, } local update_metadata = function(meta) @@ -253,7 +254,7 @@ local function worker(user_args) if art_url ~= nil and art_url ~= "" then cover_art_widget.image = art_url - cover_art_widget.forced_height = 300 + cover_art_widget.forced_height = popup_width else cover_art_widget.image = nil cover_art_widget.forced_height = 0 From cc3e74e099604390274fdcc0346afc3f65377d8f Mon Sep 17 00:00:00 2001 From: Artem Tarasov Date: Mon, 20 May 2024 20:06:14 +0200 Subject: [PATCH 07/15] show album year in the popup --- mpris-widget/init.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mpris-widget/init.lua b/mpris-widget/init.lua index 57518d5..193d6a3 100644 --- a/mpris-widget/init.lua +++ b/mpris-widget/init.lua @@ -29,7 +29,7 @@ function playerctl:cmd(cmd) end function playerctl:watch(timeout, callback, widget) - local cmd = self:cmd("-f '{{status}};{{xesam:artist}};{{xesam:title}};{{mpris:artUrl}};{{position}};{{mpris:length}};{{album}}' metadata") + local cmd = self:cmd("-f '{{status}};{{xesam:artist}};{{xesam:title}};{{mpris:artUrl}};{{position}};{{mpris:length}};{{album}};{{xesam:contentCreated}}' metadata") self.watch_params = {timeout = timeout, callback = callback, widget = widget} @@ -50,6 +50,7 @@ function playerctl:watch(timeout, callback, widget) position = position, length = length, album = words[7], + year = string.sub(words[8], 0, 4), progress = progress, } @@ -238,14 +239,13 @@ local function worker(user_args) local metadata_widget = wibox.widget { widget = wibox.widget.textbox, font = font, - forced_height = 100, forced_width = popup_width, } local update_metadata = function(meta) artist_widget:set_text(meta.artist) title_widget:set_text(meta.current_song) - metadata_widget:set_text(string.format('%s - %s (%s/%s)', meta.album, meta.current_song, duration(meta.position), duration(meta.length))) + metadata_widget:set_text(string.format('%s (%s)\n%s (%s/%s)', meta.album, meta.year, meta.current_song, duration(meta.position), duration(meta.length))) progress_widget.value = meta.progress -- poor man's urldecode From 08b401442e7f4edb44f63b9e266f4d73c8c7feba Mon Sep 17 00:00:00 2001 From: Artem Tarasov Date: Mon, 20 May 2024 20:07:57 +0200 Subject: [PATCH 08/15] put back forced_height (useful for long album names) --- mpris-widget/init.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/mpris-widget/init.lua b/mpris-widget/init.lua index 193d6a3..166c575 100644 --- a/mpris-widget/init.lua +++ b/mpris-widget/init.lua @@ -239,6 +239,7 @@ local function worker(user_args) local metadata_widget = wibox.widget { widget = wibox.widget.textbox, font = font, + forced_height = 100, forced_width = popup_width, } From 10aeeb71adf6a21b0612a115cf49f9a4062ff71c Mon Sep 17 00:00:00 2001 From: Artem Tarasov Date: Mon, 20 May 2024 20:20:56 +0200 Subject: [PATCH 09/15] handle absence of year in the metadata --- mpris-widget/init.lua | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/mpris-widget/init.lua b/mpris-widget/init.lua index 166c575..20fdc8d 100644 --- a/mpris-widget/init.lua +++ b/mpris-widget/init.lua @@ -246,7 +246,14 @@ local function worker(user_args) local update_metadata = function(meta) artist_widget:set_text(meta.artist) title_widget:set_text(meta.current_song) - metadata_widget:set_text(string.format('%s (%s)\n%s (%s/%s)', meta.album, meta.year, meta.current_song, duration(meta.position), duration(meta.length))) + + local s = meta.album; + if meta.year ~= nil and #meta.year == 4 then + s = s .. " (" .. meta.year .. ")" + end + s = s .. "\n" .. meta.current_song .. " (" .. duration(meta.position) .. "/" .. duration(meta.length) .. ")" + metadata_widget:set_text(s) + progress_widget.value = meta.progress -- poor man's urldecode From 5a1cef608c698084be77beba17780623a68e8610 Mon Sep 17 00:00:00 2001 From: Artem Tarasov Date: Mon, 20 May 2024 20:26:56 +0200 Subject: [PATCH 10/15] allow to override default player --- mpris-widget/init.lua | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/mpris-widget/init.lua b/mpris-widget/init.lua index 20fdc8d..1bd4a5f 100644 --- a/mpris-widget/init.lua +++ b/mpris-widget/init.lua @@ -12,7 +12,7 @@ local wibox = require("wibox") local gears = require("gears") local playerctl = { - player_name = 'mpv', + player_name = nil, } function playerctl:set_player(name) @@ -191,6 +191,8 @@ local function worker(user_args) local library_icon = args.library_icon or path_to_icons .. "/symbolic/places/folder-music-symbolic.svg" local popup_width = args.popup_width or 300 + playerctl.player_name = args.default_player or 'mpv' + local icon = wibox.widget { widget = wibox.widget.imagebox, image = play_icon From a69682b2404ae05499b50334da7d3345fba5b1d1 Mon Sep 17 00:00:00 2001 From: Artem Tarasov Date: Thu, 23 May 2024 08:04:51 +0200 Subject: [PATCH 11/15] use more standard progressbar direction --- mpris-widget/init.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mpris-widget/init.lua b/mpris-widget/init.lua index 1bd4a5f..f590b18 100644 --- a/mpris-widget/init.lua +++ b/mpris-widget/init.lua @@ -209,7 +209,7 @@ local function worker(user_args) forced_height = 24, forced_width = 24, rounded_edge = true, - bg = "#ffffff11", + colors = {"#ffffff11", "black"}, paddings = 2, } @@ -256,7 +256,7 @@ local function worker(user_args) s = s .. "\n" .. meta.current_song .. " (" .. duration(meta.position) .. "/" .. duration(meta.length) .. ")" metadata_widget:set_text(s) - progress_widget.value = meta.progress + progress_widget.values = {1.0 - meta.progress, meta.progress} -- poor man's urldecode local art_url = meta.art_url:gsub("file://", "/") From 0391d98acb73a532003ca0936ec2a2c6e8fa1120 Mon Sep 17 00:00:00 2001 From: Artem Tarasov Date: Thu, 23 May 2024 08:05:59 +0200 Subject: [PATCH 12/15] swap play and pause icons to indicate intended action instead of the status --- mpris-widget/init.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mpris-widget/init.lua b/mpris-widget/init.lua index f590b18..83c273a 100644 --- a/mpris-widget/init.lua +++ b/mpris-widget/init.lua @@ -279,11 +279,11 @@ local function worker(user_args) end if metadata.status == "Playing" then - icon.image = play_icon + icon.image = pause_icon widget.colors = { beautiful.widget_main_color } update_metadata(metadata) elseif metadata.status == "Paused" then - icon.image = pause_icon + icon.image = play_icon widget.colors = { beautiful.widget_main_color } update_metadata(metadata) elseif metadata.status == "Stopped" then From d58ed66d0a14efb6c808090f4173fe6bf3dc7498 Mon Sep 17 00:00:00 2001 From: Artem Tarasov Date: Fri, 24 May 2024 19:16:50 +0200 Subject: [PATCH 13/15] extra protection against division by zero --- mpris-widget/init.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mpris-widget/init.lua b/mpris-widget/init.lua index 83c273a..9fbc3be 100644 --- a/mpris-widget/init.lua +++ b/mpris-widget/init.lua @@ -38,7 +38,7 @@ function playerctl:watch(timeout, callback, widget) local position, length, progress = tonumber(words[5]), tonumber(words[6]) - if position ~= nil and length ~= nil then + if position ~= nil and length ~= nil and length > 0 then progress = position / length end @@ -256,7 +256,7 @@ local function worker(user_args) s = s .. "\n" .. meta.current_song .. " (" .. duration(meta.position) .. "/" .. duration(meta.length) .. ")" metadata_widget:set_text(s) - progress_widget.values = {1.0 - meta.progress, meta.progress} + progress_widget.values = {1.0 - (meta.progress or 0.0), meta.progress or 0.0} -- poor man's urldecode local art_url = meta.art_url:gsub("file://", "/") From cb150775ec272340ca6c7aa491ca8d1e0c347dcf Mon Sep 17 00:00:00 2001 From: Artem Tarasov Date: Sat, 1 Jun 2024 09:17:10 +0200 Subject: [PATCH 14/15] avoid passing nil to string.sub --- mpris-widget/init.lua | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/mpris-widget/init.lua b/mpris-widget/init.lua index 9fbc3be..6340bb1 100644 --- a/mpris-widget/init.lua +++ b/mpris-widget/init.lua @@ -50,10 +50,13 @@ function playerctl:watch(timeout, callback, widget) position = position, length = length, album = words[7], - year = string.sub(words[8], 0, 4), progress = progress, } + if words[8] ~= nil then + metadata.year = string.sub(words[8], 0, 4) + end + callback(widget, metadata) end From 0adeedfe999c9b33a84ea6a733c12c50961372fd Mon Sep 17 00:00:00 2001 From: Artem Tarasov Date: Sat, 1 Jun 2024 09:43:13 +0200 Subject: [PATCH 15/15] formatting with stylua (Spaces, AutoPreferSingle, NoSingleTable, FunctionOnly) --- mpris-widget/init.lua | 176 ++++++++++++++++++++++-------------------- 1 file changed, 91 insertions(+), 85 deletions(-) diff --git a/mpris-widget/init.lua b/mpris-widget/init.lua index 6340bb1..610aa86 100644 --- a/mpris-widget/init.lua +++ b/mpris-widget/init.lua @@ -5,11 +5,11 @@ -- requires - playerctl -- @copyright 2020 ------------------------------------------------- -local awful = require("awful") -local beautiful = require("beautiful") -local watch = require("awful.widget.watch") -local wibox = require("wibox") -local gears = require("gears") +local awful = require('awful') +local beautiful = require('beautiful') +local watch = require('awful.widget.watch') +local wibox = require('wibox') +local gears = require('gears') local playerctl = { player_name = nil, @@ -24,14 +24,25 @@ function playerctl:set_player(name) end end -function playerctl:cmd(cmd) - return "playerctl -p '" .. self.player_name .. "' " .. cmd -end +function playerctl:cmd(cmd) return "playerctl -p '" .. self.player_name .. "' " .. cmd end + +local watch_fields = { + [1] = 'status', + [2] = 'xesam:artist', + [3] = 'xesam:title', + [4] = 'mpris:artUrl', + [5] = 'position', + [6] = 'mpris:length', + [7] = 'album', + [8] = 'xesam:contentCreated', +} + +local watch_cmd = string.format("-f '{{%s}}' metadata", table.concat(watch_fields, '}};{{')) function playerctl:watch(timeout, callback, widget) - local cmd = self:cmd("-f '{{status}};{{xesam:artist}};{{xesam:title}};{{mpris:artUrl}};{{position}};{{mpris:length}};{{album}};{{xesam:contentCreated}}' metadata") + local cmd = self:cmd(watch_cmd) - self.watch_params = {timeout = timeout, callback = callback, widget = widget} + self.watch_params = { timeout = timeout, callback = callback, widget = widget } local cb = function(widget, stdout, _, _, _) local words = gears.string.split(stdout, ';') @@ -63,21 +74,14 @@ function playerctl:watch(timeout, callback, widget) _, self.timer = awful.widget.watch(cmd, timeout, cb, widget) end -function playerctl:toggle() - awful.spawn(self:cmd("play-pause"), false) -end +function playerctl:toggle() awful.spawn(self:cmd('play-pause'), false) end -function playerctl:next() - awful.spawn(self:cmd("next"), false) -end - -function playerctl:prev() - awful.spawn(self:cmd("previous"), false) -end +function playerctl:next() awful.spawn(self:cmd('next'), false) end +function playerctl:prev() awful.spawn(self:cmd('previous'), false) end local player_selector_popup = { - popup = awful.popup { + popup = awful.popup { bg = beautiful.bg_normal, fg = beautiful.fg_normal, ontop = true, @@ -87,34 +91,34 @@ local player_selector_popup = { border_color = beautiful.bg_focus, maximum_width = 400, offset = { y = 5 }, - widget = {} + widget = {}, }, - rows = { layout = wibox.layout.fixed.vertical }, + rows = { layout = wibox.layout.fixed.vertical }, } function player_selector_popup:add_radio_button(player_name) local checkbox = wibox.widget { - { - checked = player_name == playerctl.player_name, - color = beautiful.bg_normal, - paddings = 2, - shape = gears.shape.circle, - forced_width = 20, - forced_height = 20, - check_color = beautiful.fg_normal, - widget = wibox.widget.checkbox - }, - valign = 'center', layout = wibox.container.place, + valign = 'center', + { + checked = player_name == playerctl.player_name, + color = beautiful.bg_normal, + paddings = 2, + shape = gears.shape.circle, + forced_width = 20, + forced_height = 20, + check_color = beautiful.fg_normal, + widget = wibox.widget.checkbox, + }, } - checkbox:connect_signal("button::press", function() + checkbox:connect_signal('button::press', function() playerctl:set_player(player_name) self:toggle() end) - table.insert(self.rows, wibox.widget { + local row = wibox.widget { { { checkbox, @@ -122,28 +126,32 @@ function player_selector_popup:add_radio_button(player_name) { text = player_name, align = 'left', - widget = wibox.widget.textbox + widget = wibox.widget.textbox, }, left = 10, - layout = wibox.container.margin + layout = wibox.container.margin, }, spacing = 8, - layout = wibox.layout.align.horizontal + layout = wibox.layout.align.horizontal, }, margins = 4, - layout = wibox.container.margin + layout = wibox.container.margin, }, bg = beautiful.bg_normal, fg = beautiful.fg_normal, - widget = wibox.container.background - }) + widget = wibox.container.background, + } + + table.insert(self.rows, row) end function player_selector_popup:rebuild() - awful.spawn.easy_async("playerctl -l", function(stdout, _, _, _) - for i = 0, #self.rows do self.rows[i] = nil end + awful.spawn.easy_async('playerctl -l', function(stdout, _, _, _) + for i = 0, #self.rows do + self.rows[i] = nil + end - for name in stdout:gmatch("[^\r\n]+") do + for name in stdout:gmatch('[^\r\n]+') do if name ~= '' and name ~= nil then self:add_radio_button(name) end @@ -165,7 +173,7 @@ end local function duration(microseconds) if microseconds == nil then - return "--:--" + return '--:--' end local seconds = math.floor(microseconds / 1000000) @@ -174,9 +182,9 @@ local function duration(microseconds) local hours = math.floor(minutes / 60) minutes = minutes - hours * 60 if hours >= 1 then - return string.format("%d:%02d:%02d", hours, minutes, seconds) + return string.format('%d:%02d:%02d', hours, minutes, seconds) end - return string.format("%d:%02d", minutes, seconds) + return string.format('%d:%02d', minutes, seconds) end local mpris_widget = {} @@ -186,19 +194,19 @@ local function worker(user_args) local font = args.font or 'Roboto Condensed 16px' - local path_to_icons = "/usr/share/icons/Adwaita" + local path_to_icons = '/usr/share/icons/Adwaita' - local pause_icon = args.pause_icon or path_to_icons .. "/symbolic/actions/media-playback-pause-symbolic.svg" - local play_icon = args.play_icon or path_to_icons .. "/symbolic/actions/media-playback-start-symbolic.svg" - local stop_icon = args.stop_icon or path_to_icons .. "/symbolic/actions/media-playback-stop-symbolic.svg" - local library_icon = args.library_icon or path_to_icons .. "/symbolic/places/folder-music-symbolic.svg" - local popup_width = args.popup_width or 300 + local pause_icon = args.pause_icon or path_to_icons .. '/symbolic/actions/media-playback-pause-symbolic.svg' + local play_icon = args.play_icon or path_to_icons .. '/symbolic/actions/media-playback-start-symbolic.svg' + local stop_icon = args.stop_icon or path_to_icons .. '/symbolic/actions/media-playback-stop-symbolic.svg' + local library_icon = args.library_icon or path_to_icons .. '/symbolic/places/folder-music-symbolic.svg' + local popup_width = args.popup_width or 300 playerctl.player_name = args.default_player or 'mpv' local icon = wibox.widget { widget = wibox.widget.imagebox, - image = play_icon + image = play_icon, } local progress_widget = wibox.widget { @@ -212,18 +220,18 @@ local function worker(user_args) forced_height = 24, forced_width = 24, rounded_edge = true, - colors = {"#ffffff11", "black"}, + colors = { '#ffffff11', 'black' }, paddings = 2, } local artist_widget = wibox.widget { font = font, - widget = wibox.widget.textbox + widget = wibox.widget.textbox, } local title_widget = wibox.widget { font = font, - widget = wibox.widget.textbox + widget = wibox.widget.textbox, } mpris_widget = wibox.widget { @@ -242,30 +250,30 @@ local function worker(user_args) } local metadata_widget = wibox.widget { - widget = wibox.widget.textbox, - font = font, + widget = wibox.widget.textbox, + font = font, forced_height = 100, - forced_width = popup_width, + forced_width = popup_width, } local update_metadata = function(meta) artist_widget:set_text(meta.artist) title_widget:set_text(meta.current_song) - local s = meta.album; + local s = meta.album if meta.year ~= nil and #meta.year == 4 then - s = s .. " (" .. meta.year .. ")" + s = s .. ' (' .. meta.year .. ')' end - s = s .. "\n" .. meta.current_song .. " (" .. duration(meta.position) .. "/" .. duration(meta.length) .. ")" + s = s .. '\n' .. meta.current_song .. ' (' .. duration(meta.position) .. '/' .. duration(meta.length) .. ')' metadata_widget:set_text(s) - progress_widget.values = {1.0 - (meta.progress or 0.0), meta.progress or 0.0} + progress_widget.values = { 1.0 - (meta.progress or 0.0), meta.progress or 0.0 } -- poor man's urldecode - local art_url = meta.art_url:gsub("file://", "/") - art_url = art_url:gsub("%%(%x%x)", function(x) return string.char(tonumber(x, 16)) end) + local art_url = meta.art_url:gsub('file://', '/') + art_url = art_url:gsub('%%(%x%x)', function(x) return string.char(tonumber(x, 16)) end) - if art_url ~= nil and art_url ~= "" then + if art_url ~= nil and art_url ~= '' then cover_art_widget.image = art_url cover_art_widget.forced_height = popup_width else @@ -277,19 +285,19 @@ local function worker(user_args) local update_graphic = function(widget, metadata) if metadata.current_song ~= nil then if string.len(metadata.current_song) > 40 then - metadata.current_song = string.sub(metadata.current_song, 0, 38) .. "…" + metadata.current_song = string.sub(metadata.current_song, 0, 38) .. '…' end end - if metadata.status == "Playing" then + if metadata.status == 'Playing' then icon.image = pause_icon widget.colors = { beautiful.widget_main_color } update_metadata(metadata) - elseif metadata.status == "Paused" then + elseif metadata.status == 'Paused' then icon.image = play_icon widget.colors = { beautiful.widget_main_color } update_metadata(metadata) - elseif metadata.status == "Stopped" then + elseif metadata.status == 'Stopped' then icon.image = stop_icon else -- no player is running icon.image = library_icon @@ -310,27 +318,25 @@ local function worker(user_args) local mpris_popup = awful.popup { border_color = beautiful.border_color, - ontop = true, - visible = false, + ontop = true, + visible = false, widget = wibox.widget { cover_art_widget, metadata_widget, layout = wibox.layout.fixed.vertical, - } + }, } - mpris_widget:connect_signal('mouse::enter', - function() - mpris_popup.visible = true - mpris_popup:move_next_to(mouse.current_widget_geometry) - end) - mpris_widget:connect_signal('mouse::leave', - function() - mpris_popup.visible = false - end) + mpris_widget:connect_signal('mouse::enter', function() + mpris_popup.visible = true + mpris_popup:move_next_to(mouse.current_widget_geometry) + end) + mpris_widget:connect_signal('mouse::leave', function() mpris_popup.visible = false end) --}} return mpris_widget end -return setmetatable(mpris_widget, { __call = function(_, ...) return worker(...) end }) +return setmetatable(mpris_widget, { + __call = function(_, ...) return worker(...) end, +})