#!/usr/bin/python3 import sys from i3ipc import Connection, Event def find_parent(i3, window_id): """ Find the parent of a given window id """ def finder(con, parent, gp): if con.id == window_id: return (parent, gp) for node in con.nodes: res = finder(node, con, parent) if res: return res return None return finder(i3.get_tree(), None, None) def set_layout(i3): """ Set the layout/split for the currently focused window to either vertical or horizontal, depending on its width/height """ win = i3.get_tree().find_focused() # i don't use gp: todo: revert to original alternating_layout function # which did not return gp. parent, gp = find_parent(i3, win.id) # We never want to set the layout of a single window container, # there are already keys for that. I don't know why i3 even does # this, it is stupid. So, eliminate single window container if we # are focused on one. # # Alternatively, we could first focus the parent, but I think when # layout changes, we expect new windows to be created within that # layout. # # Todo: if the direction we are moving has a split/tabbed container # as a peer to parent, this will move our window into that container # instead of what we want. And in fact, the whole logic below is # incorrect and based on testing which did not realize that fact. # if (parent and parent.type == 'con' and len(parent.nodes) == 1): # https://unix.stackexchange.com/questions/173754/how-to-move-a-window-up-to-the-level-of-its-parent-window-in-i3wm i3.command('mark i3ha') i3.command('focus parent') i3.command('focus parent') i3.command('mark i3hb') i3.command('[con_mark="i3ha"] focus') i3.command('move window to mark i3hb') i3.command('unmark i3ha') i3.command('unmark i3hb') i3.command('layout ' + sys.argv[1]) def main(): i3 = Connection() set_layout(i3) if __name__ == "__main__": main()