From 20c281d6f8d5f25573a8c4c930a961ea9ab45380 Mon Sep 17 00:00:00 2001 From: flow Date: Tue, 22 Nov 2022 14:30:54 -0300 Subject: [PATCH] fix: reset wide bar settings when the list of actions changes This prevents changes to the actions to cause non-intuitive issues in the Wide bar, hiding items that previously weren't hidden. Signed-off-by: flow --- launcher/ui/widgets/WideBar.cpp | 35 +++++++++++++++++++++++++++++++-- launcher/ui/widgets/WideBar.h | 4 ++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/launcher/ui/widgets/WideBar.cpp b/launcher/ui/widgets/WideBar.cpp index 2ad2caec..81c445cb 100644 --- a/launcher/ui/widgets/WideBar.cpp +++ b/launcher/ui/widgets/WideBar.cpp @@ -1,6 +1,7 @@ #include "WideBar.h" #include +#include #include class ActionButton : public QToolButton { @@ -211,23 +212,53 @@ void WideBar::contextMenuEvent(QContextMenuEvent* event) state.append(entry.bar_action->isVisible() ? '1' : '0'); } + state.append(','); + state.append(getHash()); + return state; } void WideBar::setVisibilityState(QByteArray&& state) { + auto split = state.split(','); + + auto bits = split.first(); + auto hash = split.last(); + + // If the actions changed, we better not try to load the old one to avoid unwanted hiding + if (!checkHash(hash)) + return; + qsizetype i = 0; for (auto& entry : m_entries) { if (entry.type != BarEntry::Type::Action) continue; - if (i == state.size()) + if (i == bits.size()) break; - entry.bar_action->setVisible(state.at(i++) == '1'); + entry.bar_action->setVisible(bits.at(i++) == '1'); // NOTE: This is needed so that disabled actions get reflected on the button when it is made visible. static_cast(widgetForAction(entry.bar_action))->actionChanged(); } } +QByteArray WideBar::getHash() const +{ + QCryptographicHash hash(QCryptographicHash::Sha1); + for (auto const& entry : m_entries) { + if (entry.type != BarEntry::Type::Action) + continue; + hash.addData(entry.menu_action->text().toLatin1()); + } + + return hash.result().toBase64(); +} + +bool WideBar::checkHash(QByteArray const& old_hash) const +{ + return old_hash == getHash(); +} + + #include "WideBar.moc" diff --git a/launcher/ui/widgets/WideBar.h b/launcher/ui/widgets/WideBar.h index 0d60f8a4..ed4cb3c7 100644 --- a/launcher/ui/widgets/WideBar.h +++ b/launcher/ui/widgets/WideBar.h @@ -41,6 +41,10 @@ class WideBar : public QToolBar { auto getMatching(QAction* act) -> QList::iterator; + /** Used to distinguish between versions of the WideBar with different actions */ + [[nodiscard]] QByteArray getHash() const; + [[nodiscard]] bool checkHash(QByteArray const&) const; + private: QList m_entries;