From c6cf2d08b7295a477d89d01535463d18103f6311 Mon Sep 17 00:00:00 2001 From: mathieui Date: Sun, 22 Jul 2018 16:17:06 +0200 Subject: Fix close_all and change_title plugins --- doc/source/dev/events.rst | 4 ++-- plugins/change_title.py | 6 ++---- plugins/close_all.py | 7 +++---- poezio/core/core.py | 10 +++++----- poezio/core/tabs.py | 31 +++++++++++++++++++++++-------- test/test_tabs.py | 19 +++++++++++-------- 6 files changed, 46 insertions(+), 31 deletions(-) diff --git a/doc/source/dev/events.rst b/doc/source/dev/events.rst index 3af469bb..a2e6ad9d 100644 --- a/doc/source/dev/events.rst +++ b/doc/source/dev/events.rst @@ -12,8 +12,8 @@ The following events are poezio-only events, for Slixmpp events, check out - **tab:** :py:class:`~tabs.MucTab` source of the event tab_change - - **old_tab_nb:** :py:class:`int` Old current tab number. - - **new_tab_nb:** :py:class:`int` New current tab number. + - **old_tab:** :py:class:`int` Old current tab. + - **new_tab:** :py:class:`int` New current tab. Triggered whenever the user switches between tabs. diff --git a/plugins/change_title.py b/plugins/change_title.py index 92fcb5c3..e5ed96a8 100644 --- a/plugins/change_title.py +++ b/plugins/change_title.py @@ -8,7 +8,7 @@ import sys class Plugin(BasePlugin): def init(self): - self.on_tab_change(0, self.core.current_tab_nb) + self.on_tab_change(None, new_tab=self.core.tabs.current_tab) self.api.add_event_handler('tab_change', self.on_tab_change) def cleanup(self): @@ -16,8 +16,6 @@ class Plugin(BasePlugin): sys.stdout.write("\x1b]0;poezio\x07") sys.stdout.flush() - def on_tab_change(self, old, new): - new_tab = self.core.get_tab_by_number(new) + def on_tab_change(self, old_tab, new_tab): sys.stdout.write("\x1b]0;{}\x07".format(new_tab.name)) sys.stdout.flush() - diff --git a/plugins/close_all.py b/plugins/close_all.py index 93bcdcfc..faf1d0ea 100644 --- a/plugins/close_all.py +++ b/plugins/close_all.py @@ -26,19 +26,18 @@ class Plugin(BasePlugin): """ /closeall """ - current = self.core.current_tab() + current = self.api.current_tab() if not isinstance(current, (tabs.RosterInfoTab, tabs.MucTab)): self.core.go_to_roster() - current = self.core.current_tab() + current = self.api.current_tab() def filter_func(x): return not isinstance(x, (tabs.RosterInfoTab, tabs.MucTab)) - matching_tabs = list(filter(filter_func, self.core.tabs)) + matching_tabs = list(filter(filter_func, self.core.tabs.get_tabs())) length = len(matching_tabs) for tab in matching_tabs: self.core.close_tab(tab) - self.core.current_tab_nb = current.nb self.api.information('%s tabs closed.' % length, 'Info') self.core.refresh_window() diff --git a/poezio/core/core.py b/poezio/core/core.py index 480bf814..fffb5fb0 100644 --- a/poezio/core/core.py +++ b/poezio/core/core.py @@ -95,7 +95,11 @@ class Core: self.xml_tab = None self.xml_buffer = TextBuffer() - self.tabs = Tabs() + self.plugins_autoloaded = False + self.plugin_manager = PluginManager(self) + self.events = events.EventHandler() + + self.tabs = Tabs(self.events) self.previous_tab_nb = 0 own_nick = config.get('default_nick') @@ -104,10 +108,6 @@ class Core: own_nick = own_nick or 'poezio' self.own_nick = own_nick - self.plugins_autoloaded = False - self.plugin_manager = PluginManager(self) - self.events = events.EventHandler() - self.size = SizeManager(self) # Set to True whenever we consider that we have been disconnected diff --git a/poezio/core/tabs.py b/poezio/core/tabs.py index de5c44b8..e9646959 100644 --- a/poezio/core/tabs.py +++ b/poezio/core/tabs.py @@ -27,6 +27,7 @@ disabled. from typing import List, Dict, Type, Optional, Union from collections import defaultdict from poezio import tabs +from poezio.events import EventHandler class Tabs: @@ -34,11 +35,16 @@ class Tabs: Tab list class """ __slots__ = [ - '_current_index', '_current_tab', '_tabs', '_tab_types', '_tab_names', - '_previous_tab' + '_current_index', + '_current_tab', + '_tabs', + '_tab_types', + '_tab_names', + '_previous_tab', + '_events', ] - def __init__(self): + def __init__(self, events: EventHandler): """ Initialize the Tab List. Even though the list is initially empty, all methods are only valid once append() has been called @@ -53,6 +59,7 @@ class Tabs: self._tab_types = defaultdict( list) # type: Dict[Type[tabs.Tab], List[tabs.Tab]] self._tab_names = dict() # type: Dict[str, tabs.Tab] + self._events = events # type: EventHandler def __len__(self): return len(self._tabs) @@ -78,11 +85,7 @@ class Tabs: """Set the current tab index""" if 0 <= value < len(self._tabs): tab = self._tabs[value] - if not isinstance(tab, tabs.GapTab): - self._store_previous() - self._current_index = tab.nb - self._current_tab = tab - return True + return self.set_current_tab(tab) return False @property @@ -97,6 +100,10 @@ class Tabs: self._store_previous() self._current_index = tab.nb self._current_tab = tab + self._events.trigger( + 'tab_change', + old_tab=self._previous_tab, + new_tab=self._current_tab) return True return False @@ -170,6 +177,10 @@ class Tabs: self._inc_cursor() while isinstance(self.current_tab, tabs.GapTab): self._inc_cursor() + self._events.trigger( + 'tab_change', + old_tab=self._previous_tab, + new_tab=self._current_tab) def prev(self): """Go to the left of the tab list (circular)""" @@ -177,6 +188,10 @@ class Tabs: self._dec_cursor() while isinstance(self.current_tab, tabs.GapTab): self._dec_cursor() + self._events.trigger( + 'tab_change', + old_tab=self._previous_tab, + new_tab=self._current_tab) def append(self, tab: tabs.Tab): """ diff --git a/test/test_tabs.py b/test/test_tabs.py index 6db4c2cc..83f7e6dc 100644 --- a/test/test_tabs.py +++ b/test/test_tabs.py @@ -4,6 +4,9 @@ Tests for the Tabs list module from poezio.core.tabs import Tabs from poezio.tabs import GapTab +from poezio.events import EventHandler + +h = EventHandler() class DummyTab: count = 0 @@ -18,7 +21,7 @@ class DummyTab: def test_append(): DummyTab.reset() - tabs = Tabs() + tabs = Tabs(h) dummy = DummyTab() tabs.append(dummy) assert tabs[0] is dummy @@ -35,7 +38,7 @@ def test_append(): def test_delete(): DummyTab.reset() - tabs = Tabs() + tabs = Tabs(h) dummy = DummyTab() dummy2 = DummyTab() tabs.append(dummy) @@ -50,7 +53,7 @@ def test_delete(): def test_delete_restore_previous(): DummyTab.reset() - tabs = Tabs() + tabs = Tabs(h) dummy = DummyTab() dummy2 = DummyTab() dummy3 = DummyTab() @@ -71,7 +74,7 @@ def test_delete_restore_previous(): def test_delete_other_tab(): DummyTab.reset() - tabs = Tabs() + tabs = Tabs(h) dummy = DummyTab() dummy2 = DummyTab() dummy3 = DummyTab() @@ -91,7 +94,7 @@ def test_delete_other_tab(): def test_insert_and_gaps(): DummyTab.reset() - tabs = Tabs() + tabs = Tabs(h) dummy = DummyTab() dummy2 = DummyTab() dummy3 = DummyTab() @@ -114,7 +117,7 @@ def test_insert_and_gaps(): def test_replace_tabs(): DummyTab.reset() - tabs = Tabs() + tabs = Tabs(h) dummy = DummyTab() dummy2 = DummyTab() dummy3 = DummyTab() @@ -129,7 +132,7 @@ def test_replace_tabs(): def test_prev_next(): DummyTab.reset() - tabs = Tabs() + tabs = Tabs(h) dummy = DummyTab() dummy2 = DummyTab() dummy3 = DummyTab() @@ -155,7 +158,7 @@ def test_prev_next(): def test_set_current(): DummyTab.reset() - tabs = Tabs() + tabs = Tabs(h) dummy = DummyTab() dummy2 = DummyTab() dummy3 = DummyTab() -- cgit v1.2.3