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