wibox.imagebox: Add a `clip` method based on the `gears.shape` API

This commit is contained in:
Emmanuel Lepage Vallee 2016-01-20 02:09:07 -05:00
parent 98d8b8a199
commit 1d1e487d19
1 changed files with 26 additions and 1 deletions

View File

@ -12,6 +12,7 @@ local pairs = pairs
local type = type local type = type
local pcall = pcall local pcall = pcall
local print = print local print = print
local unpack = unpack or table.unpack
local imagebox = { mt = {} } local imagebox = { mt = {} }
@ -30,6 +31,12 @@ function imagebox:draw(context, cr, width, height)
cr:scale(aspect, aspect) cr:scale(aspect, aspect)
end end
-- Set the clip
if self._clip_shape then
cr:clip(self._clip_shape(cr, width, height, unpack(self._clip_args)))
end
cr:set_source_surface(self._image, 0, 0) cr:set_source_surface(self._image, 0, 0)
cr:paint() cr:paint()
end end
@ -107,6 +114,19 @@ function imagebox:set_image(image)
return true return true
end end
--- Set a clip shape for this imagebox
-- A clip shape define an area where the content is displayed and one where it
-- is trimmed.
--
-- Any other parameters will be passed to the clip shape function
--
-- @param clip_shape A `gears_shape` compatible shape function
function imagebox:set_clip_shape(clip_shape, ...)
self._clip_shape = clip_shape
self._clip_args = {...}
self:emit_signal("widget::redraw_needed")
end
--- Should the image be resized to fit into the available space? --- Should the image be resized to fit into the available space?
-- @param allowed If false, the image will be clipped, else it will be resized -- @param allowed If false, the image will be clipped, else it will be resized
-- to fit into the available space. -- to fit into the available space.
@ -117,10 +137,12 @@ function imagebox:set_resize(allowed)
end end
--- Returns a new imagebox --- Returns a new imagebox
-- Any other arguments will be passed to the clip shape function
-- @param image the image to display, may be nil -- @param image the image to display, may be nil
-- @param resize_allowed If false, the image will be clipped, else it will be resized -- @param resize_allowed If false, the image will be clipped, else it will be resized
-- to fit into the available space. -- to fit into the available space.
local function new(image, resize_allowed) -- @param clip_shape A `gears.shape` compatible function
local function new(image, resize_allowed, clip_shape)
local ret = base.make_widget() local ret = base.make_widget()
for k, v in pairs(imagebox) do for k, v in pairs(imagebox) do
@ -136,6 +158,9 @@ local function new(image, resize_allowed)
ret:set_resize(resize_allowed) ret:set_resize(resize_allowed)
end end
ret._clip_shape = clip_shape
ret._clip_args = {}
return ret return ret
end end