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.margin
|
2010-10-06 12:42:56 +02:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
local pairs = pairs
|
|
|
|
local type = type
|
|
|
|
local setmetatable = setmetatable
|
|
|
|
local base = require("wibox.layout.base")
|
|
|
|
local widget_base = require("wibox.widget.base")
|
2014-01-15 23:47:50 +01:00
|
|
|
local gcolor = require("gears.color")
|
|
|
|
local cairo = require("lgi").cairo
|
2010-10-06 12:42:56 +02:00
|
|
|
|
2012-06-12 15:29:52 +02:00
|
|
|
local margin = { mt = {} }
|
2010-10-06 12:42:56 +02:00
|
|
|
|
|
|
|
--- Draw a margin layout
|
2012-11-18 20:44:03 +01:00
|
|
|
function margin:draw(wibox, cr, width, height)
|
|
|
|
local x = self.left
|
|
|
|
local y = self.top
|
|
|
|
local w = self.right
|
|
|
|
local h = self.bottom
|
2014-01-15 23:47:50 +01:00
|
|
|
local color = self.color
|
2010-10-06 12:42:56 +02:00
|
|
|
|
2012-11-18 20:44:03 +01:00
|
|
|
if not self.widget or width <= x + w or height <= y + h then
|
2010-10-06 12:42:56 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2014-01-15 23:47:50 +01:00
|
|
|
if color then
|
|
|
|
cr:save()
|
|
|
|
cr:set_source(color)
|
|
|
|
cr:rectangle(0, 0, width, height)
|
|
|
|
cr:rectangle(x, y, width - x - w, height - y - h)
|
|
|
|
cr:set_fill_rule(cairo.FillRule.EVEN_ODD)
|
|
|
|
cr:fill()
|
|
|
|
cr:restore()
|
|
|
|
end
|
|
|
|
|
2012-11-18 20:44:03 +01:00
|
|
|
base.draw_widget(wibox, cr, self.widget, x, y, width - x - w, height - y - h)
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Fit a margin layout into the given space
|
2012-11-18 20:44:03 +01:00
|
|
|
function margin:fit(width, height)
|
|
|
|
local extra_w = self.left + self.right
|
|
|
|
local extra_h = self.top + self.bottom
|
2010-10-06 12:42:56 +02:00
|
|
|
local w, h = 0, 0
|
2012-11-18 20:44:03 +01:00
|
|
|
if self.widget then
|
2013-08-21 12:26:47 +02:00
|
|
|
w, h = base.fit_widget(self.widget, width - extra_w, height - extra_h)
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
return w + extra_w, h + extra_h
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Set the widget that this layout adds a margin on.
|
2012-11-18 20:44:03 +01:00
|
|
|
function margin:set_widget(widget)
|
|
|
|
if self.widget then
|
|
|
|
self.widget:disconnect_signal("widget::updated", self._emit_updated)
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
if widget then
|
|
|
|
widget_base.check_widget(widget)
|
2015-06-14 14:27:43 +02:00
|
|
|
widget:weak_connect_signal("widget::updated", self._emit_updated)
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
2012-11-18 20:44:03 +01:00
|
|
|
self.widget = widget
|
|
|
|
self._emit_updated()
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Set all the margins to val.
|
2012-11-18 20:44:03 +01:00
|
|
|
function margin:set_margins(val)
|
|
|
|
self.left = val
|
|
|
|
self.right = val
|
|
|
|
self.top = val
|
|
|
|
self.bottom = val
|
|
|
|
self:emit_signal("widget::updated")
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
|
2014-01-15 23:47:50 +01:00
|
|
|
--- Set the margins color to color
|
|
|
|
function margin:set_color(color)
|
|
|
|
self.color = color and gcolor(color)
|
|
|
|
self._emit_updated()
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Reset this layout. The widget will be unreferenced, the margins set to 0
|
|
|
|
-- and the color erased
|
2012-11-18 20:44:03 +01:00
|
|
|
function margin:reset()
|
|
|
|
self:set_widget(nil)
|
|
|
|
self:set_margins(0)
|
2014-01-15 23:47:50 +01:00
|
|
|
self:set_color(nil)
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Set the left margin that this layout adds to its widget.
|
|
|
|
-- @param layout The layout you are modifying.
|
|
|
|
-- @param margin The new margin to use.
|
|
|
|
-- @name set_left
|
|
|
|
-- @class function
|
|
|
|
|
|
|
|
--- Set the right margin that this layout adds to its widget.
|
|
|
|
-- @param layout The layout you are modifying.
|
|
|
|
-- @param margin The new margin to use.
|
|
|
|
-- @name set_right
|
|
|
|
-- @class function
|
|
|
|
|
|
|
|
--- Set the top margin that this layout adds to its widget.
|
|
|
|
-- @param layout The layout you are modifying.
|
|
|
|
-- @param margin The new margin to use.
|
|
|
|
-- @name set_top
|
|
|
|
-- @class function
|
|
|
|
|
|
|
|
--- Set the bottom margin that this layout adds to its widget.
|
|
|
|
-- @param layout The layout you are modifying.
|
|
|
|
-- @param margin The new margin to use.
|
|
|
|
-- @name set_bottom
|
|
|
|
-- @class function
|
|
|
|
|
|
|
|
-- Create setters for each direction
|
|
|
|
for k, v in pairs({ "left", "right", "top", "bottom" }) do
|
2012-06-12 15:29:52 +02:00
|
|
|
margin["set_" .. v] = function(layout, val)
|
2010-10-06 12:42:56 +02:00
|
|
|
layout[v] = val
|
|
|
|
layout:emit_signal("widget::updated")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Returns a new margin layout.
|
2015-02-26 22:06:34 +01:00
|
|
|
-- @param[opt] widget A widget to use.
|
|
|
|
-- @param[opt] left A margin to use on the left side of the widget.
|
|
|
|
-- @param[opt] right A margin to use on the right side of the widget.
|
|
|
|
-- @param[opt] top A margin to use on the top side of the widget.
|
|
|
|
-- @param[opt] bottom A margin to use on the bottom side of the widget.
|
|
|
|
-- @param[opt] color A color for the margins.
|
2014-01-15 23:47:50 +01:00
|
|
|
local function new(widget, left, right, top, bottom, color)
|
2010-10-06 12:42:56 +02:00
|
|
|
local ret = widget_base.make_widget()
|
|
|
|
|
2012-06-12 15:29:52 +02:00
|
|
|
for k, v in pairs(margin) do
|
2010-10-06 12:42:56 +02:00
|
|
|
if type(v) == "function" then
|
|
|
|
ret[k] = v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
ret._emit_updated = function()
|
|
|
|
ret:emit_signal("widget::updated")
|
|
|
|
end
|
|
|
|
|
2010-10-08 17:22:00 +02:00
|
|
|
ret:set_left(left or 0)
|
|
|
|
ret:set_right(right or 0)
|
|
|
|
ret:set_top(top or 0)
|
|
|
|
ret:set_bottom(bottom or 0)
|
2010-10-06 12:42:56 +02:00
|
|
|
|
2014-01-15 23:47:50 +01:00
|
|
|
ret:set_color(color)
|
|
|
|
|
2010-10-06 12:42:56 +02:00
|
|
|
if widget then
|
|
|
|
ret:set_widget(widget)
|
|
|
|
end
|
|
|
|
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
2012-06-12 15:29:52 +02:00
|
|
|
function margin.mt:__call(...)
|
|
|
|
return new(...)
|
|
|
|
end
|
|
|
|
|
|
|
|
return setmetatable(margin, margin.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
|