margin: Enable the property system

And add the missing documentation
This commit is contained in:
Emmanuel Lepage Vallee 2016-05-26 16:19:48 -04:00
parent 832e9ed054
commit 8869257940
1 changed files with 68 additions and 51 deletions

View File

@ -8,23 +8,23 @@
---------------------------------------------------------------------------
local pairs = pairs
local type = type
local setmetatable = setmetatable
local base = require("wibox.widget.base")
local gcolor = require("gears.color")
local cairo = require("lgi").cairo
local util = require("awful.util")
local margin = { mt = {} }
-- Draw a margin layout
function margin:draw(_, cr, width, height)
local x = self.left
local y = self.top
local w = self.right
local h = self.bottom
local color = self.color
local x = self._private.left
local y = self._private.top
local w = self._private.right
local h = self._private.bottom
local color = self._private.color
if not self.widget or width <= x + w or height <= y + h then
if not self._private.widget or width <= x + w or height <= y + h then
return
end
@ -39,45 +39,52 @@ end
-- Layout a margin layout
function margin:layout(_, width, height)
if self.widget then
local x = self.left
local y = self.top
local w = self.right
local h = self.bottom
if self._private.widget then
local x = self._private.left
local y = self._private.top
local w = self._private.right
local h = self._private.bottom
return { base.place_widget_at(self.widget, x, y, width - x - w, height - y - h) }
return { base.place_widget_at(self._private.widget, x, y, width - x - w, height - y - h) }
end
end
-- Fit a margin layout into the given space
function margin:fit(context, width, height)
local extra_w = self.left + self.right
local extra_h = self.top + self.bottom
local extra_w = self._private.left + self._private.right
local extra_h = self._private.top + self._private.bottom
local w, h = 0, 0
if self.widget then
w, h = base.fit_widget(self, context, self.widget, width - extra_w, height - extra_h)
if self._private.widget then
w, h = base.fit_widget(self, context, self._private.widget, width - extra_w, height - extra_h)
end
if self._draw_empty == false and (w == 0 or h == 0) then
if self._private.draw_empty == false and (w == 0 or h == 0) then
return 0, 0
end
return w + extra_w, h + extra_h
end
--- Set the widget that this layout adds a margin on.
--- The widget to be wrapped the the margins.
-- @property widget
-- @tparam widget widget The widget
function margin:set_widget(widget)
if widget then
base.check_widget(widget)
end
self.widget = widget
self._private.widget = widget
self:emit_signal("widget::layout_changed")
end
function margin:get_widget()
return self._private.widget
end
-- Get the number of children element
-- @treturn table The children
function margin:get_children()
return {self.widget}
return {self._private.widget}
end
-- Replace the layout children
@ -88,36 +95,50 @@ function margin:set_children(children)
end
--- Set all the margins to val.
-- @property margins
-- @tparam number val The margin value
function margin:set_margins(val)
if self.left == val and
self.right == val and
self.top == val and
self.bottom == val then
if self._private.left == val and
self._private.right == val and
self._private.top == val and
self._private.bottom == val then
return
end
self.left = val
self.right = val
self.top = val
self.bottom = val
self._private.left = val
self._private.right = val
self._private.top = val
self._private.bottom = val
self:emit_signal("widget::layout_changed")
end
--- Set the margins color to create a border.
-- @property color
-- @param color A color used to fill the margin.
function margin:set_color(color)
self.color = color and gcolor(color)
self._private.color = color and gcolor(color)
self:emit_signal("widget::redraw_needed")
end
function margin:get_color()
return self._private.color
end
--- Draw the margin even if the content size is 0x0 (default: true)
-- @function draw_empty
-- @tparam boolean draw_empty Draw nothing is content is 0x0 or draw the margin anyway
function margin:set_draw_empty(draw_empty)
self._draw_empty = draw_empty
self._private.draw_empty = draw_empty
self:emit_signal("widget::layout_changed")
end
function margin:get_draw_empty()
return self._private.draw_empty
end
--- Reset this layout. The widget will be unreferenced, the margins set to 0
-- and the color erased
function margin:reset()
@ -127,36 +148,32 @@ function margin:reset()
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
-- @property left
--- 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
-- @property right
--- 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
-- @property top
--- 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
-- @property bottom
-- Create setters for each direction
for _, v in pairs({ "left", "right", "top", "bottom" }) do
margin["set_" .. v] = function(layout, val)
if layout[v] == val then return end
layout[v] = val
if layout._private[v] == val then return end
layout._private[v] = val
layout:emit_signal("widget::layout_changed")
end
margin["get_" .. v] = function(layout)
return layout._private[v]
end
end
--- Returns a new margin container.
@ -170,13 +187,9 @@ end
-- @treturn table A new margin container
-- @function wibox.container.margin
local function new(widget, left, right, top, bottom, color, draw_empty)
local ret = base.make_widget()
local ret = base.make_widget(nil, nil, {enable_properties = true})
for k, v in pairs(margin) do
if type(v) == "function" then
ret[k] = v
end
end
util.table.crush(ret, margin, true)
ret:set_left(left or 0)
ret:set_right(right or 0)
@ -197,6 +210,10 @@ function margin.mt:__call(...)
return new(...)
end
--@DOC_widget_COMMON@
--@DOC_object_COMMON@
return setmetatable(margin, margin.mt)
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80