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.layout.base
|
2010-10-06 12:42:56 +02:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
2015-08-08 13:09:25 +02:00
|
|
|
local xpcall = xpcall
|
2010-10-06 12:42:56 +02:00
|
|
|
local print = print
|
2015-08-13 11:26:43 +02:00
|
|
|
local cairo = require("lgi").cairo
|
2015-07-25 15:52:53 +02:00
|
|
|
local wbase = require("wibox.widget.base")
|
2010-10-06 12:42:56 +02:00
|
|
|
|
2012-06-12 15:29:52 +02:00
|
|
|
local base = {}
|
2010-10-06 12:42:56 +02:00
|
|
|
|
2013-08-21 12:26:47 +02:00
|
|
|
--- Fit a widget for the given available width and height
|
2015-08-08 13:43:35 +02:00
|
|
|
-- @param context The context in which we are fit.
|
2013-08-21 12:26:47 +02:00
|
|
|
-- @param widget The widget to fit (this uses widget:fit(width, height)).
|
|
|
|
-- @param width The available width for the widget
|
|
|
|
-- @param height The available height for the widget
|
|
|
|
-- @return The width and height that the widget wants to use
|
2015-08-08 13:43:35 +02:00
|
|
|
function base.fit_widget(context, widget, width, height)
|
2015-07-17 18:23:55 +02:00
|
|
|
if not widget.visible then
|
|
|
|
return 0, 0
|
|
|
|
end
|
2014-03-23 17:48:26 +01:00
|
|
|
-- Sanitize the input. This also filters out e.g. NaN.
|
|
|
|
local width = math.max(0, width)
|
|
|
|
local height = math.max(0, height)
|
|
|
|
|
2015-08-13 15:48:56 +02:00
|
|
|
local w, h = widget._fit_geometry_cache:get(context, width, height)
|
|
|
|
|
|
|
|
-- Also sanitize the output.
|
|
|
|
w = math.max(0, math.min(w, width))
|
|
|
|
h = math.max(0, math.min(h, height))
|
|
|
|
return w, h
|
2013-08-21 12:26:47 +02:00
|
|
|
end
|
|
|
|
|
2010-10-06 12:42:56 +02:00
|
|
|
--- Draw a widget via a cairo context
|
2015-08-08 13:18:54 +02:00
|
|
|
-- @param context The context in which we are drawn.
|
2010-10-06 12:42:56 +02:00
|
|
|
-- @param cr The cairo context used
|
|
|
|
-- @param widget The widget to draw (this uses widget:draw(cr, width, height)).
|
|
|
|
-- @param x The position that the widget should get
|
|
|
|
-- @param y The position that the widget should get
|
|
|
|
-- @param width The widget's width
|
|
|
|
-- @param height The widget's height
|
2015-08-08 13:18:54 +02:00
|
|
|
function base.draw_widget(context, cr, widget, x, y, width, height)
|
2015-07-17 18:23:55 +02:00
|
|
|
if not widget.visible then
|
|
|
|
return
|
|
|
|
end
|
2015-08-08 13:18:54 +02:00
|
|
|
|
2010-10-06 12:42:56 +02:00
|
|
|
-- Use save() / restore() so that our modifications aren't permanent
|
|
|
|
cr:save()
|
|
|
|
|
|
|
|
-- Move (0, 0) to the place where the widget should show up
|
|
|
|
cr:translate(x, y)
|
|
|
|
|
|
|
|
-- Make sure the widget cannot draw outside of the allowed area
|
|
|
|
cr:rectangle(0, 0, width, height)
|
|
|
|
cr:clip()
|
|
|
|
|
2015-08-13 11:26:43 +02:00
|
|
|
if widget.opacity ~= 1 then
|
|
|
|
cr:push_group()
|
|
|
|
end
|
2010-10-06 12:42:56 +02:00
|
|
|
-- Let the widget draw itself
|
2015-08-08 13:09:25 +02:00
|
|
|
xpcall(function()
|
2015-08-08 13:18:54 +02:00
|
|
|
widget:draw(context, cr, width, height)
|
2015-08-08 13:09:25 +02:00
|
|
|
end, function(err)
|
|
|
|
print(debug.traceback("Error while drawing widget: "..tostring(err), 2))
|
|
|
|
end)
|
2015-08-13 11:26:43 +02:00
|
|
|
if widget.opacity ~= 1 then
|
|
|
|
cr:pop_group_to_source()
|
|
|
|
cr.operator = cairo.Operator.OVER
|
|
|
|
cr:paint_with_alpha(widget.opacity)
|
|
|
|
end
|
2010-10-06 12:42:56 +02:00
|
|
|
|
|
|
|
-- Register the widget for input handling
|
2015-08-23 15:36:55 +02:00
|
|
|
context:widget_at(widget, wbase.rect_to_device_geometry(cr, 0, 0, width, height))
|
2010-10-06 12:42:56 +02:00
|
|
|
|
|
|
|
cr:restore()
|
|
|
|
end
|
|
|
|
|
2012-06-12 15:29:52 +02:00
|
|
|
return base
|
|
|
|
|
2011-09-11 16:50:01 +02:00
|
|
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|