From dc0afe9f596f9e74e2325faedef13a919a0ce856 Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Mon, 28 Sep 2015 21:00:49 +0200 Subject: [PATCH] Add some constructor arguments to some layouts This adds new constructor arguments to align, fixed and flex which allows adding widgets directly while creating the layout. Idea originally by actionless: https://github.com/awesomeWM/awesome/pull/486#issuecomment-143606301 Closes https://github.com/awesomeWM/awesome/pull/490. Signed-off-by: Uli Schlachter --- lib/wibox/layout/align.lua | 19 +++++++++++++----- lib/wibox/layout/fixed.lua | 34 +++++++++++++++++++++----------- lib/wibox/layout/flex.lua | 29 +++++++++++++++++++-------- spec/wibox/layout/align_spec.lua | 13 ++++++++++++ spec/wibox/layout/fixed_spec.lua | 10 +++++++--- spec/wibox/layout/flex_spec.lua | 10 +++++++--- 6 files changed, 85 insertions(+), 30 deletions(-) diff --git a/lib/wibox/layout/align.lua b/lib/wibox/layout/align.lua index 8d8d78769..7d5cff066 100644 --- a/lib/wibox/layout/align.lua +++ b/lib/wibox/layout/align.lua @@ -221,7 +221,7 @@ function align:reset() self:emit_signal("widget::layout_changed") end -local function get_layout(dir) +local function get_layout(dir, first, second, third) local ret = base.make_widget() ret.dir = dir @@ -232,6 +232,9 @@ local function get_layout(dir) end ret:set_expand("inside") + ret:set_first(first) + ret:set_second(second) + ret:set_third(third) return ret end @@ -240,8 +243,11 @@ end -- three widgets. The widget set via :set_left() is left-aligned. :set_right() -- sets a widget which will be right-aligned. The remaining space between those -- two will be given to the widget set via :set_middle(). -function align.horizontal() - local ret = get_layout("x") +-- @tparam[opt] widget left Widget to be put to the left. +-- @tparam[opt] widget middle Widget to be put to the middle. +-- @tparam[opt] widget right Widget to be put to the right. +function align.horizontal(left, middle, right) + local ret = get_layout("x", left, middle, right) ret.set_left = ret.set_first ret.set_middle = ret.set_second @@ -254,8 +260,11 @@ end -- three widgets. The widget set via :set_top() is top-aligned. :set_bottom() -- sets a widget which will be bottom-aligned. The remaining space between those -- two will be given to the widget set via :set_middle(). -function align.vertical() - local ret = get_layout("y") +-- @tparam[opt] widget top Widget to be put to the top. +-- @tparam[opt] widget middle Widget to be put to the middle. +-- @tparam[opt] widget bottom Widget to be put to the right. +function align.vertical(top, middle, bottom) + local ret = get_layout("y", top, middle, bottom) ret.set_top = ret.set_first ret.set_middle = ret.set_second diff --git a/lib/wibox/layout/fixed.lua b/lib/wibox/layout/fixed.lua index f745834b4..d59d8af77 100644 --- a/lib/wibox/layout/fixed.lua +++ b/lib/wibox/layout/fixed.lua @@ -49,10 +49,16 @@ function fixed:layout(context, width, height) return result end ---- Add a widget to the given fixed layout -function fixed:add(widget) - base.check_widget(widget) - table.insert(self.widgets, widget) +--- Add some widgets to the given fixed layout +-- @tparam widget ... Widgets that should be added (must at least be one) +function fixed:add(...) + -- No table.pack in Lua 5.1 :-( + local args = { n=select('#', ...), ... } + assert(args.n > 0, "need at least one widget to add") + for i=1, args.n do + base.check_widget(args[i]) + table.insert(self.widgets, args[i]) + end self:emit_signal("widget::layout_changed") end @@ -113,7 +119,7 @@ function fixed:fill_space(val) end end -local function get_layout(dir) +local function get_layout(dir, widget1, ...) local ret = base.make_widget() for k, v in pairs(fixed) do @@ -127,21 +133,27 @@ local function get_layout(dir) ret:set_spacing(0) ret:fill_space(false) + if widget1 then + ret:add(widget1, ...) + end + return ret end --- Returns a new horizontal fixed layout. Each widget will get as much space as it -- asks for and each widget will be drawn next to its neighboring widget. --- Widgets can be added via :add(). -function fixed.horizontal() - return get_layout("x") +-- Widgets can be added via :add() or as arguments to this function. +-- @tparam widget ... Widgets that should be added to the layout. +function fixed.horizontal(...) + return get_layout("x", ...) end --- Returns a new vertical fixed layout. Each widget will get as much space as it -- asks for and each widget will be drawn next to its neighboring widget. --- Widgets can be added via :add(). -function fixed.vertical() - return get_layout("y") +-- Widgets can be added via :add() or as arguments to this function. +-- @tparam widget ... Widgets that should be added to the layout. +function fixed.vertical(...) + return get_layout("y", ...) end --- Add spacing between each layout widgets diff --git a/lib/wibox/layout/flex.lua b/lib/wibox/layout/flex.lua index 8ceab56f0..78c04e3cb 100644 --- a/lib/wibox/layout/flex.lua +++ b/lib/wibox/layout/flex.lua @@ -96,9 +96,16 @@ function flex:fit(context, orig_width, orig_height) return used_in_dir + spacing, used_in_other end -function flex:add(widget) - base.check_widget(widget) - table.insert(self.widgets, widget) +--- Add some widgets to the given flex layout +-- @tparam widget ... Widgets that should be added (must at least be one) +function flex:add(...) + -- No table.pack in Lua 5.1 :-( + local args = { n=select('#', ...), ... } + assert(args.n > 0, "need at least one widget to add") + for i=1, args.n do + base.check_widget(args[i]) + table.insert(self.widgets, args[i]) + end self:emit_signal("widget::layout_changed") end @@ -127,7 +134,7 @@ function flex:reset() self:emit_signal("widget::layout_changed") end -local function get_layout(dir) +local function get_layout(dir, widget1, ...) local ret = base.make_widget() for k, v in pairs(flex) do @@ -140,19 +147,25 @@ local function get_layout(dir) ret.widgets = {} ret:set_spacing(0) + if widget1 then + ret:add(widget1, ...) + end + return ret end --- Returns a new horizontal flex layout. A flex layout shares the available space -- equally among all widgets. Widgets can be added via :add(widget). -function flex.horizontal() - return get_layout("x") +-- @tparam widget ... Widgets that should be added to the layout. +function flex.horizontal(...) + return get_layout("x", ...) end --- Returns a new vertical flex layout. A flex layout shares the available space -- equally among all widgets. Widgets can be added via :add(widget). -function flex.vertical() - return get_layout("y") +-- @tparam widget ... Widgets that should be added to the layout. +function flex.vertical(...) + return get_layout("y", ...) end return flex diff --git a/spec/wibox/layout/align_spec.lua b/spec/wibox/layout/align_spec.lua index 81ad7a40b..cd5d9b989 100644 --- a/spec/wibox/layout/align_spec.lua +++ b/spec/wibox/layout/align_spec.lua @@ -265,6 +265,19 @@ describe("wibox.layout.align", function() layout:set_third(w2) assert.is.equal(layout_changed, 2) end) + + it("set again", function() + local w1, w2, w3 = {}, {}, {} + layout = align.vertical(w1, w2, w3) + layout:connect_signal("widget::layout_changed", function() + layout_changed = layout_changed + 1 + end) + assert.is.equal(layout_changed, 0) + layout:set_first(w1) + layout:set_second(w2) + layout:set_third(w3) + assert.is.equal(layout_changed, 0) + end) end) end) diff --git a/spec/wibox/layout/fixed_spec.lua b/spec/wibox/layout/fixed_spec.lua index 0e5c1a859..1da3d3b35 100644 --- a/spec/wibox/layout/fixed_spec.lua +++ b/spec/wibox/layout/fixed_spec.lua @@ -22,6 +22,12 @@ describe("wibox.layout.fixed", function() assert.widget_layout(layout, { 0, 0 }, {}) end) + it("empty add", function() + assert.has_error(function() + layout:add() + end) + end) + describe("with widgets", function() local first, second, third @@ -30,9 +36,7 @@ describe("wibox.layout.fixed", function() second = utils.widget_stub(15, 15) third = utils.widget_stub(10, 10) - layout:add(first) - layout:add(second) - layout:add(third) + layout:add(first, second, third) end) describe("with enough space", function() diff --git a/spec/wibox/layout/flex_spec.lua b/spec/wibox/layout/flex_spec.lua index ac83500a8..ff5a00d6f 100644 --- a/spec/wibox/layout/flex_spec.lua +++ b/spec/wibox/layout/flex_spec.lua @@ -22,6 +22,12 @@ describe("wibox.layout.flex", function() assert.widget_layout(layout, { 0, 0 }, {}) end) + it("empty add", function() + assert.has_error(function() + layout:add() + end) + end) + describe("with widgets", function() local first, second, third @@ -30,9 +36,7 @@ describe("wibox.layout.flex", function() second = utils.widget_stub(15, 15) third = utils.widget_stub(10, 10) - layout:add(first) - layout:add(second) - layout:add(third) + layout:add(first, second, third) end) describe("with enough space", function()