From: Samir Benmendil Date: Mon, 3 Aug 2020 20:57:47 +0000 (+0100) Subject: awesome/mpd_widget: add persistent popup X-Git-Url: https://git.rmz.io/dotfiles.git/commitdiff_plain/534a10c119de31237acbb8fd75a7a55e3cf2d922 awesome/mpd_widget: add persistent popup --- diff --git a/awesome/widgets/mpc.lua b/awesome/widgets/mpc.lua index f357f18..d870f2f 100644 --- a/awesome/widgets/mpc.lua +++ b/awesome/widgets/mpc.lua @@ -214,6 +214,15 @@ function mpc:change_volume(change) end) end +function mpc:currentsong() + local currentsong + self:send("currentsong", function(err, song) + if err then error(err) end + currentsong = song + end) + return currentsong +end + --[[ -- Example on how to use this (standalone) diff --git a/awesome/widgets/mpd_widget.lua b/awesome/widgets/mpd_widget.lua index 692e9a2..1dc4209 100644 --- a/awesome/widgets/mpd_widget.lua +++ b/awesome/widgets/mpd_widget.lua @@ -1,7 +1,13 @@ local mpc = require("widgets/mpc") local textbox = require("wibox.widget.textbox") +local gears = require("gears") +local awful = require("awful") +local naughty = require("naughty") local timer = require("gears.timer") +--TODO remove +local dbg = require("gears.debug") + mpd_widget = textbox() local function update_widget() @@ -37,6 +43,76 @@ mpd_widget:buttons(gears.table.join( , awful.button({}, 5, function() connection:change_volume(-5) end)) ) +local function sec_to_min(sec) + m, s = math.modf((sec or 0.0) / 60) + return string.format('%d:%02d', m, math.floor(s * 60)) +end + +local function song_duration(elapsed, duration) + return sec_to_min(elapsed) .. "/" .. sec_to_min(duration) +end + +local function get_info(callback) + local status = nil + connection:send("status", function(_, s) status = s end) + + local song = nil + connection:send("currentsong", function(_, s) song = s end) + + timer.start_new(0.10, function() + if status == nil or song == nil then return true end + + dbg.dump(status) + dbg.dump(song) + + local info = {} + info.title = status.state .. " " .. status.song .. "/" .. status.playlistlength .. " " .. song_duration(status.elapsed, status.duration) + info.text = tostring(song.artist or "") .. " - " .. tostring(song.title or "") .. "\n" + .. tostring(song.album or "") + callback(info) + return false + end) +end + +local popup = {} +function popup:show() + get_info(function(table) + if self._notification ~= nil then return end + self._notification = naughty.notify( + { title = table.title + -- , icon = img + , text = table.text + , timeout = 0 + }) + end) +end + +function popup:hide() + if self._notification ~= nil then + naughty.destroy(self._notification) + self._notification = nil + end +end + +function popup:update() + get_info(function(table) + if self._notification == nil then return end + naughty.replace_text(self._notification, table.title, table.text) + end) +end + +function popup:run() + self._timer = timer({ timeout = 1 }) + self._timer:connect_signal("start", function() self:show() end) + self._timer:connect_signal("timeout", function() self:update() end) + self._timer:connect_signal("stop", function() self:hide() end) --TODO delay + + mpd_widget:connect_signal("mouse::enter", function() self._timer:again() end) + mpd_widget:connect_signal("mouse::leave", function() self._timer:stop() end) +end + +popup:run() + globalkeys = gears.table.join(globalkeys, awful.key({ modkey }, "p", function() connection:toggle_play() end, { description = "toogle play", group = "mpd" }),