Search code examples
luaxorgawesome-wm

How to create a imagebox based battery widget in AwesomeWM


I am exploring Awesome WM and Vicious, and I'd like to create an wibox.widget.imagebox based battery widget using vicious.widgets.bat template.

So here's a problem: how to change image property of the widget.

The icons are set in theme.lua:

local icons_dir = os.getenv("HOME") .. "/.config/awesome/deco/icons/"

...

theme.battery = {}
for i = 0, 10 do
    theme.battery[i] = icons_dir.."bat/b"..(i*10)..".svg"
end

theme.battery_charging = {}
for i = 0, 10 do
    theme.battery_charging[i] = icons_dir.."bat/b"..(i*10).."c.svg"
end

This doesn't work:

mybat = wibox.widget.imagebox()
vicious.register(mybat, vicious.widgets.bat,
                 function (widget, args)
                     if args[1] == "+" then
                         return beautiful.battery_charging[((args[2] + 5) - (args[2] + 5) % 10) / 10]
                     else
                         return beautiful.battery[((args[2] + 5) - (args[2] + 5) % 10) / 10]
                     end
                 end, 30, "BAT0")

Interestingly, if we set mybat as a wibox.widget.textbox widget, it correctly shows the proper path to icon.

Neither works this:

mybat = wibox.widget.imagebox()
vicious.register(mybat, vicious.widgets.bat,
                 function (widget, args)
                     if args[1] == "+" then
                         widget.image = beautiful.battery_charging[((args[2] + 5) - (args[2] + 5) % 10) / 10]
                     else
                         widget.image = beautiful.battery[((args[2] + 5) - (args[2] + 5) % 10) / 10]
                     end
                 end, 30, "BAT0")

mybat simply remains empty.

Does anybody know how to dynamically change imagebox content?


Solution

  • Sorry about that i rushed it here you go.

     vicious.battery = wibox.widget.imagebox()
     vicious.battery:set_image("battery.png")
    
    
     vicious.battery:set_width(50)
     vicious.battery:set_height(50)
     vicious.battery:set_background_color("#000000")
     vicious.battery:set_border_color("#FFFFFF")
     vicious.battery:set_border_width(2)
    
    
     vicious.register(vicious.battery, vicious.widgets.bat, "$2", 61, 
     "BAT0")