Skip to content

Commit 95bdbe7

Browse files
cryptocoderkeene
authored andcommitted
Reduce UI lockups (#1482)
1 parent 4c7a567 commit 95bdbe7

File tree

2 files changed

+48
-18
lines changed

2 files changed

+48
-18
lines changed

rai/qt/qt.cpp

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,12 @@ wallet (wallet_a)
106106
});
107107
}
108108

109-
void rai_qt::self_pane::refresh_balance ()
109+
void rai_qt::self_pane::set_balance_text (std::pair<rai::uint128_t, rai::uint128_t> balance_a)
110110
{
111-
auto balance (wallet.node.balance_pending (wallet.account));
112-
auto final_text (std::string ("Balance: ") + wallet.format_balance (balance.first));
113-
if (!balance.second.is_zero ())
111+
auto final_text (std::string ("Balance: ") + wallet.format_balance (balance_a.first));
112+
if (!balance_a.second.is_zero ())
114113
{
115-
final_text += "\nPending: " + wallet.format_balance (balance.second);
114+
final_text += "\nPending: " + wallet.format_balance (balance_a.second);
116115
}
117116
wallet.self.balance_label->setText (QString (final_text.c_str ()));
118117
}
@@ -153,6 +152,7 @@ wallet (wallet_a)
153152
layout->addWidget (account_key_button);
154153
layout->addWidget (back);
155154
window->setLayout (layout);
155+
156156
QObject::connect (use_account, &QPushButton::released, [this]() {
157157
auto selection (view->selectionModel ()->selection ().indexes ());
158158
if (selection.size () == 1)
@@ -1045,8 +1045,44 @@ active_status (*this)
10451045
refresh ();
10461046
}
10471047

1048+
void rai_qt::wallet::ongoing_refresh ()
1049+
{
1050+
std::weak_ptr<rai_qt::wallet> wallet_w (shared_from_this ());
1051+
1052+
// Update balance if needed. This happens on an alarm thread, which posts back to the UI
1053+
// to do the actual rendering. This avoid UI lockups as balance_pending may take several
1054+
// seconds if there's a lot of pending transactions.
1055+
if (needs_balance_refresh)
1056+
{
1057+
needs_balance_refresh = false;
1058+
auto balance_l (node.balance_pending (account));
1059+
application.postEvent (&processor, new eventloop_event ([wallet_w, balance_l]() {
1060+
if (auto this_l = wallet_w.lock ())
1061+
{
1062+
this_l->self.set_balance_text (balance_l);
1063+
}
1064+
}));
1065+
}
1066+
1067+
// Updates the status line periodically with bootstrap status and block counts.
1068+
application.postEvent (&processor, new eventloop_event ([wallet_w]() {
1069+
if (auto this_l = wallet_w.lock ())
1070+
{
1071+
this_l->active_status.set_text ();
1072+
}
1073+
}));
1074+
1075+
node.alarm.add (std::chrono::steady_clock::now () + std::chrono::seconds (5), [wallet_w]() {
1076+
if (auto wallet_l = wallet_w.lock ())
1077+
{
1078+
wallet_l->ongoing_refresh ();
1079+
}
1080+
});
1081+
}
1082+
10481083
void rai_qt::wallet::start ()
10491084
{
1085+
ongoing_refresh ();
10501086
std::weak_ptr<rai_qt::wallet> this_w (shared_from_this ());
10511087
QObject::connect (settings_button, &QPushButton::released, [this_w]() {
10521088
if (auto this_l = this_w.lock ())
@@ -1250,15 +1286,7 @@ void rai_qt::wallet::start ()
12501286
node.observers.account_balance.add ([this_w](rai::account const & account_a, bool is_pending) {
12511287
if (auto this_l = this_w.lock ())
12521288
{
1253-
this_l->application.postEvent (&this_l->processor, new eventloop_event ([this_w, account_a]() {
1254-
if (auto this_l = this_w.lock ())
1255-
{
1256-
if (account_a == this_l->account)
1257-
{
1258-
this_l->self.refresh_balance ();
1259-
}
1260-
}
1261-
}));
1289+
this_l->needs_balance_refresh = this_l->needs_balance_refresh || account_a == this_l->account;
12621290
}
12631291
});
12641292
node.observers.wallet.add ([this_w](bool active_a) {
@@ -1358,7 +1386,7 @@ void rai_qt::wallet::refresh ()
13581386
assert (wallet_m->store.exists (transaction, account));
13591387
}
13601388
self.account_text->setText (QString (account.to_account ().c_str ()));
1361-
self.refresh_balance ();
1389+
needs_balance_refresh = true;
13621390
accounts.refresh ();
13631391
history.refresh ();
13641392
account_viewer.history.refresh ();
@@ -1841,10 +1869,10 @@ wallet (wallet_a)
18411869
this->wallet.pop_main_stack ();
18421870
});
18431871
QObject::connect (search_for_receivables, &QPushButton::released, [this]() {
1844-
this->wallet.wallet_m->search_pending ();
1872+
std::thread ([this] { this->wallet.wallet_m->search_pending (); }).detach ();
18451873
});
18461874
QObject::connect (bootstrap, &QPushButton::released, [this]() {
1847-
this->wallet.node.bootstrap_initiator.bootstrap ();
1875+
std::thread ([this] { this->wallet.node.bootstrap_initiator.bootstrap (); }).detach ();
18481876
});
18491877
QObject::connect (create_block, &QPushButton::released, [this]() {
18501878
this->wallet.push_main_stack (this->wallet.block_creation.window);

rai/qt/qt.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ class self_pane
151151
{
152152
public:
153153
self_pane (rai_qt::wallet &, rai::account const &);
154-
void refresh_balance ();
154+
void set_balance_text (std::pair<rai::uint128_t, rai::uint128_t>);
155155
QWidget * window;
156156
QVBoxLayout * layout;
157157
QHBoxLayout * self_layout;
@@ -351,5 +351,7 @@ class wallet : public std::enable_shared_from_this<rai_qt::wallet>
351351
rai_qt::status active_status;
352352
void pop_main_stack ();
353353
void push_main_stack (QWidget *);
354+
void ongoing_refresh ();
355+
std::atomic<bool> needs_balance_refresh;
354356
};
355357
}

0 commit comments

Comments
 (0)