wibox: Add shape property

This bridges between gears.shape and the shapes. So far, it does not try
to do any kind of anti-aliasing magic, so you get "steep edges".

Signed-off-by: Uli Schlachter <psychon@znc.in>
This commit is contained in:
Uli Schlachter 2016-10-29 13:57:04 +02:00
parent 58fed31d88
commit 1a9887335e
1 changed files with 53 additions and 0 deletions

View File

@ -17,6 +17,7 @@ local object = require("gears.object")
local grect = require("gears.geometry").rectangle local grect = require("gears.geometry").rectangle
local beautiful = require("beautiful") local beautiful = require("beautiful")
local base = require("wibox.widget.base") local base = require("wibox.widget.base")
local cairo = require("lgi").cairo
--- This provides widget box windows. Every wibox can also be used as if it were --- This provides widget box windows. Every wibox can also be used as if it were
-- a drawin. All drawin functions and properties are also available on wiboxes! -- a drawin. All drawin functions and properties are also available on wiboxes!
@ -61,6 +62,53 @@ function wibox:find_widgets(x, y)
return self._drawable:find_widgets(x, y) return self._drawable:find_widgets(x, y)
end end
function wibox:_apply_shape()
local shape = self._shape
if not shape then
self.shape_bounding = nil
self.shape_clip = nil
return
end
local geo = self:geometry()
local bw = self.border_width
-- First handle the bounding shape (things including the border)
local img = cairo.ImageSurface(cairo.Format.A1, geo.width + 2*bw, geo.height + 2*bw)
local cr = cairo.Context(img)
shape(cr, geo.width + 2*bw, geo.height + 2*bw)
cr:set_operator(cairo.Operator.SOURCE)
cr:fill()
self.shape_bounding = img._native
img:finish()
-- Now handle the clip shape (things excluding the border)
img = cairo.ImageSurface(cairo.Format.A1, geo.width, geo.height)
cr = cairo.Context(img)
shape(cr, geo.width, geo.height)
cr:set_operator(cairo.Operator.SOURCE)
cr:fill()
self.shape_clip = img._native
img:finish()
end
--- Set the wibox shape.
-- @property shape
-- @tparam gears.shape A gears.shape compatible function.
-- @see gears.shape
function wibox:set_shape(shape)
self._shape = shape
self:_apply_shape()
end
function wibox:get_shape()
return self._shape
end
function wibox:get_screen() function wibox:get_screen()
if self.screen_assigned and self.screen_assigned.valid then if self.screen_assigned and self.screen_assigned.valid then
return self.screen_assigned return self.screen_assigned
@ -196,6 +244,9 @@ local function new(args)
-- Make sure the wibox is drawn at least once -- Make sure the wibox is drawn at least once
ret.draw() ret.draw()
ret:connect_signal("property::geometry", ret._apply_shape)
ret:connect_signal("property::border_width", ret._apply_shape)
-- If a value is not found, look in the drawin -- If a value is not found, look in the drawin
setmetatable(ret, { setmetatable(ret, {
__index = function(self, k) __index = function(self, k)
@ -229,6 +280,8 @@ local function new(args)
ret:set_screen ( args.screen ) ret:set_screen ( args.screen )
end end
ret.shape = args.shape
return ret return ret
end end