forked from bitcoinafterlife/bal-electrum-plugin
fix(qt): auto-close Plugins manager, read-only field styling, RLock-safe heirs persistence
GUI / plugin lifecycle: - Auto-close Electrum's native 'Electrum Plugins' manager dialog after the BAL plugin is hot-enabled. Electrum 4.7.x no longer calls the old init_qt hook, so the close is now triggered from the create_status_bar, init_menubar and load_wallet hooks (fired when reload_windows() recreates the window). - Robust dialog matching (isinstance / class name / localized window title) to cope with zipimport module-identity mismatches. - Robust dismissal of the modal dialog (reject()/done()/close()) with a retry schedule [400, 800, 1500] ms; if it still cannot be closed, fall back to bringing it to the front (showNormal/raise_/activateWindow) so it never lingers hidden in the background. Counting only visible top-levels avoids treating an already-closed dialog as still open. Read-only field styling: - Paint the locked Delivery time / Check Alive date editors and the mining-fee spinbox with a light-grey background (#f0f0f0) so the user can see at a glance that they are not editable outside the 'Build your will' wizard; the styling is cleared when the fields are made editable again. Pickle/RLock crash on 'Build will': - heirs.save() now sanitises the heirs mapping via _json_safe() before handing it to json_db.put(), which deep-copies the value. A live runtime object (holding a threading.RLock) slipping into an heir value previously raised 'TypeError: cannot pickle _thread.RLock object' and aborted the task; such values are now coerced to str and logged with their path. - init_heirs_to_locktime() coerces the locktime to a plain serializable scalar. - log_error() now accepts both a sys.exc_info() triple and a single exception instance, fixing the secondary 'TypeError object is not subscriptable' that masked the real error.
This commit is contained in:
@@ -61,6 +61,154 @@ class Plugin(BalPlugin):
|
||||
_logger.error("Error loading plugin {}".format(e))
|
||||
raise e
|
||||
|
||||
@staticmethod
|
||||
def _close_plugins_manager_dialog():
|
||||
"""Close Electrum's "Electrum Plugins" manager dialog if it is open.
|
||||
|
||||
This is the native Electrum ``PluginsDialog`` (a ``WindowModalDialog``);
|
||||
it is not owned by this plugin, so we locate it among the application's
|
||||
top-level widgets and close it. Failures are non-fatal: leaving the
|
||||
dialog open is harmless, so we never propagate exceptions from here.
|
||||
"""
|
||||
Plugin._handle_plugins_manager_dialog(attempt=0)
|
||||
|
||||
@staticmethod
|
||||
def _find_plugins_manager_dialogs():
|
||||
"""Return the open Electrum "Electrum Plugins" manager dialog(s).
|
||||
|
||||
The match is intentionally permissive: when our plugin is loaded from a
|
||||
zip (``electrum_external_plugins``), ``isinstance`` against the imported
|
||||
``PluginsDialog`` class can fail due to differing module identities, so
|
||||
we also match by class name and by window title (including the localized
|
||||
title, since the user runs Electrum under a non-English locale).
|
||||
"""
|
||||
try:
|
||||
from PyQt6.QtWidgets import QApplication
|
||||
except Exception:
|
||||
return []
|
||||
try:
|
||||
from electrum.gui.qt.plugins_dialog import PluginsDialog
|
||||
except Exception:
|
||||
PluginsDialog = None
|
||||
app = QApplication.instance()
|
||||
if app is None:
|
||||
return []
|
||||
# Accept both the English title and the translated one. We cannot rely
|
||||
# only on _() because the dialog object may have been built with a
|
||||
# different gettext binding than ours when loaded from a zip.
|
||||
titles = {"Electrum Plugins"}
|
||||
try:
|
||||
titles.add(_("Electrum Plugins"))
|
||||
except Exception:
|
||||
pass
|
||||
found = []
|
||||
for w in app.topLevelWidgets():
|
||||
try:
|
||||
is_match = False
|
||||
if PluginsDialog is not None and isinstance(w, PluginsDialog):
|
||||
is_match = True
|
||||
elif type(w).__name__ == "PluginsDialog":
|
||||
is_match = True
|
||||
elif w.windowTitle() in titles:
|
||||
is_match = True
|
||||
if not is_match:
|
||||
continue
|
||||
# Only count it as "open" if it is actually visible: after a
|
||||
# successful close()/reject() the QDialog object still lives in
|
||||
# topLevelWidgets() but becomes invisible, so filtering by
|
||||
# isVisible() is what tells "still open" from "already closed".
|
||||
visible = w.isVisible()
|
||||
_logger.info(
|
||||
"plugins manager dialog match: cls={} title={!r} "
|
||||
"visible={}".format(
|
||||
type(w).__name__, w.windowTitle(), visible
|
||||
)
|
||||
)
|
||||
if visible:
|
||||
found.append(w)
|
||||
except Exception as e:
|
||||
_logger.debug("inspecting top-level widget failed: {}".format(e))
|
||||
return found
|
||||
|
||||
@staticmethod
|
||||
def _try_dismiss_dialog(d):
|
||||
"""Attempt to dismiss a (possibly modal) dialog as robustly as we can.
|
||||
|
||||
A ``PluginsDialog`` is opened with ``exec()`` (a nested, *application-
|
||||
modal* event loop). Inside such a loop a plain ``close()`` is not
|
||||
always honoured, so we also try ``reject()`` / ``done()`` which end the
|
||||
modal loop directly. Any of these may fail depending on Qt state, so
|
||||
each is guarded independently.
|
||||
"""
|
||||
try:
|
||||
from PyQt6.QtWidgets import QDialog
|
||||
except Exception:
|
||||
QDialog = None
|
||||
# 1) reject() / done(): the reliable way to end an exec() modal loop.
|
||||
if QDialog is not None and isinstance(d, QDialog):
|
||||
try:
|
||||
d.reject()
|
||||
except Exception as e:
|
||||
_logger.debug("reject() failed: {}".format(e))
|
||||
try:
|
||||
d.done(QDialog.DialogCode.Rejected)
|
||||
except Exception as e:
|
||||
_logger.debug("done() failed: {}".format(e))
|
||||
# 2) close(): covers non-QDialog top-levels and is a harmless extra.
|
||||
try:
|
||||
d.close()
|
||||
except Exception as e:
|
||||
_logger.debug("could not close plugins dialog: {}".format(e))
|
||||
|
||||
@staticmethod
|
||||
def _handle_plugins_manager_dialog(attempt=0):
|
||||
"""Try to auto-close the manager dialog; retry a few times.
|
||||
|
||||
Enabling the plugin happens while Electrum's ``PluginsDialog`` may still
|
||||
be running its own modal event loop, so a single ``close()`` can be
|
||||
ignored. We retry on a short schedule and, if it is still open after the
|
||||
last attempt, fall back to bringing it to the front so the user notices
|
||||
it and closes it themselves (it must not linger in the background).
|
||||
"""
|
||||
try:
|
||||
from PyQt6.QtCore import QTimer
|
||||
except Exception:
|
||||
QTimer = None
|
||||
# Schedule of retry delays (ms) measured from each call.
|
||||
retry_delays = [400, 800, 1500]
|
||||
dialogs = Plugin._find_plugins_manager_dialogs()
|
||||
_logger.info(
|
||||
"auto-close plugins dialog: attempt={} found={}".format(
|
||||
attempt, len(dialogs)
|
||||
)
|
||||
)
|
||||
for d in dialogs:
|
||||
Plugin._try_dismiss_dialog(d)
|
||||
# Re-check: anything still visible?
|
||||
still_open = Plugin._find_plugins_manager_dialogs()
|
||||
if not still_open:
|
||||
_logger.info("plugins dialog closed successfully")
|
||||
return
|
||||
if attempt < len(retry_delays) and QTimer is not None:
|
||||
QTimer.singleShot(
|
||||
retry_delays[attempt],
|
||||
lambda: Plugin._handle_plugins_manager_dialog(attempt + 1),
|
||||
)
|
||||
return
|
||||
# Final fallback: we could not close it -> at least raise it to the
|
||||
# front so it does not stay hidden in the background.
|
||||
_logger.info(
|
||||
"could not close plugins dialog after {} attempts; "
|
||||
"bringing it to front".format(attempt + 1)
|
||||
)
|
||||
for d in still_open:
|
||||
try:
|
||||
d.showNormal()
|
||||
d.raise_()
|
||||
d.activateWindow()
|
||||
except Exception as e:
|
||||
_logger.debug("could not raise plugins dialog: {}".format(e))
|
||||
|
||||
def _setup_window(self, window, *, load_open_wallet):
|
||||
"""Create the BalWindow for *window* and wire its menu (and, when
|
||||
enabling hot, the already-open wallet).
|
||||
@@ -124,11 +272,28 @@ class Plugin(BalPlugin):
|
||||
sb.addPermanentWidget(b)
|
||||
self._statusbar_buttons[key] = b
|
||||
|
||||
# When the plugin is enabled "hot" from Tools -> Plugins, Electrum keeps
|
||||
# its "Electrum Plugins" manager dialog open and even calls
|
||||
# bring_to_front on it. Enabling triggers reload_windows(), which
|
||||
# recreates the window and therefore fires this create_status_bar hook;
|
||||
# that makes this the right place to auto-close the leftover manager
|
||||
# dialog (Electrum 4.7.x no longer calls the old init_qt hook).
|
||||
#
|
||||
# We use a QTimer so this runs *after* Electrum's own bring_to_front
|
||||
# (QTimer.singleShot(100, ...)); a slightly larger delay makes our close
|
||||
# win. On a normal startup no PluginsDialog is open, so the helper is a
|
||||
# harmless no-op.
|
||||
QTimer.singleShot(250, self._close_plugins_manager_dialog)
|
||||
|
||||
@hook
|
||||
def init_menubar(self, window):
|
||||
_logger.info("HOOK init_menubar")
|
||||
w = self.get_window(window)
|
||||
w.init_menubar_tools(window.tools_menu)
|
||||
# Also try here: init_menubar is one of the hooks fired when Electrum
|
||||
# recreates the window during a hot enable (reload_windows()), so it is
|
||||
# another reliable trigger to auto-close the leftover manager dialog.
|
||||
QTimer.singleShot(300, self._close_plugins_manager_dialog)
|
||||
|
||||
@hook
|
||||
def load_wallet(self, wallet, main_window):
|
||||
@@ -142,6 +307,9 @@ class Plugin(BalPlugin):
|
||||
)
|
||||
w.disable_plugin = False
|
||||
w.ok = True
|
||||
# load_wallet is fired on the recreated window during a hot enable too;
|
||||
# use it as an extra trigger to auto-close the leftover manager dialog.
|
||||
QTimer.singleShot(350, self._close_plugins_manager_dialog)
|
||||
|
||||
@hook
|
||||
def close_wallet(self, wallet):
|
||||
|
||||
Reference in New Issue
Block a user