]> git.rmz.io Git - dotfiles.git/blob - awesome/widgets/mpd_widget.lua
awesome/mpd_widget: center scroll widget when smaller than forced_width
[dotfiles.git] / awesome / widgets / mpd_widget.lua
1 local mpc = require("widgets/mpc")
2 local textbox = require("wibox.widget.textbox")
3 local gears = require("gears")
4 local awful = require("awful")
5 local naughty = require("naughty")
6 local timer = require("gears.timer")
7 local wibox = require("wibox")
8
9 --TODO remove
10 local dbg = require("gears.debug")
11
12 local widget = {}
13 widget._textbox = textbox()
14 widget._currentsong = {}
15
16 widget.scroll = wibox.widget {
17 layout = wibox.container.scroll.horizontal,
18 max_size = 200,
19 step_function = wibox.container.scroll.step_functions.linear_increase,
20 speed = 20,
21 {
22 widget = widget._textbox,
23 }
24 }
25
26 function widget:update_widget()
27 local text = ""
28 local artist = self._currentsong.artist
29 local title = self._currentsong.title
30 local file = self._currentsong.file
31 text = text .. tostring(artist or "") .. " - " .. tostring(title or "")
32 if not artist then
33 text = string.format("%s", title or file )
34 else
35 text = string.format("%s - %s", artist, title)
36 end
37 self._textbox.text = text
38 end
39
40 local function sec_to_min(sec)
41 m, s = math.modf((sec or 0.0) / 60)
42 return string.format('%d:%02d', m, math.floor(s * 60))
43 end
44
45 local function song_duration(elapsed, duration)
46 return sec_to_min(elapsed) .. "/" .. sec_to_min(duration)
47 end
48
49 function widget:get_info(callback)
50 local status = nil
51 self._connection:send("status", function(_, s) status = s end)
52
53 local song = nil
54 self._connection:send("currentsong", function(_, s) song = s end)
55
56 self._connection:send("ping", function()
57
58 local info = {}
59 info.title = status.state .. " " .. status.song .. "/" .. status.playlistlength .. " " .. song_duration(status.elapsed, status.duration)
60 info.text = tostring(song.artist or "") .. " - " .. tostring(song.title or "") .. "\n"
61 .. tostring(song.album or "")
62 callback(info)
63 return false
64 end)
65 end
66
67 function widget:popup_show()
68 self:get_info(function(table)
69 if self._notification then return end
70 self._notification = naughty.notify(
71 { title = table.title
72 -- , icon = img
73 , text = table.text
74 , timeout = 0
75 , destroy = function() self._timer:stop() end
76 })
77 self._timer:start()
78 end)
79 end
80
81 function widget:popup_hide(delay)
82 if self._hide_timer and self._hide_timer.started then
83 self._hide_timer:again()
84 else
85 self._hide_timer = timer(
86 { timeout = delay
87 , autostart = true
88 , single_shot = true
89 , callback = function()
90 if self._hover then return end
91 if not self._notification then return end
92 naughty.destroy(self._notification)
93 self._notification = nil
94 end
95 })
96 end
97 end
98
99 function widget:popup_update()
100 self:get_info(function(table)
101 if not self._notification then return end
102 naughty.replace_text(self._notification, table.title, table.text)
103 end)
104 end
105
106 function widget:popup_oneshot(timeout)
107 if self._notification then
108 self:popup_update()
109 self:popup_hide(5)
110 else
111 self:popup_show()
112 self:popup_hide(5)
113 end
114 end
115
116 function widget:error_handler(err)
117 self._textbox:set_text("Error: " .. tostring(err))
118 timer.start_new(10, function()
119 self._connection:send("ping")
120 end)
121 end
122
123 function widget:run()
124 self._connection = mpc.new(nil, nil, nil, function(err) self:error_handler(err) end,
125 "status", function(_, result)
126 if not self._status or result.state ~= self._status.state then
127 self._status = result
128 self:update_widget()
129 self:popup_oneshot(5)
130 end
131 end,
132 "currentsong", function(_, result)
133 if not self._currentsong or result.id ~= self._currentsong.id then
134 self._currentsong = result
135 self:update_widget()
136 self:popup_oneshot(5)
137 end
138 end
139 )
140
141 self._timer = timer({ timeout = 1 })
142 self._timer:connect_signal("timeout", function() self:popup_update() end)
143
144 self._hover = false;
145 self.scroll:connect_signal("mouse::enter", function() self._hover = true; self:popup_show() end)
146 self.scroll:connect_signal("mouse::leave", function() self._hover = false; self:popup_hide(2) end)
147 end
148
149 widget:run()
150
151 widget.scroll:buttons(gears.table.join(
152 awful.button({}, 1, function() widget._connection:toggle_play() end)
153 , awful.button({}, 4, function() widget._connection:change_volume(5) end)
154 , awful.button({}, 5, function() widget._connection:change_volume(-5) end))
155 )
156
157 globalkeys = gears.table.join(globalkeys,
158 awful.key({ modkey }, "p", function() widget._connection:toggle_play() end,
159 { description = "toogle play", group = "mpd" }),
160 awful.key({ modkey }, "'", function() widget:popup_oneshot(5) end,
161 { description = "show more info", group = "mpd" }),
162 awful.key({ modkey }, ",", function() widget._connection:send("previous") end,
163 { description = "previous track", group = "mpd" }),
164 awful.key({ modkey }, ".", function() widget._connection:send("next") end,
165 { description = "next track", group = "mpd" })
166 )
167 return {
168 layout = wibox.layout.align.horizontal,
169 forced_width = 200,
170 expand = "outside",
171 nil,
172 widget.scroll,
173 nil,
174 }