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")
|
self:emit_signal("widget::layout_changed")
|
||||||
end
|
end
|
||||||
|
|
||||||
local function get_layout(dir)
|
local function get_layout(dir, first, second, third)
|
||||||
local ret = base.make_widget()
|
local ret = base.make_widget()
|
||||||
ret.dir = dir
|
ret.dir = dir
|
||||||
|
|
||||||
|
@ -232,6 +232,9 @@ local function get_layout(dir)
|
||||||
end
|
end
|
||||||
|
|
||||||
ret:set_expand("inside")
|
ret:set_expand("inside")
|
||||||
|
ret:set_first(first)
|
||||||
|
ret:set_second(second)
|
||||||
|
ret:set_third(third)
|
||||||
|
|
||||||
return ret
|
return ret
|
||||||
end
|
end
|
||||||
|
@ -240,8 +243,11 @@ end
|
||||||
-- three widgets. The widget set via :set_left() is left-aligned. :set_right()
|
-- 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
|
-- sets a widget which will be right-aligned. The remaining space between those
|
||||||
-- two will be given to the widget set via :set_middle().
|
-- two will be given to the widget set via :set_middle().
|
||||||
function align.horizontal()
|
-- @tparam[opt] widget left Widget to be put to the left.
|
||||||
local ret = get_layout("x")
|
-- @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_left = ret.set_first
|
||||||
ret.set_middle = ret.set_second
|
ret.set_middle = ret.set_second
|
||||||
|
@ -254,8 +260,11 @@ end
|
||||||
-- three widgets. The widget set via :set_top() is top-aligned. :set_bottom()
|
-- 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
|
-- sets a widget which will be bottom-aligned. The remaining space between those
|
||||||
-- two will be given to the widget set via :set_middle().
|
-- two will be given to the widget set via :set_middle().
|
||||||
function align.vertical()
|
-- @tparam[opt] widget top Widget to be put to the top.
|
||||||
local ret = get_layout("y")
|
-- @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_top = ret.set_first
|
||||||
ret.set_middle = ret.set_second
|
ret.set_middle = ret.set_second
|
||||||
|
|
|
@ -49,10 +49,16 @@ function fixed:layout(context, width, height)
|
||||||
return result
|
return result
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Add a widget to the given fixed layout
|
--- Add some widgets to the given fixed layout
|
||||||
function fixed:add(widget)
|
-- @tparam widget ... Widgets that should be added (must at least be one)
|
||||||
base.check_widget(widget)
|
function fixed:add(...)
|
||||||
table.insert(self.widgets, widget)
|
-- 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")
|
self:emit_signal("widget::layout_changed")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -113,7 +119,7 @@ function fixed:fill_space(val)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function get_layout(dir)
|
local function get_layout(dir, widget1, ...)
|
||||||
local ret = base.make_widget()
|
local ret = base.make_widget()
|
||||||
|
|
||||||
for k, v in pairs(fixed) do
|
for k, v in pairs(fixed) do
|
||||||
|
@ -127,21 +133,27 @@ local function get_layout(dir)
|
||||||
ret:set_spacing(0)
|
ret:set_spacing(0)
|
||||||
ret:fill_space(false)
|
ret:fill_space(false)
|
||||||
|
|
||||||
|
if widget1 then
|
||||||
|
ret:add(widget1, ...)
|
||||||
|
end
|
||||||
|
|
||||||
return ret
|
return ret
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Returns a new horizontal fixed layout. Each widget will get as much space as it
|
--- 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.
|
-- asks for and each widget will be drawn next to its neighboring widget.
|
||||||
-- Widgets can be added via :add().
|
-- Widgets can be added via :add() or as arguments to this function.
|
||||||
function fixed.horizontal()
|
-- @tparam widget ... Widgets that should be added to the layout.
|
||||||
return get_layout("x")
|
function fixed.horizontal(...)
|
||||||
|
return get_layout("x", ...)
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Returns a new vertical fixed layout. Each widget will get as much space as it
|
--- 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.
|
-- asks for and each widget will be drawn next to its neighboring widget.
|
||||||
-- Widgets can be added via :add().
|
-- Widgets can be added via :add() or as arguments to this function.
|
||||||
function fixed.vertical()
|
-- @tparam widget ... Widgets that should be added to the layout.
|
||||||
return get_layout("y")
|
function fixed.vertical(...)
|
||||||
|
return get_layout("y", ...)
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Add spacing between each layout widgets
|
--- 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
|
return used_in_dir + spacing, used_in_other
|
||||||
end
|
end
|
||||||
|
|
||||||
function flex:add(widget)
|
--- Add some widgets to the given flex layout
|
||||||
base.check_widget(widget)
|
-- @tparam widget ... Widgets that should be added (must at least be one)
|
||||||
table.insert(self.widgets, widget)
|
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")
|
self:emit_signal("widget::layout_changed")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -127,7 +134,7 @@ function flex:reset()
|
||||||
self:emit_signal("widget::layout_changed")
|
self:emit_signal("widget::layout_changed")
|
||||||
end
|
end
|
||||||
|
|
||||||
local function get_layout(dir)
|
local function get_layout(dir, widget1, ...)
|
||||||
local ret = base.make_widget()
|
local ret = base.make_widget()
|
||||||
|
|
||||||
for k, v in pairs(flex) do
|
for k, v in pairs(flex) do
|
||||||
|
@ -140,19 +147,25 @@ local function get_layout(dir)
|
||||||
ret.widgets = {}
|
ret.widgets = {}
|
||||||
ret:set_spacing(0)
|
ret:set_spacing(0)
|
||||||
|
|
||||||
|
if widget1 then
|
||||||
|
ret:add(widget1, ...)
|
||||||
|
end
|
||||||
|
|
||||||
return ret
|
return ret
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Returns a new horizontal flex layout. A flex layout shares the available space
|
--- Returns a new horizontal flex layout. A flex layout shares the available space
|
||||||
-- equally among all widgets. Widgets can be added via :add(widget).
|
-- equally among all widgets. Widgets can be added via :add(widget).
|
||||||
function flex.horizontal()
|
-- @tparam widget ... Widgets that should be added to the layout.
|
||||||
return get_layout("x")
|
function flex.horizontal(...)
|
||||||
|
return get_layout("x", ...)
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Returns a new vertical flex layout. A flex layout shares the available space
|
--- Returns a new vertical flex layout. A flex layout shares the available space
|
||||||
-- equally among all widgets. Widgets can be added via :add(widget).
|
-- equally among all widgets. Widgets can be added via :add(widget).
|
||||||
function flex.vertical()
|
-- @tparam widget ... Widgets that should be added to the layout.
|
||||||
return get_layout("y")
|
function flex.vertical(...)
|
||||||
|
return get_layout("y", ...)
|
||||||
end
|
end
|
||||||
|
|
||||||
return flex
|
return flex
|
||||||
|
|
|
@ -265,6 +265,19 @@ describe("wibox.layout.align", function()
|
||||||
layout:set_third(w2)
|
layout:set_third(w2)
|
||||||
assert.is.equal(layout_changed, 2)
|
assert.is.equal(layout_changed, 2)
|
||||||
end)
|
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)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
|
|
@ -22,6 +22,12 @@ describe("wibox.layout.fixed", function()
|
||||||
assert.widget_layout(layout, { 0, 0 }, {})
|
assert.widget_layout(layout, { 0, 0 }, {})
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
it("empty add", function()
|
||||||
|
assert.has_error(function()
|
||||||
|
layout:add()
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
|
||||||
describe("with widgets", function()
|
describe("with widgets", function()
|
||||||
local first, second, third
|
local first, second, third
|
||||||
|
|
||||||
|
@ -30,9 +36,7 @@ describe("wibox.layout.fixed", function()
|
||||||
second = utils.widget_stub(15, 15)
|
second = utils.widget_stub(15, 15)
|
||||||
third = utils.widget_stub(10, 10)
|
third = utils.widget_stub(10, 10)
|
||||||
|
|
||||||
layout:add(first)
|
layout:add(first, second, third)
|
||||||
layout:add(second)
|
|
||||||
layout:add(third)
|
|
||||||
end)
|
end)
|
||||||
|
|
||||||
describe("with enough space", function()
|
describe("with enough space", function()
|
||||||
|
|
|
@ -22,6 +22,12 @@ describe("wibox.layout.flex", function()
|
||||||
assert.widget_layout(layout, { 0, 0 }, {})
|
assert.widget_layout(layout, { 0, 0 }, {})
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
it("empty add", function()
|
||||||
|
assert.has_error(function()
|
||||||
|
layout:add()
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
|
||||||
describe("with widgets", function()
|
describe("with widgets", function()
|
||||||
local first, second, third
|
local first, second, third
|
||||||
|
|
||||||
|
@ -30,9 +36,7 @@ describe("wibox.layout.flex", function()
|
||||||
second = utils.widget_stub(15, 15)
|
second = utils.widget_stub(15, 15)
|
||||||
third = utils.widget_stub(10, 10)
|
third = utils.widget_stub(10, 10)
|
||||||
|
|
||||||
layout:add(first)
|
layout:add(first, second, third)
|
||||||
layout:add(second)
|
|
||||||
layout:add(third)
|
|
||||||
end)
|
end)
|
||||||
|
|
||||||
describe("with enough space", function()
|
describe("with enough space", function()
|
||||||
|
|
Loading…
Reference in New Issue