From cbea82f1c8a25e0c85007f4673ac6eb887c245eb Mon Sep 17 00:00:00 2001 From: Emmanuel Lepage Vallee Date: Mon, 8 Feb 2016 03:34:31 -0500 Subject: [PATCH] widget.background: Allow function as background image There is already a hack into `awful.widget.common`. This system aim to make the hack obselete while preserving the useful part. I think this is also necessary to properly support SVG (with DPI and resize). Finally, Qt handle this using the QBrush concept, where you can have programmatic patterns. Cairo doesn't have this concept, so there is no "clean" way to have programmatic brushes. --- lib/wibox/widget/background.lua | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/lib/wibox/widget/background.lua b/lib/wibox/widget/background.lua index 8ea11954..bc5c9141 100644 --- a/lib/wibox/widget/background.lua +++ b/lib/wibox/widget/background.lua @@ -43,9 +43,13 @@ function background:draw(context, cr, width, height) cr:paint() end if self.bgimage then - local pattern = cairo.Pattern.create_for_surface(self.bgimage) - cr:set_source(pattern) - cr:paint() + if type(self.bgimage) == "function" then + self.bgimage(context, cr, width, height,unpack(self.bgimage_args)) + else + local pattern = cairo.Pattern.create_for_surface(self.bgimage) + cr:set_source(pattern) + cr:paint() + end end end @@ -145,8 +149,12 @@ function background:set_shape_border_color(fg) end --- Set the background image to use -function background:set_bgimage(image) - self.bgimage = surface.load(image) +-- If `image` is a function, it will be called with `(context, cr, width, height)` +-- as arguments. Any other arguments passed to this method will be appended. +-- @param image A background image or a function +function background:set_bgimage(image, ...) + self.bgimage = type(image) == "function" and image or surface.load(image) + self.bgimage_args = {...} self:emit_signal("widget::redraw_needed") end