Ported wibox.widget to lua 5.2
[awesome.git] / lib / wibox / widget / systray.lua.in
blob9cc0ac28843d2a39d10e63775d46691992107f64
1 ---------------------------------------------------------------------------
2 -- @author Uli Schlachter
3 -- @copyright 2010 Uli Schlachter
4 -- @release @AWESOME_VERSION@
5 ---------------------------------------------------------------------------
7 local wbase = require("wibox.widget.base")
8 local lbase = require("wibox.layout.base")
9 local beautiful = require("beautiful")
10 local capi = { awesome = awesome }
11 local setmetatable = setmetatable
12 local error = error
14 -- wibox.widget.systray
15 local systray = { mt = {} }
17 local created_systray = false
18 local horizontal = true
19 local base_size = nil
21 function systray.draw(box, wibox, cr, width, height)
22 local x, y, width, height = lbase.rect_to_device_geometry(cr, 0, 0, width, height)
23 local num_entries = capi.awesome.systray()
24 local bg = beautiful.bg_systray or beautiful.bg_normal
26 local in_dir, ortho, base
27 if horizontal then
28 in_dir, ortho = width, height
29 else
30 ortho, in_dir = width, height
31 end
32 if ortho * num_entries <= in_dir then
33 base = ortho
34 else
35 base = in_dir / num_entries
36 end
37 capi.awesome.systray(wibox.drawin, x, y, base, horizontal, bg)
38 end
40 function systray.fit(box, width, height)
41 local num_entries = capi.awesome.systray()
42 local base = base_size
43 if base == nil then
44 if width < height then
45 base = width
46 else
47 base = height
48 end
49 end
50 if horizontal then
51 return base * num_entries, base
52 end
53 return base, base * num_entries
54 end
56 local function new()
57 local ret = wbase.make_widget()
59 if created_systray then
60 error("More than one systray created!")
61 end
62 created_systray = true
64 ret.fit = systray.fit
65 ret.draw = systray.draw
66 ret.set_base_size = function(_, size) base_size = size end
67 ret.set_horizontal = function(_, horiz) horizontal = horiz end
69 capi.awesome.connect_signal("systray::update", function()
70 ret:emit_signal("widget::updated")
71 end)
73 return ret
74 end
76 function systray.mt:__call(...)
77 return new(...)
78 end
80 return setmetatable(systray, systray.mt)
82 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80