awful.widget: check for image
[awesome.git] / lib / awful / widget.lua.in
blob30ac0161fbe18ced5da1ab6104ceaa37b6a3ea2a
1 ---------------------------------------------------------------------------
2 -- @author Julien Danjou <julien@danjou.info>
3 -- @copyright 2008 Julien Danjou
4 -- @release @AWESOME_VERSION@
5 ---------------------------------------------------------------------------
7 -- Grab environment we need
8 local ipairs = ipairs
9 local pairs = pairs
10 local table = table
11 local capi =
13 screen = screen,
14 client = client,
15 button = button,
16 widget = widget,
17 mouse = mouse
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
28 taglist = {}
29 taglist.label = {}
30 tasklist = {}
31 tasklist.label = {}
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)
38 local w = {}
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
47 -- Add more widgets
48 if len < #tags then
49 for i = len + 1, #tags do
50 w[i] = capi.widget({ type = "textbox", name = "taglist" .. i })
51 end
52 -- Remove widgets
53 elseif len > #tags then
54 for i = #tags, len do
55 w[i] = nil
56 end
57 end
58 -- Update widgets text
59 for k, tag in ipairs(tags) do
60 w[k].text = label(tag)
61 if buttons then
62 -- Replace press function by a new one calling with tags as
63 -- argument.
64 -- This is done here because order of tags can change
65 local mbuttons = {}
66 for kb, b in ipairs(buttons) do
67 -- Copy object
68 mbuttons[kb] = capi.button(b)
69 mbuttons[kb].press = function () b.press(tag) end
70 end
71 w[k]:buttons(mbuttons)
72 end
73 end
74 end
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)
81 end
82 end)
83 taglist_update(scr)
84 return w
85 end
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.
90 -- @param t The tag.
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"
110 local text
111 local background = ""
112 local sel = capi.client.focus
113 local bg_color = nil
114 local fg_color = nil
115 if t.selected then
116 bg_color = bg_focus
117 fg_color = fg_focus
119 if sel and sel:tags()[t] then
120 if taglist_squares == "true" then
121 background = "resize=\"true\" image=\"" .. taglist_squares_sel .. "\""
123 else
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
129 if c.urgent then
130 if bg_urgent then bg_color = bg_urgent end
131 if fg_urgent then fg_color = fg_urgent end
132 break
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> "
138 else
139 text = " <bg "..background.." />"..util.escape(t.name).." "
141 return text
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.
147 -- @param t The tag.
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
162 local bg_color = nil
163 local fg_color = nil
164 local text
166 if t.selected then
167 bg_color = bg_focus
168 fg_color = fg_focus
170 if bg_urgent and fg_urgent then
171 for k, c in pairs(t:clients()) do
172 if c.urgent then
173 bg_color = bg_urgent
174 fg_color = fg_urgent
175 break
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> "
181 else
182 text = " " .. util.escape(t.name) .. " "
184 return text
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)
192 local w = {}
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
205 -- Add more widgets
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" })
211 -- Remove widgets
212 elseif len > #clients then
213 for i = #clients * 2 + 1, len * 2, 2 do
214 w[i] = nil
215 w[i + 1] = nil
218 -- Update widgets text
219 for k = 1, #clients * 2, 2 do
220 if buttons then
221 -- Replace press function by a new one calling with tags as
222 -- argument
223 local mbuttons = {}
224 for kb, b in ipairs(buttons) do
225 -- Copy object
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
234 -- Set icon
235 w[k].image = clients[(k + 1) / 2].icon
236 if w[k].image then
237 w[k].visible = true
238 else
239 w[k].visible = false
241 w[k + 1].visible = true
242 else
243 w[k].visible = false
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)
253 if prop == "urgent"
254 or prop == "floating"
255 or prop == "icon"
256 or prop == "name"
257 or prop == "icon_name" then
258 tasklist_update()
260 end)
261 tasklist_update()
262 return w
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
272 local bg = nil
273 local text = "<margin left=\"2\" right=\"2\"/>"
274 local name
275 if c.floating then
276 text = text.."<bg image=\"@AWESOME_ICON_PATH@/tasklist/floatingw.png\" align=\"right\"/>"
278 if c.minimized then
279 name = util.escape(c.icon_name) or ""
280 else
281 name = util.escape(c.name) or ""
283 if capi.client.focus == c then
284 if bg_focus and fg_focus then
285 bg = bg_focus
286 text = text .. "<bg color='"..bg_focus.."'/><span color='"..util.color_strip_alpha(fg_focus).."'>"..name.."</span>"
287 else
288 text = text .. name
290 elseif c.urgent and bg_urgent and fg_urgent then
291 local bg = bg_urgent
292 text = text .. "<bg color='"..bg_urgent.."'/><span color='"..util.color_strip_alpha(fg_urgent).."'>"..name.."</span>"
293 else
294 text = text .. name
296 return text, bg
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
356 -- a real button.
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\"/>"
365 w.text = img_release
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
369 return w
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()
382 if args.command then
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)
388 w:buttons(b)
389 return w
392 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80