From 57064dcf549955bf6c3ea311713ca9ba018aa619 Mon Sep 17 00:00:00 2001 From: Tobias Witek Date: Sat, 21 Oct 2017 13:06:36 +0200 Subject: [PATCH] [core/engine] Option to disable workspace wrap Add a new parameter engine.workspacewrap (default to true) that, if set to false, makes the workspace change via mouse wheel stop when hitting the first/last workspace of an output (identical to how i3wm bar itself behaves). fixes #188 --- bumblebee/engine.py | 47 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 43 insertions(+), 4 deletions(-) diff --git a/bumblebee/engine.py b/bumblebee/engine.py index 7abc28c..e281aeb 100644 --- a/bumblebee/engine.py +++ b/bumblebee/engine.py @@ -1,6 +1,7 @@ """Core application engine""" import os +import json import pkgutil import logging import importlib @@ -118,13 +119,51 @@ class Engine(object): self._current_module = None if bumblebee.util.asbool(config.get("engine.workspacewheel", "true")): - self.input.register_callback(None, bumblebee.input.WHEEL_UP, - "i3-msg workspace prev_on_output") - self.input.register_callback(None, bumblebee.input.WHEEL_DOWN, - "i3-msg workspace next_on_output") + if bumblebee.util.asbool(config.get("engine.workspacewrap", "true")): + self.input.register_callback(None, bumblebee.input.WHEEL_UP, + "i3-msg workspace prev_on_output") + self.input.register_callback(None, bumblebee.input.WHEEL_DOWN, + "i3-msg workspace next_on_output") + else: + self.input.register_callback(None, bumblebee.input.WHEEL_UP, + cmd=self._prev_workspace) + self.input.register_callback(None, bumblebee.input.WHEEL_DOWN, + cmd=self._next_workspace) self.input.start() + def _prev_workspace(self, event): + self._change_workspace(-1) + + def _next_workspace(self, event): + self._change_workspace(1) + + def _change_workspace(self, amount): + try: + active_output = None + active_index = -1 + output_workspaces = {} + data = json.loads(bumblebee.util.execute("i3-msg -t get_workspaces")) + for workspace in data: + output_workspaces.setdefault(workspace["output"], []).append(workspace) + if workspace["focused"]: + active_output = workspace["output"] + active_index = len(output_workspaces[workspace["output"]]) - 1 + if (active_index + amount) < 0: + return + if (active_index + amount) >= len(output_workspaces[active_output]): + return + + while amount != 0: + if amount > 0: + bumblebee.util.execute("i3-msg workspace next_on_output") + amount = amount - 1 + if amount < 0: + bumblebee.util.execute("i3-msg workspace prev_on_output") + amount = amount + 1 + except Exception as e: + log.error("failed to change workspace: {}".format(e)) + def modules(self): return self._modules