-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.lua
476 lines (422 loc) · 14.2 KB
/
helpers.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
local awful = require("awful")
local gears = require("gears")
local beautiful = require("beautiful")
local xresources = require("beautiful.xresources")
local dpi = xresources.apply_dpi
local wibox = require("wibox")
local naughty = require("naughty")
local helpers = {}
function helpers.contains(_table, _c)
for _, c in ipairs(_table) do
if _c == c then
return true
end
end
return false
end
function helpers.find(rule)
local function matcher(c) return awful.rules.match(c, rule) end
local clients = client.get()
local findex = gears.table.hasitem(clients, client.focus) or 1
local start = gears.math.cycle(#clients, findex + 1)
local matches = {}
for c in awful.client.iterate(matcher, start) do
matches[#matches + 1] = c
end
return matches
end
-- Adds a maximized mask to a screen
function helpers.screen_mask(s, bg)
local mask = wibox({
visible = false,
ontop = true,
type = "splash",
screen = s
})
awful.placement.maximize(mask)
mask.bg = bg
return mask
end
function helpers.custom_shape(cr, width, height)
cr:move_to(0, height / 25)
cr:line_to(height / 25, 0)
cr:line_to(width, 0)
cr:line_to(width, height - height / 25)
cr:line_to(width - height / 25, height)
cr:line_to(0, height)
cr:close_path()
end
-- Resize gaps on the fly
helpers.resize_gaps = function(amt)
local t = awful.screen.focused().selected_tag
t.gap = t.gap + tonumber(amt)
awful.layout.arrange(awful.screen.focused())
end
-- Resize padding on the fly
helpers.resize_padding = function(amt)
local s = awful.screen.focused()
local l = s.padding.left
local r = s.padding.right
local t = s.padding.top
local b = s.padding.bottom
s.padding = {
left = l + amt,
right = r + amt,
top = t + amt,
bottom = b + amt
}
awful.layout.arrange(awful.screen.focused())
end
-- Create rounded rectangle shape (in one line)
helpers.rrect = function(radius)
return function(cr, width, height)
gears.shape.rounded_rect(cr, width, height, radius)
end
end
-- Create pi
helpers.pie = function(width, height, start_angle, end_angle, radius)
return function(cr)
gears.shape.pie(cr, width, height, start_angle, end_angle, radius)
end
end
-- Create parallelogram
helpers.prgram = function(height, base)
return function(cr, width)
gears.shape.parallelogram(cr, width, height, base)
end
end
-- Create partially rounded rect
helpers.prrect = function(radius, tl, tr, br, bl)
return function(cr, width, height)
gears.shape.partially_rounded_rect(cr, width, height, tl, tr, br, bl,
radius)
end
end
-- Create rounded bar
helpers.rbar = function(width, height)
return function(cr)
gears.shape.rounded_bar(cr, width, height)
end
end
-- Markup helper
function helpers.colorize_text(txt, fg)
return "<span foreground='" .. "#ffffff" .. "'>" .. txt .. "</span>"
end
function helpers.client_menu_toggle()
local instance = nil
return function()
if instance and instance.wibox.visible then
instance:hide()
instance = nil
else
instance = awful.menu.clients({theme = {width = dpi(250)}})
end
end
end
-- Escapes a string so that it can be displayed inside pango markup
-- tags. Modified from:
-- https://github.com/kernelsauce/turbo/blob/master/turbo/escape.lua
function helpers.pango_escape(s)
return (string.gsub(s, "[&<>]",
{["&"] = "&", ["<"] = "<", [">"] = ">"}))
end
function helpers.vertical_pad(height)
return wibox.widget {
forced_height = height,
layout = wibox.layout.fixed.vertical
}
end
function helpers.horizontal_pad(width)
return wibox.widget {
forced_width = width,
layout = wibox.layout.fixed.horizontal
}
end
-- Maximizes client and also respects gaps
function helpers.maximize(c)
c.maximized = not c.maximized
if c.maximized then
awful.placement.maximize(c, {
honor_padding = true,
honor_workarea = true,
margins = beautiful.useless_gap * 2
})
end
c:raise()
end
function helpers.move_to_edge(c, direction)
-- local workarea = awful.screen.focused().workarea
-- local client_geometry = c:geometry()
if direction == "up" then
local old_x = c:geometry().x
awful.placement.top(c, {
honor_padding = true,
honor_workarea = true,
honor_padding = true
})
c.x = old_x
-- c:geometry({ nil, y = workarea.y + beautiful.screen_margin * 2, nil, nil })
elseif direction == "down" then
local old_x = c:geometry().x
awful.placement.bottom(c, {
honor_padding = true,
honor_workarea = true,
honor_padding = true
})
c.x = old_x
-- c:geometry({ nil, y = workarea.height + workarea.y - client_geometry.height - beautiful.screen_margin * 2 - beautiful.border_width * 2, nil, nil })
elseif direction == "left" then
local old_y = c:geometry().y
awful.placement.left(c, {
honor_padding = true,
honor_workarea = true,
honor_padding = true
})
c.y = old_y
-- c:geometry({ x = workarea.x + beautiful.screen_margin * 2, nil, nil, nil })
elseif direction == "right" then
local old_y = c:geometry().y
awful.placement.right(c, {
honor_padding = true,
honor_workarea = true,
honor_padding = true
})
c.y = old_y
-- c:geometry({ x = workarea.width + workarea.x - client_geometry.width - beautiful.screen_margin * 2 - beautiful.border_width * 2, nil, nil, nil })
end
end
local double_tap_timer = nil
function helpers.single_double_tap(single_tap_function, double_tap_function)
if double_tap_timer then
double_tap_timer:stop()
double_tap_timer = nil
double_tap_function()
-- naughty.notify({text = "We got a double tap"})
return
end
double_tap_timer = gears.timer.start_new(0.20, function()
double_tap_timer = nil
-- naughty.notify({text = "We got a single tap"})
if single_tap_function then single_tap_function() end
return false
end)
end
-- Used as a custom command in rofi to move a window into the current tag
-- instead of following it.
-- Rofi has access to the X window id of the client.
function helpers.rofi_move_client_here(window)
local win = function(c) return awful.rules.match(c, {window = window}) end
for c in awful.client.iterate(win) do
c.minimized = false
c:move_to_tag(mouse.screen.selected_tag)
client.focus = c
c:raise()
end
end
-- Add a hover cursor to a widget by changing the cursor on
-- mouse::enter and mouse::leave
-- You can find the names of the available cursors by opening any
-- cursor theme and looking in the "cursors folder"
-- For example: "hand1" is the cursor that appears when hovering over
-- links
function helpers.add_hover_cursor(w, hover_cursor)
local original_cursor = "left_ptr"
w:connect_signal("mouse::enter", function()
local w = _G.mouse.current_wibox
if w then w.cursor = hover_cursor end
end)
w:connect_signal("mouse::leave", function()
local w = _G.mouse.current_wibox
if w then w.cursor = original_cursor end
end)
end
-- Tag back and forth:
-- If you try to focus the tag you are already at, go back to the previous tag.
-- Useful for quick switching after for example checking an incoming chat
-- message at tag 2 and coming back to your work at tag 1 with the same
-- keypress.
-- Also focuses urgent clients if they exist in the tag. This fixes the issue
-- (visual mismatch) where after switching to a tag which includes an urgent
-- client, the urgent client is unfocused but still covers all other windows
-- (even the currently focused window).
function helpers.tag_back_and_forth(tag_index)
local s = mouse.screen
local tag = s.tags[tag_index]
if tag then
if tag == s.selected_tag then
awful.tag.history.restore()
else
tag:view_only()
end
local urgent_clients = function(c)
return awful.rules.match(c, {urgent = true, first_tag = tag})
end
for c in awful.client.iterate(urgent_clients) do
client.focus = c
c:raise()
end
end
end
-- Resize DWIM (Do What I Mean)
-- Resize client or factor
-- Constants --
local floating_resize_amount = dpi(20)
local tiling_resize_factor = 0.05
---------------
function helpers.resize_dwim(c, direction)
if awful.layout.get(mouse.screen) == awful.layout.suit.floating or
(c and c.floating) then
if direction == "up" then
c:relative_move(0, 0, 0, -floating_resize_amount)
elseif direction == "down" then
c:relative_move(0, 0, 0, floating_resize_amount)
elseif direction == "left" then
c:relative_move(0, 0, -floating_resize_amount, 0)
elseif direction == "right" then
c:relative_move(0, 0, floating_resize_amount, 0)
end
else
if direction == "up" then
awful.client.incwfact(-tiling_resize_factor)
elseif direction == "down" then
awful.client.incwfact(tiling_resize_factor)
elseif direction == "left" then
awful.tag.incmwfact(-tiling_resize_factor)
elseif direction == "right" then
awful.tag.incmwfact(tiling_resize_factor)
end
end
end
-- Move client to screen edge, respecting the screen workarea
function helpers.move_to_edge(c, direction)
local workarea = awful.screen.focused().workarea
if direction == "up" then
c:geometry({nil, y = workarea.y + beautiful.useless_gap * 2, nil, nil})
elseif direction == "down" then
c:geometry({
nil,
y = workarea.height + workarea.y - c:geometry().height -
beautiful.useless_gap * 2 - beautiful.border_width * 2,
nil,
nil
})
elseif direction == "left" then
c:geometry({x = workarea.x + beautiful.useless_gap * 2, nil, nil, nil})
elseif direction == "right" then
c:geometry({
x = workarea.width + workarea.x - c:geometry().width -
beautiful.useless_gap * 2 - beautiful.border_width * 2,
nil,
nil,
nil
})
end
end
-- Move client DWIM (Do What I Mean)
-- Move to edge if the client / layout is floating
-- Swap by index if maximized
-- Else swap client by direction
function helpers.move_client_dwim(c, direction)
if c.floating or
(awful.layout.get(mouse.screen) == awful.layout.suit.floating) then
helpers.move_to_edge(c, direction)
elseif awful.layout.get(mouse.screen) == awful.layout.suit.max then
if direction == "up" or direction == "left" then
awful.client.swap.byidx(-1, c)
elseif direction == "down" or direction == "right" then
awful.client.swap.byidx(1, c)
end
else
awful.client.swap.bydirection(direction, c, nil)
end
end
-- Make client floating and snap to the desired edge
function helpers.float_and_edge_snap(c, direction)
-- if not c.floating then
-- c.floating = true
-- end
naughty.notify({text = "double tap"})
c.floating = true
local workarea = awful.screen.focused().workarea
if direction == "up" then
local axis = 'horizontally'
local f = awful.placement.scale + awful.placement.top +
(axis and awful.placement['maximize_' .. axis] or nil)
local geo = f(client.focus, {
honor_padding = true,
honor_workarea = true,
to_percent = 0.5
})
elseif direction == "down" then
local axis = 'horizontally'
local f = awful.placement.scale + awful.placement.bottom +
(axis and awful.placement['maximize_' .. axis] or nil)
local geo = f(client.focus, {
honor_padding = true,
honor_workarea = true,
to_percent = 0.5
})
elseif direction == "left" then
local axis = 'vertically'
local f = awful.placement.scale + awful.placement.left +
(axis and awful.placement['maximize_' .. axis] or nil)
local geo = f(client.focus, {
honor_padding = true,
honor_workarea = true,
to_percent = 0.5
})
elseif direction == "right" then
local axis = 'vertically'
local f = awful.placement.scale + awful.placement.right +
(axis and awful.placement['maximize_' .. axis] or nil)
local geo = f(client.focus, {
honor_padding = true,
honor_workarea = true,
to_percent = 0.5
})
end
end
-- Rounds a number to any number of decimals
function helpers.round(number, decimals)
local power = 10 ^ decimals
return math.floor(number * power) / power
end
function helpers.fake_escape()
root.fake_input('key_press', "Escape")
root.fake_input('key_release', "Escape")
end
function helpers.run_or_raise(match, move, spawn_cmd, spawn_args)
local matcher = function(c) return awful.rules.match(c, match) end
-- Find and raise
local found = false
for c in awful.client.iterate(matcher) do
found = true
c.minimized = false
if move then
c:move_to_tag(mouse.screen.selected_tag)
client.focus = c
c:raise()
else
c:jump_to()
end
break
end
-- Spawn if not found
if not found then awful.spawn(spawn_cmd, spawn_args) end
end
function helpers.pad(size)
local str = ""
for i = 1, size do str = str .. " " end
local pad = wibox.widget.textbox(str)
return pad
end
function helpers.float_and_resize(c, width, height)
c.width = width
c.height = height
awful.placement.centered(c, {honor_workarea = true, honor_padding = true})
awful.client.property.set(c, 'floating_geometry', c:geometry())
c.floating = true
c:raise()
end
return helpers
-- EOF ------------------------------------------------------------------------