From 05ab3b27e783c33aceaed78f283a6b05aeed54e5 Mon Sep 17 00:00:00 2001 From: unknown <1310440+hemirt@users.noreply.github.com> Date: Sat, 26 Oct 2024 16:19:57 +0200 Subject: [PATCH] switch from using two sets to one map --- src/widgets/helper/NotebookTab.cpp | 67 ++++++++++++------------------ src/widgets/helper/NotebookTab.hpp | 21 ++-------- 2 files changed, 30 insertions(+), 58 deletions(-) diff --git a/src/widgets/helper/NotebookTab.cpp b/src/widgets/helper/NotebookTab.cpp index d3b2c2517..09630c7e0 100644 --- a/src/widgets/helper/NotebookTab.cpp +++ b/src/widgets/helper/NotebookTab.cpp @@ -305,37 +305,25 @@ bool NotebookTab::isSelected() const return this->selected_; } -void NotebookTab::removeNewMessageSource( - const ChannelView::ChannelViewID &source) -{ - this->highlightSources_.newMessageSource.erase(source); -} - -void NotebookTab::removeHighlightedSource( - const ChannelView::ChannelViewID &source) -{ - this->highlightSources_.highlightedSource.erase(source); -} - void NotebookTab::removeHighlightStateChangeSources( const HighlightSources &toRemove) { - for (const auto &source : toRemove.newMessageSource) + for (const auto &[source, _] : toRemove) { - this->removeNewMessageSource(source); + this->removeHighlightSource(source); } +} - for (const auto &source : toRemove.highlightedSource) - { - this->removeHighlightedSource(source); - } +void NotebookTab::removeHighlightSource( + const ChannelView::ChannelViewID &source) +{ + this->highlightSources_.erase(source); } void NotebookTab::newHighlightSourceAdded(const ChannelView &channelViewSource) { auto channelViewId = channelViewSource.getID(); - this->removeHighlightedSource(channelViewId); - this->removeNewMessageSource(channelViewId); + this->removeHighlightSource(channelViewId); this->updateHighlightStateDueSourcesChange(); auto *splitNotebook = dynamic_cast(this->notebook_); @@ -350,8 +338,7 @@ void NotebookTab::newHighlightSourceAdded(const ChannelView &channelViewSource) auto *tab = splitContainer->getTab(); if (tab && tab != this) { - tab->removeHighlightedSource(channelViewId); - tab->removeNewMessageSource(channelViewId); + tab->removeHighlightSource(channelViewId); tab->updateHighlightStateDueSourcesChange(); } } @@ -361,13 +348,17 @@ void NotebookTab::newHighlightSourceAdded(const ChannelView &channelViewSource) void NotebookTab::updateHighlightStateDueSourcesChange() { - if (!this->highlightSources_.highlightedSource.empty()) + if (std::ranges::any_of(this->highlightSources_, [](const auto &keyval) { + return keyval.second == HighlightState::Highlighted; + })) { assert(this->highlightState_ == HighlightState::Highlighted); return; } - if (!this->highlightSources_.newMessageSource.empty()) + if (std::ranges::any_of(this->highlightSources_, [](const auto &keyval) { + return keyval.second == HighlightState::NewMessage; + })) { if (this->highlightState_ != HighlightState::NewMessage) { @@ -391,8 +382,7 @@ void NotebookTab::copyHighlightStateAndSourcesFrom(const NotebookTab *sourceTab) { if (this->isSelected()) { - assert(this->highlightSources_.highlightedSource.empty()); - assert(this->highlightSources_.newMessageSource.empty()); + assert(this->highlightSources_.empty()); assert(this->highlightState_ == HighlightState::None); return; } @@ -504,8 +494,7 @@ void NotebookTab::setHighlightState(HighlightState newHighlightStyle) { if (this->isSelected()) { - assert(this->highlightSources_.highlightedSource.empty()); - assert(this->highlightSources_.newMessageSource.empty()); + assert(this->highlightSources_.empty()); assert(this->highlightState_ == HighlightState::None); return; } @@ -534,8 +523,7 @@ void NotebookTab::updateHighlightState(HighlightState newHighlightStyle, { if (this->isSelected()) { - assert(this->highlightSources_.highlightedSource.empty()); - assert(this->highlightSources_.newMessageSource.empty()); + assert(this->highlightSources_.empty()); assert(this->highlightState_ == HighlightState::None); return; } @@ -557,19 +545,16 @@ void NotebookTab::updateHighlightState(HighlightState newHighlightStyle, switch (newHighlightStyle) { - case HighlightState::Highlighted: { - if (!this->highlightSources_.highlightedSource.contains( - channelViewId)) - { - this->highlightSources_.highlightedSource.insert(channelViewId); - } - break; - } + case HighlightState::Highlighted: + // override lower states + this->highlightSources_.insert_or_assign(channelViewId, + newHighlightStyle); case HighlightState::NewMessage: { - if (!this->highlightSources_.newMessageSource.contains( - channelViewId)) + // only insert if no state already there to avoid overriding + if (!this->highlightSources_.contains(channelViewId)) { - this->highlightSources_.newMessageSource.insert(channelViewId); + this->highlightSources_.emplace(channelViewId, + newHighlightStyle); } break; } diff --git a/src/widgets/helper/NotebookTab.hpp b/src/widgets/helper/NotebookTab.hpp index 28682eef6..e210d4829 100644 --- a/src/widgets/helper/NotebookTab.hpp +++ b/src/widgets/helper/NotebookTab.hpp @@ -125,25 +125,12 @@ private: bool shouldMessageHighlight(const ChannelView &channelViewSource, const MessagePtr &message) const; - struct HighlightSources { - // Source of the update to the highlight status is the split - // in which a message that causes the update appears. - // This message can be just a plain message causing the - // HighlightState::NewMessage state, or a message that pings the user - // causing the HighlightState::Highlighted state - std::unordered_set newMessageSource; - std::unordered_set highlightedSource; - - void clear() - { - this->newMessageSource.clear(); - this->highlightedSource.clear(); - } - } highlightSources_; + using HighlightSources = + std::unordered_map; + HighlightSources highlightSources_; void removeHighlightStateChangeSources(const HighlightSources &toRemove); - void removeNewMessageSource(const ChannelView::ChannelViewID &source); - void removeHighlightedSource(const ChannelView::ChannelViewID &source); + void removeHighlightSource(const ChannelView::ChannelViewID &source); void updateHighlightStateDueSourcesChange(); QPropertyAnimation positionChangedAnimation_;