Refactor code to update the widget

This commit is contained in:
Stefano Mazzucco 2019-12-13 10:09:00 +00:00
parent 9efe9f64c8
commit 834530e200
1 changed files with 41 additions and 22 deletions

View File

@ -47,9 +47,7 @@ local widget = wibox.widget {
widget.critical_percentage = 5 widget.critical_percentage = 5
function widget:update() function widget:_update_icon()
self.device:update_mappings()
local icon = icon_theme:lookup_icon( local icon = icon_theme:lookup_icon(
self.device.IconName, self.device.IconName,
icon_size, icon_size,
@ -59,12 +57,41 @@ function widget:update()
if icon then if icon then
self.image = icon:load_surface() self.image = icon:load_surface()
end end
end
function widget:_maybe_warn()
local warning_level = self.device.warninglevel or "None"
local percentage = math.floor(self.device.Percentage) or 0
local should_warn = false
if self.device.IsPresent then if self.device.IsPresent then
local percentage = math.floor(self.device.Percentage) should_warn = (
local warning_level = self.device.warninglevel self.device.state == power.enums.BatteryState.Discharging and
(
percentage <= self.critical_percentage
or warning_level == WarningLevel.Low
or warning_level == WarningLevel.Critical
)
)
end
if should_warn then
local msg = (warning_level.name == "None" and "Low" or warning_level.name) .. " battery!"
naughty.notify({
preset = naughty.config.presets.critical,
title = msg,
text = percentage .. "% remaining"})
end
end
function widget:_update_tooltip()
if self.device.IsPresent then
local percentage = math.floor(self.device.Percentage)
local charge_status_msg = "" local charge_status_msg = ""
local what local what
local when local when
@ -89,27 +116,19 @@ function widget:update()
charge_status_msg charge_status_msg
) )
) )
local should_warn = (
self.device.state == power.enums.BatteryState.Discharging and
(
percentage <= self.critical_percentage
or warning_level == WarningLevel.Low
or warning_level == WarningLevel.Critical
)
)
if should_warn then
local msg = (warning_level.name == "None" and "Low" or warning_level.name) .. " battery!"
naughty.notify({
preset = naughty.config.presets.critical,
title = msg,
text = percentage .. "% remaining"})
end
else else
-- We don't know how we're powered, but we must be somehow! -- We don't know how we're powered, but we must be somehow!
self.tooltip:set_text("Plugged In") self.tooltip:set_text("Plugged In")
end end
end
function widget:update()
self.device:update_mappings()
self:_update_icon()
self:_update_tooltip()
self:_maybe_warn()
end end
function widget:init() function widget:init()