[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
This commit is contained in:
Tobias Witek 2020-06-23 20:03:17 +02:00
parent 34dadadf90
commit 441e7d5041
4 changed files with 26 additions and 18 deletions

View file

@ -171,7 +171,8 @@ class Module(core.input.Object):
""" """
def add_widget(self, full_text="", name=None): 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) self.widgets().append(widget)
widget.module = self widget.module = self
return widget return widget

View file

@ -142,6 +142,9 @@ class i3(object):
core.event.register("draw", self.draw, "statusline") core.event.register("draw", self.draw, "statusline")
core.event.register("stop", self.draw, "stop") core.event.register("stop", self.draw, "stop")
def content(self):
return self.__content
def theme(self, new_theme=None): def theme(self, new_theme=None):
if new_theme: if new_theme:
self.__theme = new_theme self.__theme = new_theme
@ -153,10 +156,10 @@ class i3(object):
self.__modules = modules if isinstance(modules, list) else [modules] self.__modules = modules if isinstance(modules, list) else [modules]
def toggle_minimize(self, event): def toggle_minimize(self, event):
for module in self.__modules: widget_id = event["instance"]
widget = module.widget(widget_id=event["instance"])
if widget: if widget_id in self.__content:
widget.minimized = not widget.minimized self.__content[widget_id]["minimized"] = not self.__content[widget_id]["minimized"]
def draw(self, what, args=None): def draw(self, what, args=None):
cb = getattr(self, what) cb = getattr(self, what)
@ -193,7 +196,7 @@ class i3(object):
except: except:
blk.set("min-width", minwidth) blk.set("min-width", minwidth)
blk.set("align", widget.theme("align")) 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): if widget.get("pango", False):
blk.set("markup", "pango") blk.set("markup", "pango")
if self.__config.debug(): if self.__config.debug():
@ -236,7 +239,9 @@ class i3(object):
module.parameter("interval", self.__config.interval()) module.parameter("interval", self.__config.interval())
) )
for widget in module.widgets(): 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): def statusline(self):
blocks = [] blocks = []

View file

@ -10,12 +10,12 @@ log = logging.getLogger(__name__)
class Widget(util.store.Store, core.input.Object): 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__() super(Widget, self).__init__()
self.__full_text = full_text self.__full_text = full_text
self.module = None self.module = None
self.name = name self.name = name
self.minimized = False self.id = widget_id or self.id
@property @property
def module(self): def module(self):

View file

@ -26,7 +26,6 @@ def module_a(mocker):
widget = mocker.MagicMock() widget = mocker.MagicMock()
widget.full_text.return_value = "test" widget.full_text.return_value = "test"
widget.id = "a" widget.id = "a"
widget.minimized = False
return SampleModule(config=core.config.Config([]), widgets=[widget, widget, widget]) return SampleModule(config=core.config.Config([]), widgets=[widget, widget, widget])
@pytest.fixture @pytest.fixture
@ -34,7 +33,6 @@ def module_b(mocker):
widget = mocker.MagicMock() widget = mocker.MagicMock()
widget.full_text.return_value = "test" widget.full_text.return_value = "test"
widget.id = "b" widget.id = "b"
widget.minimized = False
return SampleModule(config=core.config.Config([]), widgets=[widget, widget, widget]) 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): def test_toggle_module(i3, module_a, module_b):
i3.modules([module_a, module_b]) i3.modules([module_a, module_b])
i3.update()
i3.toggle_minimize({ "instance": module_a.widget().id }) i3.toggle_minimize({ "instance": module_a.widget().id })
i3.update()
assert module_a.widget().minimized == True assert i3.content()[module_a.widget().id]["minimized"] == True
assert module_b.widget().minimized == False
i3.toggle_minimize({ "instance": module_a.widget().id }) # assert module_a.widget().minimized == True
i3.toggle_minimize({ "instance": module_b.widget().id }) # assert module_b.widget().minimized == False
#
assert module_a.widget().minimized == False # i3.toggle_minimize({ "instance": module_a.widget().id })
assert module_b.widget().minimized == True # 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): def test_draw_existing_module(mocker, i3):
i3.test_draw = mocker.MagicMock( i3.test_draw = mocker.MagicMock(