-
Notifications
You must be signed in to change notification settings - Fork 30
/
init.lua
443 lines (360 loc) · 11.8 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
local cairo = require("lgi").cairo
local mouse = mouse
local screen = screen
local wibox = require('wibox')
local table = table
local timer = timer
local keygrabber = keygrabber
local math = require('math')
local awful = require('awful')
local gears = require("gears")
local client = client
awful.client = require('awful.client')
local naughty = require("naughty")
local string = string
local tostring = tostring
local tonumber = tonumber
local debug = debug
local pairs = pairs
module("alttab")
local surface = cairo.ImageSurface(cairo.Format.RGB24,20,20)
local cr = cairo.Context(surface)
-- settings
local settings = {
preview_box = true,
preview_box_bg = "#ddddddaa",
preview_box_border = "#22222200",
preview_box_fps = 30,
preview_box_delay = 150,
client_opacity = false,
client_opacity_value = 0.5,
client_opacity_delay = 150,
}
-- Create a wibox to contain all the client-widgets
local preview_wbox = wibox({ width = screen[mouse.screen].geometry.width })
preview_wbox.border_width = 3
preview_wbox.ontop = true
preview_wbox.visible = false
local preview_live_timer = timer({}) --( {timeout = 1/settings.preview_box_fps} )
local preview_widgets = {}
local altTabTable = {}
local altTabIndex = 1
local applyOpacity = false
local source = string.sub(debug.getinfo(1,'S').source, 2)
local path = string.sub(source, 1, string.find(source, "/[^/]*$"))
local noicon = path .. "noicon.png"
local function preview()
if not settings.preview_box then return end
-- Apply settings
preview_wbox:set_bg(settings.preview_box_bg)
preview_wbox.border_color = settings.preview_box_border
local preview_widgets = {}
-- Make the wibox the right size, based on the number of clients
local n = math.max(7, #altTabTable)
local W = screen[mouse.screen].geometry.width + 2 * preview_wbox.border_width
local w = W / n -- widget width
local h = w * 0.75 -- widget height
local textboxHeight = 30
local x = -preview_wbox.border_width
local y = (screen[mouse.screen].geometry.height - h - textboxHeight) / 2
preview_wbox:geometry({x = x, y = y, width = W, height = h + textboxHeight})
-- create a list that holds the clients to preview, from left to right
local leftRightTab = {}
local nLeft
local nRight
if #altTabTable == 2 then
nLeft = 0
nRight = 2
else
nLeft = math.floor(#altTabTable / 2)
nRight = math.ceil(#altTabTable / 2)
end
for i = 1, nLeft do
table.insert(leftRightTab, altTabTable[#altTabTable - nLeft + i])
end
for i = 1, nRight do
table.insert(leftRightTab, altTabTable[i])
end
-- determine fontsize -> find maximum classname-length
local text, textWidth, textHeight, maxText
local maxTextWidth = 0
local maxTextHeight = 0
local bigFont = textboxHeight / 2
cr:set_font_size(fontSize)
for i = 1, #leftRightTab do
text = " - " .. leftRightTab[i].class
textWidth = cr:text_extents(text).width
textHeight = cr:text_extents(text).height
if textWidth > maxTextWidth or textHeight > maxTextHeight then
maxTextHeight = textHeight
maxTextWidth = textWidth
maxText = text
end
end
while true do
cr:set_font_size(bigFont)
textWidth = cr:text_extents(maxText).width
textHeight = cr:text_extents(maxText).height
if textWidth < w - textboxHeight and textHeight < textboxHeight then
break
end
bigFont = bigFont - 1
end
local smallFont = bigFont * 0.8
-- create all the widgets
for i = 1, #leftRightTab do
preview_widgets[i] = wibox.widget.base.make_widget()
preview_widgets[i].fit = function(preview_widget, width, height)
return w, h
end
preview_widgets[i].draw = function(preview_widget, preview_wbox, cr, width, height)
if width ~= 0 and height ~= 0 then
local c = leftRightTab[i]
local a = 0.8
local overlay = 0.6
local fontSize = smallFont
if c == altTabTable[altTabIndex] then
a = 0.9
overlay = 0
fontSize = bigFont
end
local sx, sy, tx, ty
-- Icons
local icon
if c.icon == nil then
icon = gears.surface(gears.surface.load(noicon))
else
icon = gears.surface(c.icon)
end
local iconboxWidth = 0.9 * textboxHeight
local iconboxHeight = iconboxWidth
-- Titles
cr:select_font_face("sans", "italic", "normal")
cr:set_font_face(cr:get_font_face())
cr:set_font_size(fontSize)
text = " - " .. c.class
textWidth = cr:text_extents(text).width
textHeight = cr:text_extents(text).height
local titleboxWidth = textWidth + iconboxWidth
local titleboxHeight = textboxHeight
-- Draw icons
tx = (w - titleboxWidth) / 2
ty = h
sx = iconboxWidth / icon.width
sy = iconboxHeight / icon.height
cr:translate(tx, ty)
cr:scale(sx, sy)
cr:set_source_surface(icon, 0, 0)
cr:paint()
cr:scale(1/sx, 1/sy)
cr:translate(-tx, -ty)
-- Draw titles
tx = tx + iconboxWidth
ty = h + (textboxHeight + textHeight) / 2
cr:set_source_rgba(0,0,0,1)
cr:move_to(tx, ty)
cr:show_text(text)
cr:stroke()
-- Draw previews
local cg = c:geometry()
if cg.width > cg.height then
sx = a * w / cg.width
sy = math.min(sx, a * h / cg.height)
else
sy = a * h / cg.height
sx = math.min(sy, a * h / cg.width)
end
tx = (w - sx * cg.width) / 2
ty = (h - sy * cg.height) / 2
local tmp = gears.surface(c.content)
cr:translate(tx, ty)
cr:scale(sx, sy)
cr:set_source_surface(tmp, 0, 0)
cr:paint()
tmp:finish()
-- Overlays
cr:scale(1/sx, 1/sy)
cr:translate(-tx, -ty)
cr:set_source_rgba(0,0,0,overlay)
cr:rectangle(tx, ty, sx * cg.width, sy * cg.height)
cr:fill()
end
end
preview_live_timer.timeout = 1 / settings.preview_box_fps
preview_live_timer:connect_signal("timeout", function()
preview_widgets[i]:emit_signal("widget::updated")
end)
end
-- Spacers left and right
local spacer = wibox.widget.base.make_widget()
spacer.fit = function(leftSpacer, width, height)
return (W - w * #altTabTable) / 2, preview_wbox.height
end
spacer.draw = function(preview_widget, preview_wbox, cr, width, height) end
--layout
preview_layout = wibox.layout.fixed.horizontal()
preview_layout:add(spacer)
for i = 1, #leftRightTab do
preview_layout:add(preview_widgets[i])
end
preview_layout:add(spacer)
preview_wbox:set_widget(preview_layout)
end
local function clientOpacity(altTabTable, altTabIndex)
if not settings.client_opacity then return end
for i,c in pairs(altTabTable) do
if i == altTabIndex then
c.opacity = 1
elseif applyOpacity then
c.opacity = settings.client_opacity_value
end
end
end
local function cycle(altTabTable, altTabIndex, dir)
-- Switch to next client
altTabIndex = altTabIndex + dir
if altTabIndex > #altTabTable then
altTabIndex = 1 -- wrap around
elseif altTabIndex < 1 then
altTabIndex = #altTabTable -- wrap around
end
altTabTable[altTabIndex].minimized = false
if not settings.preview_box and not settings.client_opacity then
client.focus = altTabTable[altTabIndex]
end
if settings.client_opacity then
clientOpacity(altTabTable, altTabIndex)
end
return altTabIndex
end
local function switch(dir, alt, tab, shift_tab)
altTabTable = {}
local altTabMinimized = {}
local altTabOpacity = {}
-- Get focus history for current tag
local s = mouse.screen;
local idx = 0
local c = awful.client.focus.history.get(s, idx)
while c do
table.insert(altTabTable, c)
table.insert(altTabMinimized, c.minimized)
table.insert(altTabOpacity, c.opacity)
idx = idx + 1
c = awful.client.focus.history.get(s, idx)
end
-- Minimized clients will not appear in the focus history
-- Find them by cycling through all clients, and adding them to the list
-- if not already there.
-- This will preserve the history AND enable you to focus on minimized clients
local t = awful.tag.selected(s)
local all = client.get(s)
for i = 1, #all do
local c = all[i]
local ctags = c:tags();
-- check if the client is on the current tag
local isCurrentTag = false
for j = 1, #ctags do
if t == ctags[j] then
isCurrentTag = true
break
end
end
if isCurrentTag then
-- check if client is already in the history
-- if not, add it
local addToTable = true
for k = 1, #altTabTable do
if altTabTable[k] == c then
addToTable = false
break
end
end
if addToTable then
table.insert(altTabTable, c)
table.insert(altTabMinimized, c.minimized)
table.insert(altTabOpacity, c.opacity)
end
end
end
if #altTabTable == 0 then
return
elseif #altTabTable == 1 then
altTabTable[1].minimized = false
altTabTable[1]:raise()
return
end
-- reset index
altTabIndex = 1
-- preview delay timer
local previewDelay = settings.preview_box_delay / 1000
local previewDelayTimer = timer({timeout = previewDelay})
previewDelayTimer:connect_signal("timeout", function()
preview_wbox.visible = true
previewDelayTimer:stop()
preview(altTabTable, altTabIndex)
end)
previewDelayTimer:start()
preview_live_timer:start()
-- opacity delay timer
local opacityDelay = settings.client_opacity_delay / 1000
local opacityDelayTimer = timer({timeout = opacityDelay})
opacityDelayTimer:connect_signal("timeout", function()
applyOpacity = true
opacityDelayTimer:stop()
clientOpacity(altTabTable, altTabIndex)
end)
opacityDelayTimer:start()
-- Now that we have collected all windows, we should run a keygrabber
-- as long as the user is alt-tabbing:
if(keygrabber.isrunning() == false) then
keygrabber.run(
function (mod, key, event)
-- Stop alt-tabbing when the alt-key is released
if key == alt or key == "Escape" and event == "release" then
preview_wbox.visible = false
applyOpacity = false
preview_live_timer:stop()
previewDelayTimer:stop()
opacityDelayTimer:stop()
if key == "Escape" then
for i,c in pairs(altTabTable) do
c.opacity = altTabOpacity[i]
end
keygrabber.stop()
return
end
-- Raise clients in order to restore history
local c
for i = 1, altTabIndex - 1 do
c = altTabTable[altTabIndex - i]
if not altTabMinimized[i] then
c:raise()
client.focus = c
end
end
-- raise chosen client on top of all
c = altTabTable[altTabIndex]
c:raise()
client.focus = c
-- restore minimized clients
for i = 1, #altTabTable do
if i ~= altTabIndex and altTabMinimized[i] then
altTabTable[i].minimized = true
end
altTabTable[i].opacity = altTabOpacity[i]
end
keygrabber.stop()
-- Move to next client on each Tab-press
elseif (key == tab or key == "Right") and event == "press" then
altTabIndex = cycle(altTabTable, altTabIndex, 1)
-- Move to previous client on Shift-Tab
elseif (key == shift_tab or key == "Left") and event == "press" then
altTabIndex = cycle(altTabTable, altTabIndex, -1)
end
end
)
end
-- switch to next client
altTabIndex = cycle(altTabTable, altTabIndex, dir)
end -- function altTab
return {switch = switch, settings = settings}