1
0

plugins: move wallet-related settings to the wallet menu

Plugins should use the init_menubar hook.
References are kept to the various menu objects.
This commit is contained in:
ThomasV
2025-04-15 09:43:01 +02:00
parent 8c028f7528
commit 0831fc3b80
3 changed files with 89 additions and 93 deletions

View File

@@ -1,22 +1,19 @@
from functools import partial
import traceback
import sys
from typing import TYPE_CHECKING
from PyQt6.QtCore import QObject, pyqtSignal
from PyQt6.QtWidgets import (QHBoxLayout, QLabel, QVBoxLayout)
from electrum.plugin import hook
from electrum.i18n import _
from electrum.gui.qt.util import ThreadedButton, Buttons, EnterButton, WindowModalDialog, OkButton
from electrum.gui.qt.util import TaskThread
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)
@@ -28,36 +25,32 @@ class Plugin(LabelsPlugin):
self.obj = QLabelsSignalObject()
self._init_qt_received = False
def requires_settings(self):
return True
def settings_dialog(self, window: WindowModalDialog, wallet: 'Abstract_Wallet'):
@hook
def init_menubar(self, window: 'ElectrumWindow'):
wallet = window.wallet
if not wallet.get_fingerprint():
window.show_error(_("{} plugin does not support this type of wallet.")
.format("Label Sync"))
return
d = WindowModalDialog(window, _("Label Settings"))
hbox = QHBoxLayout()
hbox.addWidget(QLabel("Label sync options:"))
upload = ThreadedButton("Force upload",
partial(self.push, wallet),
partial(self.done_processing_success, d),
partial(self.done_processing_error, d))
download = ThreadedButton("Force download",
partial(self.pull, wallet, True),
partial(self.done_processing_success, d),
partial(self.done_processing_error, d))
vbox = QVBoxLayout()
vbox.addWidget(upload)
vbox.addWidget(download)
hbox.addLayout(vbox)
vbox = QVBoxLayout(d)
vbox.addLayout(hbox)
vbox.addSpacing(20)
vbox.addLayout(Buttons(OkButton(d)))
return bool(d.exec())
m = window.wallet_menu.addMenu('LabelSync')
m.addAction("Force upload", lambda: self.do_push(window))
m.addAction("Force download", lambda: self.do_pull(window))
def on_pulled(self, wallet):
def do_push(self, window: 'ElectrumWindow'):
thread = TaskThread(window)
thread.add(
partial(self.push, window.wallet),
partial(self.done_processing_success, window),
thread.stop,
partial(self.done_processing_error, window))
def do_pull(self, window: 'ElectrumWindow'):
thread = TaskThread(window)
thread.add(
partial(self.pull, window.wallet, True),
partial(self.done_processing_success, window),
thread.stop,
partial(self.done_processing_error, window))
def on_pulled(self, wallet: 'Abstract_Wallet'):
self.obj.labels_changed_signal.emit(wallet)
def done_processing_success(self, dialog, result):

View File

@@ -1,6 +1,6 @@
from electrum.i18n import _
from .nwcserver import NWCServerPlugin
from electrum.gui.qt.util import WindowModalDialog, Buttons, EnterButton, OkButton, CancelButton, \
from electrum.gui.qt.util import WindowModalDialog, Buttons, OkButton, CancelButton, \
CloseButton
from electrum.gui.common_qt.util import paintQR
from electrum.plugin import hook
@@ -16,7 +16,7 @@ from typing import TYPE_CHECKING, Optional
if TYPE_CHECKING:
from electrum.wallet import Abstract_Wallet
from electrum.gui.qt.main_window import ElectrumWindow
from electrum.gui.qt import ElectrumGui
class Plugin(NWCServerPlugin):
def __init__(self, *args):
@@ -29,19 +29,22 @@ class Plugin(NWCServerPlugin):
return
self.start_plugin(wallet)
def requires_settings(self):
return True
@hook
def init_menubar(self, window):
window.wallet_menu.addAction('Nostr Wallet Connect', partial(self.settings_dialog, window))
def settings_dialog(self, window: WindowModalDialog, wallet: 'Abstract_Wallet'):
def settings_dialog(self, window: WindowModalDialog):
if not self.initialized:
window.show_error(
_("{} plugin requires a lightning enabled wallet. Open a lightning-enabled wallet first.")
.format("NWC"))
return
if window.wallet != self.nwc_server.wallet:
window.show_error('not using this wallet')
return
d = WindowModalDialog(window, _("Nostr Wallet Connect"))
main_layout = QVBoxLayout(d)
main_layout.addWidget(QLabel(_("Using wallet:") + ' ' + self.nwc_server.wallet.basename()))
# Connections list
main_layout.addWidget(QLabel(_("Existing Connections:")))
@@ -117,6 +120,7 @@ class Plugin(NWCServerPlugin):
# Create Connection button
create_btn = QPushButton(_("Create Connection"))
def create_connection():
# Show a dialog to create a new connection
connection_string = self.connection_info_input_dialog(window)
@@ -179,8 +183,8 @@ class Plugin(NWCServerPlugin):
# dropdown menu to select prioritized nwc relay from self.config.NOSTR_RELAYS
main_relay_label = QLabel(_("Main NWC Relay:"))
relay_tooltip = (
_("Most clients only use the first relay url encoded in the connection string.")
+ "\n" + _("The selected relay will be put first in the connection string."))
_("Most clients only use the first relay url encoded in the connection string.")
+ "\n" + _("The selected relay will be put first in the connection string."))
main_relay_label.setToolTip(relay_tooltip)
layout.addWidget(main_relay_label)
relay_combo = QComboBox()