constraint: Enable the property system
And add the missing documentation
This commit is contained in:
parent
a5edff396c
commit
832e9ed054
|
@ -7,49 +7,55 @@
|
||||||
-- @classmod wibox.container.constraint
|
-- @classmod wibox.container.constraint
|
||||||
---------------------------------------------------------------------------
|
---------------------------------------------------------------------------
|
||||||
|
|
||||||
local pairs = pairs
|
|
||||||
local type = type
|
|
||||||
local setmetatable = setmetatable
|
local setmetatable = setmetatable
|
||||||
local base = require("wibox.widget.base")
|
local base = require("wibox.widget.base")
|
||||||
|
local util = require("awful.util")
|
||||||
local math = math
|
local math = math
|
||||||
|
|
||||||
local constraint = { mt = {} }
|
local constraint = { mt = {} }
|
||||||
|
|
||||||
-- Layout a constraint layout
|
-- Layout a constraint layout
|
||||||
function constraint:layout(_, width, height)
|
function constraint:layout(_, width, height)
|
||||||
if self.widget then
|
if self._private.widget then
|
||||||
return { base.place_widget_at(self.widget, 0, 0, width, height) }
|
return { base.place_widget_at(self._private.widget, 0, 0, width, height) }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Fit a constraint layout into the given space
|
-- Fit a constraint layout into the given space
|
||||||
function constraint:fit(context, width, height)
|
function constraint:fit(context, width, height)
|
||||||
local w, h
|
local w, h
|
||||||
if self.widget then
|
if self._private.widget then
|
||||||
w = self._strategy(width, self._width)
|
w = self._private.strategy(width, self._private.width)
|
||||||
h = self._strategy(height, self._height)
|
h = self._private.strategy(height, self._private.height)
|
||||||
|
|
||||||
w, h = base.fit_widget(self, context, self.widget, w, h)
|
w, h = base.fit_widget(self, context, self._private.widget, w, h)
|
||||||
else
|
else
|
||||||
w, h = 0, 0
|
w, h = 0, 0
|
||||||
end
|
end
|
||||||
|
|
||||||
w = self._strategy(w, self._width)
|
w = self._private.strategy(w, self._private.width)
|
||||||
h = self._strategy(h, self._height)
|
h = self._private.strategy(h, self._private.height)
|
||||||
|
|
||||||
return w, h
|
return w, h
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Set the widget that this layout adds a constraint on.
|
--- The widget to be constrained.
|
||||||
|
-- @property widget
|
||||||
|
-- @tparam widget widget The widget
|
||||||
|
|
||||||
function constraint:set_widget(widget)
|
function constraint:set_widget(widget)
|
||||||
self.widget = widget
|
self._private.widget = widget
|
||||||
self:emit_signal("widget::layout_changed")
|
self:emit_signal("widget::layout_changed")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function constraint: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 constraint:get_children()
|
function constraint:get_children()
|
||||||
return {self.widget}
|
return {self._private.widget}
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Replace the layout children
|
--- Replace the layout children
|
||||||
|
@ -61,6 +67,8 @@ end
|
||||||
|
|
||||||
--- Set the strategy to use for the constraining. Valid values are 'max',
|
--- Set the strategy to use for the constraining. Valid values are 'max',
|
||||||
-- 'min' or 'exact'. Throws an error on invalid values.
|
-- 'min' or 'exact'. Throws an error on invalid values.
|
||||||
|
-- @property strategy
|
||||||
|
|
||||||
function constraint:set_strategy(val)
|
function constraint:set_strategy(val)
|
||||||
local func = {
|
local func = {
|
||||||
min = function(real_size, limit)
|
min = function(real_size, limit)
|
||||||
|
@ -78,27 +86,45 @@ function constraint:set_strategy(val)
|
||||||
error("Invalid strategy for constraint layout: " .. tostring(val))
|
error("Invalid strategy for constraint layout: " .. tostring(val))
|
||||||
end
|
end
|
||||||
|
|
||||||
self._strategy = func[val]
|
self._private.strategy = func[val]
|
||||||
self:emit_signal("widget::layout_changed")
|
self:emit_signal("widget::layout_changed")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function constraint:get_strategy()
|
||||||
|
return self._private.strategy
|
||||||
|
end
|
||||||
|
|
||||||
--- Set the maximum width to val. nil for no width limit.
|
--- Set the maximum width to val. nil for no width limit.
|
||||||
|
-- @property height
|
||||||
|
-- @param number
|
||||||
|
|
||||||
function constraint:set_width(val)
|
function constraint:set_width(val)
|
||||||
self._width = val
|
self._private.width = val
|
||||||
self:emit_signal("widget::layout_changed")
|
self:emit_signal("widget::layout_changed")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function constraint:get_width()
|
||||||
|
return self._private.width
|
||||||
|
end
|
||||||
|
|
||||||
--- Set the maximum height to val. nil for no height limit.
|
--- Set the maximum height to val. nil for no height limit.
|
||||||
|
-- @property width
|
||||||
|
-- @param number
|
||||||
|
|
||||||
function constraint:set_height(val)
|
function constraint:set_height(val)
|
||||||
self._height = val
|
self._private.height = val
|
||||||
self:emit_signal("widget::layout_changed")
|
self:emit_signal("widget::layout_changed")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function constraint:get_height()
|
||||||
|
return self._private.height
|
||||||
|
end
|
||||||
|
|
||||||
--- Reset this layout. The widget will be unreferenced, strategy set to "max"
|
--- Reset this layout. The widget will be unreferenced, strategy set to "max"
|
||||||
-- and the constraints set to nil.
|
-- and the constraints set to nil.
|
||||||
function constraint:reset()
|
function constraint:reset()
|
||||||
self._width = nil
|
self._private.width = nil
|
||||||
self._height = nil
|
self._private.height = nil
|
||||||
self:set_strategy("max")
|
self:set_strategy("max")
|
||||||
self:set_widget(nil)
|
self:set_widget(nil)
|
||||||
end
|
end
|
||||||
|
@ -117,13 +143,9 @@ end
|
||||||
-- @treturn table A new constraint container
|
-- @treturn table A new constraint container
|
||||||
-- @function wibox.container.constraint
|
-- @function wibox.container.constraint
|
||||||
local function new(widget, strategy, width, height)
|
local function new(widget, strategy, width, height)
|
||||||
local ret = base.make_widget()
|
local ret = base.make_widget(nil, nil, {enable_properties = true})
|
||||||
|
|
||||||
for k, v in pairs(constraint) do
|
util.table.crush(ret, constraint, true)
|
||||||
if type(v) == "function" then
|
|
||||||
ret[k] = v
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
ret:set_strategy(strategy or "max")
|
ret:set_strategy(strategy or "max")
|
||||||
ret:set_width(width)
|
ret:set_width(width)
|
||||||
|
@ -140,6 +162,10 @@ function constraint.mt:__call(...)
|
||||||
return new(...)
|
return new(...)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--@DOC_widget_COMMON@
|
||||||
|
|
||||||
|
--@DOC_object_COMMON@
|
||||||
|
|
||||||
return setmetatable(constraint, constraint.mt)
|
return setmetatable(constraint, constraint.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
|
||||||
|
|
Loading…
Reference in New Issue