From 33c943d9b8480c73717338d500615dcda7f004b9 Mon Sep 17 00:00:00 2001 From: Emmanuel Lepage Vallee Date: Mon, 1 Feb 2016 23:39:03 -0500 Subject: [PATCH] widget.background: Add shape border support --- lib/wibox/widget/background.lua | 42 ++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/lib/wibox/widget/background.lua b/lib/wibox/widget/background.lua index cbe2e711a..8ea11954a 100644 --- a/lib/wibox/widget/background.lua +++ b/lib/wibox/widget/background.lua @@ -8,10 +8,12 @@ local base = require("wibox.widget.base") local color = require("gears.color") local surface = require("gears.surface") +local beautiful = require("beautiful") local cairo = require("lgi").cairo local setmetatable = setmetatable local pairs = pairs local type = type +local unpack = unpack or table.unpack local background = { mt = {} } @@ -21,8 +23,18 @@ function background:draw(context, cr, width, height) return end + -- Keep the shape path in case there is a border + self._path = nil + 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() end @@ -35,6 +47,20 @@ function background:draw(context, cr, width, height) cr:set_source(pattern) cr:paint() 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 --- Prepare drawing the children of this widget @@ -104,6 +130,20 @@ function background:set_shape(shape, ...) self:emit_signal("widget::redraw_needed") 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 function background:set_bgimage(image) self.bgimage = surface.load(image)