From dcd4db69e6a12c06263a02bc8fa546457f82c102 Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Sun, 21 Jun 2015 14:58:13 +0200 Subject: [PATCH] fixed layout: Test signal emission Signed-off-by: Uli Schlachter --- lib/wibox/layout/fixed.lua | 18 +++++++---- spec/wibox/layout/fixed_spec.lua | 54 ++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 6 deletions(-) diff --git a/lib/wibox/layout/fixed.lua b/lib/wibox/layout/fixed.lua index 110189a5..fc251993 100644 --- a/lib/wibox/layout/fixed.lua +++ b/lib/wibox/layout/fixed.lua @@ -17,7 +17,7 @@ local fixed = {} -- @param height The available height. function fixed:layout(context, width, height) local result = {} - local pos,spacing = 0,self._spacing or 0 + local pos,spacing = 0, self._spacing for k, v in pairs(self.widgets) do local x, y, w, h, _ @@ -89,7 +89,7 @@ function fixed:fit(context, orig_width, orig_height) end end - local spacing = ((self._spacing or 0)*(#self.widgets-1)) + local spacing = self._spacing * (#self.widgets-1) if self.dir == "y" then return used_max, used_in_dir + spacing @@ -107,8 +107,10 @@ end -- widget will get all the space that is left. If this is false, the last widget -- won't be handled specially and there can be space left unused. function fixed:fill_space(val) - self._fill_space = val - self:emit_signal("widget::layout_changed") + if self._fill_space ~= val then + self._fill_space = not not val + self:emit_signal("widget::layout_changed") + end end local function get_layout(dir) @@ -122,6 +124,8 @@ local function get_layout(dir) ret.dir = dir ret.widgets = {} + ret:set_spacing(0) + ret:fill_space(false) return ret end @@ -143,8 +147,10 @@ end --- Add spacing between each layout widgets -- @param spacing Spacing between widgets. function fixed: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 return fixed diff --git a/spec/wibox/layout/fixed_spec.lua b/spec/wibox/layout/fixed_spec.lua index 8d5db26c..0e5c1a85 100644 --- a/spec/wibox/layout/fixed_spec.lua +++ b/spec/wibox/layout/fixed_spec.lua @@ -4,6 +4,7 @@ --------------------------------------------------------------------------- local fixed = require("wibox.layout.fixed") +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.fixed", 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("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("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("fill_space", function() + assert.is.equal(layout_changed, 0) + layout:fill_space(false) + assert.is.equal(layout_changed, 0) + layout:fill_space(true) + assert.is.equal(layout_changed, 1) + layout:fill_space(true) + assert.is.equal(layout_changed, 1) + layout:fill_space(false) + assert.is.equal(layout_changed, 2) + end) + end) end) -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80