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

View File

@ -13,25 +13,21 @@ local pairs = pairs
local fixed = {}
--- 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 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_fixed(dir, widgets, fill_space, wibox, cr, width, height)
function fixed:draw(wibox, cr, width, height)
local pos = 0
for k, v in pairs(widgets) do
for k, v in pairs(self.widgets) do
local x, y, w, h, _
local in_dir
if dir == "y" then
if self.dir == "y" then
x, y = 0, 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);
end
pos = pos + h
@ -39,15 +35,15 @@ function fixed.draw_fixed(dir, widgets, fill_space, wibox, cr, width, height)
else
x, y = pos, 0
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);
end
pos = pos + w
in_dir = w
end
if (dir == "y" and pos > height) or
(dir ~= "y" and pos > width) then
if (self.dir == "y" and pos > height) or
(self.dir ~= "y" and pos > width) then
break
end
base.draw_widget(wibox, cr, v, x, y, w, h)
@ -63,18 +59,16 @@ function fixed:add(widget)
end
--- 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_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 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 in_dir, max
if dir == "y" then
if self.dir == "y" then
max, in_dir = w, h
height = height - in_dir
else
@ -87,7 +81,7 @@ function fixed.fit_fixed(dir, widgets, orig_width, orig_height)
used_in_dir = used_in_dir + in_dir
if width <= 0 or height <= 0 then
if dir == "y" then
if self.dir == "y" then
used_in_dir = orig_height
else
used_in_dir = orig_width
@ -96,7 +90,7 @@ function fixed.fit_fixed(dir, widgets, orig_width, orig_height)
end
end
if dir == "y" then
if self.dir == "y" then
return used_max, used_in_dir
end
return used_in_dir, used_max
@ -120,21 +114,16 @@ function fixed:fill_space(val)
end
local function get_layout(dir)
local function draw(layout, ...)
fixed.draw_fixed(dir, layout.widgets, layout._fill_space, ...)
local ret = widget_base.make_widget()
for k, v in pairs(fixed) do
if type(v) == "function" then
ret[k] = v
end
local function fit(layout, ...)
return fixed.fit_fixed(dir, layout.widgets, ...)
end
local ret = widget_base.make_widget()
ret.draw = draw
ret.fit = fit
ret.add = fixed.add
ret.reset = fixed.reset
ret.fill_space = fixed.fill_space
ret.dir = dir
ret.widgets = {}
ret.get_dir = function () return dir end
ret._emit_updated = function()
ret:emit_signal("widget::updated")
end

View File

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