forked from BurntSushi/pyndow
/
focus.py
68 lines (50 loc) · 1.53 KB
/
focus.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
import xcb.xproto
import state
import workspace
__stack = []
def get_stack():
return __stack[:]
def add(client):
if client not in __stack:
__stack.append(client)
return True
return False
def remove(client):
if client in __stack:
__stack.remove(client)
def above(client):
assert client in __stack
__stack.remove(client)
__stack.append(client)
def fallback():
def fallbackable(c):
return (c.mapped
and (c.workspace is None or workspace.current() == c.workspace))
stck = [client for client in get_stack() if fallbackable(client)]
# This is *really* important. On occasion, it seems that focus can stay
# with a destroyed window. If this happens to be the last window, and
# there is nothing left in the focus stack, we *must* fall back to the
# root!
if not stck:
state.root_focus()
else:
client = stck[-1]
# If the window isn't alive, pop the stack until we get a good window
if client.is_alive():
client.stack_raise()
client.focus()
else:
remove(client)
fallback()
def focused():
stck = [client for client in get_stack() if client.mapped]
if not stck:
return None
return stck[-1]
def focus_workspace(work):
def mapped_on_workspace(client):
return client.mapped and client.workspace is work
stck = [client for client in get_stack() if mapped_on_workspace(client)]
if not stck:
return None
return stck[-1]