Browse Source

kivy: remove dynamic screen loading.

The performance gain it brings is negligible,
and it causes object duplication, which makes
things difficult to debug.
hard-fail-on-bad-server-string
ThomasV 5 years ago
parent
commit
8b79e9fed1
  1. 106
      electrum/gui/kivy/uix/screens.py
  2. 2
      electrum/gui/kivy/uix/ui_screens/history.kv
  3. 10
      electrum/gui/kivy/uix/ui_screens/receive.kv
  4. 10
      electrum/gui/kivy/uix/ui_screens/send.kv

106
electrum/gui/kivy/uix/screens.py

@ -58,7 +58,6 @@ class PaymentRecycleView(RecycleView):
class CScreen(Factory.Screen): class CScreen(Factory.Screen):
__events__ = ('on_activate', 'on_deactivate', 'on_enter', 'on_leave') __events__ = ('on_activate', 'on_deactivate', 'on_enter', 'on_leave')
action_view = ObjectProperty(None) action_view = ObjectProperty(None)
loaded = False
kvname = None kvname = None
app = App.get_running_app() # type: ElectrumWindow app = App.get_running_app() # type: ElectrumWindow
@ -80,17 +79,8 @@ class CScreen(Factory.Screen):
def update(self): def update(self):
pass pass
@profiler
def load_screen(self):
self.screen = Builder.load_file('electrum/gui/kivy/uix/ui_screens/' + self.kvname + '.kv')
self.add_widget(self.screen)
self.loaded = True
self.update()
setattr(self.app, self.kvname + '_screen', self)
def on_activate(self): def on_activate(self):
if self.kvname and not self.loaded: setattr(self.app, self.kvname + '_screen', self)
self.load_screen()
#Clock.schedule_once(lambda dt: self._change_action_view()) #Clock.schedule_once(lambda dt: self._change_action_view())
def on_leave(self): def on_leave(self):
@ -114,6 +104,12 @@ TX_ICONS = [
"confirmed", "confirmed",
] ]
Builder.load_file('electrum/gui/kivy/uix/ui_screens/history.kv')
Builder.load_file('electrum/gui/kivy/uix/ui_screens/send.kv')
Builder.load_file('electrum/gui/kivy/uix/ui_screens/receive.kv')
class HistoryScreen(CScreen): class HistoryScreen(CScreen):
tab = ObjectProperty(None) tab = ObjectProperty(None)
@ -182,7 +178,7 @@ class HistoryScreen(CScreen):
return return
self.history = wallet.get_full_history(self.app.fx) self.history = wallet.get_full_history(self.app.fx)
history = reversed(self.history.values()) history = reversed(self.history.values())
history_card = self.screen.ids.history_container history_card = self.ids.history_container
history_card.data = [self.get_card(item) for item in history] history_card.data = [self.get_card(item) for item in history]
@ -204,11 +200,11 @@ class SendScreen(CScreen):
return return
self.parsed_URI = uri self.parsed_URI = uri
amount = uri.get('amount') amount = uri.get('amount')
self.screen.address = uri.get('address', '') self.address = uri.get('address', '')
self.screen.message = uri.get('message', '') self.message = uri.get('message', '')
self.screen.amount = self.app.format_amount_and_units(amount) if amount else '' self.amount = self.app.format_amount_and_units(amount) if amount else ''
self.payment_request = None self.payment_request = None
self.screen.is_lightning = False self.is_lightning = False
def set_ln_invoice(self, invoice): def set_ln_invoice(self, invoice):
try: try:
@ -217,15 +213,13 @@ class SendScreen(CScreen):
except Exception as e: except Exception as e:
self.app.show_info(invoice + _(" is not a valid Lightning invoice: ") + repr(e)) # repr because str(Exception()) == '' self.app.show_info(invoice + _(" is not a valid Lightning invoice: ") + repr(e)) # repr because str(Exception()) == ''
return return
self.screen.address = invoice self.address = invoice
self.screen.message = dict(lnaddr.tags).get('d', None) self.message = dict(lnaddr.tags).get('d', None)
self.screen.amount = self.app.format_amount_and_units(lnaddr.amount * bitcoin.COIN) if lnaddr.amount else '' self.amount = self.app.format_amount_and_units(lnaddr.amount * bitcoin.COIN) if lnaddr.amount else ''
self.payment_request = None self.payment_request = None
self.screen.is_lightning = True self.is_lightning = True
def update(self): def update(self):
if not self.loaded:
return
if self.app.wallet and self.payment_request_queued: if self.app.wallet and self.payment_request_queued:
self.set_URI(self.payment_request_queued) self.set_URI(self.payment_request_queued)
self.payment_request_queued = None self.payment_request_queued = None
@ -233,7 +227,7 @@ class SendScreen(CScreen):
_list.reverse() _list.reverse()
lnworker_logs = self.app.wallet.lnworker.logs if self.app.wallet.lnworker else {} lnworker_logs = self.app.wallet.lnworker.logs if self.app.wallet.lnworker else {}
_list = [x for x in _list if x and x.get('status') != PR_PAID or x.get('rhash') in lnworker_logs] _list = [x for x in _list if x and x.get('status') != PR_PAID or x.get('rhash') in lnworker_logs]
payments_container = self.screen.ids.payments_container payments_container = self.ids.payments_container
payments_container.data = [self.get_card(item) for item in _list] payments_container.data = [self.get_card(item) for item in _list]
def show_item(self, obj): def show_item(self, obj):
@ -263,20 +257,20 @@ class SendScreen(CScreen):
} }
def do_clear(self): def do_clear(self):
self.screen.amount = '' self.amount = ''
self.screen.message = '' self.message = ''
self.screen.address = '' self.address = ''
self.payment_request = None self.payment_request = None
self.screen.is_lightning = False self.is_lightning = False
self.screen.is_bip70 = False self.is_bip70 = False
self.parsed_URI = None self.parsed_URI = None
def set_request(self, pr: 'PaymentRequest'): def set_request(self, pr: 'PaymentRequest'):
self.screen.address = pr.get_requestor() self.address = pr.get_requestor()
amount = pr.get_amount() amount = pr.get_amount()
self.screen.amount = self.app.format_amount_and_units(amount) if amount else '' self.amount = self.app.format_amount_and_units(amount) if amount else ''
self.screen.message = pr.get_memo() self.message = pr.get_memo()
self.screen.locked = True self.locked = True
self.payment_request = pr self.payment_request = pr
def do_paste(self): def do_paste(self):
@ -301,20 +295,20 @@ class SendScreen(CScreen):
self.set_URI(data) self.set_URI(data)
def read_invoice(self): def read_invoice(self):
address = str(self.screen.address) address = str(self.address)
if not address: if not address:
self.app.show_error(_('Recipient not specified.') + ' ' + _('Please scan a Bitcoin address or a payment request')) self.app.show_error(_('Recipient not specified.') + ' ' + _('Please scan a Bitcoin address or a payment request'))
return return
if not self.screen.amount: if not self.amount:
self.app.show_error(_('Please enter an amount')) self.app.show_error(_('Please enter an amount'))
return return
try: try:
amount = self.app.get_amount(self.screen.amount) amount = self.app.get_amount(self.amount)
except: except:
self.app.show_error(_('Invalid amount') + ':\n' + self.screen.amount) self.app.show_error(_('Invalid amount') + ':\n' + self.amount)
return return
message = self.screen.message message = self.message
if self.screen.is_lightning: if self.is_lightning:
return self.app.wallet.lnworker.parse_bech32_invoice(address) return self.app.wallet.lnworker.parse_bech32_invoice(address)
else: # on-chain else: # on-chain
if self.payment_request: if self.payment_request:
@ -423,32 +417,32 @@ class ReceiveScreen(CScreen):
return self.app.electrum_config.get('request_expiry', PR_DEFAULT_EXPIRATION_WHEN_CREATING) return self.app.electrum_config.get('request_expiry', PR_DEFAULT_EXPIRATION_WHEN_CREATING)
def clear(self): def clear(self):
self.screen.address = '' self.address = ''
self.screen.amount = '' self.amount = ''
self.screen.message = '' self.message = ''
self.screen.lnaddr = '' self.lnaddr = ''
def set_address(self, addr): def set_address(self, addr):
self.screen.address = addr self.address = addr
def on_address(self, addr): def on_address(self, addr):
req = self.app.wallet.get_request(addr) req = self.app.wallet.get_request(addr)
self.screen.status = '' self.status = ''
if req: if req:
self.screen.message = req.get('memo', '') self.message = req.get('memo', '')
amount = req.get('amount') amount = req.get('amount')
self.screen.amount = self.app.format_amount_and_units(amount) if amount else '' self.amount = self.app.format_amount_and_units(amount) if amount else ''
status = req.get('status', PR_UNKNOWN) status = req.get('status', PR_UNKNOWN)
self.screen.status = _('Payment received') if status == PR_PAID else '' self.status = _('Payment received') if status == PR_PAID else ''
def get_URI(self): def get_URI(self):
from electrum.util import create_bip21_uri from electrum.util import create_bip21_uri
amount = self.screen.amount amount = self.amount
if amount: if amount:
a, u = self.screen.amount.split() a, u = self.amount.split()
assert u == self.app.base_unit assert u == self.app.base_unit
amount = Decimal(a) * pow(10, self.app.decimal_point()) amount = Decimal(a) * pow(10, self.app.decimal_point())
return create_bip21_uri(self.screen.address, amount, self.screen.message) return create_bip21_uri(self.address, amount, self.message)
def do_copy(self): def do_copy(self):
uri = self.get_URI() uri = self.get_URI()
@ -456,17 +450,17 @@ class ReceiveScreen(CScreen):
self.app.show_info(_('Request copied to clipboard')) self.app.show_info(_('Request copied to clipboard'))
def new_request(self, lightning): def new_request(self, lightning):
amount = self.screen.amount amount = self.amount
amount = self.app.get_amount(amount) if amount else 0 amount = self.app.get_amount(amount) if amount else 0
message = self.screen.message message = self.message
if lightning: if lightning:
key = self.app.wallet.lnworker.add_request(amount, message, self.expiry()) key = self.app.wallet.lnworker.add_request(amount, message, self.expiry())
else: else:
addr = self.screen.address or self.app.wallet.get_unused_address() addr = self.address or self.app.wallet.get_unused_address()
if not addr: if not addr:
self.app.show_info(_('No address available. Please remove some of your pending requests.')) self.app.show_info(_('No address available. Please remove some of your pending requests.'))
return return
self.screen.address = addr self.address = addr
req = self.app.wallet.make_payment_request(addr, amount, message, self.expiry()) req = self.app.wallet.make_payment_request(addr, amount, message, self.expiry())
self.app.wallet.add_payment_request(req) self.app.wallet.add_payment_request(req)
key = addr key = addr
@ -497,11 +491,9 @@ class ReceiveScreen(CScreen):
return ci return ci
def update(self): def update(self):
if not self.loaded:
return
_list = self.app.wallet.get_sorted_requests() _list = self.app.wallet.get_sorted_requests()
_list.reverse() _list.reverse()
requests_container = self.screen.ids.requests_container requests_container = self.ids.requests_container
requests_container.data = [self.get_card(item) for item in _list if item.get('status') != PR_PAID] requests_container.data = [self.get_card(item) for item in _list if item.get('status') != PR_PAID]
def show_item(self, obj): def show_item(self, obj):

