From 496fbde9b4f6288497af6d20c47786ab855642df Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Sun, 14 Jun 2015 16:17:05 +0200 Subject: [PATCH] Convert the flex layout to the new widget system Signed-off-by: Uli Schlachter --- lib/wibox/layout/flex.lua | 64 ++++++++++++++++----------------- spec/wibox/layout/flex_spec.lua | 42 ++++++++++------------ 2 files changed, 49 insertions(+), 57 deletions(-) diff --git a/lib/wibox/layout/flex.lua b/lib/wibox/layout/flex.lua index 021867cd1..1bc13934f 100644 --- a/lib/wibox/layout/flex.lua +++ b/lib/wibox/layout/flex.lua @@ -14,13 +14,16 @@ local round = require("awful.util").round 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 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 flex:draw(context, cr, width, height) +function flex:layout(context, width, height) + local result = {} local pos,spacing = 0,self._spacing or 0 local num = #self.widgets local total_spacing = (spacing*(num-1)) @@ -45,7 +48,7 @@ function flex:draw(context, cr, width, height) x, y = round(pos), 0 w, h = floor(space_per_item), height 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 @@ -54,21 +57,8 @@ function flex:draw(context, cr, width, height) break end end -end -function flex:add(widget) - 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") + return result end --- 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 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() - for k, v in pairs(self.widgets) do - v:disconnect_signal("widget::updated", self._emit_updated) - end self.widgets = {} self._max_widget_size = nil - self:emit_signal("widget::updated") + self:emit_signal("widget::layout_changed") end local function get_layout(dir) @@ -127,9 +135,6 @@ local function get_layout(dir) ret.dir = dir ret.widgets = {} - ret._emit_updated = function() - ret:emit_signal("widget::updated") - end return ret end @@ -146,13 +151,6 @@ function flex.vertical() return get_layout("y") 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 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80 diff --git a/spec/wibox/layout/flex_spec.lua b/spec/wibox/layout/flex_spec.lua index a324861c5..9f0e698ab 100644 --- a/spec/wibox/layout/flex_spec.lua +++ b/spec/wibox/layout/flex_spec.lua @@ -5,6 +5,7 @@ local flex = require("wibox.layout.flex") local utils = require("wibox.test_utils") +local p = require("wibox.widget.base").place_widget_at describe("wibox.layout.flex", function() local layout @@ -12,16 +13,12 @@ describe("wibox.layout.flex", function() layout = flex.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 }) - utils.check_widgets_drawn({}) end) - it("empty layout draw", function() - layout:draw(nil, nil, 0, 0) + it("empty layout layout", function() + assert.widget_layout(layout, { 0, 0 }, {}) end) describe("with widgets", function() @@ -42,12 +39,11 @@ describe("wibox.layout.flex", 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, 33 }, - { second, 0, 33, 100, 33 }, - { third, 0, 67, 100, 33 }, + it("layout", function() + assert.widget_layout(layout, { 100, 100 }, { + p(first, 0, 0, 100, 33), + p(second, 0, 33, 100, 33), + p(third, 0, 67, 100, 33), }) end) end) @@ -57,12 +53,11 @@ describe("wibox.layout.flex", 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, 33 }, - { second, 0, 33, 5, 33 }, - { third, 0, 67, 5, 33 }, + it("layout", function() + assert.widget_layout(layout, { 5, 100 }, { + p(first, 0, 0, 5, 33), + p(second, 0, 33, 5, 33), + p(third, 0, 67, 5, 33), }) end) end) @@ -72,12 +67,11 @@ describe("wibox.layout.flex", function() 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, 6 }, - { second, 0, 7, 100, 6 }, - { third, 0, 13, 100, 6 }, + it("layout", function() + assert.widget_layout(layout, { 100, 20 }, { + p(first, 0, 0, 100, 6), + p(second, 0, 7, 100, 6), + p(third, 0, 13, 100, 6), }) end) end)