qt: defer refreshing tabs until they are visible

very loosely based on Electron-Cash/Electron-Cash@522e7ca59e
This commit is contained in:
SomberNight 2020-01-19 07:31:50 +01:00
parent 356a0a2865
commit 1d0fc6665b
No known key found for this signature in database
GPG key ID: B33B5F232C6271E9
7 changed files with 33 additions and 1 deletions

View file

@ -137,6 +137,8 @@ class AddressList(MyTreeView):
@profiler @profiler
def update(self): def update(self):
if self.maybe_defer_update():
return
current_address = self.current_item_user_role(col=self.Columns.LABEL) current_address = self.current_item_user_role(col=self.Columns.LABEL)
if self.show_change == AddressTypeFilter.RECEIVING: if self.show_change == AddressTypeFilter.RECEIVING:
addr_list = self.wallet.get_receiving_addresses() addr_list = self.wallet.get_receiving_addresses()

View file

@ -102,6 +102,8 @@ class ContactList(MyTreeView):
menu.exec_(self.viewport().mapToGlobal(position)) menu.exec_(self.viewport().mapToGlobal(position))
def update(self): def update(self):
if self.maybe_defer_update():
return
current_key = self.current_item_user_role(col=self.Columns.NAME) current_key = self.current_item_user_role(col=self.Columns.NAME)
self.model().clear() self.model().clear()
self.update_headers(self.__class__.headers) self.update_headers(self.__class__.headers)

View file

@ -264,6 +264,8 @@ class HistoryModel(QAbstractItemModel, Logger):
self.logger.info(f"refreshing... reason: {reason}") self.logger.info(f"refreshing... reason: {reason}")
assert self.parent.gui_thread == threading.current_thread(), 'must be called from GUI thread' assert self.parent.gui_thread == threading.current_thread(), 'must be called from GUI thread'
assert self.view, 'view not set' assert self.view, 'view not set'
if self.view.maybe_defer_update():
return
selected = self.view.selectionModel().currentIndex() selected = self.view.selectionModel().currentIndex()
selected_row = None selected_row = None
if selected: if selected:
@ -430,6 +432,9 @@ class HistoryList(MyTreeView, AcceptFileDragDrop):
sm = QHeaderView.Stretch if col == self.stretch_column else QHeaderView.ResizeToContents sm = QHeaderView.Stretch if col == self.stretch_column else QHeaderView.ResizeToContents
self.header().setSectionResizeMode(col, sm) self.header().setSectionResizeMode(col, sm)
def update(self):
self.hm.refresh('HistoryList.update()')
def format_date(self, d): def format_date(self, d):
return str(datetime.date(d.year, d.month, d.day)) if d else _('None') return str(datetime.date(d.year, d.month, d.day)) if d else _('None')

View file

@ -94,6 +94,8 @@ class InvoiceList(MyTreeView):
status_item.setIcon(read_QIcon(pr_icons.get(status))) status_item.setIcon(read_QIcon(pr_icons.get(status)))
def update(self): def update(self):
if self.maybe_defer_update():
return
_list = self.parent.wallet.get_invoices() _list = self.parent.wallet.get_invoices()
# filter out paid invoices unless we have the log # filter out paid invoices unless we have the log
lnworker_logs = self.parent.wallet.lnworker.logs if self.parent.wallet.lnworker else {} lnworker_logs = self.parent.wallet.lnworker.logs if self.parent.wallet.lnworker else {}

View file

@ -107,6 +107,8 @@ class RequestList(MyTreeView):
status_item.setIcon(read_QIcon(pr_icons.get(status))) status_item.setIcon(read_QIcon(pr_icons.get(status)))
def update(self): def update(self):
if self.maybe_defer_update():
return
self.parent.update_receive_address_styling() self.parent.update_receive_address_styling()
self.model().clear() self.model().clear()
self.update_headers(self.__class__.headers) self.update_headers(self.__class__.headers)

View file

@ -12,7 +12,7 @@ from functools import partial, lru_cache
from typing import NamedTuple, Callable, Optional, TYPE_CHECKING, Union, List, Dict, Any from typing import NamedTuple, Callable, Optional, TYPE_CHECKING, Union, List, Dict, Any
from PyQt5.QtGui import (QFont, QColor, QCursor, QPixmap, QStandardItem, from PyQt5.QtGui import (QFont, QColor, QCursor, QPixmap, QStandardItem,
QPalette, QIcon, QFontMetrics) QPalette, QIcon, QFontMetrics, QShowEvent)
from PyQt5.QtCore import (Qt, QPersistentModelIndex, QModelIndex, pyqtSignal, from PyQt5.QtCore import (Qt, QPersistentModelIndex, QModelIndex, pyqtSignal,
QCoreApplication, QItemSelectionModel, QThread, QCoreApplication, QItemSelectionModel, QThread,
QSortFilterProxyModel, QSize, QLocale) QSortFilterProxyModel, QSize, QLocale)
@ -513,6 +513,9 @@ class MyTreeView(QTreeView):
# only look at as many rows as currently visible. # only look at as many rows as currently visible.
self.header().setResizeContentsPrecision(0) self.header().setResizeContentsPrecision(0)
self._pending_update = False
self._forced_update = False
def set_editability(self, items): def set_editability(self, items):
for idx, i in enumerate(items): for idx, i in enumerate(items):
i.setEditable(idx in self.editable_columns) i.setEditable(idx in self.editable_columns)
@ -664,6 +667,20 @@ class MyTreeView(QTreeView):
def place_text_on_clipboard(self, text: str, *, title: str = None) -> None: def place_text_on_clipboard(self, text: str, *, title: str = None) -> None:
self.parent.do_copy(text, title=title) self.parent.do_copy(text, title=title)
def showEvent(self, e: 'QShowEvent'):
super().showEvent(e)
if e.isAccepted() and self._pending_update:
self._forced_update = True
self.update()
self._forced_update = False
def maybe_defer_update(self) -> bool:
"""Returns whether we should defer an update/refresh."""
defer = not self.isVisible() and not self._forced_update
# side-effect: if we decide to defer update, the state will become stale:
self._pending_update = defer
return defer
class ButtonsWidget(QWidget): class ButtonsWidget(QWidget):

View file

@ -71,6 +71,8 @@ class UTXOList(MyTreeView):
self.update() self.update()
def update(self): def update(self):
if self.maybe_defer_update():
return
utxos = self.wallet.get_utxos() utxos = self.wallet.get_utxos()
self._maybe_reset_spend_list(utxos) self._maybe_reset_spend_list(utxos)
self._utxo_dict = {} self._utxo_dict = {}