diff --git a/lib/wibox/drawable.lua b/lib/wibox/drawable.lua index 75cce527d..10cd8cfd4 100644 --- a/lib/wibox/drawable.lua +++ b/lib/wibox/drawable.lua @@ -123,8 +123,11 @@ local function do_redraw(self) cr:set_source(self.background_color) cr:paint() + cr:restore() + -- Paint the background image if self.background_image then + cr:save() if type(self.background_image) == "function" then self.background_image(context, cr, width, height, unpack(self.background_image_args)) else @@ -132,10 +135,9 @@ local function do_redraw(self) cr:set_source(pattern) cr:paint() end + cr:restore() end - cr:restore() - -- Draw the widget if self._widget_hierarchy then cr:set_source(self.foreground_color) @@ -237,6 +239,9 @@ end -- as arguments. Any other arguments passed to this method will be appended. -- @param image A background image or a function function drawable:set_bgimage(image, ...) + if type(image) ~= "function" then + image = surface(image) + end self.background_image = image self.background_image_args = {...} diff --git a/tests/test-drawable-bgimage.lua b/tests/test-drawable-bgimage.lua new file mode 100644 index 000000000..b3fb7a74f --- /dev/null +++ b/tests/test-drawable-bgimage.lua @@ -0,0 +1,56 @@ +local runner = require("_runner") +local wibox = require("wibox") +local beautiful = require("beautiful") +local cairo = require("lgi").cairo + +local w = wibox { + x = 10, + y = 10, + width = 20, + height = 20, + visible = true, + bg = "#ff0000", +} + +local callback_called +local function callback(context, cr, width, height, arg) + assert(type(context) == "table", type(context)) + assert(type(context.dpi) == "number", context.dpi) + assert(cairo.Context:is_type_of(cr), cr) + assert(width == 20, width) + assert(height == 20, height) + assert(arg == "argument: 42", arg) + + callback_called = true +end + +runner.run_steps({ + -- Set some bg image + function() + local img = assert(beautiful.titlebar_close_button_normal) + w:set_bgimage(img) + return true + end, + + -- Do nothing for a while iteration to give the repaint some time to happen + function(arg) + if arg == 3 then + return true + end + end, + + -- Set some bg image function + function() + w:set_bgimage(callback, "argument: 42") + return true + end, + + -- Wait for the function to be done + function() + if callback_called then + return true + end + end, +}) + +-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80