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 = {} 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 context The context in which we are drawn.
-- @param cr The cairo context to use.
-- @param width The available width. -- @param width The available width.
-- @param height The available height. -- @param height The available height.
-- @return The total space needed by the layout. function fixed:layout(context, width, height)
function fixed:draw(context, cr, width, height) local result = {}
local pos,spacing = 0,self._spacing or 0 local pos,spacing = 0,self._spacing or 0
for k, v in pairs(self.widgets) do 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 (self.dir ~= "y" and pos-spacing > width) then
break break
end 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 end
return result
end end
--- Add a widget to the given fixed layout --- Add a widget to the given fixed layout
function fixed:add(widget) function fixed:add(widget)
widget_base.check_widget(widget) widget_base.check_widget(widget)
table.insert(self.widgets, widget) table.insert(self.widgets, widget)
widget:weak_connect_signal("widget::updated", self._emit_updated) self:emit_signal("widget::layout_changed")
self._emit_updated()
end end
--- Fit the fixed layout into the given space --- Fit the fixed layout into the given space
@ -101,11 +100,8 @@ end
--- Reset a fixed layout. This removes all widgets from the layout. --- Reset a fixed layout. This removes all widgets from the layout.
function fixed:reset() function fixed:reset()
for k, v in pairs(self.widgets) do
v:disconnect_signal("widget::updated", self._emit_updated)
end
self.widgets = {} self.widgets = {}
self:emit_signal("widget::updated") self:emit_signal("widget::layout_changed")
end end
--- Set the layout's fill_space property. If this property is true, the last --- 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. -- won't be handled specially and there can be space left unused.
function fixed:fill_space(val) function fixed:fill_space(val)
self._fill_space = val self._fill_space = val
self:emit_signal("widget::updated") self:emit_signal("widget::layout_changed")
end end
local function get_layout(dir) local function get_layout(dir)
@ -127,9 +123,6 @@ local function get_layout(dir)
ret.dir = dir ret.dir = dir
ret.widgets = {} ret.widgets = {}
ret._emit_updated = function()
ret:emit_signal("widget::updated")
end
return ret return ret
end end
@ -152,7 +145,7 @@ end
-- @param spacing Spacing between widgets. -- @param spacing Spacing between widgets.
function fixed:set_spacing(spacing) function fixed:set_spacing(spacing)
self._spacing = spacing self._spacing = spacing
self:emit_signal("widget::updated") self:emit_signal("widget::layout_changed")
end end
return fixed return fixed

View File

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