From 7c021b66c15b8592ec85e5bd75ee35ff11e3dd20 Mon Sep 17 00:00:00 2001 From: RMT Date: Mon, 28 Sep 2020 22:14:48 +0800 Subject: [PATCH] add battery panel --- theme/fly/widget/battery.lua | 72 +++++++++++++++++++++++++++++++++--- 1 file changed, 67 insertions(+), 5 deletions(-) diff --git a/theme/fly/widget/battery.lua b/theme/fly/widget/battery.lua index 28bf72c..1325192 100644 --- a/theme/fly/widget/battery.lua +++ b/theme/fly/widget/battery.lua @@ -2,27 +2,52 @@ local icons = require("theme.assets.icons") local wibox = require("wibox") local awful = require("awful") local sig = require("theme.fly.signal") +local beautiful = require("beautiful") local constants = require("utils.constants") local battery = {} +local have_moved = false + +local items = {} +local progressbars = {} + function battery:init(config) self.widget = wibox.widget { image = icons.fly.battery_unknown, resize = true, forced_height = config.height, forced_width = config.width, - widget = wibox.widget.imagebox + widget = wibox.widget.imagebox, + } + + self.panel_content = wibox.widget { + spacing = 5, + layout = wibox.layout.fixed.vertical } self.panel = awful.popup { widget = { - { - - }, + self.panel_content, + margins = 5, widget = wibox.container.margin - } + }, + preferred_positions = "bottom", + preferred_anchors = "middle", + bg = beautiful.startup_bg, + visible = false, + ontop = true } + + self.widget:connect_signal("button::press", function(geo, lx, ly, button, mods, w) + if not have_moved then + battery.panel:move_next_to(w) + have_moved = true + else + battery.panel.visible = not battery.panel.visible + end + end) + awesome.connect_signal(sig.battery_manager.update_device, function(data) if data.type == constants.BATTERY_DEVICE_DISPLAY then if data.state == constants.BATTERY_DEVICE_STATE_UNKNOWN then @@ -46,6 +71,43 @@ function battery:init(config) end end else + if items[data.path] then + if progressbars[data.path] then + progressbars[data.path].value = data.percentage + end + else + local markup = data.model + if data.device_type == constants.BATTERY_DEVICE_TYPE_BATTERY then + markup = "Battery" + end + + progressbars[data.path] = wibox.widget { + max_value = 100, + value = data.percentage, + forced_height = 15, + forced_width = 100, + paddings = 1, + border_width = 1, + border_color = beautiful.border_color, + widget = wibox.widget.progressbar, + } + + items[data.path] = wibox.widget { + { + wibox.widget { + markup = markup, + align = 'center', + valign = 'center', + widget = wibox.widget.textbox + }, + progressbars[data.path], + layout = wibox.layout.align.vertical + }, + widget = wibox.container.margin + } + + battery.panel_content:add(items[data.path]) + end end end) end