-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathi3-switch-recent
More file actions
executable file
·62 lines (47 loc) · 2.08 KB
/
i3-switch-recent
File metadata and controls
executable file
·62 lines (47 loc) · 2.08 KB
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
#!/usr/bin/env python3
"""
Switch between 2 most recent containers.
This operation ignores scratchpads and explicitly unfocuses them when called to
switch between normal containers.
Ignoring scratchpads is implemented with a blacklist, the window::move
event provides the most authoritive information in container.scratchpad_state.
"""
import i3ipc
SCRATCHPAD_BLACKLIST = set()
WINDOW_STACK = []
SWITCH_KEYBIND = 'Mod4+BackSpace' # see $HOME/.config/i3/config
def switch(conn, event):
binding = '%s+%s' % ('+'.join(event.binding.mods), event.binding.symbol)
if len(WINDOW_STACK) > 1 and binding == SWITCH_KEYBIND:
# if currently focused window is scratchpad hide it. `scratchpad show`
# actually toggles and doesn't just "show".
if conn.get_tree().find_focused().id in SCRATCHPAD_BLACKLIST:
conn.command('scratchpad show')
window_id = WINDOW_STACK.pop()
conn.command('[con_id="%s"] focus' % window_id)
def insert(conn, event):
id_ = event.container.id
if (id_ not in SCRATCHPAD_BLACKLIST
# prevent a stack with the same id twice.
and not (WINDOW_STACK and id_ == WINDOW_STACK[0])):
WINDOW_STACK.insert(0, id_)
# Only keep a list of max two elements. [focused, recent].
del WINDOW_STACK[2:]
def blacklist(conn, event):
global SCRATCHPAD_BLACKLIST
# when a scratchpad is focused it is first moved in the tree. The state is
# then set to fresh. This is the clearest point to identify a scratchpad
# and to blacklist it. Also, a scratchpad can also have multiple clients,
# so add all to the blacklist.
if event.container.scratchpad_state == 'fresh':
SCRATCHPAD_BLACKLIST |= {c.id for c in event.container.leaves()}
def clean(conn, event):
# Prevent the blacklist from growing out of control.
SCRATCHPAD_BLACKLIST.discard(event.container.id)
if __name__ == '__main__':
conn = i3ipc.Connection()
conn.on('binding::run', switch)
conn.on('window::focus', insert)
conn.on('window::move', blacklist)
conn.on('window::close', clean)
conn.main()