URI: 
       tplugins: fix labels plugin FIXME re "just enabled plugin" - electrum - Electrum Bitcoin wallet
  HTML git clone https://git.parazyd.org/electrum
   DIR Log
   DIR Files
   DIR Refs
   DIR Submodules
       ---
   DIR commit 24221f8fcab22c88552d6bf480687ad758d5cd68
   DIR parent cdecc4e3fa2119d052e047ddca98924f9fd606bf
  HTML Author: SomberNight <somber.night@protonmail.com>
       Date:   Mon, 25 May 2020 17:31:13 +0200
       
       plugins: fix labels plugin FIXME re "just enabled plugin"
       
       Diffstat:
         M electrum/gui/qt/__init__.py         |       4 ++--
         M electrum/gui/qt/main_window.py      |       1 +
         M electrum/plugins/cosigner_pool/qt.… |       7 ++++++-
         M electrum/plugins/labels/labels.py   |       8 +++++++-
         M electrum/plugins/labels/qt.py       |      21 +++++++++++++++++----
       
       5 files changed, 33 insertions(+), 8 deletions(-)
       ---
   DIR diff --git a/electrum/gui/qt/__init__.py b/electrum/gui/qt/__init__.py
       t@@ -28,7 +28,7 @@ import signal
        import sys
        import traceback
        import threading
       -from typing import Optional, TYPE_CHECKING
       +from typing import Optional, TYPE_CHECKING, List
        
        
        try:
       t@@ -106,7 +106,7 @@ class ElectrumGui(Logger):
                self.config = config
                self.daemon = daemon
                self.plugins = plugins
       -        self.windows = []
       +        self.windows = []  # type: List[ElectrumWindow]
                self.efilter = OpenFileEventFilter(self.windows)
                self.app = QElectrumApplication(sys.argv)
                self.app.installEventFilter(self.efilter)
   DIR diff --git a/electrum/gui/qt/main_window.py b/electrum/gui/qt/main_window.py
       t@@ -2951,6 +2951,7 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, Logger):
                    p = plugins.toggle(name)
                    cb.setChecked(bool(p))
                    enable_settings_widget(p, name, i)
       +            # note: all enabled plugins will receive this hook:
                    run_hook('init_qt', self.gui_object)
        
                for i, descr in enumerate(plugins.descriptions.values()):
   DIR diff --git a/electrum/plugins/cosigner_pool/qt.py b/electrum/plugins/cosigner_pool/qt.py
       t@@ -43,6 +43,7 @@ from electrum.gui.qt.transaction_dialog import show_transaction, TxDialog
        from electrum.gui.qt.util import WaitingDialog
        
        if TYPE_CHECKING:
       +    from electrum.gui.qt import ElectrumGui
            from electrum.gui.qt.main_window import ElectrumWindow
        
        
       t@@ -101,9 +102,13 @@ class Plugin(BasePlugin):
                self.obj.cosigner_receive_signal.connect(self.on_receive)
                self.keys = []  # type: List[Tuple[str, str, ElectrumWindow]]
                self.cosigner_list = []  # type: List[Tuple[ElectrumWindow, str, bytes, str]]
       +        self._init_qt_received = False
        
            @hook
       -    def init_qt(self, gui):
       +    def init_qt(self, gui: 'ElectrumGui'):
       +        if self._init_qt_received:  # only need/want the first signal
       +            return
       +        self._init_qt_received = True
                for window in gui.windows:
                    self.on_new_window(window)
        
   DIR diff --git a/electrum/plugins/labels/labels.py b/electrum/plugins/labels/labels.py
       t@@ -3,7 +3,7 @@ import hashlib
        import json
        import sys
        import traceback
       -from typing import Union
       +from typing import Union, TYPE_CHECKING
        
        import base64
        
       t@@ -13,6 +13,9 @@ from electrum.i18n import _
        from electrum.util import log_exceptions, ignore_exceptions, make_aiohttp_session
        from electrum.network import Network
        
       +if TYPE_CHECKING:
       +    from electrum.wallet import Abstract_Wallet
       +
        
        class ErrorConnectingServer(Exception):
            def __init__(self, reason: Union[str, Exception] = None):
       t@@ -152,6 +155,9 @@ class LabelsPlugin(BasePlugin):
                self.set_nonce(wallet, response["nonce"] + 1)
                self.on_pulled(wallet)
        
       +    def on_pulled(self, wallet: 'Abstract_Wallet') -> None:
       +        raise NotImplementedError()
       +
            @ignore_exceptions
            @log_exceptions
            async def pull_safe_thread(self, wallet, force):
   DIR diff --git a/electrum/plugins/labels/qt.py b/electrum/plugins/labels/qt.py
       t@@ -1,6 +1,7 @@
        from functools import partial
        import traceback
        import sys
       +from typing import TYPE_CHECKING
        
        from PyQt5.QtCore import QObject, pyqtSignal
        from PyQt5.QtWidgets import (QHBoxLayout, QLabel, QVBoxLayout)
       t@@ -11,6 +12,10 @@ from electrum.gui.qt.util import ThreadedButton, Buttons, EnterButton, WindowMod
        
        from .labels import LabelsPlugin
        
       +if TYPE_CHECKING:
       +    from electrum.gui.qt import ElectrumGui
       +    from electrum.gui.qt.main_window import ElectrumWindow
       +    from electrum.wallet import Abstract_Wallet
        
        class QLabelsSignalObject(QObject):
            labels_changed_signal = pyqtSignal(object)
       t@@ -21,6 +26,7 @@ class Plugin(LabelsPlugin):
            def __init__(self, *args):
                LabelsPlugin.__init__(self, *args)
                self.obj = QLabelsSignalObject()
       +        self._init_qt_received = False
        
            def requires_settings(self):
                return True
       t@@ -63,10 +69,17 @@ class Plugin(LabelsPlugin):
                dialog.show_error(_("Error synchronising labels") + f':\n{repr(exc_info[1])}')
        
            @hook
       -    def load_wallet(self, wallet, window):
       -        # FIXME if the user just enabled the plugin, this hook won't be called
       -        # as the wallet is already loaded, and hence the plugin will be in
       -        # a non-functional state for that window
       +    def init_qt(self, gui: 'ElectrumGui'):
       +        if self._init_qt_received:  # only need/want the first signal
       +            return
       +        self._init_qt_received = True
       +        # If the user just enabled the plugin, the 'load_wallet' hook would not
       +        # get called for already loaded wallets, hence we call it manually for those:
       +        for window in gui.windows:
       +            self.load_wallet(window.wallet, window)
       +
       +    @hook
       +    def load_wallet(self, wallet: 'Abstract_Wallet', window: 'ElectrumWindow'):
                self.obj.labels_changed_signal.connect(window.update_tabs)
                self.start_wallet(wallet)