i3 improvements wip
[distro-setup] / i3-split-push
1 #!/usr/bin/python3
2
3 # There are only 2 cases where I want single window split containers.
4 #
5 # * just before creating a new window in it.
6 #
7 # * When I want to make 1 window a split container and bring an existing
8 # window into it. In vanilla i3, this is super awkward. Usually, you are
9 # starting out focused on the window you want to move into the
10 # container. So, you focus the window which is to become a container,
11 # split it, focus the window you want to join the container, move it
12 # into that container. 4 actions, totally annoying. Lets simplify this
13 # to 2 actions, a key to say what split we want, then a key to say which
14 # direction to move the current window. Since we have a hook that erases
15 # all single window split containers on focus change, we can consider a
16 # single window split container to indicate the split we want.
17
18 import sys
19 from i3ipc import Connection, Event
20 # for debugging
21 from pprint import pprint
22 import os
23
24
25 def find_parent(i3, window_id):
26 """
27 Find the parent of a given window id
28 """
29
30 def finder(con, parent, gp):
31 if con.id == window_id:
32 return (parent, gp)
33 for node in con.nodes:
34 res = finder(node, con, parent)
35 if res:
36 return res
37 return None
38
39 return finder(i3.get_tree(), None, None)
40
41
42 def set_layout(i3):
43 """
44 Set the layout/split for the currently
45 focused window to either vertical or
46 horizontal, depending on its width/height
47 """
48
49 direction = sys.argv[1]
50
51 win = i3.get_tree().find_focused()
52 parent, gp = find_parent(i3, win.id)
53 layout = parent.layout
54
55 if (parent and gp and len(parent.nodes) == 1):
56 i3.command('focus ' + direction)
57
58 exists = False
59 if os.path.exists('/tmp/iank-i3-no-auto'):
60 exists = True
61 else:
62 open('/tmp/iank-i3-no-auto', 'a')
63
64 if (layout == 'splith'):
65 i3.command('split horizontal')
66 elif (layout == 'splitv'):
67 i3.command('split vertical')
68 elif (layout == 'tabbed'):
69 i3.command('split vertical')
70 i3.command('layout tabbed')
71
72 i3.command('[con_id=%s] focus' % win.id)
73 i3.command('move ' + direction)
74 if (not exists):
75 os.remove('/tmp/iank-i3-no-auto')
76 else:
77 i3.command('move ' + direction)
78
79
80
81 def main():
82 i3 = Connection()
83 set_layout(i3)
84
85
86 if __name__ == "__main__":
87 main()