Merge pull request #678 from Elv13/bgimage

Extend bgimage support
This commit is contained in:
Emmanuel Lepage Vallée 2016-02-10 03:59:06 -05:00
commit ff8f2aef27
4 changed files with 54 additions and 9 deletions

View File

@ -95,6 +95,7 @@ local function new(c, args)
local args = bars[position].args
ret:set_bg(get_color("bg", c, args))
ret:set_fg(get_color("fg", c, args))
ret:set_bgimage(get_color("bgimage", c, args))
end
bars[position] = {

View File

@ -22,6 +22,7 @@ local timer = require("gears.timer")
local matrix = require("gears.matrix")
local hierarchy = require("wibox.hierarchy")
local base = require("wibox.widget.base")
local unpack = unpack or table.unpack
local drawables = setmetatable({}, { __mode = 'k' })
local wallpaper = nil
@ -70,18 +71,19 @@ local function do_redraw(self)
local cr = cairo.Context(surf)
local geom = self.drawable:geometry();
local x, y, width, height = geom.x, geom.y, geom.width, geom.height
local context = get_widget_context(self)
-- Relayout
if self._need_relayout or self._need_complete_repaint then
self._need_relayout = false
if self._widget_hierarchy and self.widget then
self._widget_hierarchy:update(get_widget_context(self),
self._widget_hierarchy:update(context,
self.widget, width, height, self._dirty_area)
else
self._need_complete_repaint = true
if self.widget then
self._widget_hierarchy_callback_arg = {}
self._widget_hierarchy = hierarchy.new(get_widget_context(self), self.widget, width, height,
self._widget_hierarchy = hierarchy.new(context, self.widget, width, height,
self._redraw_callback, self._layout_callback, self._widget_hierarchy_callback_arg)
else
self._widget_hierarchy = nil
@ -128,12 +130,24 @@ local function do_redraw(self)
cr:set_source(self.background_color)
cr:paint()
-- Paint the background image
if self.background_image then
if type(self.background_image) == "function" then
self.background_image(context, cr, width, height, unpack(self.background_image_args))
else
local pattern = cairo.Pattern.create_for_surface(self.background_image)
cr:set_source(pattern)
cr:paint()
end
end
cr:restore()
-- Draw the widget
if self._widget_hierarchy then
cr:set_source(self.foreground_color)
self._widget_hierarchy:draw(get_widget_context(self), cr)
self._widget_hierarchy:draw(context, cr)
end
self.drawable:refresh()
@ -199,7 +213,9 @@ end
-- nil or a string that gears.color() understands.
function drawable:set_bg(c)
local c = c or "#000000"
if type(c) == "string" or type(c) == "table" then
local t = type(c)
if t == "string" or t == "table" then
c = color(c)
end
@ -224,6 +240,18 @@ function drawable:set_bg(c)
self._do_complete_repaint()
end
--- Set the background image of the drawable
-- 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 drawable:set_bgimage(image, ...)
self.background_image = image
self.background_image_args = {...}
self._do_complete_repaint()
end
--- Set the foreground of the drawable
-- @param c The foreground to use. This must either be a cairo pattern object,
-- nil or a string that gears.color() understands.

View File

@ -51,6 +51,14 @@ function wibox:set_bg(c)
self._drawable:set_bg(c)
end
--- Set the background image of the drawable
-- 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 wibox:set_bgimage(image, ...)
self._drawable:set_bgimage(image, ...)
end
--- Set the foreground of the wibox
-- @param c The foreground to use. This must either be a cairo pattern object,
-- nil or a string that gears.color() understands.

View File

@ -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
@ -153,8 +157,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