From 03663fe778794c2e4229a4d03a0bd24060f3b6a0 Mon Sep 17 00:00:00 2001 From: kindlycat Date: Wed, 12 Aug 2015 22:31:06 +0000 Subject: [PATCH 1/2] Add "opacity" property for widgets --- lib/wibox/drawable.lua | 3 +++ lib/wibox/layout/base.lua | 3 +++ lib/wibox/widget/base.lua | 9 +++++++++ 3 files changed, 15 insertions(+) diff --git a/lib/wibox/drawable.lua b/lib/wibox/drawable.lua index c9d0a483d..3f05df9bd 100644 --- a/lib/wibox/drawable.lua +++ b/lib/wibox/drawable.lua @@ -59,7 +59,10 @@ local function do_redraw(self) self._widget_geometries = {} if self.widget and self.widget.visible then cr:set_source(self.foreground_color) + cr:push_group() self.widget:draw(self.widget_arg, cr, width, height) + cr:pop_group_to_source() + cr:paint_with_alpha(self.widget.opacity) self:widget_at(self.widget, 0, 0, width, height) end diff --git a/lib/wibox/layout/base.lua b/lib/wibox/layout/base.lua index 97e939661..3b85c2e1d 100644 --- a/lib/wibox/layout/base.lua +++ b/lib/wibox/layout/base.lua @@ -66,11 +66,14 @@ function base.draw_widget(wibox, cr, widget, x, y, width, height) cr:rectangle(0, 0, width, height) cr:clip() + cr:push_group() -- Let the widget draw itself local success, msg = pcall(widget.draw, widget, wibox, cr, width, height) if not success then print("Error while drawing widget: " .. msg) end + cr:pop_group_to_source() + cr:paint_with_alpha(widget.opacity) -- Register the widget for input handling wibox:widget_at(widget, base.rect_to_device_geometry(cr, 0, 0, width, height)) diff --git a/lib/wibox/widget/base.lua b/lib/wibox/widget/base.lua index f57c01f56..dd37d5c0b 100644 --- a/lib/wibox/widget/base.lua +++ b/lib/wibox/widget/base.lua @@ -116,6 +116,15 @@ function base.make_widget(proxy, widget_name) end end + -- Add opacity property and setter. + ret.opacity = 1 + function ret:set_opacity(b) + if b ~= self.opacity then + self.opacity = b + self:emit_signal("widget::updated") + end + end + -- Add __tostring method to metatable. ret.widget_name = widget_name or object.modulename(3) local mt = {} From d2407c3de19cf26a75e82822c1da43acb86e1e7a Mon Sep 17 00:00:00 2001 From: Grigory Mischenko Date: Thu, 13 Aug 2015 12:26:43 +0300 Subject: [PATCH 2/2] Widget opacity: draw with alpha only if transparent --- lib/wibox/drawable.lua | 13 ++++++++++--- lib/wibox/layout/base.lua | 12 +++++++++--- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/lib/wibox/drawable.lua b/lib/wibox/drawable.lua index 3f05df9bd..a1e83a22b 100644 --- a/lib/wibox/drawable.lua +++ b/lib/wibox/drawable.lua @@ -59,10 +59,17 @@ local function do_redraw(self) self._widget_geometries = {} if self.widget and self.widget.visible then cr:set_source(self.foreground_color) - cr:push_group() + + if self.widget.opacity ~= 1 then + cr:push_group() + end self.widget:draw(self.widget_arg, cr, width, height) - cr:pop_group_to_source() - cr:paint_with_alpha(self.widget.opacity) + if self.widget.opacity ~= 1 then + cr:pop_group_to_source() + cr.operator = cairo.Operator.OVER + cr:paint_with_alpha(self.widget.opacity) + end + self:widget_at(self.widget, 0, 0, width, height) end diff --git a/lib/wibox/layout/base.lua b/lib/wibox/layout/base.lua index 3b85c2e1d..36ebc61bb 100644 --- a/lib/wibox/layout/base.lua +++ b/lib/wibox/layout/base.lua @@ -10,6 +10,7 @@ local pcall = pcall local print = print local min = math.min local max = math.max +local cairo = require("lgi").cairo local base = {} @@ -66,14 +67,19 @@ function base.draw_widget(wibox, cr, widget, x, y, width, height) cr:rectangle(0, 0, width, height) cr:clip() - cr:push_group() + if widget.opacity ~= 1 then + cr:push_group() + end -- Let the widget draw itself local success, msg = pcall(widget.draw, widget, wibox, cr, width, height) + if widget.opacity ~= 1 then + cr:pop_group_to_source() + cr.operator = cairo.Operator.OVER + cr:paint_with_alpha(widget.opacity) + end if not success then print("Error while drawing widget: " .. msg) end - cr:pop_group_to_source() - cr:paint_with_alpha(widget.opacity) -- Register the widget for input handling wibox:widget_at(widget, base.rect_to_device_geometry(cr, 0, 0, width, height))