| 1 | -- flaw, a Lua OO management framework for Awesome WM widgets. |
| 2 | -- Copyright (C) 2009,2010,2011 David Soulayrol <david.soulayrol AT gmail DOT net> |
| 3 | |
| 4 | -- This program is free software: you can redistribute it and/or modify |
| 5 | -- it under the terms of the GNU General Public License as published by |
| 6 | -- the Free Software Foundation, either version 3 of the License, or |
| 7 | -- (at your option) any later version. |
| 8 | |
| 9 | -- This program is distributed in the hope that it will be useful, |
| 10 | -- but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | -- GNU General Public License for more details. |
| 13 | |
| 14 | -- You should have received a copy of the GNU General Public License |
| 15 | -- along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | |
| 17 | |
| 18 | -- Grab environment. |
| 19 | local ipairs = ipairs |
| 20 | local pairs = pairs |
| 21 | local setmetatable = setmetatable |
| 22 | local string = string |
| 23 | local tonumber = tonumber |
| 24 | |
| 25 | local awful = { |
| 26 | widget = require('awful.widget'), |
| 27 | tooltip = require('awful.tooltip'), |
| 28 | util = require('awful.util'), |
| 29 | } |
| 30 | |
| 31 | local capi = { |
| 32 | widget = widget, |
| 33 | } |
| 34 | |
| 35 | local flaw = { |
| 36 | helper = require('flaw.helper'), |
| 37 | } |
| 38 | |
| 39 | |
| 40 | --- Core gadgets factory and handling. |
| 41 | -- |
| 42 | -- <p>To add functionality to awesome widgets, <b>flaw</b> defines |
| 43 | -- gadget objects, which are a wrapper around a widget. Gadgets have |
| 44 | -- properties, events, a refresh mechanism and a data provider. Even |
| 45 | -- though gadgets could wrap any <b>awesome</b> widget type, |
| 46 | -- <b>flaw</b> is focused on the most commonly used to report |
| 47 | -- information: text boxes, image boxes, graphs or progress |
| 48 | -- bars. These gadgets are <a |
| 49 | -- href='#TextGadget'><code>TextGadget</code></a>, <a |
| 50 | -- href='#GraphGadget'><code>GraphGadget</code></a>, <a |
| 51 | -- href='#BarGadget'><code>BarGadget</code></a> and <a |
| 52 | -- href='#IconGadget'><code>IconGadget</code></a> which embed a text |
| 53 | -- box, a graph, a progress bar and an image box |
| 54 | -- respectively.</p> |
| 55 | -- |
| 56 | -- <p><b>flaw</b> provides many gadgets for common system information |
| 57 | -- (like battery, CPU or memory activity) in different modules. This |
| 58 | -- one contains the core gadget prototypes and functions.</p> |
| 59 | -- |
| 60 | -- <h2>Instantiation, Initialization Parameters</h2> |
| 61 | -- |
| 62 | -- <p>The following is the complete pattern to instantiate a |
| 63 | -- gadget:</p> |
| 64 | -- |
| 65 | -- <div class='example'> |
| 66 | -- g = flaw.gadget.<type>.<name>(<id>, <gopts>, <wopts>) |
| 67 | -- </div> |
| 68 | -- |
| 69 | -- <p>A gadget is first identified by its <code>type</code> and its |
| 70 | -- <code>name</code>. The <code>type</code> is what identifies the |
| 71 | -- gadget kind and can be <code>Text</code>, <code>Icon</code>, |
| 72 | -- <code>Graph</code> and <code>Bar</code>. The <code>name</code> is |
| 73 | -- usually the name of the module which defines it (eg. the battery |
| 74 | -- module defines battery gadgets).</p> |
| 75 | -- |
| 76 | -- <p>Whatever the gadget, up to three parameters can be passed at |
| 77 | -- creation time, only the first being mandatory.</p> |
| 78 | -- |
| 79 | -- <ul> |
| 80 | -- |
| 81 | -- <li><code>id</code> |
| 82 | -- |
| 83 | -- <p>This first and most important parameter is the gadget |
| 84 | -- identifier, or ID. It is altogether the gadget unique name and a |
| 85 | -- string that can be used by this gadget provider to identify a |
| 86 | -- resource (eg. the CPU to monitor or the id of the battery). Whether |
| 87 | -- the provider needs an ID or not is described in service modules |
| 88 | -- pages, but this parameter must always be non-null.</p></li> |
| 89 | -- |
| 90 | -- <li><code>gopts</code> |
| 91 | -- |
| 92 | -- <p>The next parameter is a table providing properties for the |
| 93 | -- gadget to create. Again, modules documentation will provide |
| 94 | -- information on the available properties. This table can also be |
| 95 | -- used to store user data which will be available in the event |
| 96 | -- triggering functions for example.</p></li> |
| 97 | -- |
| 98 | -- <li><code>wopts</code> |
| 99 | -- |
| 100 | -- <p>The last parameter is another table providing properties for the |
| 101 | -- wrapped <b>awful</b> widget. The acceptable options are detailed in |
| 102 | -- the <b>awesome</b> documentation.</p></li> |
| 103 | -- |
| 104 | -- </ul> |
| 105 | -- |
| 106 | -- <p>All created gadgets are kept in a <a |
| 107 | -- href='#_gadgets_cache'>global store</a> from which they can be |
| 108 | -- retrieved anytime. This store knows not only about gadgets |
| 109 | -- instances, but also about their prototype, their provider, and |
| 110 | -- defaults properties.</p> |
| 111 | -- |
| 112 | -- <h2>Usage in <b>awesome</b> configuration</h2> |
| 113 | -- |
| 114 | -- <p>Gadgets are meant to be displayed in <i>wiboxes</i>, like any |
| 115 | -- other <b>awful</b> widget. Actually, it is the wrapped widget that |
| 116 | -- will be added to the <i>wibox</i>. To achieve this, every gadget |
| 117 | -- exposes its underlying widget with the <code>widget</code> |
| 118 | -- member.</p> |
| 119 | -- |
| 120 | -- <h2>Types Details</h2> |
| 121 | -- |
| 122 | -- <h3>Icon Gadgets</h3> |
| 123 | -- |
| 124 | -- <p>Icon gadgets wrap the <b>awful</b> <i>imagebox</i> widget. They |
| 125 | -- can display an image and that is all, which makes them the simpler |
| 126 | -- gadget provided by <b>flaw</b>. What's interesting about them |
| 127 | -- though is that like any other gadget they rely on one provider and |
| 128 | -- can make use of events.</p> |
| 129 | -- |
| 130 | -- <p>Assuming the beautiful property <code>battery_icon</code> is the |
| 131 | -- path to a default battery icon path, the following statement |
| 132 | -- creates a new battery status icon gadget. Note the |
| 133 | -- <code>image</code> widget property which is explained in |
| 134 | -- <b>awful</b> documentation.</p> |
| 135 | -- |
| 136 | -- <div class='example'> |
| 137 | -- g = flaw.gadget.icon.battery(<br/> |
| 138 | -- 'BAT0', {}, { image = image(beautiful.battery_icon) })<br/> |
| 139 | -- </div> |
| 140 | -- |
| 141 | -- <h3>Text Gadgets</h3> |
| 142 | -- |
| 143 | -- <p>Text gadgets allow you to display any textual information. What |
| 144 | -- makes them really interesting is that the display can be used to |
| 145 | -- format information returned by the provider associated to the |
| 146 | -- gadget.</p> |
| 147 | -- |
| 148 | -- <p>All text gadgets provide a default content related to the |
| 149 | -- information they gather from the provider (percents of available |
| 150 | -- memory, or of battery load for example). The <code>pattern</code> |
| 151 | -- gadget option allows every text gadget to be customized.</p> |
| 152 | -- |
| 153 | -- <div class='example'> |
| 154 | -- g = flaw.gadget.text.alsa('0',<br/> |
| 155 | -- { pattern = '<span color="#ffffff"> |
| 156 | -- $volume</span>%($mute)' }) |
| 157 | -- </div> |
| 158 | -- |
| 159 | -- <p>The variables to be formatted must be prefixed by a dollar |
| 160 | -- sign. The available variables are detailed in the providers |
| 161 | -- documentations.</p> |
| 162 | -- |
| 163 | -- <h3>Graph Gadgets</h3> |
| 164 | -- |
| 165 | -- <p>Graph gadgets are an interesting way to represent data variation |
| 166 | -- in the time. As of text gadgets, all graph gadgets provide default |
| 167 | -- content to be drawn. This content can be customized uing the |
| 168 | -- <code>values</code> gadget option.</p> |
| 169 | -- |
| 170 | -- <div class='example'> |
| 171 | -- g = flaw.gadget.graph.memory('', { values = {'free'} }, {<br/> |
| 172 | -- width = '35',<br/> |
| 173 | -- height = '0.8',<br/> |
| 174 | -- grow = 'right',<br/> |
| 175 | -- bg = beautiful.bg_normal,<br/> |
| 176 | -- fg = beautiful.fg_normal,<br/> |
| 177 | -- max_value = '100',<br/> |
| 178 | -- }) |
| 179 | -- </div> |
| 180 | -- |
| 181 | -- <h3>Bar Gadgets</h3> |
| 182 | -- |
| 183 | -- <p><b>FIXME</b></p> |
| 184 | -- |
| 185 | -- |
| 186 | -- @author David Soulayrol <david.soulayrol AT gmail DOT com> |
| 187 | -- @copyright 2009,2010,2011 David Soulayrol |
| 188 | module('flaw.gadget') |
| 189 | |
| 190 | |
| 191 | --- Types store. |
| 192 | -- |
| 193 | -- <p>This table indexes the available gadget types and their default |
| 194 | -- implementation.</p> |
| 195 | -- |
| 196 | -- <p>This table is private to the <code>gadget</code> module. It is |
| 197 | -- mainly used as a reference for registering gadget prototypes.</p> |
| 198 | -- |
| 199 | -- @class table |
| 200 | -- @name _gadgets_types |
| 201 | local _gadgets_types = {} |
| 202 | |
| 203 | --- Global gadgets store. |
| 204 | -- |
| 205 | -- <p>This table stores all the registered prototypes as well as all |
| 206 | -- the gagdet instances. The store behaves like a dictionnary where |
| 207 | -- the keys are prototype types and entries a table which |
| 208 | -- contains:</p> |
| 209 | -- |
| 210 | -- <ul> |
| 211 | -- <li>The prototype itself.</li> |
| 212 | -- <li>A factory for the provider used by the prototype.</li> |
| 213 | -- <li>A table of instances.</li> |
| 214 | -- <li>A table of default values to be set on each new instance.</li> |
| 215 | -- </ul> |
| 216 | -- |
| 217 | -- <p>This table is private to the <code>gadget</code> module. It can |
| 218 | -- be accessed using the <a href='#get'><code>get</code></a> and <a |
| 219 | -- href='#register'><code>register</code></a> functions of this |
| 220 | -- module.</p> |
| 221 | -- |
| 222 | -- @class table |
| 223 | -- @name _gadgets_cache |
| 224 | local _gadgets_cache = {} |
| 225 | |
| 226 | --- Store a gadget prototype. |
| 227 | -- |
| 228 | -- <p>This function creates a new entry in the global store. This |
| 229 | -- entry retains the given prototype, provider factory and defaults |
| 230 | -- tables and associates a gadget instance table to them.</p> |
| 231 | -- |
| 232 | -- <p>The function fails if the given prototype is nil or has a nil |
| 233 | -- type.</p> |
| 234 | -- |
| 235 | -- <p>Note that unless you create your own gadget prototype, you do |
| 236 | -- not have to call this function. Also note that the current |
| 237 | -- implementation silently overwrites the previous entry if it |
| 238 | -- existed.</p> |
| 239 | -- |
| 240 | -- @param t the type of prototype to register. |
| 241 | -- @param n the name of prototype to register. |
| 242 | -- @param p the prototype to register. |
| 243 | -- @param pf the provider factory to use when creating a new gadget |
| 244 | -- using this new prototype. |
| 245 | -- @param gopt the default prototype options. |
| 246 | -- @param wopt the default options for the wrapped widget. |
| 247 | function register_prototype(t, n, p, pf, gopt, wopt) |
| 248 | if t == nil or n == nil or p == nil or pf == nil then |
| 249 | flaw.helper.debug.error('flaw.gadget.register_prototype: invalid arguments.') |
| 250 | return |
| 251 | end |
| 252 | |
| 253 | if _gadgets_cache[t] == nil then |
| 254 | flaw.helper.debug.error('flaw.gadget.register_prototype: invalid type: ' .. t) |
| 255 | return |
| 256 | end |
| 257 | |
| 258 | -- Keep only the latest part of a dotted notation in name. |
| 259 | n = n:gsub('^%w*%.', '') |
| 260 | |
| 261 | _gadgets_cache[t][n] = { |
| 262 | prototype = p, |
| 263 | provider_factory = pf, |
| 264 | instances = {}, |
| 265 | defaults = { gadget = gopt or {}, widget = wopt or {} } |
| 266 | } |
| 267 | end |
| 268 | |
| 269 | --- Store a gadget instance. |
| 270 | -- |
| 271 | -- <p> This function stores the given instance in the global gadgets |
| 272 | -- store. It fails if the instance is invalid, that is if it is nil or |
| 273 | -- if its type or its ID is nil. It also fails if the store has no |
| 274 | -- entry for this gadget type.</p> |
| 275 | -- |
| 276 | -- <p>You normally do not have to call this function since it is |
| 277 | -- silently invoked each time you call the gadget factory <a |
| 278 | -- href='#new'><code>new</code></a> function.</p> |
| 279 | -- |
| 280 | -- @param t the type of gadget to store. |
| 281 | -- @param g the gadget to store. |
| 282 | -- @return The gadget stored, or nil if the given gadget could not be stored. |
| 283 | function add(t, n, g) |
| 284 | if t == nil or n == nil or g == nil or g.id == nil then |
| 285 | flaw.helper.debug.error('flaw.gadget.add: invalid gadget.') |
| 286 | else |
| 287 | if _gadgets_cache[t] == nil or _gadgets_cache[t][n] == nil then |
| 288 | flaw.helper.debug.error('flaw.gadget.add: unknown gadget class: ' .. g.type) |
| 289 | end |
| 290 | _gadgets_cache[t][n].instances[g.id] = g |
| 291 | return g |
| 292 | end |
| 293 | return nil |
| 294 | end |
| 295 | |
| 296 | --- Retrieve a gadget instance. |
| 297 | -- |
| 298 | -- <p>This function returns the gadget matching the given |
| 299 | -- information. It immediately fails if the given type or identifier |
| 300 | -- is nil. It also fails if no gadget in the store matches the given |
| 301 | -- parameters.</p> |
| 302 | -- |
| 303 | -- @param t the type of the gadget to retrieve. |
| 304 | -- @param id the uniquer identifier of the gadget to retrieve. |
| 305 | -- @return The matching gadget instance, or nil if information was |
| 306 | -- incomplete or if there is no such gadget. |
| 307 | function get(t, n, id) |
| 308 | if t ~= nil and n ~= nil and id ~= nil then |
| 309 | return _gadgets_cache[t] and _gadgets_cache[t][n] ~= nil |
| 310 | and _gadgets_cache[t].instances[id] |
| 311 | end |
| 312 | flaw.helper.debug.error('flaw.gadget.get: invalid information.') |
| 313 | return nil |
| 314 | end |
| 315 | |
| 316 | --- Create a new gadget. |
| 317 | -- |
| 318 | -- <p>This function is the only gadget factory and the main |
| 319 | -- <b>flaw</b> interface for a user to populate its <i>wibox</i> with |
| 320 | -- gadgets. However, it is not necessary to call it directly since the |
| 321 | -- module provides intelligent indexation to hide it.</p> |
| 322 | -- |
| 323 | -- <p>It immediately fails if given type or identifier is nil, or if |
| 324 | -- no such type was registered in the store. If it was, the gadget is |
| 325 | -- created from the data available in the matching store entry.</p> |
| 326 | -- |
| 327 | -- <p>If a matching entry is found in the store, the function first |
| 328 | -- completes the given options tables with the defaults found in the |
| 329 | -- entry. Then it creates the gadget using the found prototype and |
| 330 | -- provider factory, and applies the options tables to the gadget and |
| 331 | -- the wrapped widget respectively. At last, it starts the gadget |
| 332 | -- monitoring with the <code>delay</code> gadget option and stores it |
| 333 | -- in the store entry instances table.</p> |
| 334 | -- |
| 335 | -- @param t the type of the gadget to create. |
| 336 | -- @param id the uniquer identifier of the gadget to create. |
| 337 | -- @param gopt the options to pass to the created gadget. |
| 338 | -- @param wopt the options to pass to the created wrapped widget. |
| 339 | -- @return A brand new gadget instance, or nil it was not successfully |
| 340 | -- created. Note that the returned widget is present in the |
| 341 | -- gadget store and can be retrieved with the <a |
| 342 | -- href='#get'><code>get</code></a> function from now on. |
| 343 | function new(t, n, id, gopt, wopt) |
| 344 | if t == nil or n == nil or id == nil then |
| 345 | flaw.helper.debug.error('flaw.gadget.new: invalid information.') |
| 346 | return nil |
| 347 | end |
| 348 | |
| 349 | local e = _gadgets_cache[t][n] |
| 350 | if e == nil then |
| 351 | flaw.helper.debug.error('flaw.gadget.new: unknown gadget class: ' .. t .. '.' .. n) |
| 352 | return nil |
| 353 | end |
| 354 | |
| 355 | -- Load default parameters. |
| 356 | gopt = gopt or {} |
| 357 | wopt = wopt or {} |
| 358 | for k in pairs(e.defaults.gadget) do |
| 359 | gopt[k] = gopt[k] or e.defaults.gadget[k] |
| 360 | end |
| 361 | for k in pairs(e.defaults.widget) do |
| 362 | wopt[k] = wopt[k] or e.defaults.widget[k] |
| 363 | end |
| 364 | |
| 365 | -- Create the widget. |
| 366 | local g = e.prototype:new{ id = id, provider = e.provider_factory(id) } |
| 367 | |
| 368 | -- Configure the gadget. |
| 369 | for k in pairs(gopt) do g[k] = gopt[k] end |
| 370 | if g.create ~= nil then |
| 371 | g:create(wopt) |
| 372 | for k in pairs(wopt) do g.widget[k] = wopt[k] end |
| 373 | end |
| 374 | |
| 375 | -- Start monitoring. |
| 376 | g:register(gopt.delay) |
| 377 | |
| 378 | return add(t, n, g) |
| 379 | end |
| 380 | |
| 381 | |
| 382 | --- The Gadget prototype. |
| 383 | -- |
| 384 | -- <p>This is the root prototype of all gadgets. It provides common |
| 385 | -- methods for events and refresh handling.</p> |
| 386 | -- |
| 387 | -- @class table |
| 388 | -- @name Gadget |
| 389 | Gadget = {} |
| 390 | |
| 391 | --- Gadget constructor. |
| 392 | -- |
| 393 | -- <p>Note that this constructor is only used internally, or to create |
| 394 | -- new gadget prototypes. To create gadget instances, you should use |
| 395 | -- the gadget factory <a href='#new'><code>new</code></a> |
| 396 | -- function.</p> |
| 397 | -- |
| 398 | -- @param o a table with default values. |
| 399 | -- @return The brand new gadget. |
| 400 | function Gadget:new(o) |
| 401 | o = o or {} |
| 402 | o.events = {} |
| 403 | setmetatable(o, self) |
| 404 | self.__index = self |
| 405 | return o |
| 406 | end |
| 407 | |
| 408 | --- Hook the gadget to clock events. |
| 409 | -- |
| 410 | -- <p>This method is called automatically by the <a |
| 411 | -- href='#new'><code>new</code></a> function, with the |
| 412 | -- <code>delay</code> argument of its <code>gopt</code> options |
| 413 | -- table. The <code>delay</code> will be considered or not by the |
| 414 | -- provider depending on its kind.</p> |
| 415 | -- |
| 416 | -- @param delay the delay between gadget updates in seconds. It can be |
| 417 | -- nil if the underlying provider is not a <a |
| 418 | -- href="flaw.provider.html#CyclicProvider"><code>CyclicProvider</code></a>. |
| 419 | function Gadget:register(delay) |
| 420 | -- Subscribe this gadget to the provider. |
| 421 | if self.provider ~= nil then |
| 422 | self.provider:subscribe(self, delay) |
| 423 | end |
| 424 | end |
| 425 | |
| 426 | --- Register an event to the gadget. |
| 427 | -- |
| 428 | -- @param t the event trigger. |
| 429 | -- @param a the action taken upon the event occurrence. |
| 430 | function Gadget:add_event(t, a) |
| 431 | if t == nil or a == nil then |
| 432 | flaw.helper.debug.error('flaw.gadget.Gadget:add_event: invalid event') |
| 433 | else |
| 434 | self.events[t] = a |
| 435 | end |
| 436 | end |
| 437 | |
| 438 | --- Sets the tooltip for this gadget. |
| 439 | -- |
| 440 | -- @param pattern the pattern to use for the tooltip content. This |
| 441 | -- pattern will be parsed like a <a |
| 442 | -- href='#TextGadget'><code>TextGadget</code></a> pattern. |
| 443 | function Gadget:set_tooltip(pattern) |
| 444 | if pattern == nil then |
| 445 | flaw.helper.debug.error('flaw.gadget.Gadget:set_tooltip: invalid pattern.') |
| 446 | return nil |
| 447 | end |
| 448 | |
| 449 | self.tooltip = { |
| 450 | widget = awful.tooltip({ objects = { self.widget }, }), |
| 451 | pattern = pattern } |
| 452 | |
| 453 | if self.provider ~= nil and self.provider.data ~= nil then |
| 454 | self.tooltip.widget:set_text( |
| 455 | flaw.helper.strings.format( |
| 456 | pattern, self.provider.data[self.id] or self.provider.data)) |
| 457 | else |
| 458 | flaw.helper.debug.warn('flaw.gadget.Gadget:set_tooltip: useless tooltip.') |
| 459 | end |
| 460 | end |
| 461 | |
| 462 | --- Callback for gadget update. |
| 463 | -- |
| 464 | -- <p>This function is called by awful if the gadget has been |
| 465 | -- registered to awful time events. It first asks the gadget provider |
| 466 | -- to refresh itself, and then applies any triggered event. At last, |
| 467 | -- it calls its own redraw function, if defined.</p> |
| 468 | function Gadget:update() |
| 469 | if self.provider ~= nil then |
| 470 | for c, a in pairs(self.events) do |
| 471 | if c:test(self.provider.data) then |
| 472 | a(self, t) |
| 473 | end |
| 474 | end |
| 475 | if self.redraw then self:redraw() end |
| 476 | end |
| 477 | end |
| 478 | |
| 479 | |
| 480 | --- The text boxes wrapper gadget. |
| 481 | -- |
| 482 | -- <p>This specialised gadget relies on a pattern property which is |
| 483 | -- used to format the text output. The provider data are used when |
| 484 | -- parsing the pattern. See <a |
| 485 | -- href='flaw.helper.html#strings.format'><code>helper.strings.format</code></a> |
| 486 | -- to understand how the pattern is used to build the output.</p> |
| 487 | -- |
| 488 | -- @class table |
| 489 | -- @name TextGadget |
| 490 | TextGadget = Gadget:new{} |
| 491 | |
| 492 | -- Create the wrapped gadget. |
| 493 | function TextGadget:create(wopt) |
| 494 | self.widget = capi.widget( |
| 495 | awful.util.table.join(wopt, { type = 'textbox', name = self.id })) |
| 496 | end |
| 497 | |
| 498 | --- Specialised callback for text gadget update. |
| 499 | -- |
| 500 | -- <p>This implementation support two data models. First, it can apply |
| 501 | -- the gadget pattern directly on the provider data table. But if the |
| 502 | -- gadget identifier is a key of the provider data, then the gadget |
| 503 | -- pattern is applied using the content of this entry only in the |
| 504 | -- provider data set.</p> |
| 505 | function TextGadget:redraw() |
| 506 | local data_set = {} |
| 507 | if self.provider ~= nil and self.provider.data ~= nil then |
| 508 | data_set = self.provider.data[self.id] or self.provider.data |
| 509 | end |
| 510 | if self.pattern ~= nil then |
| 511 | self.widget.text = flaw.helper.strings.format(self.pattern, data_set) |
| 512 | end |
| 513 | if self.tooltip ~= nil then |
| 514 | self.tooltip.widget:set_text( |
| 515 | flaw.helper.strings.format(self.tooltip.pattern, data_set)) |
| 516 | end |
| 517 | end |
| 518 | |
| 519 | _gadgets_types.text = TextGadget |
| 520 | _gadgets_cache.text = {} |
| 521 | |
| 522 | --- The graphs wrapper gadget. |
| 523 | -- |
| 524 | -- <p>This specialised gadget proposes a list of data values to track |
| 525 | -- from the provider data and to plot.</p> |
| 526 | -- |
| 527 | -- @class table |
| 528 | -- @name GraphGadget |
| 529 | GraphGadget = Gadget:new{} |
| 530 | |
| 531 | --- Specialised callback for graph gadget update. |
| 532 | -- |
| 533 | -- <p>This implementation support two data models. First, it can |
| 534 | -- search the gadget values directly on the provider data table. But |
| 535 | -- if the gadget identifier is a key of the provider data, then the |
| 536 | -- gadget values is searched using the content of this entry only in |
| 537 | -- the provider data set.</p> |
| 538 | function GraphGadget:redraw() |
| 539 | if self.provider ~= nil and self.provider.data ~= nil then |
| 540 | local data_set = self.provider.data[self.id] or self.provider.data |
| 541 | for i, v in ipairs(self.values) do |
| 542 | self.hull:add_value(tonumber(data_set[v]) / 100) |
| 543 | end |
| 544 | end |
| 545 | end |
| 546 | |
| 547 | -- Create the wrapped gadget. |
| 548 | -- |
| 549 | -- In this case, the raw widget is still stored in |
| 550 | -- <code>widget</code>, but the <code>awful.widget.graph</code> is |
| 551 | -- stored in the <code>hull</code> one. |
| 552 | function GraphGadget:create(wopt) |
| 553 | self.hull = awful.widget.graph( |
| 554 | awful.util.table.join(wopt, {name = self.id})) |
| 555 | self.widget = self.hull.widget |
| 556 | end |
| 557 | |
| 558 | _gadgets_types.graph = GraphGadget |
| 559 | _gadgets_cache.graph = {} |
| 560 | |
| 561 | --- The progress bar wrapper gadget. |
| 562 | -- |
| 563 | -- The progress bar represents a numeric value using a horizontal or |
| 564 | -- vertical gauge. The gadget <code>value</code> field is the name of |
| 565 | -- the provider property to be represented. |
| 566 | -- |
| 567 | -- @class table |
| 568 | -- @name BarGadget |
| 569 | BarGadget = Gadget:new{} |
| 570 | |
| 571 | --- Specialised callback for progress gadget update. |
| 572 | -- |
| 573 | -- <p>This implementation supports two data models. First, it can |
| 574 | -- search the gadget <code>value</code> field directly in the provider |
| 575 | -- data table. But if the gadget identifier is a key of the provider |
| 576 | -- data table, then the gadget <code>value</code> field is searched in |
| 577 | -- the provider's <code>data[id]</code> table.</p> |
| 578 | function BarGadget:redraw() |
| 579 | if self.provider ~= nil and self.provider.data ~= nil then |
| 580 | local data_set = self.provider.data[self.id] or self.provider.data |
| 581 | self.hull:set_value(tonumber(data_set[self.value]) / 100) |
| 582 | end |
| 583 | end |
| 584 | |
| 585 | -- Create the wrapped gadget. |
| 586 | -- |
| 587 | -- In this case, the raw widget is still stored in |
| 588 | -- <code>widget</code>, but the <code>awful.widget.progressbar</code> |
| 589 | -- is stored in the <code>hull</code> one. To configure the |
| 590 | -- <code>hull</code>, see |
| 591 | -- <http://awesome.naquadah.org/doc/api/modules/awful.widget.progressbar.html>. |
| 592 | function BarGadget:create(wopt) |
| 593 | self.hull = awful.widget.progressbar( |
| 594 | awful.util.table.join(wopt, {name = self.id})) |
| 595 | self.widget = self.hull.widget |
| 596 | end |
| 597 | |
| 598 | _gadgets_types.bar = BarGadget |
| 599 | _gadgets_cache.bar = {} |
| 600 | |
| 601 | --- The image boxes wrapper gadget. |
| 602 | -- |
| 603 | -- <p>This specialised gadget is a placeholder for specific icon |
| 604 | -- treatments. For now, it brings nothing. Interesting stuff can be |
| 605 | -- handled using the events mechanism.</p> |
| 606 | -- |
| 607 | -- @class table |
| 608 | -- @name IconGadget |
| 609 | IconGadget = Gadget:new{ type = 'unknown.imagebox' } |
| 610 | |
| 611 | _gadgets_types.icon = IconGadget |
| 612 | _gadgets_cache.icon = {} |
| 613 | |
| 614 | |
| 615 | -- TODO |
| 616 | function IconGadget:create() |
| 617 | self.widget = capi.widget({ type = 'imagebox', name = self.id }) |
| 618 | end |
| 619 | |
| 620 | -- Below are nifty shortcuts to register or create new gadgets. |
| 621 | register = {} |
| 622 | setmetatable(register, { |
| 623 | __index = function(_, t) |
| 624 | return function(m, gopt, wopt, p) |
| 625 | if m == nil or m._NAME == nil or m.provider_factory == nil then |
| 626 | flaw.helper.debug.error( |
| 627 | 'flaw.gadget.register.' .. t .. ': invalid module.') |
| 628 | return |
| 629 | end |
| 630 | |
| 631 | register_prototype(t, m._NAME, p or _gadgets_types[t], |
| 632 | m.provider_factory, gopt, wopt) |
| 633 | end |
| 634 | end }) |
| 635 | |
| 636 | |
| 637 | setmetatable(_M, { |
| 638 | __index = function(_, t) |
| 639 | _t = {} |
| 640 | _mt = { __index = function(_, n) |
| 641 | return function(id, gopt, wopt) |
| 642 | return new(t, n, id, gopt, wopt) |
| 643 | end |
| 644 | end } |
| 645 | setmetatable(_t, _mt) |
| 646 | return _t |
| 647 | end }) |