i3-companion: improve a bit code for new workspaces

This commit is contained in:
Vincent Bernat 2021-07-05 19:54:21 +02:00
parent c5b2baca1e
commit b8bfc9d586

View file

@ -113,19 +113,24 @@ def workspace_rename(i3, event):
def new_workspace(i3, event): def new_workspace(i3, event):
"""Create a new workspace.""" """Create a new workspace and optionally move a window to it."""
if event.payload not in {"new-workspace", "move-to-new-workspace"}: if event.payload not in {"new-workspace", "move-to-new-workspace"}:
return return
# Get the currently focused window
if event.payload == "move-to-new-workspace": if event.payload == "move-to-new-workspace":
current = i3.get_tree().find_focused() current = i3.get_tree().find_focused()
if not current:
return
# Create a new workspace
workspace_nums = {w.num for w in i3.get_workspaces()} workspace_nums = {w.num for w in i3.get_workspaces()}
max_num = max(workspace_nums) max_num = max(workspace_nums)
available = (set(range(1, max_num + 2)) - workspace_nums).pop() available = (set(range(1, max_num + 2)) - workspace_nums).pop()
logger.info(f'create new workspace number {available}') logger.info(f'create new workspace number {available}')
i3.command(f'workspace number "{available}"') i3.command(f'workspace number "{available}"')
# Move the window to this workspace
if event.payload == "move-to-new-workspace": if event.payload == "move-to-new-workspace":
current.command(f'move container to workspace number "{available}"') current.command(f'move container to workspace number "{available}"')