-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA256 Uli Schlachter wrote: > Attached is a New And Improved(tm) version of the last patch. Compare it > yourself, you'll see that the new version is better.
Gregor breaks it - Uli fixes it Uli - -- "Do you know that books smell like nutmeg or some spice from a foreign land?" -- Faber in Fahrenheit 451 -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (GNU/Linux) iQEcBAEBCAAGBQJKgCLRAAoJECLkKOvLj8sGPMgH/RU9Idenq910yG6D5x+NbZfM yt+o1KzZ0zNwHmWNe7qaZcHISr4OcElbeT8UcJhInN6g8L/XMYGGvylBKq4NVv4M dZwFd/UeZAeUSL5iLCbWCvvjmp/8bXUMXIw0cy8fn+9gL7gzzrE4EvoGMrgE0Wuj b2/pqnRSMXkl9daOTGkUOIH6aEhYcFfraUEGNKcI9tZw1q0BAxMVAoQRlK15z2T4 N/nEDNz6Xc7AY+F8YHD2T3RzHdbm3yHq9OjQRGrQjbjxv4OmeMsCUpbHfhZAdYH/ rVn8sTVMsSNuMRLeg/8R4K01HHUobuFzaoCzx6rjhiiJn+V5SXG9D/pi7vwcY6w= =exFF -----END PGP SIGNATURE-----
>From 4f5c934d6c7f03c698ee2ff2d20e58da10f90b9d Mon Sep 17 00:00:00 2001 From: Uli Schlachter <psyc...@znc.in> Date: Sat, 8 Aug 2009 16:12:49 +0200 Subject: [PATCH 03/11] Add general widgets based on data source Obvious modules can now call obvious.lib.widget.from_data_source() with a table. This table has to contain a function named "get" which returns a data point and optionally an entry named "max" which is the maximum possible return value for "get". The user can use the from_data_source-widgets like normal widgets. By default they behave like a graph widget (same functions available), but it can be changed into a progressbar with :set_type("progressbar"). New widget types can be added by creating a new table under obvious.lib.widget which has to have a create() member function. This is a safety-measure against accidentally using sth which wasn't meant to be used this way. Signed-off-by: Uli Schlachter <psyc...@znc.in> --- lib/widget/graph.lua | 18 +++++++++ lib/widget/init.lua | 84 ++++++++++++++++++++++++++++++++++++++++++++ lib/widget/progressbar.lua | 15 ++++++++ 3 files changed, 117 insertions(+), 0 deletions(-) diff --git a/lib/widget/graph.lua b/lib/widget/graph.lua index 9cf3e91..497b24e 100644 --- a/lib/widget/graph.lua +++ b/lib/widget/graph.lua @@ -29,6 +29,24 @@ function graph(layout, scale) return widget end +function create(data, layout) + local scale = true + if data.max then + scale = false + end + + local widget = graph(layout, scale) + + widget.update = function(widget) + local max = widget.data.max or 1 + local val = widget.data:get() or max + widget:add_value(val / max) + end + + widget.data = data + + return widget +end setmetatable(_M, { __call = function (_, ...) return graph(...) end }) -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80 diff --git a/lib/widget/init.lua b/lib/widget/init.lua index cd62a5c..20df4f4 100644 --- a/lib/widget/init.lua +++ b/lib/widget/init.lua @@ -6,6 +6,90 @@ require("obvious.lib.widget.graph") require("obvious.lib.widget.progressbar") +local setmetatable = setmetatable +local getmetatable = getmetatable +local pairs = pairs +local type = type +local lib = { + hooks = require("obvious.lib.hooks") +} +local layout = { + horizontal = require("awful.widget.layout.horizontal") +} + module("obvious.lib.widget") +-- The functions each object from from_data_source will get +local funcs = { } + +funcs.set_type = function (obj, widget_type) + local widget_type = _M[widget_type] + if not widget_type or not widget_type.create then + return + end + + local meta = getmetatable(obj) + + local widget = widget_type.create(meta.data, obj.widget.layout) + obj.widget = widget + obj.update() + + return obj +end + +funcs.set_layout = function (obj, layout) + obj.widget.layout = layout + obj.layout = layout + return obj +end + +function from_data_source(data) + local ret = { } + + for k, v in pairs(funcs) do + ret[k] = v + end + + -- We default to graph since progressbars can't handle sources without an + -- upper bound on their value + ret.widget = _M.graph.create(data) + ret.layout = nil + + ret.update = function() + -- because this uses ret, if ret.widget is changed this automatically + -- picks up the new widget + ret.widget:update() + end + + -- Fire up the timer which keeps this widget up-to-date + lib.hooks.timer.register(10, 60, ret.update) + lib.hooks.timer.start(ret.update) + ret.update() + + local meta = { } + + meta.data = data + + -- This is called when an unexesting key is accessed + meta.__index = function (obj, key) + local ret = obj.widget[key] + if key ~= "layout" and type(ret) == "function" then + return function(_, ...) + -- Ugly hack: this function wants to be called on the right object + ret(obj.widget, ...) + -- Ugly hack 2: We force obj to be returned again and discard + -- the function's return value + return obj + end + end + return ret + end + + -- Default layout is leftright, users can override this if they want to + ret:set_layout(layout.horizontal.leftright) + + setmetatable(ret, meta) + return ret +end + -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80 diff --git a/lib/widget/progressbar.lua b/lib/widget/progressbar.lua index b67e25c..b41a1e2 100644 --- a/lib/widget/progressbar.lua +++ b/lib/widget/progressbar.lua @@ -29,5 +29,20 @@ function progressbar(layout) return widget end +function create(data, layout) + local widget = progressbar(layout) + + widget.update = function(widget) + -- TODO: We don't support data sources without a fixed upper bound + local max = widget.data.max or 1 + local val = widget.data:get() or max + widget:set_value(val / max) + end + + widget.data = data + + return widget +end + setmetatable(_M, { __call = function (_, ...) return progressbar(...) end }) -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80 -- 1.6.3.3