forked from qtile/qtile-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
traverse.py
93 lines (72 loc) · 2.43 KB
/
traverse.py
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
"""
This plugin exports four functions - up, down, left and right - that when called will
move window focus to the first window in that general direction. Focussing is based
entirely on position and geometry, so is independent of screens, layouts and whether
windows are floating or tiled. It can also move focus to and from empty screens.
Example usage:
import traverse
keys.extend([
Key([mod], 'k', lazy.function(traverse.up)),
Key([mod], 'j', lazy.function(traverse.down)),
Key([mod], 'h', lazy.function(traverse.left)),
Key([mod], 'l', lazy.function(traverse.right)),
])
Qtile versions known to work: 0.16 - 0.18
"""
from libqtile.config import Screen
def up(qtile):
_focus_window(qtile, -1, "y")
def down(qtile):
_focus_window(qtile, 1, "y")
def left(qtile):
_focus_window(qtile, -1, "x")
def right(qtile):
_focus_window(qtile, 1, "x")
def _focus_window(qtile, dir, axis):
win = None
win_wide = None
dist = 10000
dist_wide = 10000
cur = qtile.current_window
if not cur:
cur = qtile.current_screen
if axis == "x":
dim = "width"
band_axis = "y"
band_dim = "height"
cur_pos = cur.x
band_min = cur.y
band_max = cur.y + cur.height
else:
dim = "height"
band_axis = "x"
band_dim = "width"
band_min = cur.x
cur_pos = cur.y
band_max = cur.x + cur.width
cur_pos += getattr(cur, dim) / 2
windows = [w for g in qtile.groups if g.screen for w in g.windows]
windows.extend([s for s in qtile.screens if not s.group.windows])
if cur in windows:
windows.remove(cur)
for w in windows:
if isinstance(w, Screen) or not w.minimized and w.is_visible():
pos = getattr(w, axis) + getattr(w, dim) / 2
gap = dir * (pos - cur_pos)
if gap > 5:
band_pos = getattr(w, band_axis) + getattr(w, band_dim) / 2
if band_min < band_pos < band_max:
if gap < dist:
dist = gap
win = w
else:
if gap < dist_wide:
dist_wide = gap
win_wide = w
if not win:
win = win_wide
if win:
qtile.focus_screen(win.group.screen.index)
win.group.focus(win, True)
if not isinstance(win, Screen):
win.focus(False)