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")
12 local floor = math
.floor
17 local function round(x
)
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
)
35 space_per_item
= height
/ num
37 space_per_item
= width
/ num
40 for k
, v
in pairs(widgets
) do
44 w
, h
= width
, floor(space_per_item
)
47 w
, h
= floor(space_per_item
), height
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
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()
67 local function reset(layout
)
68 for k
, v
in pairs(layout
.widgets
) do
69 v
:disconnect_signal("widget::updated", layout
._emit_updated
)
72 layout
:emit_signal("widget::updated")
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
)
80 local function fit(layout
, width
, height
)
81 return fixed
.fit_fixed(dir
, layout
.widgets
, width
, height
)
84 local ret
= widget_base
.make_widget()
90 ret
.get_dir
= function () return dir
end
91 ret
._emit_updated
= function()
92 ret
:emit_signal("widget::updated")
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")
112 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80