mirror: Enable the property system

And add the missing documentation
This commit is contained in:
Emmanuel Lepage Vallee 2016-05-26 16:26:50 -04:00
parent 8869257940
commit bf74ca8a8c
1 changed files with 34 additions and 26 deletions

View File

@ -9,57 +9,63 @@
local type = type local type = type
local error = error local error = error
local pairs = pairs
local ipairs = ipairs local ipairs = ipairs
local setmetatable = setmetatable local setmetatable = setmetatable
local base = require("wibox.widget.base") local base = require("wibox.widget.base")
local matrix = require("gears.matrix") local matrix = require("gears.matrix")
local util = require("awful.util")
local mirror = { mt = {} } local mirror = { mt = {} }
-- Layout this layout -- Layout this layout
function mirror:layout(_, width, height) function mirror:layout(_, width, height)
if not self.widget then return end if not self._private.widget then return end
local m = matrix.identity local m = matrix.identity
local t = { x = 0, y = 0 } -- translation local t = { x = 0, y = 0 } -- translation
local s = { x = 1, y = 1 } -- scale local s = { x = 1, y = 1 } -- scale
if self.horizontal then if self._private.horizontal then
t.x = width t.x = width
s.x = -1 s.x = -1
end end
if self.vertical then if self._private.vertical then
t.y = height t.y = height
s.y = -1 s.y = -1
end end
m = m:translate(t.x, t.y) m = m:translate(t.x, t.y)
m = m:scale(s.x, s.y) m = m:scale(s.x, s.y)
return { base.place_widget_via_matrix(self.widget, m, width, height) } return { base.place_widget_via_matrix(self._private.widget, m, width, height) }
end end
-- Fit this layout into the given area -- Fit this layout into the given area
function mirror:fit(context, ...) function mirror:fit(context, ...)
if not self.widget then if not self._private.widget then
return 0, 0 return 0, 0
end end
return base.fit_widget(self, context, self.widget, ...) return base.fit_widget(self, context, self._private.widget, ...)
end end
--- Set the widget that this layout mirrors. --- The widget to be reflected.
-- @param widget The widget to mirror -- @property widget
-- @tparam widget widget The widget
function mirror:set_widget(widget) function mirror:set_widget(widget)
if widget then if widget then
base.check_widget(widget) base.check_widget(widget)
end end
self.widget = widget self._private.widget = widget
self:emit_signal("widget::layout_changed") self:emit_signal("widget::layout_changed")
end end
function mirror:get_widget()
return self._private.widget
end
--- Get the number of children element --- Get the number of children element
-- @treturn table The children -- @treturn table The children
function mirror:get_children() function mirror:get_children()
return {self.widget} return {self._private.widget}
end end
--- Replace the layout children --- Replace the layout children
@ -71,13 +77,11 @@ end
--- Reset this layout. The widget will be removed and the axes reset. --- Reset this layout. The widget will be removed and the axes reset.
function mirror:reset() function mirror:reset()
self.horizontal = false self._private.horizontal = false
self.vertical = false self._private.vertical = false
self:set_widget(nil) self:set_widget(nil)
end end
--- Set the reflection of this mirror layout.
-- @param reflection a table which contains new values for horizontal and/or vertical (booleans)
function mirror:set_reflection(reflection) function mirror:set_reflection(reflection)
if type(reflection) ~= 'table' then if type(reflection) ~= 'table' then
error("Invalid type of reflection for mirror layout: " .. error("Invalid type of reflection for mirror layout: " ..
@ -85,16 +89,20 @@ function mirror:set_reflection(reflection)
end end
for _, ref in ipairs({"horizontal", "vertical"}) do for _, ref in ipairs({"horizontal", "vertical"}) do
if reflection[ref] ~= nil then if reflection[ref] ~= nil then
self[ref] = reflection[ref] self._private[ref] = reflection[ref]
end end
end end
self:emit_signal("widget::layout_changed") self:emit_signal("widget::layout_changed")
end end
--- Get the reflection of this mirror layout. --- Get the reflection of this mirror layout.
-- @return a table of booleans with the keys "horizontal", "vertical". -- @property reflection
-- @param table reflection A table of booleans with the keys "horizontal", "vertical".
-- @param boolean reflection.horizontal
-- @param boolean reflection.vertical
function mirror:get_reflection() function mirror:get_reflection()
return { horizontal = self.horizontal, vertical = self.vertical } return { horizontal = self._private.horizontal, vertical = self._private.vertical }
end end
--- Returns a new mirror container. --- Returns a new mirror container.
@ -107,15 +115,11 @@ end
-- @treturn table A new mirror container -- @treturn table A new mirror container
-- @function wibox.container.mirror -- @function wibox.container.mirror
local function new(widget, reflection) local function new(widget, reflection)
local ret = base.make_widget() local ret = base.make_widget(nil, nil, {enable_properties = true})
ret.horizontal = false ret._private.horizontal = false
ret.vertical = false ret._private.vertical = false
for k, v in pairs(mirror) do util.table.crush(ret, mirror, true)
if type(v) == "function" then
ret[k] = v
end
end
ret:set_widget(widget) ret:set_widget(widget)
ret:set_reflection(reflection or {}) ret:set_reflection(reflection or {})
@ -127,6 +131,10 @@ function mirror.mt:__call(...)
return new(...) return new(...)
end end
--@DOC_widget_COMMON@
--@DOC_object_COMMON@
return setmetatable(mirror, mirror.mt) return setmetatable(mirror, mirror.mt)
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80