From 35b15c40886a9af8be748dabde5e80bfc02d8455 Mon Sep 17 00:00:00 2001 From: nerix Date: Fri, 24 Jan 2025 12:43:30 +0100 Subject: [PATCH] feat: prefer visible/selected channels when joining (#5850) --- CHANGELOG.md | 1 + src/providers/twitch/TwitchIrcServer.cpp | 37 +++++++++++++++--------- src/singletons/WindowManager.cpp | 20 +++++++++++++ src/singletons/WindowManager.hpp | 3 ++ 4 files changed, 48 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index df0d4a83..c85de573 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - Minor: Treat all browsers starting with `firefox` as a Firefox browser. (#5805) - Minor: Remove incognito browser support for `opera/launcher` (this should no longer be a thing). (#5805) - Minor: Remove incognito browser support for `iexplore`, because internet explorer is EOL. (#5810) +- Minor: When (re-)connecting, visible channels are now joined first. (#5850) - Bugfix: Fixed a potential way to escape the Lua Plugin sandbox. (#5846) - Bugfix: Fixed a crash relating to Lua HTTP. (#5800) - Bugfix: Fixed a crash that could occur on Linux and macOS when clicking "Install" from the update prompt. (#5818) diff --git a/src/providers/twitch/TwitchIrcServer.cpp b/src/providers/twitch/TwitchIrcServer.cpp index b669c502..f1cc50c4 100644 --- a/src/providers/twitch/TwitchIrcServer.cpp +++ b/src/providers/twitch/TwitchIrcServer.cpp @@ -24,6 +24,7 @@ #include "providers/twitch/TwitchChannel.hpp" #include "singletons/Settings.hpp" #include "singletons/StreamerMode.hpp" +#include "singletons/WindowManager.hpp" #include "util/PostToThread.hpp" #include "util/RatelimitBucket.hpp" @@ -935,15 +936,31 @@ void TwitchIrcServer::onReadConnected(IrcConnection *connection) { (void)connection; - std::lock_guard lock(this->channelMutex); + std::vector activeChannels; + { + std::lock_guard lock(this->channelMutex); + + activeChannels.reserve(this->channels.size()); + for (const auto &weak : this->channels) + { + if (auto channel = weak.lock()) + { + activeChannels.push_back(channel); + } + } + } + + // put the visible channels first + auto visible = getApp()->getWindows()->getVisibleChannelNames(); + + std::ranges::stable_partition(activeChannels, [&](const auto &chan) { + return visible.contains(chan->getName()); + }); // join channels - for (auto &&weak : this->channels) + for (const auto &channel : activeChannels) { - if (auto channel = weak.lock()) - { - this->joinBucket_->send(channel->getName()); - } + this->joinBucket_->send(channel->getName()); } // connected/disconnected message @@ -952,14 +969,8 @@ void TwitchIrcServer::onReadConnected(IrcConnection *connection) auto reconnected = makeSystemMessage("reconnected"); reconnected->flags.set(MessageFlag::ConnectedMessage); - for (std::weak_ptr &weak : this->channels.values()) + for (const auto &chan : activeChannels) { - std::shared_ptr chan = weak.lock(); - if (!chan) - { - continue; - } - LimitedQueueSnapshot snapshot = chan->getMessageSnapshot(); bool replaceMessage = diff --git a/src/singletons/WindowManager.cpp b/src/singletons/WindowManager.cpp index 9d3be8ca..b95ff357 100644 --- a/src/singletons/WindowManager.cpp +++ b/src/singletons/WindowManager.cpp @@ -595,6 +595,26 @@ void WindowManager::toggleAllOverlayInertia() } } +std::set WindowManager::getVisibleChannelNames() const +{ + std::set visible; + for (auto *window : this->windows_) + { + auto *page = window->getNotebook().getSelectedPage(); + if (!page) + { + continue; + } + + for (auto *split : page->getSplits()) + { + visible.emplace(split->getChannel()->getName()); + } + } + + return visible; +} + void WindowManager::encodeTab(SplitContainer *tab, bool isSelected, QJsonObject &obj) { diff --git a/src/singletons/WindowManager.hpp b/src/singletons/WindowManager.hpp index 8e737147..f5957bdf 100644 --- a/src/singletons/WindowManager.hpp +++ b/src/singletons/WindowManager.hpp @@ -9,6 +9,7 @@ #include #include +#include namespace chatterino { @@ -131,6 +132,8 @@ public: /// Toggles the inertia in all open overlay windows void toggleAllOverlayInertia(); + std::set getVisibleChannelNames() const; + /// Signals pajlada::Signals::NoArgSignal gifRepaintRequested;