From cbea82f1c8a25e0c85007f4673ac6eb887c245eb Mon Sep 17 00:00:00 2001 From: Emmanuel Lepage Vallee Date: Mon, 8 Feb 2016 03:34:31 -0500 Subject: [PATCH 1/4] 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 8ea11954a..bc5c91419 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 From 76669765387097388a9258342fcbc24753864c54 Mon Sep 17 00:00:00 2001 From: Emmanuel Lepage Vallee Date: Mon, 8 Feb 2016 03:37:09 -0500 Subject: [PATCH 2/4] drawable: Add background image support Copy what's done in `widget.background` --- lib/wibox/drawable.lua | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/lib/wibox/drawable.lua b/lib/wibox/drawable.lua index 0e6fb0208..cce232369 100644 --- a/lib/wibox/drawable.lua +++ b/lib/wibox/drawable.lua @@ -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. From c957b3d5a88b0ae91850c5b4fdf96b6a840b673b Mon Sep 17 00:00:00 2001 From: Emmanuel Lepage Vallee Date: Mon, 8 Feb 2016 03:37:37 -0500 Subject: [PATCH 3/4] wibox: Support bg_image --- lib/wibox/init.lua | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/wibox/init.lua b/lib/wibox/init.lua index 5952571ae..9c14e5086 100644 --- a/lib/wibox/init.lua +++ b/lib/wibox/init.lua @@ -43,6 +43,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. From 4a20f9e5333356cb6413b6f3f3f5e5f2411b78f6 Mon Sep 17 00:00:00 2001 From: Emmanuel Lepage Vallee Date: Mon, 8 Feb 2016 03:37:49 -0500 Subject: [PATCH 4/4] titlebar: Support bgimage --- lib/awful/titlebar.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/awful/titlebar.lua b/lib/awful/titlebar.lua index 41ec779b5..6834f488a 100644 --- a/lib/awful/titlebar.lua +++ b/lib/awful/titlebar.lua @@ -89,6 +89,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] = {