From 5365dfdb79d1ba8ef8010baba1a0a072a6eb2e01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Hr=C3=A1zk=C3=BD?= Date: Sat, 19 Jan 2013 15:59:27 +0100 Subject: [PATCH] wibox.layout.constraint: fix the min and max strategies MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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ý Signed-off-by: Uli Schlachter --- lib/wibox/layout/constraint.lua.in | 70 +++++++++++++++--------------- 1 file changed, 34 insertions(+), 36 deletions(-) diff --git a/lib/wibox/layout/constraint.lua.in b/lib/wibox/layout/constraint.lua.in index 0cafc036d..9f75f0324 100644 --- a/lib/wibox/layout/constraint.lua.in +++ b/lib/wibox/layout/constraint.lua.in @@ -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)