2016-05-23 05:56:45 +02:00
|
|
|
---------------------------------------------------------------------------
|
2019-11-29 01:45:07 +01:00
|
|
|
-- Restrict a widget size using one of multiple available strategies.
|
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
|
2021-03-27 20:05:10 +01:00
|
|
|
-- @supermodule wibox.widget.base
|
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.
|
2019-11-29 01:45:07 +01:00
|
|
|
--
|
2016-05-26 21:37:33 +02:00
|
|
|
-- @property widget
|
|
|
|
-- @tparam widget widget The widget
|
2019-11-29 01:45:07 +01:00
|
|
|
-- @interface container
|
2016-05-26 21:37:33 +02:00
|
|
|
|
2018-12-27 05:54:10 +01:00
|
|
|
constraint.set_widget = base.set_widget_common
|
2016-05-23 05:56:45 +02:00
|
|
|
|
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
|
|
|
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
|
|
|
|
|
|
|
|
function constraint:set_children(children)
|
|
|
|
self:set_widget(children[1])
|
|
|
|
end
|
|
|
|
|
2019-11-29 01:45:07 +01:00
|
|
|
--- Set the strategy to use for the constraining.
|
|
|
|
-- Valid values are:
|
|
|
|
--
|
|
|
|
-- * **max**: Never allow the size to be larger than the limit.
|
|
|
|
-- * **min**: Never allow the size to tbe below the limit.
|
|
|
|
-- * **exact**: Force the widget size.
|
|
|
|
--
|
2016-05-26 21:37:33 +02:00
|
|
|
-- @property strategy
|
2019-11-29 01:45:07 +01:00
|
|
|
-- @tparam string strategy Either 'max', 'min' or 'exact'.
|
|
|
|
-- @propemits true false
|
2016-05-26 21:37:33 +02:00
|
|
|
|
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")
|
2019-11-29 01:45:07 +01:00
|
|
|
self:emit_signal("property::strategy", val)
|
2016-05-23 05:56:45 +02:00
|
|
|
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.
|
2019-11-29 01:45:07 +01:00
|
|
|
--
|
2022-02-12 11:07:22 +01:00
|
|
|
-- @property width
|
|
|
|
-- @tparam number width
|
2019-11-29 01:45:07 +01:00
|
|
|
-- @propemits true false
|
2016-05-26 21:37:33 +02:00
|
|
|
|
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")
|
2019-11-29 01:45:07 +01:00
|
|
|
self:emit_signal("property::width", val)
|
2016-05-23 05:56:45 +02:00
|
|
|
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.
|
2019-11-29 01:45:07 +01:00
|
|
|
--
|
2022-02-12 11:07:22 +01:00
|
|
|
-- @property height
|
|
|
|
-- @tparam number height
|
2019-11-29 01:45:07 +01:00
|
|
|
-- @propemits true false
|
2016-05-26 21:37:33 +02:00
|
|
|
|
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")
|
2019-11-29 01:45:07 +01:00
|
|
|
self:emit_signal("property::height", val)
|
2016-05-23 05:56:45 +02:00
|
|
|
end
|
|
|
|
|
2016-05-26 21:37:33 +02:00
|
|
|
function constraint:get_height()
|
|
|
|
return self._private.height
|
|
|
|
end
|
|
|
|
|
2019-11-29 01:45:07 +01:00
|
|
|
--- Reset this layout.
|
|
|
|
--
|
|
|
|
--The widget will be unreferenced, strategy set to "max"
|
2016-05-23 05:56:45 +02:00
|
|
|
-- and the constraints set to nil.
|
2019-11-29 01:45:07 +01:00
|
|
|
--
|
2019-06-06 22:32:53 +02:00
|
|
|
-- @method reset
|
2022-07-05 10:37:14 +02:00
|
|
|
-- @noreturn
|
2019-11-29 01:45:07 +01:00
|
|
|
-- @interface container
|
2016-05-23 05:56:45 +02:00
|
|
|
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.
|
2019-11-29 01:45:07 +01:00
|
|
|
--
|
2016-05-23 08:46:28 +02:00
|
|
|
-- 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
|
2019-06-07 20:59:34 +02:00
|
|
|
-- @constructorfct 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
|
|
|
|
|
|
|
|
return setmetatable(constraint, constraint.mt)
|
|
|
|
|
|
|
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|