widget.background: Add shape border support

This commit is contained in:
Emmanuel Lepage Vallee 2016-02-01 23:39:03 -05:00
parent 19437930a2
commit 33c943d9b8
1 changed files with 41 additions and 1 deletions

View File

@ -8,10 +8,12 @@
local base = require("wibox.widget.base") local base = require("wibox.widget.base")
local color = require("gears.color") local color = require("gears.color")
local surface = require("gears.surface") local surface = require("gears.surface")
local beautiful = require("beautiful")
local cairo = require("lgi").cairo local cairo = require("lgi").cairo
local setmetatable = setmetatable local setmetatable = setmetatable
local pairs = pairs local pairs = pairs
local type = type local type = type
local unpack = unpack or table.unpack
local background = { mt = {} } local background = { mt = {} }
@ -21,8 +23,18 @@ function background:draw(context, cr, width, height)
return return
end end
-- Keep the shape path in case there is a border
self._path = nil
if self._shape then if self._shape then
self._shape(cr, width, height, unpack(self._shape_args or {})) -- Only add the offset if there is something to draw
local offset = ((self._shape_border_width and self._shape_border_color)
and self._shape_border_width or 0) / 2
cr:translate(offset, offset)
self._shape(cr, width - 2*offset, height - 2*offset, unpack(self._shape_args or {}))
cr:translate(-offset, -offset)
self._path = cr:copy_path()
cr:clip() cr:clip()
end end
@ -35,6 +47,20 @@ function background:draw(context, cr, width, height)
cr:set_source(pattern) cr:set_source(pattern)
cr:paint() cr:paint()
end end
end
-- Draw the border
function background:after_draw_children(context, cr, width, height)
-- Draw the border
if self._path and self._shape_border_width and self._shape_border_width > 0 then
cr:append_path(self._path)
cr:set_source(color(self._shape_border_color or self.foreground or beautiful.fg_normal))
cr:set_line_width(self._shape_border_width)
cr:stroke()
self._path = nil
end
end end
--- Prepare drawing the children of this widget --- Prepare drawing the children of this widget
@ -104,6 +130,20 @@ function background:set_shape(shape, ...)
self:emit_signal("widget::redraw_needed") self:emit_signal("widget::redraw_needed")
end end
--- When a `shape` is set, also draw a border
-- @tparam number width The border width
function background:set_shape_border_width(width)
self._shape_border_width = width
self:emit_signal("widget::redraw_needed")
end
--- When a `shape` is set, also draw a border
-- @param[opt=self.foreground] fg The border color, pattern or gradient
function background:set_shape_border_color(fg)
self._shape_border_color = fg
self:emit_signal("widget::redraw_needed")
end
--- Set the background image to use --- Set the background image to use
function background:set_bgimage(image) function background:set_bgimage(image)
self.bgimage = surface.load(image) self.bgimage = surface.load(image)