Convert the fixed layout to the new widget system

Signed-off-by: Uli Schlachter <psychon@znc.in>
This commit is contained in:
Uli Schlachter 2015-06-14 16:12:44 +02:00
parent e67e2813e9
commit 199b553895
2 changed files with 27 additions and 41 deletions

View File

@ -12,13 +12,12 @@ local pairs = pairs
local fixed = {}
--- Draw a fixed layout. Each widget gets just the space it asks for.
--- Layout a fixed layout. Each widget gets just the space it asks for.
-- @param context The context in which we are drawn.
-- @param cr The cairo context to use.
-- @param width The available width.
-- @param height The available height.
-- @return The total space needed by the layout.
function fixed:draw(context, cr, width, height)
function fixed:layout(context, width, height)
local result = {}
local pos,spacing = 0,self._spacing or 0
for k, v in pairs(self.widgets) do
@ -46,16 +45,16 @@ function fixed:draw(context, cr, width, height)
(self.dir ~= "y" and pos-spacing > width) then
break
end
base.draw_widget(context, cr, v, x, y, w, h)
table.insert(result, widget_base.place_widget_at(v, x, y, w, h))
end
return result
end
--- Add a widget to the given fixed layout
function fixed:add(widget)
widget_base.check_widget(widget)
table.insert(self.widgets, widget)
widget:weak_connect_signal("widget::updated", self._emit_updated)
self._emit_updated()
self:emit_signal("widget::layout_changed")
end
--- Fit the fixed layout into the given space
@ -101,11 +100,8 @@ end
--- Reset a fixed layout. This removes all widgets from the layout.
function fixed:reset()
for k, v in pairs(self.widgets) do
v:disconnect_signal("widget::updated", self._emit_updated)
end
self.widgets = {}
self:emit_signal("widget::updated")
self:emit_signal("widget::layout_changed")
end
--- Set the layout's fill_space property. If this property is true, the last
@ -113,7 +109,7 @@ end
-- 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::updated")
self:emit_signal("widget::layout_changed")
end
local function get_layout(dir)
@ -127,9 +123,6 @@ local function get_layout(dir)
ret.dir = dir
ret.widgets = {}
ret._emit_updated = function()
ret:emit_signal("widget::updated")
end
return ret
end
@ -152,7 +145,7 @@ end
-- @param spacing Spacing between widgets.
function fixed:set_spacing(spacing)
self._spacing = spacing
self:emit_signal("widget::updated")
self:emit_signal("widget::layout_changed")
end
return fixed

View File

@ -5,6 +5,7 @@
local fixed = require("wibox.layout.fixed")
local utils = require("wibox.test_utils")
local p = require("wibox.widget.base").place_widget_at
describe("wibox.layout.fixed", function()
local layout
@ -12,16 +13,12 @@ describe("wibox.layout.fixed", function()
layout = fixed.vertical()
end)
before_each(utils.stub_draw_widget)
after_each(utils.revert_draw_widget)
it("empty layout fit", function()
assert.widget_fit(layout, { 10, 10 }, { 0, 0 })
end)
it("empty layout draw", function()
layout:draw(nil, nil, 0, 0)
utils.check_widgets_drawn({})
it("empty layout layout", function()
assert.widget_layout(layout, { 0, 0 }, {})
end)
describe("with widgets", function()
@ -42,12 +39,11 @@ describe("wibox.layout.fixed", function()
assert.widget_fit(layout, { 100, 100 }, { 15, 35 })
end)
it("draw", function()
layout:draw("wibox", "cr", 100, 100)
utils.check_widgets_drawn({
{ first, 0, 0, 100, 10 },
{ second, 0, 10, 100, 15 },
{ third, 0, 25, 100, 10 },
it("layout", function()
assert.widget_layout(layout, { 100, 100 }, {
p(first, 0, 0, 100, 10),
p(second, 0, 10, 100, 15),
p(third, 0, 25, 100, 10),
})
end)
end)
@ -57,28 +53,25 @@ describe("wibox.layout.fixed", function()
assert.widget_fit(layout, { 5, 100 }, { 5, 35 })
end)
it("draw", function()
layout:draw("wibox", "cr", 5, 100)
utils.check_widgets_drawn({
{ first, 0, 0, 5, 10 },
{ second, 0, 10, 5, 15 },
{ third, 0, 25, 5, 10 },
it("layout", function()
assert.widget_layout(layout, { 5, 100 }, {
p(first, 0, 0, 5, 10),
p(second, 0, 10, 5, 15),
p(third, 0, 25, 5, 10),
})
end)
end)
describe("without enough width", function()
it("fit", function()
-- XXX: Is this really what should happen?
assert.widget_fit(layout, { 100, 20 }, { 15, 20 })
end)
it("draw", function()
layout:draw("wibox", "cr", 100, 20)
utils.check_widgets_drawn({
{ first, 0, 0, 100, 10 },
{ second, 0, 10, 100, 10 },
{ third, 0, 20, 100, 0 },
it("layout", function()
assert.widget_layout(layout, { 100, 20 }, {
p(first, 0, 0, 100, 10),
p(second, 0, 10, 100, 10),
p(third, 0, 20, 100, 0),
})
end)
end)