2
electrum/gui/kivy/uix/ui_screens/history.kv

@ -76,7 +76,7 @@
orientation: 'vertical' orientation: 'vertical'
HistoryScreen: <HistoryScreen>:
name: 'history' name: 'history'
content: history_container content: history_container
BoxLayout: BoxLayout:

10
electrum/gui/kivy/uix/ui_screens/receive.kv

@ -60,7 +60,7 @@
orientation: 'vertical' orientation: 'vertical'
ReceiveScreen: <ReceiveScreen>:
id: s id: s
name: 'receive' name: 'receive'
address: '' address: ''
@ -135,22 +135,22 @@ ReceiveScreen:
icon: 'atlas://electrum/gui/kivy/theming/light/list' icon: 'atlas://electrum/gui/kivy/theming/light/list'
size_hint: 0.5, None size_hint: 0.5, None
height: '48dp' height: '48dp'
on_release: Clock.schedule_once(lambda dt: s.parent.clear_requests_dialog()) on_release: Clock.schedule_once(lambda dt: s.clear_requests_dialog())
IconButton: IconButton:
icon: 'atlas://electrum/gui/kivy/theming/light/clock1' icon: 'atlas://electrum/gui/kivy/theming/light/clock1'
size_hint: 0.5, None size_hint: 0.5, None
height: '48dp' height: '48dp'
on_release: Clock.schedule_once(lambda dt: s.parent.expiration_dialog(s)) on_release: Clock.schedule_once(lambda dt: s.expiration_dialog(s))
Button: Button:
text: _('Clear') text: _('Clear')
size_hint: 1, None size_hint: 1, None
height: '48dp' height: '48dp'
on_release: Clock.schedule_once(lambda dt: s.parent.clear()) on_release: Clock.schedule_once(lambda dt: s.clear())
Button: Button:
text: _('Request') text: _('Request')
size_hint: 1, None size_hint: 1, None
height: '48dp' height: '48dp'
on_release: Clock.schedule_once(lambda dt: s.parent.new_request(root.is_lightning)) on_release: Clock.schedule_once(lambda dt: s.new_request(root.is_lightning))
Widget: Widget:
size_hint: 1, 0.1 size_hint: 1, 0.1
RequestRecycleView: RequestRecycleView:

