Debian Codegraph @

revelation/init.lua

raw symbols SCIP
1 -- revelation.lua
2 --
3 -- Library that implements Expose like behavior.
4 --
5 -- @author Perry Hargrave [email protected]
6 -- @author Espen Wiborg [email protected]
7 -- @author Julien Danjou [email protected]
8 -- @auther Quan Guo [email protected]
9 --
10 -- @copyright 2008 Espen Wiborg, Julien Danjou
11 -- @copyright 2015 Quan Guo
12 --
13
14
15 local beautiful = require("beautiful")
16 local wibox = require("wibox")
17 local awful = require('awful')
18 local aw_rules = require('awful.rules')
19 local pairs = pairs
20 local setmetatable = setmetatable
21 local naughty = require("naughty")
22 local table = table
23 local clock = os.clock
24 local tostring = tostring
25 local capi = {
26 awesome = awesome,
27 tag = tag,
28 client = client,
29 keygrabber = keygrabber,
30 mousegrabber = mousegrabber,
31 mouse = mouse,
32 screen = screen
33 }
34
35 -- disable for now.
36 -- It seems there is not way to pass err handling function into the delayed_call()
37
38 local function debuginfo(message)
39 message = message or "No information available"
40 nid = naughty.notify({ text = tostring(message), timeout = 10 })
41 end
42
43 local delayed_call = (type(timer) ~= 'table' and require("gears.timer").delayed_call)
44
45 local view_only_func
46 local toggle_tag_func
47 local jump_to_func
48
49
50 if type(awful.client.object) == 'table' then
51 view_only_func = function (tag) tag:view_only() end
52 toggle_tag_func = function (t, c) c:toggle_tag(t) end
53 jump_to_func = function(c) c:jump_to() end
54 else
55 view_only_func = function (tag) awful.tag.viewonly(tag) end
56 toggle_tag_func = function (t, c) awful.client.toggletag(t, c) end
57 jump_to_func = function(c) awful.client.jumpto(c) end
58 end
59
60
61 local hintbox = {} -- Table of letter wiboxes with characters as the keys
62 local hintindex = {} -- Table of visible clients with the hint letter as the keys
63
64 local clients = {} --Table of clients to be exposed after fitlering
65 local clientData = {} -- table that holds the positions and sizes of floating clients
66
67 local revelation = {
68 -- Name of expose tag.
69 tag_name = "Revelation",
70
71 charorder = "jkluiopyhnmfdsatgvcewqzx1234567890",
72
73 -- Match function can be defined by user.
74 -- Must accept a `rule` and `client` and return `boolean`.
75 -- The rule forms follow `awful.rules` syntax except we also check the
76 -- special `rule.any` key. If its true, then we use the `match.any` function
77 -- for comparison.
78 match = {
79 exact = aw_rules.match,
80 any = aw_rules.match_any
81 },
82 property_to_watch={
83 maximized = false,
84 minimized = false,
85 fullscreen = false,
86 maximized_horizontal = false,
87 maximized_vertical = false,
88 sticky = false,
89 ontop = false,
90 above = false,
91 below = false,
92 },
93 tags_status = {},
94 is_excluded = false,
95 curr_tag_only = false,
96 font = beautiful.revelation_font or "monospace 20",
97 fg = beautiful.revelation_fg_normal or beautiful.fg_normal or "#DCDCCC",
98 bg = beautiful.revelation_bg_normal or beautiful.bg_normal or "#000000",
99 border_color = beautiful.revelation_border_color or beautiful.border_focus or "#DCDCCC",
100 border_width = beautiful.revelation_border_width or beautiful.border_width or 2,
101 hintsize = (type(beautiful.xresources) == 'table' and beautiful.xresources.apply_dpi(beautiful.revelation_hintsize or 50) or 60)
102 }
103
104
105
106 -- Executed when user selects a client from expose view.
107 --
108 -- @param restore Function to reset the current tags view.
109 local function selectfn(_, t, zt)
110 return function(c)
111 revelation.restore(t, zt)
112 -- Focus and raise
113 --
114 if type(delayed_call) == 'function' then
115 capi.awesome.emit_signal("refresh")
116 end
117
118 if awful.util.table.hasitem(hintindex, c) then
119 if c.minimized then
120 c.minimized = false
121 end
122
123 jump_to_func(c)
124 end
125 end
126 end
127
128 -- Tags all matching clients with tag t
129 -- @param rule The rule. Conforms to awful.rules syntax.
130 -- @param clients A table of clients to check.
131 -- @param t The tag to give matching clients.
132 local function match_clients(rule, _clients, t, is_excluded)
133
134 local mfc = rule.any and revelation.match.any or revelation.match.exact
135 local mf = is_excluded and function(c,_rule) return not mfc(c,_rule) end or mfc
136 local flt
137
138 for _, c in pairs(_clients) do
139 if mf(c, rule) then
140 -- Store geometry before setting their tags
141 clientData[c] = {}
142 clientData[c]["geometry"] = c:geometry()
143 flt = awful.client.property.get(c, "floating")
144 if flt ~= nil then
145 clientData[c]["floating"] = flt
146 awful.client.property.set(c, "floating", false)
147 end
148
149
150 for k,v in pairs(revelation.property_to_watch) do
151 clientData[c][k] = c[k]
152 c[k] = v
153
154 end
155 toggle_tag_func(t, c)
156 if c:isvisible() then
157 table.insert(clients, c)
158 end
159 end
160 end
161
162 end
163
164
165 -- Implement Exposé (ala Mac OS X).
166 --
167 -- @param rule A table with key and value to match. [{class=""}]
168 function revelation.expose(args)
169 args = args or {}
170 local rule = args.rule or {}
171 local is_excluded = args.is_excluded or revelation.is_excluded
172 local curr_tag_only = args.curr_tag_only or revelation.curr_tag_only
173
174 local t={}
175 local zt={}
176
177 clients = {}
178 clientData = {}
179
180 for scr=1,capi.screen.count() do
181 t[scr] = awful.tag.new({revelation.tag_name},
182 scr, awful.layout.suit.fair)[1]
183 zt[scr] = awful.tag.new({revelation.tag_name.."_zoom"},
184 scr, awful.layout.suit.fair)[1]
185
186 if curr_tag_only then
187 match_clients(rule, awful.client.visible(scr), t[scr], is_excluded)
188 else
189 match_clients(rule, capi.client.get(scr), t[scr], is_excluded)
190 end
191
192 view_only_func(t[scr])
193 end
194
195 if type(delayed_call) == 'function' then
196 capi.awesome.emit_signal("refresh")
197 end
198 -- No need for awesome WM 3.5.6: capi.awesome.emit_signal("refresh")
199 --
200 local status, err=pcall(revelation.expose_callback, t, zt, clients)
201
202 --revelation.expose_callback(t, zt)
203 if not status then
204 debuginfo('Oops!, something is wrong in revelation.expose_callback!')
205
206 if err.msg then
207 debuginfo(err.msg)
208 end
209
210 if err.code then
211 debuginfo('error code is '.. tostring(err.code))
212 end
213
214 revelation.restore(t, zt)
215
216 end
217 end
218
219
220 function revelation.restore(t, zt)
221 for scr=1, capi.screen.count() do
222 awful.tag.history.restore(scr)
223 t[scr].screen = nil
224 end
225
226 capi.keygrabber.stop()
227 capi.mousegrabber.stop()
228
229 for _, c in pairs(clients) do
230 if clientData[c] then
231 for k,v in pairs(clientData[c]) do
232 if v ~= nil then
233 if k== "geometry" then
234 c:geometry(v)
235 elseif k == "floating" then
236 awful.client.property.set(c, "floating", v)
237 else
238 c[k]=v
239 end
240 end
241 end
242 end
243 end
244
245 for scr=1, capi.screen.count() do
246 t[scr].activated = false
247 zt[scr].activated = false
248 end
249
250 for i,j in pairs(hintindex) do
251 hintbox[i].visible = false
252 end
253 end
254
255 local function hintbox_display_toggle(c, show)
256 for char, thisclient in pairs(hintindex) do
257 if char and char ~= c then
258 hintindex[char] = thisclient
259 if show then
260 hintbox[char].visible = true
261 else
262 hintbox[char].visible = false
263 end
264
265 end
266 end
267 end
268
269 local function hintbox_pos(char)
270 local client = hintindex[char]
271 local geom = client:geometry()
272 hintbox[char].x = math.floor(geom.x + geom.width/2 - revelation.hintsize/2)
273 hintbox[char].y = math.floor(geom.y + geom.height/2 - revelation.hintsize/2)
274 end
275
276
277 function revelation.expose_callback(t, zt, clientlist)
278
279 hintindex = {}
280 for i,thisclient in pairs(clientlist) do
281 -- Move wiboxes to center of visible windows and populate hintindex
282 local char = revelation.charorder:sub(i,i)
283 if char and char ~= '' then
284 hintindex[char] = thisclient
285 hintbox_pos(char)
286 hintbox[char].visible = true
287 hintbox[char].screen = thisclient.screen
288 end
289 end
290
291 local zoomed = false
292 local zoomedClient = nil
293 local key_char_zoomed = nil
294
295 capi.keygrabber.run(function (mod, key, event)
296 local c
297 if event == "release" then return true end
298
299 if awful.util.table.hasitem(mod, "Shift") then
300 key_char = string.lower(key)
301 c = hintindex[key_char]
302 if not zoomed and c ~= nil then
303 --debuginfo(c.screen)
304 view_only_func(zt[c.screen.index or c.screen])
305 toggle_tag_func(zt[c.screen.index or c.screen], c)
306 zoomedClient = c
307 key_char_zoomed = key_char
308 zoomed = true
309 -- update the position of this hintbox, since it is zoomed
310 if type(delayed_call) == 'function' then
311 capi.awesome.emit_signal("refresh")
312 end
313 hintbox_pos(key_char)
314 hintbox_display_toggle(key_char, false)
315
316
317 elseif zoomedClient ~= nil then
318 awful.tag.history.restore(zoomedClient.screen.index or zoomedClient.screen)
319 toggle_tag_func(zt[zoomedClient.screen.index or zoomedClient.screen], zoomedClient)
320 hintbox_display_toggle(key_char_zoomed, true)
321 if type(delayed_call) == 'function' then
322 capi.awesome.emit_signal("refresh")
323 end
324 hintbox_pos(key_char_zoomed)
325
326 zoomedClient = nil
327 zoomed = false
328 key_char_zoomed = nil
329 end
330 end
331
332 if hintindex[key] then
333 --client.focus = hintindex[key]
334 --hintindex[key]:raise()
335
336 selectfn(restore,t, zt)(hintindex[key])
337
338 for i,j in pairs(hintindex) do
339 hintbox[i].visible = false
340 end
341
342 return false
343 end
344
345 if key == "Escape" then
346 if zoomedClient ~= nil then
347 awful.tag.history.restore(zoomedClient.screen)
348 toggle_tag_func(zt[zoomedClient.screen.index or zoomedClient.screen], zoomedClient)
349 hintbox_display_toggle(string.lower(key), true)
350 if type(delayed_call) == 'function' then
351 capi.awesome.emit_signal("refresh")
352 end
353 hintbox_pos(key_char_zoomed)
354
355 zoomedClient = nil
356 zoomed = false
357 else
358 for i,j in pairs(hintindex) do
359 hintbox[i].visible = false
360 end
361 revelation.restore(t, zt)
362 return false
363 end
364 end
365
366 return true
367 end)
368
369 local pressedMiddle = false
370
371 local lastClient = nil
372 capi.mousegrabber.run(function(mouse)
373 local c
374
375 if type(awful.client.object) == 'table' then
376 c = capi.mouse.current_client
377 else
378 c = awful.mouse.client_under_pointer()
379 end
380 if c then
381 lastClient = c
382 else
383 local current_wibox = capi.mouse.current_wibox
384 if current_wibox then
385 for i = 1, #revelation.charorder do
386 local char = revelation.charorder:sub(i,i)
387 if hintbox[char] == current_wibox then
388 c = lastClient
389 break
390 end
391 end
392 end
393 end
394 local key_char = awful.util.table.hasitem(hintindex, c)
395 if mouse.buttons[1] == true then
396 if c ~= nil then
397 selectfn(restore, t, zt)(c)
398
399 for i,j in pairs(hintindex) do
400 hintbox[i].visible = false
401 end
402 return false
403 else
404 return true
405 end
406 elseif mouse.buttons[2] == true and pressedMiddle == false and c ~= nil then
407 -- is true whenever the button is down.
408 pressedMiddle = true
409 -- extra variable needed to prevent script from spam-closing windows
410 --
411 if zoomed == true and zoomedClient ~=nil then
412 awful.tag.history.restore(zoomedClient.screen.index or zoomedClient.screen)
413 toggle_tag_func(zt[zoomedClient.screen.index or zoomedClient.screen], zoomedClient)
414 end
415 c:kill()
416 hintbox[key_char].visible = false
417 hintindex[key_char] = nil
418 local pos = awful.util.table.hasitem(clients, c)
419 table.remove(clients, pos)
420
421
422 if zoomed == true and zoomedClient ~=nil then
423 hintbox_display_toggle(key_char_zoomed, true)
424 zoomedClient = nil
425 zoomed = false
426 key_char_zoomed = nil
427 end
428
429 return true
430
431 elseif mouse.buttons[2] == false and pressedMiddle == true then
432 pressedMiddle = false
433 for key, _ in pairs(hintindex) do
434 hintbox_pos(key)
435 end
436 elseif mouse.buttons[3] == true then
437 if not zoomed and c ~= nil then
438 view_only_func(zt[c.screen.index or c.screen])
439 toggle_tag_func(zt[c.screen.index or c.screen], c)
440 if key_char ~= nil then
441 hintbox_display_toggle(key_char, false)
442 if type(delayed_call) == 'function' then
443 capi.awesome.emit_signal("refresh")
444 end
445 hintbox_pos(key_char)
446 end
447 zoomedClient = c
448 zoomed = true
449 key_char_zoomed = key_char
450 elseif zoomedClient ~= nil then
451 awful.tag.history.restore(zoomedClient.screen.index or zoomedClient.screen)
452 toggle_tag_func(zt[zoomedClient.screen.index or zoomedClient.screen], zoomedClient)
453 hintbox_display_toggle(key_char_zoomed, true)
454 if type(delayed_call) == 'function' then
455 capi.awesome.emit_signal("refresh")
456 end
457 hintbox_pos(key_char_zoomed)
458
459 zoomedClient = nil
460 zoomed = false
461 key_char_zoomed = nil
462 end
463 end
464
465 return true
466 --Strange but on my machine only fleur worked as a string.
467 --stole it from
468 --https://github.com/Elv13/awesome-configs/blob/master/widgets/layout/desktopLayout.lua#L175
469 end,"fleur")
470
471 end
472
473 -- Create the wiboxes, but don't show them
474 --
475 function revelation.init(args)
476 local letterbox = {}
477
478 args = args or {}
479
480 revelation.tag_name = args.tag_name or revelation.tag_name
481 if args.match then
482 revelation.match.exact = args.match.exact or revelation.match.exact
483 revelation.match.any = args.match.any or revelation.match.any
484 end
485
486 revelation.charorder = args.charorder or revelation.charorder
487
488 for i = 1, #revelation.charorder do
489 local char = revelation.charorder:sub(i,i)
490 hintbox[char] = wibox({
491 fg=revelation.fg,
492 bg=revelation.bg,
493 border_color=revelation.border_color,
494 border_width=revelation.border_width
495 })
496 hintbox[char].ontop = true
497 hintbox[char].width = revelation.hintsize
498 hintbox[char].height = revelation.hintsize
499 letterbox[char] = wibox.widget.textbox()
500 letterbox[char]:set_markup(char.upper(char))
501 letterbox[char]:set_font(revelation.font)
502 letterbox[char]:set_align("center")
503 hintbox[char]:set_widget(letterbox[char])
504 end
505 end
506
507 setmetatable(revelation, { __call = function(_, ...) return revelation.expose(...) end })
508
509 return revelation