wibox.layout: remove unnecessary wrapping of draw and fit functions

Signed-off-by: Lukáš Hrázký <lukkash@email.cz>
Signed-off-by: Uli Schlachter <psychon@znc.in>
This commit is contained in:
Lukáš Hrázký 2013-01-05 16:12:47 +01:00 committed by Uli Schlachter
parent 987c2b9b30
commit 9d333113dd
3 changed files with 68 additions and 85 deletions

View File

@ -14,51 +14,55 @@ local widget_base = require("wibox.widget.base")
-- wibox.layout.align -- wibox.layout.align
local align = {} local align = {}
-- Draw the given align layout. dir describes the orientation of the layout, "x" --- Draw an align layout.
-- means horizontal while "y" is vertical. -- @param wibox The wibox that this widget is drawn to.
local function draw(dir, layout, wibox, cr, width, height) -- @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 align:draw(wibox, cr, width, height)
local size_first = 0 local size_first = 0
local size_third = 0 local size_third = 0
local size_limit = dir == "y" and height or width local size_limit = self.dir == "y" and height or width
if layout.first then if self.first then
local w, h, _ = width, height, nil local w, h, _ = width, height, nil
if dir == "y" then if self.dir == "y" then
_, h = layout.first:fit(w, h) _, h = self.first:fit(w, h)
size_first = h size_first = h
else else
w, _ = layout.first:fit(w, h) w, _ = self.first:fit(w, h)
size_first = w size_first = w
end end
base.draw_widget(wibox, cr, layout.first, 0, 0, w, h) base.draw_widget(wibox, cr, self.first, 0, 0, w, h)
end end
if layout.third and size_first < size_limit then if self.third and size_first < size_limit then
local w, h, x, y, _ local w, h, x, y, _
if dir == "y" then if self.dir == "y" then
w, h = width, height - size_first w, h = width, height - size_first
_, h = layout.third:fit(w, h) _, h = self.third:fit(w, h)
x, y = 0, height - h x, y = 0, height - h
size_third = h size_third = h
else else
w, h = width - size_first, height w, h = width - size_first, height
w, _ = layout.third:fit(w, h) w, _ = self.third:fit(w, h)
x, y = width - w, 0 x, y = width - w, 0
size_third = w size_third = w
end end
base.draw_widget(wibox, cr, layout.third, x, y, w, h) base.draw_widget(wibox, cr, self.third, x, y, w, h)
end end
if layout.second and size_first + size_third < size_limit then if self.second and size_first + size_third < size_limit then
local x, y, w, h local x, y, w, h
if dir == "y" then if self.dir == "y" then
x, y = 0, size_first x, y = 0, size_first
w, h = width, size_limit - size_first - size_third w, h = width, size_limit - size_first - size_third
else else
x, y = size_first, 0 x, y = size_first, 0
w, h = size_limit - size_first - size_third, height w, h = size_limit - size_first - size_third, height
end end
base.draw_widget(wibox, cr, layout.second, x, y, w, h) base.draw_widget(wibox, cr, self.second, x, y, w, h)
end end
end end
@ -99,14 +103,9 @@ function align:reset()
end end
local function get_layout(dir) local function get_layout(dir)
local function draw_dir(layout, wibox, cr, width, height)
draw(dir, layout, wibox, cr, width, height)
end
local ret = widget_base.make_widget() local ret = widget_base.make_widget()
ret.draw = draw_dir
ret.fit = function(box, ...) return ... end ret.fit = function(box, ...) return ... end
ret.get_dir = function () return dir end ret.dir = dir
ret._emit_updated = function() ret._emit_updated = function()
ret:emit_signal("widget::updated") ret:emit_signal("widget::updated")
end end

View File

