From 441e7d5041b9f91ddb516eaead83f8975cc74e01 Mon Sep 17 00:00:00 2001 From: Tobias Witek Date: Tue, 23 Jun 2020 20:03:17 +0200 Subject: [PATCH] [core] fix minimize for all modules (nic, traffic, etc.) make it possible to toggle the display state of a widget between "displayed" and "minimized" also for modules that re-create their widgets during each iteration. see #661 --- bumblebee_status/core/module.py | 3 ++- bumblebee_status/core/output.py | 17 +++++++++++------ bumblebee_status/core/widget.py | 4 ++-- tests/core/test_output.py | 20 +++++++++++--------- 4 files changed, 26 insertions(+), 18 deletions(-) diff --git a/bumblebee_status/core/module.py b/bumblebee_status/core/module.py index af708db..23ea4b5 100644 --- a/bumblebee_status/core/module.py +++ b/bumblebee_status/core/module.py @@ -171,7 +171,8 @@ class Module(core.input.Object): """ def add_widget(self, full_text="", name=None): - widget = core.widget.Widget(full_text=full_text, name=name) + widget_id = "{}::{}".format(self.name, len(self.widgets())) + widget = core.widget.Widget(full_text=full_text, name=name, widget_id=widget_id) self.widgets().append(widget) widget.module = self return widget diff --git a/bumblebee_status/core/output.py b/bumblebee_status/core/output.py index 4aaf1e9..929f0f4 100644 --- a/bumblebee_status/core/output.py +++ b/bumblebee_status/core/output.py @@ -142,6 +142,9 @@ class i3(object): core.event.register("draw", self.draw, "statusline") core.event.register("stop", self.draw, "stop") + def content(self): + return self.__content + def theme(self, new_theme=None): if new_theme: self.__theme = new_theme @@ -153,10 +156,10 @@ class i3(object): 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 + widget_id = event["instance"] + + if widget_id in self.__content: + self.__content[widget_id]["minimized"] = not self.__content[widget_id]["minimized"] def draw(self, what, args=None): cb = getattr(self, what) @@ -193,7 +196,7 @@ class i3(object): except: blk.set("min-width", minwidth) blk.set("align", widget.theme("align")) - blk.set("full_text", "\u2026" if widget.minimized else self.__content[widget]) + blk.set("full_text", "\u2026" if self.__content[widget.id]["minimized"] else self.__content[widget.id]["text"]) if widget.get("pango", False): blk.set("markup", "pango") if self.__config.debug(): @@ -236,7 +239,9 @@ class i3(object): module.parameter("interval", self.__config.interval()) ) for widget in module.widgets(): - self.__content[widget] = widget.full_text() + if not widget.id in self.__content: + self.__content[widget.id] = { "minimized": False } + self.__content[widget.id]["text"] = widget.full_text() def statusline(self): blocks = [] diff --git a/bumblebee_status/core/widget.py b/bumblebee_status/core/widget.py index 928aedd..b1c3b4e 100644 --- a/bumblebee_status/core/widget.py +++ b/bumblebee_status/core/widget.py @@ -10,12 +10,12 @@ log = logging.getLogger(__name__) class Widget(util.store.Store, core.input.Object): - def __init__(self, full_text="", name=None): + def __init__(self, full_text="", name=None, widget_id=None): super(Widget, self).__init__() self.__full_text = full_text self.module = None self.name = name - self.minimized = False + self.id = widget_id or self.id @property def module(self): diff --git a/tests/core/test_output.py b/tests/core/test_output.py index 828078c..4e0d905 100644 --- a/tests/core/test_output.py +++ b/tests/core/test_output.py @@ -26,7 +26,6 @@ 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 @@ -34,7 +33,6 @@ 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]) @@ -86,16 +84,20 @@ def test_register_multiple_modules(i3, module_a): def test_toggle_module(i3, module_a, module_b): i3.modules([module_a, module_b]) + i3.update() i3.toggle_minimize({ "instance": module_a.widget().id }) + i3.update() - assert module_a.widget().minimized == True - assert module_b.widget().minimized == False + assert i3.content()[module_a.widget().id]["minimized"] == True - 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 +# 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(