Hide all implementation, expose just the widget and its config

This commit is contained in:
Stefano Mazzucco 2019-12-13 11:05:41 +00:00
parent 072c113f2a
commit 6ced769c55
1 changed files with 47 additions and 49 deletions

View File

@ -40,16 +40,15 @@ end
local icon_size = 64 local icon_size = 64
local icon_flags = {IconLookupFlags.GENERIC_FALLBACK} local icon_flags = {IconLookupFlags.GENERIC_FALLBACK}
local notification = nil local notification = nil
local device = nil
local widget = wibox.widget { local power_widget = wibox.widget {
resize = true, resize = true,
widget = wibox.widget.imagebox widget = wibox.widget.imagebox
} }
widget.critical_percentage = 5 local function get_percentage(widget)
local percentage = device.Percentage
local function _get_percentage(widget)
local percentage = widget.device.Percentage
if percentage then if percentage then
return math.floor(percentage) return math.floor(percentage)
@ -58,21 +57,21 @@ local function _get_percentage(widget)
return 0 return 0
end end
function widget:_update_icon() local function update_icon(widget)
local icon = icon_theme:lookup_icon( local icon = icon_theme:lookup_icon(
self.device.IconName, device.IconName,
icon_size, icon_size,
icon_flags icon_flags
) )
if icon then if icon then
self.image = icon:load_surface() widget.image = icon:load_surface()
end end
end end
function widget:_maybe_warn(warning_condition, notification_preset) local function maybe_warn(widget, warning_condition, notification_preset)
local warning_level = self.device.warninglevel or "None" local warning_level = device.warninglevel or "None"
local percentage = _get_percentage(self) local percentage = get_percentage(widget)
if warning_condition then if warning_condition then
local msg = (warning_level.name == "None" and "Low" or warning_level.name) .. " battery!" local msg = (warning_level.name == "None" and "Low" or warning_level.name) .. " battery!"
@ -91,95 +90,94 @@ function widget:_maybe_warn(warning_condition, notification_preset)
end end
end end
function widget:_update_tooltip() local function update_tooltip(widget)
if self.device.IsPresent then if device.IsPresent then
local percentage = _get_percentage(self) local percentage = get_percentage(widget)
local charge_status_msg = "" local charge_status_msg = ""
local what local what
local when local when
if self.device.type == power.enums.DeviceType.Battery then if device.type == power.enums.DeviceType.Battery then
if self.device.TimeToEmpty > 0 then if device.TimeToEmpty > 0 then
what = "Emtpy" what = "Emtpy"
when = self.device.TimeToEmpty when = device.TimeToEmpty
elseif self.device.TimeToFull > 0 then elseif device.TimeToFull > 0 then
what = "Full" what = "Full"
when = self.device.TimeToFull when = device.TimeToFull
end end
if when then if when then
charge_status_msg = string.format("\n%s in %s", what, to_hour_min_str(when)) charge_status_msg = string.format("\n%s in %s", what, to_hour_min_str(when))
end end
end end
self.tooltip:set_text( widget.tooltip:set_text(
string.format( string.format(
"%d%% - %s%s", "%d%% - %s%s",
percentage, percentage,
self.device.state.name, device.state.name,
charge_status_msg charge_status_msg
) )
) )
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") widget.tooltip:set_text("Plugged In")
end end
end end
local function _should_warn_critical(widget) local function should_warn_critical(widget)
if not widget.device.IsPresent then if not device.IsPresent then
return false return false
end end
local percentage = _get_percentage(widget) local percentage = get_percentage(widget)
return ( return (
widget.device.state == power.enums.BatteryState.Discharging and device.state == power.enums.BatteryState.Discharging and
( (
percentage <= widget.critical_percentage percentage <= widget.critical_percentage
or widget.device.warninglevel == WarningLevel.Low or device.warninglevel == WarningLevel.Low
or widget.device.warninglevel == WarningLevel.Critical or device.warninglevel == WarningLevel.Critical
) )
) )
end end
function widget:update() local function update(widget)
self.device:update_mappings() device:update_mappings()
self:_update_icon() update_icon(widget)
self:_update_tooltip() update_tooltip(widget)
self:_maybe_warn( maybe_warn(
_should_warn_critical(self), widget,
should_warn_critical(widget),
naughty.config.presets.critical naughty.config.presets.critical
) )
end end
function widget:init() local function init(widget)
local manager = power.Manager
self.manager = manager
-- https://upower.freedesktop.org/docs/UPower.html#UPower.GetDisplayDevice -- https://upower.freedesktop.org/docs/UPower.html#UPower.GetDisplayDevice
self.device = power.create_device("/org/freedesktop/UPower/devices/DisplayDevice") device = power.create_device("/org/freedesktop/UPower/devices/DisplayDevice")
self.device:on_properties_changed( device:on_properties_changed(
function () function ()
self:update() update(widget)
end end
) )
self.tooltip = awful.tooltip({ objects = { widget },}) widget.tooltip = awful.tooltip({ objects = { widget },})
self.gui_client = nil widget.gui_client = ni
widget.critical_percentage = 5
self:update() update(widget)
self:buttons(awful.util.table.join( widget:buttons(awful.util.table.join(
awful.button({ }, 3, awful.button({ }, 3,
function () function ()
if self.gui_client then if widget.gui_client then
spawn_with_shell(self.gui_client) spawn_with_shell(widget.gui_client)
end end
end end
))) )))
return self return widget
end end
return widget:init() return init(power_widget)