@ -13,25 +13,21 @@ local pairs = pairs
local fixed = {} local fixed = {}
--- Draw a fixed layout. Each widget gets just the space it asks for. --- Draw a fixed layout. Each widget gets just the space it asks for.
-- @param dir "x" for a horizontal layout and "y" for vertical.
-- @param widgets The widgets to draw.
-- @param fill_space Use all the available space, giving all that is left to the
-- last widget
-- @param wibox The wibox that this widget is drawn to. -- @param wibox The wibox that this widget is drawn to.
-- @param cr The cairo context to use. -- @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. -- @return The total space needed by the layout.
function fixed.draw_fixed(dir, widgets, fill_space, wibox, cr, width, height) function fixed:draw(wibox, cr, width, height)
local pos = 0 local pos = 0
for k, v in pairs(widgets) do for k, v in pairs(self.widgets) do
local x, y, w, h, _ local x, y, w, h, _
local in_dir local in_dir
if dir == "y" then if self.dir == "y" then
x, y = 0, pos x, y = 0, pos
w, h = width, height - pos w, h = width, height - pos
if k ~= #widgets or not fill_space then if k ~= #self.widgets or not self.fill_space then
_, h = v:fit(w, h); _, h = v:fit(w, h);
end end
pos = pos + h pos = pos + h
@ -39,15 +35,15 @@ function fixed.draw_fixed(dir, widgets, fill_space, wibox, cr, width, height)
else else
x, y = pos, 0 x, y = pos, 0
w, h = width - pos, height w, h = width - pos, height
if k ~= #widgets or not fill_space then if k ~= #self.widgets or not self.fill_space then
w, _ = v:fit(w, h); w, _ = v:fit(w, h);
end end
pos = pos + w pos = pos + w
in_dir = w in_dir = w
end end
if (dir == "y" and pos > height) or if (self.dir == "y" and pos > height) or
(dir ~= "y" and pos > width) then (self.dir ~= "y" and pos > width) then
break break
end end
base.draw_widget(wibox, cr, v, x, y, w, h) base.draw_widget(wibox, cr, v, x, y, w, h)
@ -63,18 +59,16 @@ function fixed:add(widget)
end end
--- Fit the fixed layout into the given space --- Fit the fixed layout into the given space
-- @param dir "x" for a horizontal layout and "y" for vertical.
-- @param widgets The widgets to fit.
-- @param orig_width The available width. -- @param orig_width The available width.
-- @param orig_height The available height. -- @param orig_height The available height.
function fixed.fit_fixed(dir, widgets, orig_width, orig_height) function fixed:fit(orig_width, orig_height)
local width, height = orig_width, orig_height local width, height = orig_width, orig_height
local used_in_dir, used_max = 0, 0 local used_in_dir, used_max = 0, 0
for k, v in pairs(widgets) do for k, v in pairs(self.widgets) do
local w, h = v:fit(width, height) local w, h = v:fit(width, height)
local in_dir, max local in_dir, max
if dir == "y" then if self.dir == "y" then
max, in_dir = w, h max, in_dir = w, h
height = height - in_dir height = height - in_dir
else else
@ -87,7 +81,7 @@ function fixed.fit_fixed(dir, widgets, orig_width, orig_height)
used_in_dir = used_in_dir + in_dir used_in_dir = used_in_dir + in_dir
if width <= 0 or height <= 0 then if width <= 0 or height <= 0 then
if dir == "y" then if self.dir == "y" then
used_in_dir = orig_height used_in_dir = orig_height
else else
used_in_dir = orig_width used_in_dir = orig_width
@ -96,7 +90,7 @@ function fixed.fit_fixed(dir, widgets, orig_width, orig_height)
end end
end end
if dir == "y" then if self.dir == "y" then
return used_max, used_in_dir return used_max, used_in_dir
end end
return used_in_dir, used_max return used_in_dir, used_max
@ -120,21 +114,16 @@ function fixed:fill_space(val)
end end
local function get_layout(dir) local function get_layout(dir)
local function draw(layout, ...) local ret = widget_base.make_widget()
fixed.draw_fixed(dir, layout.widgets, layout._fill_space, ...)
end for k, v in pairs(fixed) do
local function fit(layout, ...) if type(v) == "function" then
return fixed.fit_fixed(dir, layout.widgets, ...) ret[k] = v
end
end end
local ret = widget_base.make_widget() ret.dir = dir
ret.draw = draw
ret.fit = fit
ret.add = fixed.add
ret.reset = fixed.reset
ret.fill_space = fixed.fill_space
ret.widgets = {} ret.widgets = {}
ret.get_dir = function () return dir end
ret._emit_updated = function() ret._emit_updated = function()
ret:emit_signal("widget::updated") ret:emit_signal("widget::updated")
end end

