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 <psychon@znc.in>
This commit is contained in:
parent
532ec0cd90
commit
dc0afe9f59
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in New Issue