awesomewm/modules/battery/widget.lua

222 lines
5.5 KiB
Lua
Raw Normal View History

2022-07-16 16:11:20 -07:00
local battery = req_rel(..., "util")
local widget = {}
-- Percentages to warn at
-- (must be in order least -> greatest)
widget.warnings = {
5, 10, 25, 50
}
widget.warninglog = {}
for i=1, #widget.warnings do
widget.warninglog[i] = false
end
2022-07-16 21:37:04 -07:00
widget.image_path = beautiful.icons.battery.missing
2022-07-16 16:11:20 -07:00
widget.icon = wibox.widget {
2022-07-16 21:37:04 -07:00
image = beautiful.icons.battery.missing,
2022-07-16 16:11:20 -07:00
resize = true,
widget = wibox.widget.imagebox,
}
widget.progressbar = wibox.widget {
max_value = 100,
widget = wibox.widget.progressbar,
paddings = beautiful.dpi(2),
color = beautiful.color.bar.active,
background_color = beautiful.color.transparent,
border_color = beautiful.color.bar.active,
border_width = beautiful.dpi(1),
margins = beautiful.dpi(3)
}
widget.arc = wibox.widget {
{
widget.icon,
top = beautiful.dpi(1),
bottom = beautiful.dpi(1),
layout = wibox.container.margin,
},
max_value = 100,
thickness = beautiful.dpi(4),
start_angle = 4.71238898, -- 2pi*3/4
--forced_height = beautiful.dpi(16),
--forced_width = beautiful.dpi(16),
colors = {"#27D4CC", "#00446B"},
bg = "#FFFFFF30",
paddings = beautiful.dpi(2),
widget = wibox.container.arcchart
}
widget.rotator = wibox.widget {
widget.progressbar,
forced_width = beautiful.dpi(15),
direction = "east",
layout = wibox.container.rotate,
}
widget.widget = wibox.widget {
{
{ -- Right space
widget = wibox.widget.separator,
color = beautiful.color.transparent,
forced_width = beautiful.dpi(3)
},
{
widget.arc,
top = beautiful.dpi(2),
bottom = beautiful.dpi(2),
layout = wibox.container.margin,
},
{ -- Left space
widget = wibox.widget.separator,
color = beautiful.color.transparent,
forced_width = beautiful.dpi(3)
},
layout = wibox.layout.align.horizontal,
},
layout = wibox.container.background,
}
widget.widget:connect_signal("mouse::enter", function(result)
widget.widget.bg = beautiful.color.bar.hover_bg
end)
widget.widget:connect_signal("mouse::leave", function(result)
widget.widget.bg = beautiful.color.transparent
end)
widget.update = function(stdout)
local batpec = string.match(stdout, "(%d?%d?%d)%%")
batpec = tonumber(string.format("% 3d", batpec))
2023-08-21 19:53:40 -07:00
local discharging = string.match(stdout, "Discharging") or false
2022-07-16 16:11:20 -07:00
-- Handle low power notifications
if discharging then
for i=1, #widget.warnings do
v = widget.warnings[i]
if (batpec <= v) and (not widget.warninglog[i]) then
widget.warninglog[i] = true
naughty.notify({
title = "Low power",
text = "Battery is at " .. tostring(batpec) .. "%",
2022-07-16 21:37:04 -07:00
icon = beautiful.icons.battery.caution,
2022-07-16 16:11:20 -07:00
timeout = 5,
ignore_suspend = true,
2022-09-10 21:52:40 -07:00
border_color = beautiful.color.battery.danger,
2022-07-16 16:11:20 -07:00
preset = beautiful.notification_templates.bottom_right
})
break
end
end
else
for i=1, #widget.warnings do
if (batpec >= widget.warnings[i]) then
widget.warninglog[i] = false
end
end
end
widget.progressbar.value = batpec
widget.arc.value = batpec
if batpec > 60 then
2022-09-10 21:52:40 -07:00
widget.progressbar.color = beautiful.color.battery.good
2022-07-16 16:11:20 -07:00
elseif batpec > 40 then
2022-09-10 21:52:40 -07:00
widget.progressbar.color = beautiful.color.battery.low
2022-07-16 16:11:20 -07:00
elseif batpec <= 40 then
2022-09-10 21:52:40 -07:00
widget.progressbar.color = beautiful.color.battery.danger
2022-07-16 16:11:20 -07:00
end
2022-07-16 21:37:04 -07:00
widget.image_path = beautiful.icons.battery.missing
2022-07-16 16:11:20 -07:00
-- Set current battery icon
if (not discharging) then
2022-07-16 21:37:04 -07:00
if batpec > 80 then widget.image_path = beautiful.icons.battery.charging.full
elseif batpec > 60 then widget.image_path = beautiful.icons.battery.charging.good
elseif batpec > 40 then widget.image_path = beautiful.icons.battery.charging.low
elseif batpec > 20 then widget.image_path = beautiful.icons.battery.charging.caution
elseif batpec <= 20 then widget.image_path = beautiful.icons.battery.charging.empty end
2022-07-16 16:11:20 -07:00
else
2022-07-16 21:37:04 -07:00
if batpec > 80 then widget.image_path = beautiful.icons.battery.full
elseif batpec > 60 then widget.image_path = beautiful.icons.battery.good
elseif batpec > 40 then widget.image_path = beautiful.icons.battery.low
elseif batpec > 20 then widget.image_path = beautiful.icons.battery.caution
elseif batpec <= 20 then widget.image_path = beautiful.icons.battery.empty end
2022-07-16 16:11:20 -07:00
end
widget.icon.image = widget.image_path
if (not discharging) and (batpec > 90) then
2022-09-10 21:52:40 -07:00
widget.progressbar.border_color = beautiful.color.battery.good
2022-07-16 16:11:20 -07:00
elseif (discharging) and (batpec <= 25) then
2022-09-10 21:52:40 -07:00
widget.progressbar.border_color = beautiful.color.battery.danger
2022-07-16 16:11:20 -07:00
else
widget.progressbar.border_color = beautiful.color.bar.active
end
if discharging then
widget.rotator.direction = "east"
else
widget.rotator.direction = "west"
end
end
widget.readupdate = function()
battery.status(
function(stdout, stderr, exitreason, exitcode)
widget.update(stdout)
end
)
end
widget.widget:connect_signal("button::press",
function(_, _, _, button, mods)
if (button == 1) then
battery.status(
function(stdout, stderr, exitreason, exitcode)
local batpec = string.match(stdout, "(%d?%d?%d)%%") -- (\d?\d?\d)\%)
local batstat = string.match(stdout, "discharging") or false
if batstat then
batstat = "Discharging, "
else
batstat = "Charging, "
end
local out = naughty.notify({
title = "Battery:",
text = batstat .. batpec .. "%",
2022-07-16 21:37:04 -07:00
icon = widget.icon.image,
2022-07-16 16:11:20 -07:00
replaces_id = widget.notid,
ignore_suspend = true,
preset = beautiful.notification_templates.bottom_right
})
widget.notid = out.id
end
)
end
end
)
battery.watch(
10,
function(_, stdout)
widget.update(stdout)
end,
widget.widget
)
return widget