Convert the flex 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:17:05 +02:00
parent 199b553895
commit 496fbde9b4
2 changed files with 49 additions and 57 deletions

View File

@ -14,13 +14,16 @@ local round = require("awful.util").round
local flex = {} local flex = {}
--- Draw a flex layout. Each widget gets an equal share of the available space. local function round(x)
return floor(x + 0.5)
end
--- Layout a flex layout. Each widget gets an equal share of the available space.
-- @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 flex:layout(context, width, height)
function flex:draw(context, cr, width, height) local result = {}
local pos,spacing = 0,self._spacing or 0 local pos,spacing = 0,self._spacing or 0
local num = #self.widgets local num = #self.widgets
local total_spacing = (spacing*(num-1)) local total_spacing = (spacing*(num-1))
@ -45,7 +48,7 @@ function flex:draw(context, cr, width, height)
x, y = round(pos), 0 x, y = round(pos), 0
w, h = floor(space_per_item), height w, h = floor(space_per_item), height
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))
pos = pos + space_per_item + spacing pos = pos + space_per_item + spacing
@ -54,21 +57,8 @@ function flex:draw(context, cr, width, height)
break break
end end
end end
end
function flex:add(widget) return result
widget_base.check_widget(widget)
table.insert(self.widgets, widget)
widget:weak_connect_signal("widget::updated", self._emit_updated)
self._emit_updated()
end
--- Set the maximum size the widgets in this layout will take (that is,
-- maximum width for horizontal and maximum height for vertical).
-- @param val The maximum size of the widget.
function flex:set_max_widget_size(val)
self._max_widget_size = val
self:emit_signal("widget::updated")
end end
--- Fit the flex layout into the given space. --- Fit the flex layout into the given space.
@ -107,13 +97,31 @@ 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)
widget_base.check_widget(widget)
table.insert(self.widgets, widget)
self:emit_signal("widget::layout_changed")
end
--- Set the maximum size the widgets in this layout will take (that is,
-- maximum width for horizontal and maximum height for vertical).
-- @param val The maximum size of the widget.
function flex:set_max_widget_size(val)
self._max_widget_size = val
self:emit_signal("widget::layout_changed")
end
--- Add spacing between each layout widgets
-- @param spacing Spacing between widgets.
function flex:set_spacing(spacing)
self._spacing = spacing
self:emit_signal("widget::layout_changed")
end
function flex:reset() function flex:reset()
for k, v in pairs(self.widgets) do
v:disconnect_signal("widget::updated", self._emit_updated)
end
self.widgets = {} self.widgets = {}
self._max_widget_size = nil self._max_widget_size = nil
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 +135,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
@ -146,13 +151,6 @@ function flex.vertical()
return get_layout("y") return get_layout("y")
end end
--- Add spacing between each layout widgets
-- @param spacing Spacing between widgets.
function flex:set_spacing(spacing)
self._spacing = spacing
self:emit_signal("widget::updated")
end
return flex return flex
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80

View File

@ -5,6 +5,7 @@
local flex = require("wibox.layout.flex") local flex = require("wibox.layout.flex")
local utils = require("wibox.test_utils") local utils = require("wibox.test_utils")
local p = require("wibox.widget.base").place_widget_at
describe("wibox.layout.flex", function() describe("wibox.layout.flex", function()
local layout local layout
@ -12,16 +13,12 @@ describe("wibox.layout.flex", function()
layout = flex.vertical() layout = flex.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 })
utils.check_widgets_drawn({})
end) end)
it("empty layout draw", function() it("empty layout layout", function()
layout:draw(nil, nil, 0, 0) assert.widget_layout(layout, { 0, 0 }, {})
end) end)
describe("with widgets", function() describe("with widgets", function()
@ -42,12 +39,11 @@ describe("wibox.layout.flex", 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, 33),
{ first, 0, 0, 100, 33 }, p(second, 0, 33, 100, 33),
{ second, 0, 33, 100, 33 }, p(third, 0, 67, 100, 33),
{ third, 0, 67, 100, 33 },
}) })
end) end)
end) end)
@ -57,12 +53,11 @@ describe("wibox.layout.flex", 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, 33),
{ first, 0, 0, 5, 33 }, p(second, 0, 33, 5, 33),
{ second, 0, 33, 5, 33 }, p(third, 0, 67, 5, 33),
{ third, 0, 67, 5, 33 },
}) })
end) end)
end) end)
@ -72,12 +67,11 @@ describe("wibox.layout.flex", function()
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, 6),
{ first, 0, 0, 100, 6 }, p(second, 0, 7, 100, 6),
{ second, 0, 7, 100, 6 }, p(third, 0, 13, 100, 6),
{ third, 0, 13, 100, 6 },
}) })
end) end)
end) end)