2010-10-06 12:42:56 +02:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
-- @author Uli Schlachter
|
|
|
|
-- @copyright 2010 Uli Schlachter
|
|
|
|
-- @release @AWESOME_VERSION@
|
2015-02-25 11:18:53 +01:00
|
|
|
-- @classmod wibox.widget.background
|
2010-10-06 12:42:56 +02:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
local base = require("wibox.widget.base")
|
|
|
|
local color = require("gears.color")
|
2012-05-27 19:20:34 +02:00
|
|
|
local surface = require("gears.surface")
|
|
|
|
local cairo = require("lgi").cairo
|
2010-10-06 12:42:56 +02:00
|
|
|
local setmetatable = setmetatable
|
|
|
|
local pairs = pairs
|
|
|
|
local type = type
|
|
|
|
|
2012-06-12 15:55:10 +02:00
|
|
|
local background = { mt = {} }
|
2010-10-06 12:42:56 +02:00
|
|
|
|
|
|
|
--- Draw this widget
|
2015-08-08 13:18:54 +02:00
|
|
|
function background:draw(context, cr, width, height)
|
2015-07-17 18:23:55 +02:00
|
|
|
if not self.widget or not self.widget.visible then
|
2010-10-06 12:42:56 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2016-01-18 23:08:21 +01:00
|
|
|
if self._shape then
|
|
|
|
self._shape(cr, width, height, unpack(self._shape_args or {}))
|
|
|
|
cr:clip()
|
|
|
|
end
|
|
|
|
|
2012-11-18 20:44:03 +01:00
|
|
|
if self.background then
|
|
|
|
cr:set_source(self.background)
|
2010-10-06 12:42:56 +02:00
|
|
|
cr:paint()
|
|
|
|
end
|
2012-11-18 20:44:03 +01:00
|
|
|
if self.bgimage then
|
|
|
|
local pattern = cairo.Pattern.create_for_surface(self.bgimage)
|
2010-10-06 12:42:56 +02:00
|
|
|
cr:set_source(pattern)
|
|
|
|
cr:paint()
|
|
|
|
end
|
2015-06-14 16:35:19 +02:00
|
|
|
end
|
2010-10-06 12:42:56 +02:00
|
|
|
|
2015-06-14 16:35:19 +02:00
|
|
|
--- Prepare drawing the children of this widget
|
2015-12-29 11:17:52 +01:00
|
|
|
function background:before_draw_children(context, cr, width, height)
|
2012-11-18 20:44:03 +01:00
|
|
|
if self.foreground then
|
|
|
|
cr:set_source(self.foreground)
|
2011-03-30 04:00:24 +02:00
|
|
|
end
|
2015-06-14 16:35:19 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Layout this widget
|
|
|
|
function background:layout(context, width, height)
|
|
|
|
if self.widget then
|
|
|
|
return { base.place_widget_at(self.widget, 0, 0, width, height) }
|
2011-03-30 04:00:24 +02:00
|
|
|
end
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Fit this widget into the given area
|
2015-08-08 13:43:35 +02:00
|
|
|
function background:fit(context, width, height)
|
2012-11-18 20:44:03 +01:00
|
|
|
if not self.widget then
|
2010-10-06 12:42:56 +02:00
|
|
|
return 0, 0
|
|
|
|
end
|
|
|
|
|
2015-09-17 15:01:50 +02:00
|
|
|
return base.fit_widget(self, context, self.widget, width, height)
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Set the widget that is drawn on top of the background
|
2012-11-18 20:44:03 +01:00
|
|
|
function background:set_widget(widget)
|
2010-10-06 12:42:56 +02:00
|
|
|
if widget then
|
2010-10-06 18:45:13 +02:00
|
|
|
base.check_widget(widget)
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
2012-11-18 20:44:03 +01:00
|
|
|
self.widget = widget
|
2015-06-14 16:35:19 +02:00
|
|
|
self:emit_signal("widget::layout_changed")
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
|
2016-01-16 07:13:07 +01:00
|
|
|
--- Get the number of children element
|
|
|
|
-- @treturn table The children
|
|
|
|
function background:get_children()
|
|
|
|
return {self.widget}
|
|
|
|
end
|
|
|
|
|
2010-10-06 12:42:56 +02:00
|
|
|
--- Set the background to use
|
2012-11-18 20:44:03 +01:00
|
|
|
function background:set_bg(bg)
|
2010-10-06 12:42:56 +02:00
|
|
|
if bg then
|
2012-11-18 20:44:03 +01:00
|
|
|
self.background = color(bg)
|
2010-10-06 12:42:56 +02:00
|
|
|
else
|
2012-11-18 20:44:03 +01:00
|
|
|
self.background = nil
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
2015-06-14 16:35:19 +02:00
|
|
|
self:emit_signal("widget::redraw_needed")
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
|
2011-03-30 04:00:24 +02:00
|
|
|
--- Set the foreground to use
|
2012-11-18 20:44:03 +01:00
|
|
|
function background:set_fg(fg)
|
2011-03-30 04:00:24 +02:00
|
|
|
if fg then
|
2012-11-18 20:44:03 +01:00
|
|
|
self.foreground = color(fg)
|
2011-03-30 04:00:24 +02:00
|
|
|
else
|
2012-11-18 20:44:03 +01:00
|
|
|
self.foreground = nil
|
2011-03-30 04:00:24 +02:00
|
|
|
end
|
2015-06-14 16:35:19 +02:00
|
|
|
self:emit_signal("widget::redraw_needed")
|
2011-03-30 04:00:24 +02:00
|
|
|
end
|
|
|
|
|
2016-01-18 23:08:21 +01:00
|
|
|
--- Set the background shape
|
|
|
|
-- @param shape A function taking a context, width and height as arguments
|
|
|
|
-- Any other arguments will be passed to the shape function
|
|
|
|
function background:set_shape(shape, ...)
|
2016-01-22 04:48:26 +01:00
|
|
|
self._shape = shape
|
|
|
|
self._shape_args = {...}
|
2016-01-18 23:08:21 +01:00
|
|
|
self:emit_signal("widget::redraw_needed")
|
|
|
|
end
|
|
|
|
|
2010-10-06 12:42:56 +02:00
|
|
|
--- Set the background image to use
|
2012-11-18 20:44:03 +01:00
|
|
|
function background:set_bgimage(image)
|
|
|
|
self.bgimage = surface.load(image)
|
2015-06-14 16:35:19 +02:00
|
|
|
self:emit_signal("widget::redraw_needed")
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
|
2013-03-10 13:46:28 +01:00
|
|
|
--- Returns a new background layout. A background layout applies a background
|
|
|
|
-- and foreground color to another widget.
|
2015-02-26 22:06:34 +01:00
|
|
|
-- @param[opt] widget The widget to display.
|
|
|
|
-- @param[opt] bg The background to use for that widget.
|
2016-01-18 23:08:21 +01:00
|
|
|
-- @param[opt] shape A `gears.shape` compatible shape function
|
|
|
|
local function new(widget, bg, shape)
|
2010-10-06 12:42:56 +02:00
|
|
|
local ret = base.make_widget()
|
|
|
|
|
2012-06-12 15:55:10 +02:00
|
|
|
for k, v in pairs(background) do
|
2010-10-06 12:42:56 +02:00
|
|
|
if type(v) == "function" then
|
|
|
|
ret[k] = v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-01-18 23:08:21 +01:00
|
|
|
ret._shape = shape
|
|
|
|
|
2012-11-25 19:16:31 +01:00
|
|
|
ret:set_widget(widget)
|
2013-03-10 13:46:28 +01:00
|
|
|
ret:set_bg(bg)
|
2012-11-25 19:16:31 +01:00
|
|
|
|
2010-10-06 12:42:56 +02:00
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
2012-06-12 15:55:10 +02:00
|
|
|
function background.mt:__call(...)
|
|
|
|
return new(...)
|
|
|
|
end
|
|
|
|
|
|
|
|
return setmetatable(background, background.mt)
|
2010-10-06 12:42:56 +02:00
|
|
|
|
2011-09-11 16:50:01 +02:00
|
|
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|