From c91f8da1a09d939494b15975442f142dc40a435e Mon Sep 17 00:00:00 2001 From: Ezri Date: Wed, 12 Feb 2025 13:15:26 -0700 Subject: [PATCH] Added prev/next workspace commands --- sway_context_manager/interface.py | 30 ++++++++++++++++++++------ sway_context_manager/workspace_tree.py | 5 +++++ 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/sway_context_manager/interface.py b/sway_context_manager/interface.py index d0ea29f..50041d5 100644 --- a/sway_context_manager/interface.py +++ b/sway_context_manager/interface.py @@ -62,12 +62,14 @@ class ContextMngrInterface( @dbus_method_async(input_signature="y", result_signature="b") async def move_container(self, user_index: int) -> bool: """Move the focused container to a workspace by its 1-indexed ID in its group.""" - print("Moving focused container to workspace", user_index) - workspace, _ = self.workspace_tree.get_workspace(user_index) - if workspace is None: + try: + print("Moving focused container to workspace", user_index, flush=True) + workspace, _ = self.workspace_tree.get_workspace(user_index) + await workspace.move_container(self.connection) + return True + except Exception as e: + print(e, flush=True) return False - await workspace[0].move_container(self.connection) - return True @dbus_method_async(input_signature="", result_signature="b") async def launch_default(self) -> bool: @@ -79,13 +81,29 @@ class ContextMngrInterface( return True @dbus_method_async(input_signature="y", result_signature="s") - async def get_workspace(self, user_index: int) -> bool: + async def get_workspace(self, user_index: int) -> str: """Get a workspace by its 1-indexed ID in its group.""" workspace = self.workspace_tree.get_workspace(user_index) if workspace is None: return "" return json.dumps(workspace.__json__()) + @dbus_method_async(input_signature="", result_signature="") + async def focus_next_workspace(self) -> None: + """Switch to the next workspace on the focused output.""" + try: + await self.workspace_tree.current_context.active_group.get_relative_workspace(1).focus(self.connection) + except Exception as e: + print(e, flush=True) + + @dbus_method_async(input_signature="", result_signature="") + async def focus_previous_workspace(self) -> None: + """Switch to the previous workspace on the focused output.""" + try: + await self.workspace_tree.current_context.active_group.get_relative_workspace(-1).focus(self.connection) + except Exception as e: + print(e, flush=True) + async def __get_workspace_data(self) -> dict: """Get the workspace tree.""" try: diff --git a/sway_context_manager/workspace_tree.py b/sway_context_manager/workspace_tree.py index 2d9abbc..f5a407e 100644 --- a/sway_context_manager/workspace_tree.py +++ b/sway_context_manager/workspace_tree.py @@ -169,6 +169,11 @@ class WorkspaceGroup: f"output {selector} position {self.position[0]} {self.position[1]} {mode} {transform} enable" ) + def get_relative_workspace(self, offset: int) -> Workspace: + """Returns the workspace offset from the active workspace, looping around if necessary.""" + active_index = self.workspaces.index(self.active_workspace) + return self.workspaces[(active_index + offset) % len(self.workspaces)] + async def validate(self, i3: Connection, workspaces: list[WorkspaceReply]): """Validate that each workspace in the group is assigned to and present on the correct output.""" ouput_name = self.get_output_name(i3)