2016-05-23 05:56:45 +02:00
|
|
|
---------------------------------------------------------------------------
|
2016-05-24 19:53:21 +02:00
|
|
|
--
|
|
|
|
--@DOC_wibox_container_defaults_constraint_EXAMPLE@
|
2016-05-23 05:56:45 +02:00
|
|
|
-- @author Lukáš Hrázký
|
|
|
|
-- @copyright 2012 Lukáš Hrázký
|
2019-06-06 08:10:36 +02:00
|
|
|
-- @containermod wibox.container.constraint
|
2016-05-23 05:56:45 +02:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
local setmetatable = setmetatable
|
|
|
|
local base = require("wibox.widget.base")
|
2017-03-08 21:18:33 +01:00
|
|
|
local gtable = require("gears.table")
|
2016-05-23 05:56:45 +02:00
|
|
|
local math = math
|
|
|
|
|
|
|
|
local constraint = { mt = {} }
|
|
|
|
|
2016-05-23 09:02:11 +02:00
|
|
|
-- Layout a constraint layout
|
2016-05-23 05:56:45 +02:00
|
|
|
function constraint:layout(_, width, height)
|
2016-05-26 21:37:33 +02:00
|
|
|
if self._private.widget then
|
|
|
|
return { base.place_widget_at(self._private.widget, 0, 0, width, height) }
|
2016-05-23 05:56:45 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-05-23 09:02:11 +02:00
|
|
|
-- Fit a constraint layout into the given space
|
2016-05-23 05:56:45 +02:00
|
|
|
function constraint:fit(context, width, height)
|
|
|
|
local w, h
|
2016-05-26 21:37:33 +02:00
|
|
|
if self._private.widget then
|
|
|
|
w = self._private.strategy(width, self._private.width)
|
|
|
|
h = self._private.strategy(height, self._private.height)
|
2016-05-23 05:56:45 +02:00
|
|
|
|
2016-05-26 21:37:33 +02:00
|
|
|
w, h = base.fit_widget(self, context, self._private.widget, w, h)
|
2016-05-23 05:56:45 +02:00
|
|
|
else
|
|
|
|
w, h = 0, 0
|
|
|
|
end
|
|
|
|
|
2016-05-26 21:37:33 +02:00
|
|
|
w = self._private.strategy(w, self._private.width)
|
|
|
|
h = self._private.strategy(h, self._private.height)
|
2016-05-23 05:56:45 +02:00
|
|
|
|
|
|
|
return w, h
|
|
|
|
end
|
|
|
|
|
2016-05-26 21:37:33 +02:00
|
|
|
--- The widget to be constrained.
|
|
|
|
-- @property widget
|
|
|
|
-- @tparam widget widget The widget
|
|
|
|
|
2016-05-23 05:56:45 +02:00
|
|
|
function constraint:set_widget(widget)
|
2016-05-26 21:37:33 +02:00
|
|
|
self._private.widget = widget
|
2016-05-23 05:56:45 +02:00
|
|
|
self:emit_signal("widget::layout_changed")
|
|
|
|
end
|
|
|
|
|
2016-05-26 21:37:33 +02:00
|
|
|
function constraint:get_widget()
|
|
|
|
return self._private.widget
|
|
|
|
end
|
|
|
|
|
2016-05-23 05:56:45 +02:00
|
|
|
--- Get the number of children element
|
|
|
|
-- @treturn table The children
|
|
|
|
function constraint:get_children()
|
2016-05-26 21:37:33 +02:00
|
|
|
return {self._private.widget}
|
2016-05-23 05:56:45 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Replace the layout children
|
|
|
|
-- This layout only accept one children, all others will be ignored
|
|
|
|
-- @tparam table children A table composed of valid widgets
|
|
|
|
function constraint:set_children(children)
|
|
|
|
self:set_widget(children[1])
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Set the strategy to use for the constraining. Valid values are 'max',
|
|
|
|
-- 'min' or 'exact'. Throws an error on invalid values.
|
2016-05-26 21:37:33 +02:00
|
|
|
-- @property strategy
|
|
|
|
|
2016-05-23 05:56:45 +02:00
|
|
|
function constraint:set_strategy(val)
|
|
|
|
local func = {
|
|
|
|
min = function(real_size, limit)
|
|
|
|
return limit and math.max(limit, real_size) or real_size
|
|
|
|
end,
|
|
|
|
max = function(real_size, limit)
|
|
|
|
return limit and math.min(limit, real_size) or real_size
|
|
|
|
end,
|
|
|
|
exact = function(real_size, limit)
|
|
|
|
return limit or real_size
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
if not func[val] then
|
|
|
|
error("Invalid strategy for constraint layout: " .. tostring(val))
|
|
|
|
end
|
|
|
|
|
2016-05-26 21:37:33 +02:00
|
|
|
self._private.strategy = func[val]
|
2016-05-23 05:56:45 +02:00
|
|
|
self:emit_signal("widget::layout_changed")
|
|
|
|
end
|
|
|
|
|
2016-05-26 21:37:33 +02:00
|
|
|
function constraint:get_strategy()
|
|
|
|
return self._private.strategy
|
|
|
|
end
|
|
|
|
|
2016-05-23 05:56:45 +02:00
|
|
|
--- Set the maximum width to val. nil for no width limit.
|
2016-05-26 21:37:33 +02:00
|
|
|
-- @property height
|
|
|
|
-- @param number
|
|
|
|
|
2016-05-23 05:56:45 +02:00
|
|
|
function constraint:set_width(val)
|
2016-05-26 21:37:33 +02:00
|
|
|
self._private.width = val
|
2016-05-23 05:56:45 +02:00
|
|
|
self:emit_signal("widget::layout_changed")
|
|
|
|
end
|
|
|
|
|
2016-05-26 21:37:33 +02:00
|
|
|
function constraint:get_width()
|
|
|
|
return self._private.width
|
|
|
|
end
|
|
|
|
|
2016-05-23 05:56:45 +02:00
|
|
|
--- Set the maximum height to val. nil for no height limit.
|
2016-05-26 21:37:33 +02:00
|
|
|
-- @property width
|
|
|
|
-- @param number
|
|
|
|
|
2016-05-23 05:56:45 +02:00
|
|
|
function constraint:set_height(val)
|
2016-05-26 21:37:33 +02:00
|
|
|
self._private.height = val
|
2016-05-23 05:56:45 +02:00
|
|
|
self:emit_signal("widget::layout_changed")
|
|
|
|
end
|
|
|
|
|
2016-05-26 21:37:33 +02:00
|
|
|
function constraint:get_height()
|
|
|
|
return self._private.height
|
|
|
|
end
|
|
|
|
|
2016-05-23 05:56:45 +02:00
|
|
|
--- Reset this layout. The widget will be unreferenced, strategy set to "max"
|
|
|
|
-- and the constraints set to nil.
|
|
|
|
function constraint:reset()
|
2016-05-26 21:37:33 +02:00
|
|
|
self._private.width = nil
|
|
|
|
self._private.height = nil
|
2016-05-23 05:56:45 +02:00
|
|
|
self:set_strategy("max")
|
|
|
|
self:set_widget(nil)
|
|
|
|
end
|
|
|
|
|
2016-05-23 08:46:28 +02:00
|
|
|
--- Returns a new constraint container.
|
|
|
|
-- This container will constraint the size of a
|
2016-05-23 05:56:45 +02:00
|
|
|
-- widget according to the strategy. Note that this will only work for layouts
|
|
|
|
-- that respect the widget's size, eg. fixed layout. In layouts that don't
|
|
|
|
-- (fully) respect widget's requested size, the inner widget still might get
|
|
|
|
-- drawn with a size that does not fit the constraint, eg. in flex layout.
|
|
|
|
-- @param[opt] widget A widget to use.
|
|
|
|
-- @param[opt] strategy How to constraint the size. 'max' (default), 'min' or
|
|
|
|
-- 'exact'.
|
|
|
|
-- @param[opt] width The maximum width of the widget. nil for no limit.
|
|
|
|
-- @param[opt] height The maximum height of the widget. nil for no limit.
|
2016-05-23 08:46:28 +02:00
|
|
|
-- @treturn table A new constraint container
|
|
|
|
-- @function wibox.container.constraint
|
2016-05-23 05:56:45 +02:00
|
|
|
local function new(widget, strategy, width, height)
|
2016-05-26 21:37:33 +02:00
|
|
|
local ret = base.make_widget(nil, nil, {enable_properties = true})
|
2016-05-23 05:56:45 +02:00
|
|
|
|
2017-03-08 21:18:33 +01:00
|
|
|
gtable.crush(ret, constraint, true)
|
2016-05-23 05:56:45 +02:00
|
|
|
|
|
|
|
ret:set_strategy(strategy or "max")
|
|
|
|
ret:set_width(width)
|
|
|
|
ret:set_height(height)
|
|
|
|
|
|
|
|
if widget then
|
|
|
|
ret:set_widget(widget)
|
|
|
|
end
|
|
|
|
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
|
|
|
function constraint.mt:__call(...)
|
|
|
|
return new(...)
|
|
|
|
end
|
|
|
|
|
2016-05-26 21:37:33 +02:00
|
|
|
--@DOC_widget_COMMON@
|
|
|
|
|
|
|
|
--@DOC_object_COMMON@
|
|
|
|
|
2016-05-23 05:56:45 +02:00
|
|
|
return setmetatable(constraint, constraint.mt)
|
|
|
|
|
|
|
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|