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.
This commit is contained in:
Emmanuel Lepage Vallee 2016-02-08 03:34:31 -05:00
parent 720768330c
commit cbea82f1c8
1 changed files with 13 additions and 5 deletions

View File

@ -43,9 +43,13 @@ function background:draw(context, cr, width, height)
cr:paint() cr:paint()
end end
if self.bgimage then if self.bgimage then
local pattern = cairo.Pattern.create_for_surface(self.bgimage) if type(self.bgimage) == "function" then
cr:set_source(pattern) self.bgimage(context, cr, width, height,unpack(self.bgimage_args))
cr:paint() else
local pattern = cairo.Pattern.create_for_surface(self.bgimage)
cr:set_source(pattern)
cr:paint()
end
end end
end end
@ -145,8 +149,12 @@ function background:set_shape_border_color(fg)
end end
--- Set the background image to use --- Set the background image to use
function background:set_bgimage(image) -- If `image` is a function, it will be called with `(context, cr, width, height)`
self.bgimage = surface.load(image) -- 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") self:emit_signal("widget::redraw_needed")
end end