Advertisement
ahoka

Awesome 3.4 common.lua w/ centered tasklist

Apr 25th, 2013
355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.67 KB | None | 0 0
  1. ---------------------------------------------------------------------------
  2. -- @author Julien Danjou <[email protected]>
  3. -- @copyright 2008-2009 Julien Danjou
  4. -- @release @AWESOME_VERSION@
  5. ---------------------------------------------------------------------------
  6.  
  7. -- Grab environment we need
  8. local math = math
  9. local type = type
  10. local pcall = pcall
  11. local ipairs = ipairs
  12. local setmetatable = setmetatable
  13. local capi = { widget = widget, button = button }
  14.  
  15. --- Common widget code
  16. module("awful.widget.common")
  17.  
  18. -- Private structures
  19. tagwidgets = setmetatable({}, { __mode = 'k' })
  20.  
  21. function list_update(w, buttons, label, data, widgets, objects)
  22.     -- Hack: if it has been registered as a widget in a wibox,
  23.     -- it's w.len since __len meta does not work on table until Lua 5.2.
  24.     -- Otherwise it's standard #w.
  25.     local len = (w.len or #w) / 2
  26.     -- Add more widgets
  27.     if len < #objects then
  28.         for i = len * 2 + 1, #objects * 2, 2 do
  29.             local ib = capi.widget({ type = "imagebox", align = widgets.imagebox.align })
  30.             local tb = capi.widget({ type = "textbox", align = widgets.textbox.align })
  31.  
  32.             w[i] = ib
  33.             w[i + 1] = tb
  34.             w[i + 1]:margin({ left = widgets.textbox.margin.left, right = widgets.textbox.margin.right })
  35.             w[i + 1].bg_resize = widgets.textbox.bg_resize or false
  36.             w[i + 1].bg_align = widgets.textbox.bg_align or ""
  37.         w[i + 1].align = "center"
  38.  
  39.             if type(objects[math.floor(i / 2) + 1]) == "tag" then
  40.                 tagwidgets[ib] = objects[math.floor(i / 2) + 1]
  41.                 tagwidgets[tb] = objects[math.floor(i / 2) + 1]
  42.             end
  43.         end
  44.     -- Remove widgets
  45.     elseif len > #objects then
  46.         for i = #objects * 2 + 1, len * 2, 2 do
  47.             w[i] = nil
  48.             w[i + 1] = nil
  49.         end
  50.     end
  51.  
  52.     -- update widgets text
  53.     for k = 1, #objects * 2, 2 do
  54.         local o = objects[(k + 1) / 2]
  55.         if buttons then
  56.             -- Use a local variable so that the garbage collector doesn't strike
  57.             -- between now and the :buttons() call.
  58.             local btns = data[o]
  59.             if not btns then
  60.                 btns = {}
  61.                 data[o] = btns
  62.                 for kb, b in ipairs(buttons) do
  63.                     -- Create a proxy button object: it will receive the real
  64.                     -- press and release events, and will propagate them the the
  65.                     -- button object the user provided, but with the object as
  66.                     -- argument.
  67.                     local btn = capi.button { modifiers = b.modifiers, button = b.button }
  68.                     btn:add_signal("press", function () b:emit_signal("press", o) end)
  69.                     btn:add_signal("release", function () b:emit_signal("release", o) end)
  70.                     btns[#btns + 1] = btn
  71.                 end
  72.             end
  73.             w[k]:buttons(btns)
  74.             w[k + 1]:buttons(btns)
  75.         end
  76.  
  77.         local text, bg, bg_image, icon = label(o)
  78.  
  79.         -- Check if we got a valid text here, it might contain e.g. broken utf8.
  80.         if not pcall(function() w[k + 1].text = text end) then
  81.             w[k + 1].text = "<i>Invalid</i>"
  82.         end
  83.  
  84.         w[k + 1].bg, w[k + 1].bg_image = bg, bg_image
  85.         w[k].bg, w[k].image = bg, icon
  86.         if not w[k + 1].text then
  87.             w[k+1].visible = false
  88.         else
  89.             w[k+1].visible = true
  90.         end
  91.         if not w[k].image then
  92.             w[k].visible = false
  93.         else
  94.             w[k].visible = true
  95.         end
  96.    end
  97. end
  98.  
  99. -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement