Lots of random documentation fixes
[awesome.git] / lib / wibox / layout / flex.lua.in
blobe1dc9509e8af54ffbbbe02eca3196a1894c6987c
1 ---------------------------------------------------------------------------
2 -- @author Uli Schlachter
3 -- @copyright 2010 Uli Schlachter
4 -- @release @AWESOME_VERSION@
5 ---------------------------------------------------------------------------
7 local base = require("wibox.layout.base")
8 local fixed = require("wibox.layout.fixed")
9 local widget_base = require("wibox.widget.base")
10 local table = table
11 local pairs = pairs
12 local floor = math.floor
14 -- wibox.layout.flex
15 local flex = {}
17 local function round(x)
18 return floor(x + 0.5)
19 end
21 --- Draw a flex layout. Each widget gets an equal share of the available space
22 -- @param dir "x" for a horizontal layout and "y" for vertical.
23 -- @param widgets The widgets to draw.
24 -- @param wibox The wibox that this widget is drawn to.
25 -- @param cr The cairo context to use.
26 -- @param width The available width.
27 -- @param height The available height.
28 -- @return The total space needed by the layout.
29 function flex.draw_flex(dir, widgets, wibox, cr, width, height)
30 local pos = 0
32 local num = #widgets
33 local space_per_item
34 if dir == "y" then
35 space_per_item = height / num
36 else
37 space_per_item = width / num
38 end
40 for k, v in pairs(widgets) do
41 local x, y, w, h
42 if dir == "y" then
43 x, y = 0, round(pos)
44 w, h = width, floor(space_per_item)
45 else
46 x, y = round(pos), 0
47 w, h = floor(space_per_item), height
48 end
49 base.draw_widget(wibox, cr, v, x, y, w, h)
51 pos = pos + space_per_item
53 if (dir == "y" and pos >= height) or
54 (dir ~= "y" and pos >= width) then
55 break
56 end
57 end
58 end
60 local function add(layout, widget)
61 widget_base.check_widget(widget)
62 table.insert(layout.widgets, widget)
63 widget:connect_signal("widget::updated", layout._emit_updated)
64 layout._emit_updated()
65 end
67 local function reset(layout)
68 for k, v in pairs(layout.widgets) do
69 v:disconnect_signal("widget::updated", layout._emit_updated)
70 end
71 layout.widgets = {}
72 layout:emit_signal("widget::updated")
73 end
75 local function get_layout(dir)
76 local function draw(layout, wibox, cr, width, height)
77 flex.draw_flex(dir, layout.widgets, wibox, cr, width, height)
78 end
80 local function fit(layout, width, height)
81 return fixed.fit_fixed(dir, layout.widgets, width, height)
82 end
84 local ret = widget_base.make_widget()
85 ret.draw = draw
86 ret.fit = fit
87 ret.add = add
88 ret.reset = reset
89 ret.widgets = {}
90 ret.get_dir = function () return dir end
91 ret._emit_updated = function()
92 ret:emit_signal("widget::updated")
93 end
95 return ret
96 end
98 --- Returns a new horizontal flex layout. A flex layout shares the available space
99 -- equally among all widgets. Widgets can be added via :add(widget).
100 function flex.horizontal()
101 return get_layout("x")
104 --- Returns a new vertical flex layout. A flex layout shares the available space
105 -- equally among all widgets. Widgets can be added via :add(widget).
106 function flex.vertical()
107 return get_layout("y")
110 return flex
112 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80