6e28c717fdb5444e10de445d6603f52daccfc150
[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, gp):
13 if con.id == window_id:
14 return (parent, gp)
15 for node in con.nodes:
16 res = finder(node, con, parent)
17 if res:
18 return res
19 return None
20
21 return finder(i3.get_tree(), None, 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 win = i3.get_tree().find_focused()
32 # i don't use gp: todo: revert to original alternating_layout function
33 # which did not return gp.
34 parent, gp = 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, we could first focus the parent, but I think when
43 # layout changes, we expect new windows to be created within that
44 # layout.
45 #
46 # Todo: if the direction we are moving has a split/tabbed container
47 # as a peer to parent, this will move our window into that container
48 # instead of what we want. And in fact, the whole logic below is
49 # incorrect and based on testing which did not realize that fact.
50 #
51 if (parent and parent.type == 'con' and len(parent.nodes) == 1):
52 # https://unix.stackexchange.com/questions/173754/how-to-move-a-window-up-to-the-level-of-its-parent-window-in-i3wm
53 i3.command('mark i3ha')
54 i3.command('focus parent')
55 i3.command('focus parent')
56 i3.command('mark i3hb')
57 i3.command('[con_mark="i3ha"] focus')
58 i3.command('move window to mark i3hb')
59 i3.command('unmark i3ha')
60 i3.command('unmark i3hb')
61 i3.command('layout ' + sys.argv[1])
62
63 def main():
64 i3 = Connection()
65 set_layout(i3)
66
67
68 if __name__ == "__main__":
69 main()