awful.widget.tasklist: add label.focused (FS#595)
[awesome.git] / lib / awful / widget / tasklist.lua.in
blob448a38d0fa7e37bb573ef486b50f460dfb9ca3e1
1 ---------------------------------------------------------------------------
2 -- @author Julien Danjou <julien@danjou.info>
3 -- @copyright 2008-2009 Julien Danjou
4 -- @release @AWESOME_VERSION@
5 ---------------------------------------------------------------------------
7 -- Grab environment we need
8 local capi = { screen = screen,
9 image = image,
10 client = client }
11 local ipairs = ipairs
12 local type = type
13 local setmetatable = setmetatable
14 local table = table
15 local common = require("awful.widget.common")
16 local beautiful = require("beautiful")
17 local client = require("awful.client")
18 local util = require("awful.util")
19 local tag = require("awful.tag")
20 local layout = require("awful.widget.layout")
22 --- Tasklist widget module for awful
23 module("awful.widget.tasklist")
25 -- Public structures
26 label = {}
28 local function tasklist_update(w, buttons, label, data, widgets)
29 local clients = capi.client.get()
30 local shownclients = {}
31 for k, c in ipairs(clients) do
32 if not (c.skip_taskbar or c.hidden
33 or c.type == "splash" or c.type == "dock" or c.type == "desktop") then
34 table.insert(shownclients, c)
35 end
36 end
37 clients = shownclients
39 common.list_update(w, buttons, label, data, widgets, clients)
40 end
42 --- Create a new tasklist widget.
43 -- @param label Label function to use.
44 -- @param buttons A table with buttons binding to set.
45 function new(label, buttons)
46 local w = {
47 layout = layout.horizontal.flex
49 local widgets = { }
50 widgets.imagebox = { }
51 widgets.textbox = { margin = { left = 2,
52 right = 2 },
53 bg_resize = true,
54 bg_align = "right"
56 local data = setmetatable({}, { __mode = 'k' })
57 local u = function () tasklist_update(w, buttons, label, data, widgets) end
58 for s = 1, capi.screen.count() do
59 tag.attached_add_signal(s, "property::selected", u)
60 capi.screen[s]:add_signal("tag::attach", u)
61 capi.screen[s]:add_signal("tag::detach", u)
62 end
63 capi.client.add_signal("new", function (c)
64 c:add_signal("property::urgent", u)
65 c:add_signal("property::floating", u)
66 c:add_signal("property::maximized_horizontal", u)
67 c:add_signal("property::maximized_vertical", u)
68 c:add_signal("property::name", u)
69 c:add_signal("property::icon_name", u)
70 c:add_signal("property::skip_taskbar", u)
71 c:add_signal("property::hidden", u)
72 c:add_signal("tagged", u)
73 c:add_signal("untagged", u)
74 end)
75 capi.client.add_signal("unmanage", u)
76 capi.client.add_signal("list", u)
77 capi.client.add_signal("focus", u)
78 capi.client.add_signal("unfocus", u)
79 u()
80 return w
81 end
83 local function widget_tasklist_label_common(c, args)
84 if not args then args = {} end
85 local theme = beautiful.get()
86 local fg_focus = args.fg_focus or theme.tasklist_fg_focus or theme.fg_focus
87 local bg_focus = args.bg_focus or theme.tasklist_bg_focus or theme.bg_focus
88 local fg_urgent = args.fg_urgent or theme.tasklist_fg_urgent or theme.fg_urgent
89 local bg_urgent = args.bg_urgent or theme.tasklist_bg_urgent or theme.bg_urgent
90 local fg_minimize = args.fg_minimize or theme.tasklist_fg_minimize or theme.fg_minimize
91 local bg_minimize = args.bg_minimize or theme.tasklist_bg_minimize or theme.bg_minimize
92 local floating_icon = args.floating_icon or theme.tasklist_floating_icon
93 local font = args.font or theme.tasklist_font or theme.font or ""
94 local bg = nil
95 local text = "<span font_desc='"..font.."'>"
96 local name
97 local status_image
98 if client.floating.get(c) and floating_icon then
99 status_image = capi.image(floating_icon)
101 if c.minimized then
102 name = util.escape(c.icon_name) or util.escape(c.name) or util.escape("<untitled>")
103 else
104 name = util.escape(c.name) or util.escape("<untitled>")
106 if capi.client.focus == c then
107 bg = bg_focus
108 if fg_focus then
109 text = text .. "<span color='"..util.color_strip_alpha(fg_focus).."'>"..name.."</span>"
110 else
111 text = text .. name
113 elseif c.urgent and fg_urgent then
114 bg = bg_urgent
115 text = text .. "<span color='"..util.color_strip_alpha(fg_urgent).."'>"..name.."</span>"
116 elseif c.minimized and fg_minimize and bg_minimize then
117 bg = bg_minimize
118 text = text .. "<span color='"..util.color_strip_alpha(fg_minimize).."'>"..name.."</span>"
119 else
120 text = text .. name
122 text = text .. "</span>"
123 return text, bg, status_image, c.icon
126 --- Return labels for a tasklist widget with clients from all tags and screen.
127 -- It returns the client name and set a special
128 -- foreground and background color for focused client.
129 -- It also puts a special icon for floating windows.
130 -- @param c The client.
131 -- @param screen The screen we are drawing on.
132 -- @param args The arguments table.
133 -- bg_focus The background color for focused client.
134 -- fg_focus The foreground color for focused client.
135 -- bg_urgent The background color for urgent clients.
136 -- fg_urgent The foreground color for urgent clients.
137 -- @return A string to print, a background color and a status image.
138 function label.allscreen(c, screen, args)
139 return widget_tasklist_label_common(c, args)
142 --- Return labels for a tasklist widget with clients from all tags.
143 -- It returns the client name and set a special
144 -- foreground and background color for focused client.
145 -- It also puts a special icon for floating windows.
146 -- @param c The client.
147 -- @param screen The screen we are drawing on.
148 -- @param args The arguments table.
149 -- bg_focus The background color for focused client.
150 -- fg_focus The foreground color for focused client.
151 -- bg_urgent The background color for urgent clients.
152 -- fg_urgent The foreground color for urgent clients.
153 -- @return A string to print, a background color and a status image.
154 function label.alltags(c, screen, args)
155 -- Only print client on the same screen as this widget
156 if c.screen ~= screen then return end
157 return widget_tasklist_label_common(c, args)
160 --- Return labels for a tasklist widget with clients from currently selected tags.
161 -- It returns the client name and set a special
162 -- foreground and background color for focused client.
163 -- It also puts a special icon for floating windows.
164 -- @param c The client.
165 -- @param screen The screen we are drawing on.
166 -- @param args The arguments table.
167 -- bg_focus The background color for focused client.
168 -- fg_focus The foreground color for focused client.
169 -- bg_urgent The background color for urgent clients.
170 -- fg_urgent The foreground color for urgent clients.
171 -- @return A string to print, a background color and a status image.
172 function label.currenttags(c, screen, args)
173 -- Only print client on the same screen as this widget
174 if c.screen ~= screen then return end
175 -- Include sticky client too
176 if c.sticky then return widget_tasklist_label_common(c, args) end
177 for k, t in ipairs(capi.screen[screen]:tags()) do
178 if t.selected then
179 local ctags = c:tags()
180 for _, v in ipairs(ctags) do
181 if v == t then
182 return widget_tasklist_label_common(c, args)
189 --- Return label for only the currently focused client.
190 -- It returns the client name and set a special
191 -- foreground and background color for focused client.
192 -- It also puts a special icon for floating windows.
193 -- @param c The client.
194 -- @param screen The screen we are drawing on.
195 -- @param args The arguments table.
196 -- bg_focus The background color for focused client.
197 -- fg_focus The foreground color for focused client.
198 -- bg_urgent The background color for urgent clients.
199 -- fg_urgent The foreground color for urgent clients.
200 -- @return A string to print, a background color and a status image.
201 function label.focused(c, screen, args)
202 -- Only print client on the same screen as this widget
203 if c.screen == screen and capi.client.focus == c then
204 return widget_tasklist_label_common(c, args)
208 setmetatable(_M, { __call = function(_, ...) return new(...) end })
210 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80