10
electrum/gui/kivy/uix/ui_screens/send.kv

@ -62,7 +62,7 @@
height: self.minimum_height height: self.minimum_height
orientation: 'vertical' orientation: 'vertical'
SendScreen: <SendScreen>:
id: s id: s
name: 'send' name: 'send'
address: '' address: ''
@ -150,12 +150,12 @@ SendScreen:
height: '48dp' height: '48dp'
IconButton: IconButton:
size_hint: 0.5, 1 size_hint: 0.5, 1
on_release: s.parent.do_save() on_release: s.do_save()
icon: 'atlas://electrum/gui/kivy/theming/light/save' icon: 'atlas://electrum/gui/kivy/theming/light/save'
IconButton: IconButton:
size_hint: 0.5, 1 size_hint: 0.5, 1
icon: 'atlas://electrum/gui/kivy/theming/light/copy' icon: 'atlas://electrum/gui/kivy/theming/light/copy'
on_release: s.parent.do_paste() on_release: s.do_paste()
IconButton: IconButton:
id: qr id: qr
size_hint: 1, 1 size_hint: 1, 1
@ -164,11 +164,11 @@ SendScreen:
Button: Button:
text: _('Clear') text: _('Clear')
size_hint: 1, 1 size_hint: 1, 1
on_release: s.parent.do_clear() on_release: s.do_clear()
Button: Button:
text: _('Pay') text: _('Pay')
size_hint: 1, 1 size_hint: 1, 1
on_release: s.parent.do_pay() on_release: s.do_pay()
Widget: Widget:
size_hint: 1, 0.1 size_hint: 1, 0.1
PaymentRecycleView: PaymentRecycleView:

Loading…
Cancel
Save