View File

@ -18,28 +18,28 @@ local function round(x)
return floor(x + 0.5) return floor(x + 0.5)
end end
--- Draw a flex layout. Each widget gets an equal share of the available space flex.fit = fixed.fit
-- @param dir "x" for a horizontal layout and "y" for vertical.
-- @param widgets The widgets to draw. --- Draw a flex layout. Each widget gets an equal share of the available space.
-- @param wibox The wibox that this widget is drawn to. -- @param wibox The wibox that this widget is drawn to.
-- @param cr The cairo context to use. -- @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. -- @return The total space needed by the layout.
function flex.draw_flex(dir, widgets, wibox, cr, width, height) function flex:draw(wibox, cr, width, height)
local pos = 0 local pos = 0
local num = #widgets local num = #self.widgets
local space_per_item local space_per_item
if dir == "y" then if self.dir == "y" then
space_per_item = height / num space_per_item = height / num
else else
space_per_item = width / num space_per_item = width / num
end end
for k, v in pairs(widgets) do for k, v in pairs(self.widgets) do
local x, y, w, h local x, y, w, h
if dir == "y" then if self.dir == "y" then
x, y = 0, round(pos) x, y = 0, round(pos)
w, h = width, floor(space_per_item) w, h = width, floor(space_per_item)
else else
@ -50,44 +50,39 @@ function flex.draw_flex(dir, widgets, wibox, cr, width, height)
pos = pos + space_per_item pos = pos + space_per_item
if (dir == "y" and pos >= height) or if (self.dir == "y" and pos >= height) or
(dir ~= "y" and pos >= width) then (self.dir ~= "y" and pos >= width) then
break break
end end
end end
end end
local function add(layout, widget) function flex:add(widget)
widget_base.check_widget(widget) widget_base.check_widget(widget)
table.insert(layout.widgets, widget) table.insert(self.widgets, widget)
widget:connect_signal("widget::updated", layout._emit_updated) widget:connect_signal("widget::updated", self._emit_updated)
layout._emit_updated() self._emit_updated()
end end
local function reset(layout) function flex:reset()
for k, v in pairs(layout.widgets) do for k, v in pairs(self.widgets) do
v:disconnect_signal("widget::updated", layout._emit_updated) v:disconnect_signal("widget::updated", self._emit_updated)
end end
layout.widgets = {} self.widgets = {}
layout:emit_signal("widget::updated") self:emit_signal("widget::updated")
end end
local function get_layout(dir) local function get_layout(dir)
local function draw(layout, wibox, cr, width, height)
flex.draw_flex(dir, layout.widgets, wibox, cr, width, height)
end
local function fit(layout, width, height)
return fixed.fit_fixed(dir, layout.widgets, width, height)
end
local ret = widget_base.make_widget() local ret = widget_base.make_widget()
ret.draw = draw
ret.fit = fit for k, v in pairs(flex) do
ret.add = add if type(v) == "function" then
ret.reset = reset ret[k] = v
end
end
ret.dir = dir
ret.widgets = {} ret.widgets = {}
ret.get_dir = function () return dir end
ret._emit_updated = function() ret._emit_updated = function()
ret:emit_signal("widget::updated") ret:emit_signal("widget::updated")
end end