From 34dadadf90705e50099af8b66aa0afd38cd73560 Mon Sep 17 00:00:00 2001 From: Tobias Witek Date: Tue, 23 Jun 2020 15:51:14 +0200 Subject: [PATCH] [core] re-enable minimize of widgets by default, allow toggling the minimized state of a widget via the middle mouse and draw a single unicode char instead of the actual widget, maintaining all states. fixes #661 --- bumblebee-status | 5 +++++ bumblebee_status/core/module.py | 8 +++++--- bumblebee_status/core/output.py | 8 +++++++- bumblebee_status/core/widget.py | 1 + tests/core/test_module.py | 7 +++++++ tests/core/test_output.py | 33 +++++++++++++++++++++++---------- 6 files changed, 48 insertions(+), 14 deletions(-) diff --git a/bumblebee-status b/bumblebee-status index f995850..21f8c32 100755 --- a/bumblebee-status +++ b/bumblebee-status @@ -19,6 +19,7 @@ import core.module import core.input import core.event +import util.format started = False @@ -110,6 +111,10 @@ def main(): modules.reverse() output.modules(modules) + + if util.format.asbool(config.get("engine.collapsible", True)) == True: + core.input.register(None, core.input.MIDDLE_MOUSE, output.toggle_minimize) + core.event.trigger("start") started = True while True: diff --git a/bumblebee_status/core/module.py b/bumblebee_status/core/module.py index cba01ac..af708db 100644 --- a/bumblebee_status/core/module.py +++ b/bumblebee_status/core/module.py @@ -184,12 +184,14 @@ class Module(core.input.Object): :rtype: bumblebee_status.widget.Widget """ - def widget(self, name=None): - if not name: + def widget(self, name=None, widget_id=None): + if not name and not widget_id: return self.widgets()[0] for w in self.widgets(): - if w.name == name: + if name and w.name == name: + return w + if w.id == widget_id: return w return None diff --git a/bumblebee_status/core/output.py b/bumblebee_status/core/output.py index ab68c6a..4aaf1e9 100644 --- a/bumblebee_status/core/output.py +++ b/bumblebee_status/core/output.py @@ -152,6 +152,12 @@ class i3(object): return self.__modules self.__modules = modules if isinstance(modules, list) else [modules] + def toggle_minimize(self, event): + for module in self.__modules: + widget = module.widget(widget_id=event["instance"]) + if widget: + widget.minimized = not widget.minimized + def draw(self, what, args=None): cb = getattr(self, what) data = cb(args) if args else cb() @@ -187,7 +193,7 @@ class i3(object): except: blk.set("min-width", minwidth) blk.set("align", widget.theme("align")) - blk.set("full_text", self.__content[widget]) + blk.set("full_text", "\u2026" if widget.minimized else self.__content[widget]) if widget.get("pango", False): blk.set("markup", "pango") if self.__config.debug(): diff --git a/bumblebee_status/core/widget.py b/bumblebee_status/core/widget.py index dd9fe1f..928aedd 100644 --- a/bumblebee_status/core/widget.py +++ b/bumblebee_status/core/widget.py @@ -15,6 +15,7 @@ class Widget(util.store.Store, core.input.Object): self.__full_text = full_text self.module = None self.name = name + self.minimized = False @property def module(self): diff --git a/tests/core/test_module.py b/tests/core/test_module.py index ef65c88..68415b7 100644 --- a/tests/core/test_module.py +++ b/tests/core/test_module.py @@ -129,6 +129,13 @@ def test_get_widget_by_name(empty_config, widget_a, widget_b): assert module.widget("i-do-not-exist") == None assert module.widget() == widget_a +def test_get_widget_by_id(empty_config, widget_a, widget_b): + module = SampleModule(config=empty_config, widgets=[widget_a, widget_b]) + + assert module.widget(widget_id=widget_a.id) == widget_a + assert module.widget(widget_id=widget_b.id) == widget_b + assert module.widget(widget_id="i-do-not-exist") == None + def test_default_thresholds(empty_config, widget_a, widget_b): module = SampleModule(config=empty_config, widgets=[widget_a, widget_b]) diff --git a/tests/core/test_output.py b/tests/core/test_output.py index fcd9e22..828078c 100644 --- a/tests/core/test_output.py +++ b/tests/core/test_output.py @@ -25,6 +25,16 @@ def i3(): def module_a(mocker): widget = mocker.MagicMock() widget.full_text.return_value = "test" + widget.id = "a" + widget.minimized = False + return SampleModule(config=core.config.Config([]), widgets=[widget, widget, widget]) + +@pytest.fixture +def module_b(mocker): + widget = mocker.MagicMock() + widget.full_text.return_value = "test" + widget.id = "b" + widget.minimized = False return SampleModule(config=core.config.Config([]), widgets=[widget, widget, widget]) @@ -46,16 +56,6 @@ def block_a(separatorTheme, module_a): theme=separatorTheme, module=module_a, widget=module_a.widget(), ) - -# def setUp(self): -# self.i3 = core.output.i3() -# widget = unittest.mock.MagicMock() -# widget.full_text.return_value = "test" -# self.someModule = SampleModule( -# config=core.config.Config([]), widgets=[widget, widget, widget] -# ) -# self.separator = "***" -# def test_start(i3): all_data = i3.start() data = all_data["blocks"] @@ -83,6 +83,19 @@ def test_register_multiple_modules(i3, module_a): i3.modules([module_a, module_a, module_a]) assert i3.modules() == [module_a, module_a, module_a] +def test_toggle_module(i3, module_a, module_b): + i3.modules([module_a, module_b]) + + i3.toggle_minimize({ "instance": module_a.widget().id }) + + assert module_a.widget().minimized == True + assert module_b.widget().minimized == False + + i3.toggle_minimize({ "instance": module_a.widget().id }) + i3.toggle_minimize({ "instance": module_b.widget().id }) + + assert module_a.widget().minimized == False + assert module_b.widget().minimized == True def test_draw_existing_module(mocker, i3): i3.test_draw = mocker.MagicMock(