diff --git a/lib/wibox/layout/flex.lua b/lib/wibox/layout/flex.lua index d2330031..47d983c4 100644 --- a/lib/wibox/layout/flex.lua +++ b/lib/wibox/layout/flex.lua @@ -23,7 +23,7 @@ end -- @param height The available height. function flex:layout(context, width, height) local result = {} - local pos,spacing = 0,self._spacing or 0 + local pos,spacing = 0, self._spacing local num = #self.widgets local total_spacing = (spacing*(num-1)) @@ -88,7 +88,7 @@ function flex:fit(context, orig_width, orig_height) #self.widgets * self._max_widget_size) end - local spacing = ((self._spacing or 0)*(#self.widgets-1)) + local spacing = self._spacing * (#self.widgets-1) if self.dir == "y" then return used_in_other, used_in_dir + spacing @@ -106,15 +106,19 @@ end -- maximum width for horizontal and maximum height for vertical). -- @param val The maximum size of the widget. function flex:set_max_widget_size(val) - self._max_widget_size = val - self:emit_signal("widget::layout_changed") + if self._max_widget_size ~= val then + self._max_widget_size = val + self:emit_signal("widget::layout_changed") + end end --- Add spacing between each layout widgets -- @param spacing Spacing between widgets. function flex:set_spacing(spacing) - self._spacing = spacing - self:emit_signal("widget::layout_changed") + if self._spacing ~= spacing then + self._spacing = spacing + self:emit_signal("widget::layout_changed") + end end function flex:reset() @@ -134,6 +138,7 @@ local function get_layout(dir) ret.dir = dir ret.widgets = {} + ret:set_spacing(0) return ret end diff --git a/spec/wibox/layout/flex_spec.lua b/spec/wibox/layout/flex_spec.lua index 9f0e698a..ac83500a 100644 --- a/spec/wibox/layout/flex_spec.lua +++ b/spec/wibox/layout/flex_spec.lua @@ -4,6 +4,7 @@ --------------------------------------------------------------------------- local flex = require("wibox.layout.flex") +local base = require("wibox.widget.base") local utils = require("wibox.test_utils") local p = require("wibox.widget.base").place_widget_at @@ -76,6 +77,59 @@ describe("wibox.layout.flex", function() end) end) end) + + describe("emitting signals", function() + local layout_changed + before_each(function() + layout:connect_signal("widget::layout_changed", function() + layout_changed = layout_changed + 1 + end) + layout_changed = 0 + end) + + it("add", function() + local w1, w2 = base.empty_widget(), base.empty_widget() + assert.is.equal(layout_changed, 0) + layout:add(w1) + assert.is.equal(layout_changed, 1) + layout:add(w2) + assert.is.equal(layout_changed, 2) + layout:add(w2) + assert.is.equal(layout_changed, 3) + end) + + it("reset", function() + assert.is.equal(layout_changed, 0) + layout:add(base.make_widget()) + assert.is.equal(layout_changed, 1) + layout:reset() + assert.is.equal(layout_changed, 2) + end) + + it("set_spacing", function() + assert.is.equal(layout_changed, 0) + layout:set_spacing(0) + assert.is.equal(layout_changed, 0) + layout:set_spacing(5) + assert.is.equal(layout_changed, 1) + layout:set_spacing(2) + assert.is.equal(layout_changed, 2) + layout:set_spacing(2) + assert.is.equal(layout_changed, 2) + end) + + it("set_max_widget_size", function() + assert.is.equal(layout_changed, 0) + layout:set_max_widget_size(nil) + assert.is.equal(layout_changed, 0) + layout:set_max_widget_size(20) + assert.is.equal(layout_changed, 1) + layout:set_max_widget_size(20) + assert.is.equal(layout_changed, 1) + layout:set_max_widget_size(nil) + assert.is.equal(layout_changed, 2) + end) + end) end) -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80