wrap widget in a function

This commit is contained in:
streetturtle 2020-11-23 09:55:37 -05:00
parent 0e1d31ad6f
commit 30199568c6
1 changed files with 106 additions and 95 deletions

View File

@ -3,7 +3,7 @@
-- Modelled after Pavel Makhov's work -- Modelled after Pavel Makhov's work
-- @author Mohammed Gaber -- @author Mohammed Gaber
-- requires - playerctl -- requires - playerctl
-- @copyright 2020 -- @copyright 2020
------------------------------------------------- -------------------------------------------------
local awful = require("awful") local awful = require("awful")
local beautiful = require("beautiful") local beautiful = require("beautiful")
@ -19,7 +19,7 @@ local TOGGLE_MPD_CMD = "playerctl play-pause"
local PAUSE_MPD_CMD = "playerctl pause" local PAUSE_MPD_CMD = "playerctl pause"
local STOP_MPD_CMD = "playerctl stop" local STOP_MPD_CMD = "playerctl stop"
local NEXT_MPD_CMD = "playerctl next" local NEXT_MPD_CMD = "playerctl next"
local PREV_MPD_CMD = "playerctl prev" local PREV_MPD_CMD = "playerctl previous"
local PATH_TO_ICONS = "/usr/share/icons/Arc" local PATH_TO_ICONS = "/usr/share/icons/Arc"
local PAUSE_ICON_NAME = PATH_TO_ICONS .. "/actions/24/player_pause.png" local PAUSE_ICON_NAME = PATH_TO_ICONS .. "/actions/24/player_pause.png"
@ -27,110 +27,121 @@ 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 STOP_ICON_NAME = PATH_TO_ICONS .. "/actions/24/player_stop.png"
local LIBRARY_ICON_NAME = PATH_TO_ICONS .. "/actions/24/music-library.png" local LIBRARY_ICON_NAME = PATH_TO_ICONS .. "/actions/24/music-library.png"
-- retriving song info
current_song, artist = nil, nil
local icon = wibox.widget { local mpdarc_widget = {}
id = "icon",
widget = wibox.widget.imagebox,
image = PLAY_ICON_NAME
}
local mirrored_icon = wibox.container.mirror(icon, {horizontal = true})
local mpdarc = wibox.widget { local function worker(args)
mirrored_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
}
local mpdarc_icon_widget = wibox.container.mirror(mpdarc, {horizontal = true}) -- retriving song info
local mpdarc_current_song_widget = wibox.widget { local current_song, artist, mpdstatus
id = 'current_song',
widget = wibox.widget.textbox,
font = 'Play 10'
}
local update_graphic = function(widget, stdout, _, _, _) local icon = wibox.widget {
mpdstatus, artist, current_song = stdout:match("(%w+)%;+(.-)%;(.*)") id = "icon",
if current_song ~= nil then widget = wibox.widget.imagebox,
if current_song.len == 18 then image = PLAY_ICON_NAME
current_song = string.sub(current_song, 0, 9) .. ".." }
local mirrored_icon = wibox.container.mirror(icon, {horizontal = true})
local mpdarc = wibox.widget {
mirrored_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
}
local mpdarc_icon_widget = wibox.container.mirror(mpdarc, {horizontal = true})
local mpdarc_current_song_widget = wibox.widget {
id = 'current_song',
widget = wibox.widget.textbox,
font = 'Play 10'
}
local update_graphic = function(widget, stdout, _, _, _)
mpdstatus, artist, current_song = stdout:match("(%w+)%;+(.-)%;(.*)")
if current_song ~= nil then
if current_song.len == 18 then
current_song = string.sub(current_song, 0, 9) .. ".."
end
end
if mpdstatus == "Playing" then
mpdarc_icon_widget.visible = true
icon.image = PLAY_ICON_NAME
widget.colors = {beautiful.widget_main_color}
mpdarc_current_song_widget.markup = current_song
elseif mpdstatus == "Paused" then
mpdarc_icon_widget.visible = true
icon.image = PAUSE_ICON_NAME
widget.colors = {beautiful.widget_main_color}
mpdarc_current_song_widget.markup = current_song
elseif mpdstatus == "Stopped" then
mpdarc_icon_widget.visible = true
icon.image = STOP_ICON_NAME
mpdarc_current_song_widget.markup = ""
else -- no player is running
icon.image = LIBRARY_ICON_NAME
mpdarc_icon_widget.visible = false
mpdarc_current_song_widget.markup = ""
widget.colors = {beautiful.widget_red}
end end
end end
if mpdstatus == "Playing" then mpdarc:connect_signal("button::press", function(_, _, _, button)
mpdarc_icon_widget.visible = true if (button == 1) then
icon.image = PLAY_ICON_NAME awful.spawn(TOGGLE_MPD_CMD, false) -- left click
widget.colors = {beautiful.widget_main_color} elseif (button == 2) then
mpdarc_current_song_widget.markup = current_song awful.spawn(STOP_MPD_CMD, false)
elseif mpdstatus == "Paused" then elseif (button == 3) then
mpdarc_icon_widget.visible = true awful.spawn(PAUSE_MPD_CMD, false)
icon.image = PAUSE_ICON_NAME elseif (button == 4) then
widget.colors = {beautiful.widget_main_color} awful.spawn(NEXT_MPD_CMD, false) -- scroll up
mpdarc_current_song_widget.markup = current_song elseif (button == 5) then
elseif mpdstatus == "Stopped" then awful.spawn(PREV_MPD_CMD, false) -- scroll down
mpdarc_icon_widget.visible = true end
icon.image = STOP_ICON_NAME
mpdarc_current_song_widget.markup = ""
else -- no player is running
icon.image = LIBRARY_ICON_NAME
mpdarc_icon_widget.visible = false
mpdarc_current_song_widget.markup = ""
widget.colors = {beautiful.widget_red}
end
end
mpdarc:connect_signal("button::press", function(_, _, _, button) spawn.easy_async(GET_MPD_CMD, function(stdout, stderr, exitreason, exitcode)
if (button == 1) then update_graphic(mpdarc, stdout, stderr, exitreason, exitcode)
awful.spawn(TOGGLE_MPD_CMD, false) -- left click end)
elseif (button == 2) then end)
awful.spawn(STOP_MPD_CMD, false)
elseif (button == 3) then local notification
awful.spawn(PAUSE_MPD_CMD, false) local function show_MPD_status()
elseif (button == 4) then spawn.easy_async(GET_MPD_CMD, function(stdout, _, _, _)
awful.spawn(NEXT_MPD_CMD, false) -- scroll up notification = naughty.notify {
elseif (button == 5) then text = current_song .. " by " .. artist,
awful.spawn(PREV_MPD_CMD, false) -- scroll down title = mpdstatus,
timeout = 5,
hover_timeout = 0.5,
width = 600
}
end)
end end
spawn.easy_async(GET_MPD_CMD, function(stdout, stderr, exitreason, exitcode) mpdarc:connect_signal("mouse::enter", function()
update_graphic(mpdarc, stdout, stderr, exitreason, exitcode) if current_song ~= nil and artist ~= nil then show_MPD_status() end
end) end)
end) mpdarc:connect_signal("mouse::leave",
function() naughty.destroy(notification) end)
watch(GET_MPD_CMD, 1, update_graphic, mpdarc)
mpdarc_widget = wibox.widget {
screen = 'primary',
mpdarc_icon_widget,
mpdarc_current_song_widget,
layout = wibox.layout.align.horizontal
}
return mpdarc_widget
local notification
function show_MPD_status()
spawn.easy_async(GET_MPD_CMD, function(stdout, _, _, _)
notification = naughty.notify {
text = current_song .. " by " .. artist,
title = mpdstatus,
timeout = 5,
hover_timeout = 0.5,
width = 600
}
end)
end end
mpdarc:connect_signal("mouse::enter", function() return setmetatable(mpdarc_widget, { __call = function(_, ...)
if current_song ~= nil and artist ~= nil then show_MPD_status() end return worker(...)
end) end })
mpdarc:connect_signal("mouse::leave",
function() naughty.destroy(notification) end)
watch(GET_MPD_CMD, 1, update_graphic, mpdarc)
local mpdarc_widget = wibox.widget {
screen = 'primary',
mpdarc_icon_widget,
mpdarc_current_song_widget,
layout = wibox.layout.align.horizontal
}
return mpdarc_widget