2010-10-06 12:42:56 +02:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
-- @author Uli Schlachter
|
|
|
|
-- @copyright 2010 Uli Schlachter
|
|
|
|
-- @release @AWESOME_VERSION@
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
local base = require("wibox.layout.base")
|
|
|
|
local fixed = require("wibox.layout.fixed")
|
|
|
|
local widget_base = require("wibox.widget.base")
|
|
|
|
local table = table
|
|
|
|
local pairs = pairs
|
|
|
|
local floor = math.floor
|
|
|
|
|
2012-06-12 15:29:52 +02:00
|
|
|
-- wibox.layout.flex
|
|
|
|
local flex = {}
|
2010-10-06 12:42:56 +02:00
|
|
|
|
|
|
|
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.
|
2012-11-19 14:09:10 +01:00
|
|
|
-- @param wibox The wibox that this widget is drawn to.
|
2010-10-06 12:42:56 +02:00
|
|
|
-- @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.
|
2012-06-12 15:29:52 +02:00
|
|
|
function flex.draw_flex(dir, widgets, wibox, cr, width, height)
|
2010-10-06 12:42:56 +02:00
|
|
|
local pos = 0
|
|
|
|
|
|
|
|
local num = #widgets
|
|
|
|
local space_per_item
|
|
|
|
if dir == "y" then
|
|
|
|
space_per_item = height / num
|
|
|
|
else
|
|
|
|
space_per_item = width / num
|
|
|
|
end
|
|
|
|
|
|
|
|
for k, v in pairs(widgets) do
|
|
|
|
local x, y, w, h
|
|
|
|
if dir == "y" then
|
|
|
|
x, y = 0, round(pos)
|
|
|
|
w, h = width, floor(space_per_item)
|
|
|
|
else
|
|
|
|
x, y = round(pos), 0
|
|
|
|
w, h = floor(space_per_item), height
|
|
|
|
end
|
|
|
|
base.draw_widget(wibox, cr, v, x, y, w, h)
|
|
|
|
|
|
|
|
pos = pos + space_per_item
|
|
|
|
|
|
|
|
if (dir == "y" and pos >= height) or
|
|
|
|
(dir ~= "y" and pos >= width) then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function add(layout, widget)
|
|
|
|
widget_base.check_widget(widget)
|
|
|
|
table.insert(layout.widgets, widget)
|
|
|
|
widget:connect_signal("widget::updated", layout._emit_updated)
|
|
|
|
layout._emit_updated()
|
|
|
|
end
|
|
|
|
|
|
|
|
local function reset(layout)
|
|
|
|
for k, v in pairs(layout.widgets) do
|
|
|
|
v:disconnect_signal("widget::updated", layout._emit_updated)
|
|
|
|
end
|
|
|
|
layout.widgets = {}
|
|
|
|
layout:emit_signal("widget::updated")
|
|
|
|
end
|
|
|
|
|
|
|
|
local function get_layout(dir)
|
|
|
|
local function draw(layout, wibox, cr, width, height)
|
2012-06-12 15:29:52 +02:00
|
|
|
flex.draw_flex(dir, layout.widgets, wibox, cr, width, height)
|
2010-10-06 12:42:56 +02:00
|
|
|
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
|
|
|
|
ret.widgets = {}
|
2011-03-30 04:05:59 +02:00
|
|
|
ret.get_dir = function () return dir end
|
2010-10-06 12:42:56 +02:00
|
|
|
ret._emit_updated = function()
|
|
|
|
ret:emit_signal("widget::updated")
|
|
|
|
end
|
|
|
|
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Returns a new horizontal flex layout. A flex layout shares the available space
|
|
|
|
-- equally among all widgets. Widgets can be added via :add(widget).
|
2012-06-12 15:29:52 +02:00
|
|
|
function flex.horizontal()
|
2010-10-06 12:42:56 +02:00
|
|
|
return get_layout("x")
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Returns a new vertical flex layout. A flex layout shares the available space
|
|
|
|
-- equally among all widgets. Widgets can be added via :add(widget).
|
2012-06-12 15:29:52 +02:00
|
|
|
function flex.vertical()
|
2010-10-06 12:42:56 +02:00
|
|
|
return get_layout("y")
|
|
|
|
end
|
|
|
|
|
2012-06-12 15:29:52 +02:00
|
|
|
return flex
|
|
|
|
|
2011-09-11 16:50:01 +02:00
|
|
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|