1 ---------------------------------------------------------------------------
2 -- @author Julien Danjou <julien@danjou.info>
3 -- @copyright 2008 Julien Danjou
4 -- @release @AWESOME_VERSION@
5 ---------------------------------------------------------------------------
7 -- Grab environment we need
19 local util
= require("awful.util")
20 local hooks
= require("awful.hooks")
21 local beautiful
= require("awful.beautiful")
22 local menu
= require("awful.menu")
24 --- Widget module for awful
25 module("awful.widget")
27 -- Various public structures
33 --- Create a new taglist widget.
34 -- @param screen The screen to draw tag list.
35 -- @param label Label function to use.
36 -- @param buttons A table with buttons binding to set.
37 function taglist
.new(scr
, label
, buttons
)
39 local function taglist_update (screen
)
40 -- Return right now if we do not care about this screen
41 if scr
~= screen
then return end
42 local tags
= capi
.screen
[screen
]:tags()
43 -- Hack: if it has been registered as a widget in a wibox,
44 -- it's w.len since __len meta does not work on table until Lua 5.2.
45 -- Otherwise it's standard #w.
46 local len
= w
.len
or #w
49 for i
= len
+ 1, #tags
do
50 w
[i
] = capi
.widget({ type = "textbox", name
= "taglist" .. i
})
53 elseif len
> #tags
then
58 -- Update widgets text
59 for k
, tag in ipairs(tags
) do
60 w
[k
].text
= label(tag)
62 -- Replace press function by a new one calling with tags as
64 -- This is done here because order of tags can change
66 for kb
, b
in ipairs(buttons
) do
68 mbuttons
[kb
] = capi
.button(b
)
69 mbuttons
[kb
].press
= function () b
.press(tag) end
71 w
[k
]:buttons(mbuttons
)
75 hooks
.arrange
.register(taglist_update
)
76 hooks
.tags
.register(taglist_update
)
77 hooks
.tagged
.register(function (c
, tag) taglist_update(c
.screen
) end)
78 hooks
.property
.register(function (c
, prop
)
79 if c
.screen
== scr
and prop
== "urgent" then
80 taglist_update(c
.screen
)
87 --- Return labels for a taglist widget with all tag from screen.
88 -- It returns the tag name and set a special
89 -- foreground and background color for selected tags.
91 -- @param args The arguments table.
92 -- bg_focus The background color for selected tag.
93 -- fg_focus The foreground color for selected tag.
94 -- bg_urgent The background color for urgent tags.
95 -- fg_urgent The foreground color for urgent tags.
96 -- taglist_squares Optional: set "true" or nil to display the taglist squares.
97 -- taglist_squares_sel Optional: an user provided image for selected squares.
98 -- taglist_squares_unsel Optional: an user provided image for unselected squares.
99 -- @return A string to print.
100 function taglist
.label
.all(t
, args
)
101 if not args
then args
= {} end
102 local theme
= beautiful
.get()
103 local fg_focus
= args
.fg_focus
or theme
.taglist_fg_focus
or theme
.fg_focus
104 local bg_focus
= args
.bg_focus
or theme
.taglist_bg_focus
or theme
.bg_focus
105 local fg_urgent
= args
.fg_urgent
or theme
.taglist_fg_urgent
or theme
.fg_urgent
106 local bg_urgent
= args
.bg_urgent
or theme
.taglist_bg_urgent
or theme
.bg_urgent
107 local taglist_squares
= args
.taglist_squares
or theme
.taglist_squares
or "true"
108 local taglist_squares_sel
= args
.squares_sel
or theme
.squares_sel
or "@AWESOME_ICON_PATH@/taglist/squarefw.png"
109 local taglist_squares_unsel
= args
.squares_unsel
or theme
.squares_unsel
or "@AWESOME_ICON_PATH@/taglist/squarew.png"
111 local background
= ""
112 local sel
= capi
.client
.focus
119 if sel
and sel
:tags()[t
] then
120 if taglist_squares
== "true" then
121 background
= "resize=\"true\" image=\"" .. taglist_squares_sel
.. "\""
124 local cls
= t
:clients()
125 if #cls
> 0 and taglist_squares
== "true" then
126 background
= "resize=\"true\" image=\"" .. taglist_squares_unsel
.. "\""
128 for k
, c
in pairs(cls
) do
130 if bg_urgent
then bg_color
= bg_urgent
end
131 if fg_urgent
then fg_color
= fg_urgent
end
136 if bg_color
and fg_color
then
137 text
= "<bg "..background
.." color='"..bg_color
.."'/> <span color='"..util
.color_strip_alpha(fg_color
).."'>"..util
.escape(t
.name
).."</span> "
139 text
= " <bg "..background
.." />"..util
.escape(t
.name
).." "
144 --- Return labels for a taglist widget with all *non empty* tags from screen.
145 -- It returns the tag name and set a special
146 -- foreground and background color for selected tags.
148 -- @param args The arguments table.
149 -- bg_focus The background color for selected tag.
150 -- fg_focus The foreground color for selected tag.
151 -- bg_urgent The background color for urgent tags.
152 -- fg_urgent The foreground color for urgent tags.
153 -- @return A string to print.
154 function taglist
.label
.noempty(t
, args
)
155 if #t
:clients() > 0 or t
.selected
then
156 if not args
then args
= {} end
157 local theme
= beautiful
.get()
158 local fg_focus
= args
.fg_focus
or theme
.taglist_fg_focus
or theme
.fg_focus
159 local bg_focus
= args
.bg_focus
or theme
.taglist_bg_focus
or theme
.bg_focus
160 local fg_urgent
= args
.fg_urgent
or theme
.taglist_fg_urgent
or theme
.fg_urgent
161 local bg_urgent
= args
.bg_urgent
or theme
.taglist_bg_urgent
or theme
.bg_urgent
170 if bg_urgent
and fg_urgent
then
171 for k
, c
in pairs(t
:clients()) do
179 if fg_color
and bg_color
then
180 text
= "<bg color='" .. bg_color
.. "'/> <span color='" .. util
.color_strip_alpha(fg_color
) .. "'>" .. util
.escape(t
.name
) .. "</span> "
182 text
= " " .. util
.escape(t
.name
) .. " "
188 --- Create a new tasklist widget.
189 -- @param label Label function to use.
190 -- @param buttons A table with buttons binding to set.
191 function tasklist
.new(label
, buttons
)
193 local function tasklist_update ()
194 local clients
= capi
.client
.get()
195 for k
, c
in ipairs(clients
) do
196 if c
.skip_taskbar
or c
.hide
197 or c
.type == "splash" or c
.type == "dock" or c
.type == "desktop" then
198 table.remove(clients
, k
)
201 -- Hack: if it has been registered as a widget in a wibox,
202 -- it's w.len since __len meta does not work on table until Lua 5.2.
203 -- Otherwise it's standard #w.
204 local len
= (w
.len
or #w
) / 2
206 if len
< #clients
then
207 for i
= len
* 2 + 1, #clients
* 2, 2 do
208 w
[i
] = capi
.widget({ type = "imagebox", name
= "tasklist_icon" .. i
, align
= "flex" })
209 w
[i
+ 1] = capi
.widget({ type = "textbox", name
= "tasklist_text" .. i
, align
= "flex" })
212 elseif len
> #clients
then
213 for i
= #clients
* 2 + 1, len
* 2, 2 do
218 -- Update widgets text
219 for k
= 1, #clients
* 2, 2 do
221 -- Replace press function by a new one calling with tags as
224 for kb
, b
in ipairs(buttons
) do
226 mbuttons
[kb
] = capi
.button(b
)
227 mbuttons
[kb
].press
= function () b
.press(clients
[(k
+ 1) / 2]) end
229 w
[k
]:buttons(mbuttons
)
230 w
[k
+ 1]:buttons(mbuttons
)
232 w
[k
+ 1].text
, w
[k
].bg
= label(clients
[(k
+ 1) / 2])
233 if w
[k
+ 1].text
then
235 w
[k
].image
= clients
[(k
+ 1) / 2].icon
241 w
[k
+ 1].visible
= true
244 w
[k
+ 1].visible
= false
248 hooks
.clients
.register(tasklist_update
)
249 hooks
.tagged
.register(tasklist_update
)
250 hooks
.focus
.register(tasklist_update
)
251 hooks
.unfocus
.register(tasklist_update
)
252 hooks
.property
.register(function (c
, prop
)
254 or prop
== "floating"
257 or prop
== "icon_name" then
265 local function widget_tasklist_label_common(c
, args
)
266 if not args
then args
= {} end
267 local theme
= beautiful
.get()
268 local fg_focus
= args
.fg_focus
or theme
.tasklist_fg_focus
or theme
.fg_focus
269 local bg_focus
= args
.bg_focus
or theme
.tasklist_bg_focus
or theme
.bg_focus
270 local fg_urgent
= args
.fg_urgent
or theme
.tasklist_fg_urgent
or theme
.fg_urgent
271 local bg_urgent
= args
.bg_urgent
or theme
.tasklist_bg_urgent
or theme
.bg_urgent
273 local text
= "<margin left=\"2\" right=\"2\"/>"
276 text
= text
.."<bg image=\"@AWESOME_ICON_PATH@/tasklist/floatingw.png\" align=\"right\"/>"
279 name
= util
.escape(c
.icon_name
) or ""
281 name
= util
.escape(c
.name
) or ""
283 if capi
.client
.focus
== c
then
284 if bg_focus
and fg_focus
then
286 text
= text
.. "<bg color='"..bg_focus
.."'/><span color='"..util
.color_strip_alpha(fg_focus
).."'>"..name
.."</span>"
290 elseif c
.urgent
and bg_urgent
and fg_urgent
then
292 text
= text
.. "<bg color='"..bg_urgent
.."'/><span color='"..util
.color_strip_alpha(fg_urgent
).."'>"..name
.."</span>"
299 --- Return labels for a tasklist widget with clients from all tags and screen.
300 -- It returns the client name and set a special
301 -- foreground and background color for focused client.
302 -- It also puts a special icon for floating windows.
303 -- @param c The client.
304 -- @param screen The screen we are drawing on.
305 -- @param args The arguments table.
306 -- bg_focus The background color for focused client.
307 -- fg_focus The foreground color for focused client.
308 -- bg_urgent The background color for urgent clients.
309 -- fg_urgent The foreground color for urgent clients.
310 -- @return A string to print.
311 function tasklist
.label
.allscreen(c
, screen
, args
)
312 return widget_tasklist_label_common(c
, args
)
315 --- Return labels for a tasklist widget with clients from all tags.
316 -- It returns the client name and set a special
317 -- foreground and background color for focused client.
318 -- It also puts a special icon for floating windows.
319 -- @param c The client.
320 -- @param screen The screen we are drawing on.
321 -- @param args The arguments table.
322 -- bg_focus The background color for focused client.
323 -- fg_focus The foreground color for focused client.
324 -- bg_urgent The background color for urgent clients.
325 -- fg_urgent The foreground color for urgent clients.
326 -- @return A string to print.
327 function tasklist
.label
.alltags(c
, screen
, args
)
328 -- Only print client on the same screen as this widget
329 if c
.screen
~= screen
then return end
330 return widget_tasklist_label_common(c
, args
)
333 --- Return labels for a tasklist widget with clients from currently selected tags.
334 -- It returns the client name and set a special
335 -- foreground and background color for focused client.
336 -- It also puts a special icon for floating windows.
337 -- @param c The client.
338 -- @param screen The screen we are drawing on.
339 -- @param args The arguments table.
340 -- bg_focus The background color for focused client.
341 -- fg_focus The foreground color for focused client.
342 -- bg_urgent The background color for urgent clients.
343 -- fg_urgent The foreground color for urgent clients.
344 -- @return A string to print.
345 function tasklist
.label
.currenttags(c
, screen
, args
)
346 -- Only print client on the same screen as this widget
347 if c
.screen
~= screen
then return end
348 for k
, t
in ipairs(capi
.screen
[screen
]:tags()) do
349 if t
.selected
and c
:tags()[t
] then
350 return widget_tasklist_label_common(c
, args
)
355 --- Create a button widget. When clicked, the image is deplaced to make it like
357 -- @param args Standard widget table arguments, plus image for the image path.
358 -- @return A textbox widget configured as a button.
359 function button(args
)
360 if not args
or not args
.image
then return end
361 args
.type = "textbox"
362 local w
= capi
.widget(args
)
363 local img_release
= "<bg image=\"" .. args
.image
.. "\" resize=\"true\"/>"
364 local img_press
= "<bg_margin top=\"2\" left=\"2\"/><bg image=\"" .. args
.image
.. "\" resize=\"true\"/>"
366 w
:buttons({ capi
.button({}, 1, function () w
.text
= img_press
end, function () w
.text
= img_release
end) })
367 function w
.mouse_leave(s
) w
.text
= img_release
end
368 function w
.mouse_enter(s
) if capi
.mouse
.coords().buttons
[1] then w
.text
= img_press
end end
372 --- Create a button widget which will launch a command.
373 -- @param args Standard widget table arguments, plus image for the image path
374 -- and command for the command to run on click, or either menu to create menu.
375 -- @return A launcher widget.
376 function launcher(args
)
377 if not args
.command
and not args
.menu
then return end
378 local w
= button(args
)
379 if not w
then return end
380 local b
= w
:buttons()
383 b
[#b
+ 1] = capi
.button({}, 1, nil, function () util
.spawn(args
.command
) end)
384 elseif args
.menu
then
385 b
[#b
+ 1] = capi
.button({}, 1, nil, function () args
.menu
:toggle() end)
392 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80