--------------------------------------------------------------------------- -- An implementation of my battery_widget widget with a dynamicly generated -- icon based on the battery percentage level. -- -- Find more about my battery_widget widget at -- https://github.com/Aire-One/awesome-battery_widget -- -- @author Aire-One -- @copyright 2020 Aire-One --------------------------------------------------------------------------- local lgi = require 'lgi' local cairo = lgi.cairo local rsvg = lgi.Rsvg local atooltip = require 'awful.tooltip' local beautiful = require 'beautiful' local gcolor = require 'gears.color' local imagebox = require 'wibox.widget.imagebox' local battery_widget = require 'battery-widget' local my_battery = {} local mt = {} --- Generate a drawing for the battery icon. -- @tparam number percentage The percentage of battery remaining. -- @tparam gears.color|string color The color of the drawing. -- @treturn cairo.ImageSurface The generated surface for the drawing. function my_battery.draw_battery (percentage, color) local svg_handle = rsvg.Handle.new_from_file(beautiful.icon_battery_outline) if not svg_handle then return end local surface = cairo.ImageSurface.create(cairo.Format.ARGB32, 24, 24) local cr = cairo.Context(surface) svg_handle:render_cairo(cr) local max_height = 14 local x = 8 local width = 8 local height = percentage / 100 * max_height local y = 6 + max_height - height cr:set_source(gcolor(color)) cr:rectangle(x, y, width, height) cr:fill() return surface end --- Update handler for the battery widget. This is the function called -- by the signal system on battery update. -- @tparam my_battery widget The battery widget to update. -- @tparam UPowerGLib.Device device The UPower device to monitor. function my_battery.update (widget, device) widget.image = my_battery.draw_battery(device.percentage, widget.color) end --- Give the widget template for the battery widget. -- (It's a basic `wibox.widget.imagebox` for my battery implementation) function my_battery.widget_template () local widget = imagebox() widget.resize = true return widget end --- Constructor of my battery widget! -- @tparam table args Table of parameters. -- @tparam screen|number args.screen The screen of the wodget. -- @tparam string args.device_path The path of the UPower device to monitor. -- @tparam gears.color|string args.color Color to use to draw the battery. -- @treturn my_battery The instantiated widget. function my_battery.new (args) local widget = battery_widget { screen = args.screen, device_path = args.device_path, widget_template = my_battery.widget_template(), instant_update = true } widget.color = args.color widget:connect_signal('upower::update', function (w, device) my_battery.update(w, device) end) widget.tooltip = atooltip { objects = { widget }, timer_function = function () return string.format('%3d', widget.device.percentage) .. '%' end } return widget end function mt.__call (self, ...) return my_battery.new(...) end return setmetatable(my_battery, mt)