wibox.layout.constraint: fix the min and max strategies

Fixes mixed up min/max strategies and other bugs in min and max. Also
removes enforcing the size in draw, adhering more to awesome's layout
concept.

Signed-off-by: Lukáš Hrázký <lukkash@email.cz>
Signed-off-by: Uli Schlachter <psychon@znc.in>
This commit is contained in:
Lukáš Hrázký 2013-01-19 15:59:27 +01:00 committed by Uli Schlachter
parent c34e9780b2
commit 5365dfdb79
1 changed files with 34 additions and 36 deletions

View File

@ -20,35 +20,23 @@ function constraint:draw(wibox, cr, width, height)
return
end
local w, h = self:fit(width, height)
base.draw_widget(wibox, cr, self.widget, 0, 0, w, h)
base.draw_widget(wibox, cr, self.widget, 0, 0, width, height)
end
--- Fit a constraint layout into the given space
function constraint:fit(width, height)
local w, h
if self.widget then
w, h = self.widget:fit(width, height)
w = self._strategy(width, self._width)
h = self._strategy(height, self._height)
w, h = self.widget:fit(w, h)
else
w, h = 0, 0
end
if self._width > 0 then
if self.strategy == "exact" then
w = self._width
else
w = math[self.strategy](w, self._width)
end
end
if self._height > 0 then
if self.strategy == "exact" then
h = self._height
else
h = math[self.strategy](h, self._height)
end
end
w = self._strategy(w, self._width)
h = self._strategy(h, self._height)
return w, h
end
@ -69,47 +57,57 @@ end
--- Set the strategy to use for the constraining. Valid values are 'max',
-- 'min' or 'exact'. Throws an error on invalid values.
function constraint:set_strategy(val)
local allowed = {
max = true,
min = true,
exact = true
local func = {
min = function(real_size, constraint)
return constraint and math.max(constraint, real_size) or real_size
end,
max = function(real_size, constraint)
return constraint and math.min(constraint, real_size) or real_size
end,
exact = function(real_size, constraint)
return constraint or real_size
end
}
if not allowed[val] then
if not func[val] then
error("Invalid strategy for constraint layout: " .. tostring(val))
end
self.strategy = val
self._strategy = func[val]
self:emit_signal("widget::updated")
end
--- Set the maximum width to val.
--- Set the maximum width to val. nil for no width limit.
function constraint:set_width(val)
self._width = val
self:emit_signal("widget::updated")
end
--- Set the maximum height to val.
--- Set the maximum height to val. nil for no height limit.
function constraint:set_height(val)
self._height = val
self:emit_signal("widget::updated")
end
--- Reset this layout. The widget will be unreferenced, strategy set to "max"
-- and the constraints set to 0.
-- and the constraints set to nil.
function constraint:reset()
self.strategy = "max"
self._width = 0
self._height = 0
self._width = nil
self._height = nil
self:set_strategy("max")
self:set_widget(nil)
end
--- Returns a new constraint layout.
--- Returns a new constraint layout. This layout will constraint the size of a
-- 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 widget A widget to use (optional)
-- @param strategy How to constraint the size. 'max' (default), 'min' or
-- 'exact'. (optional)
-- @param width The maximum width of the widget. 0 for no limit. (optional)
-- @param height The maximum height of the widget. 0 for no limit. (optional)
-- @param width The maximum width of the widget. nil for no limit. (optional)
-- @param height The maximum height of the widget. nil for no limit. (optional)
local function new(widget, strategy, width, height)
local ret = widget_base.make_widget()
@ -124,8 +122,8 @@ local function new(widget, strategy, width, height)
end
ret:set_strategy(strategy or "max")
ret:set_width(width or 0)
ret:set_height(height or 0)
ret:set_width(width)
ret:set_height(height)
if widget then
ret:set_widget(widget)