89780d17be88643f7d6dc34f78738e3dc0a0b7ec
[distro-setup] / i3-set-layout
1 #!/usr/bin/python3
2
3 import sys
4 from i3ipc import Connection, Event
5
6
7 def find_parent(i3, window_id):
8 """
9 Find the parent of a given window id
10 """
11
12 def finder(con, parent):
13 if con.id == window_id:
14 return parent
15 for node in con.nodes:
16 res = finder(node, con)
17 if res:
18 return res
19 return None
20
21 return finder(i3.get_tree(), None)
22
23
24 def set_layout(i3):
25 """
26 Set the layout/split for the currently
27 focused window to either vertical or
28 horizontal, depending on its width/height
29 """
30
31
32
33 win = i3.get_tree().find_focused()
34 parent = find_parent(i3, win.id)
35
36
37 # We never want to set the layout of a single window container,
38 # there are already keys for that. I don't know why i3 even does
39 # this, it is stupid. So, eliminate single window container if we
40 # are focused on one.
41 #
42 # Alternatively, it could first focus the parent, but I think when
43 # layout changes, we expect new windows to be created within that
44 # layout.
45 if (parent and len(parent.nodes) == 1):
46 gp = find_parent(i3, parent.id)
47 if (gp.nodes[0].id == parent.id):
48 if (gp.layout == 'splitv'):
49 i3.command('move down')
50 else: # splith or tabbed
51 i3.command('move right')
52 else:
53 if (gp.layout == 'splitv'):
54 i3.command('move up')
55 else:
56 i3.command('move left')
57 i3.command('layout ' + sys.argv[1])
58
59 def main():
60 i3 = Connection()
61 set_layout(i3)
62
63
64 if __name__ == "__main__":
65 main()