Debian Codegraph @

obvious/lib/widget/init.lua

raw symbols SCIP
1 -----------------------------------
2 -- Author: Uli Schlachter --
3 -- Copyright 2009 Uli Schlachter --
4 -----------------------------------
5
6 local widgets_by_type = {
7 graph = require 'obvious.lib.widget.graph',
8 progressbar = require 'obvious.lib.widget.progressbar',
9 textbox = require 'obvious.lib.widget.textbox',
10 }
11
12 local setmetatable = setmetatable
13 local getmetatable = getmetatable
14 local pairs = pairs
15 local type = type
16 local lib = {
17 hooks = require("obvious.lib.hooks")
18 }
19
20 local defaults = { }
21
22 -- The functions each object from from_data_source will get
23 local funcs = { }
24
25 funcs.set_type = function (obj, widget_type)
26 local widget_type = widgets_by_type[widget_type]
27 if not widget_type or not widget_type.create then
28 return
29 end
30
31 local meta = getmetatable(obj)
32
33 local widget = widget_type.create(meta.data)
34 obj[1] = widget
35 obj.update()
36 return obj
37 end
38
39 local function from_data_source(data)
40 local ret = { }
41
42 for k, v in pairs(funcs) do
43 ret[k] = v
44 end
45
46 -- We default to graph since progressbars can't handle sources without an
47 -- upper bound on their value
48 ret[1] = widgets_by_type.graph.create(data)
49
50 ret.update = function()
51 -- because this uses ret, if ret[1] is changed this automatically
52 -- picks up the new widget
53 ret[1]:update()
54 end
55
56 -- Fire up the timer which keeps this widget up-to-date
57 lib.hooks.timer.register(10, 60, ret.update)
58 lib.hooks.timer.start(ret.update)
59 ret.update()
60
61 local meta = { }
62
63 meta.data = data
64
65 -- This is called when an unexesting key is accessed
66 meta.__index = function (obj, key)
67 local ret = obj[1][key]
68 if key ~= "layout" and type(ret) == "function" then
69 -- Ugly hack: this function wants to be called on the right object
70 return function(_, ...)
71 -- Ugly hack: this function wants to be called on the right object
72 return ret(obj[1], ...)
73 end
74 end
75 return ret
76 end
77
78 setmetatable(ret, meta)
79 return ret
80 end
81
82 return {
83 from_data_source = from_data_source,
84 }
85
86 -- vim:ft=lua:ts=2:sw=2:sts=2:tw=80:et