]> git.rmz.io Git - dotfiles.git/commitdiff
awesome/mpd_widget: add persistent popup
authorSamir Benmendil <me@rmz.io>
Mon, 3 Aug 2020 20:57:47 +0000 (21:57 +0100)
committerSamir Benmendil <me@rmz.io>
Sat, 8 Aug 2020 10:48:16 +0000 (11:48 +0100)
awesome/widgets/mpc.lua
awesome/widgets/mpd_widget.lua

index f357f18b48b54dd85a70241b5a41dc7e2534e818..d870f2f8f3cc374b63be48fd23f43a65a5a4450c 100644 (file)
@@ -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)
index 692e9a2ba8969a6a21355b2bf541182ca3629b48..1dc42093e36e2b8db97b98fc7a8e0672da06ab8d 100644 (file)
@@ -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" }),