Skip to content

Commit

Permalink
refactor: turn StreamerMode into a singleton(-like thing) (Chatteri…
Browse files Browse the repository at this point in the history
  • Loading branch information
Nerixyz authored Mar 1, 2024
1 parent ea19c5c commit c1fa512
Show file tree
Hide file tree
Showing 28 changed files with 386 additions and 210 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@
- Dev: Twitch messages can be sent using Twitch's Helix API instead of IRC (disabled by default). (#5200)
- Dev: Added estimation for image sizes to avoid layout shifts. (#5192)
- Dev: Added the `launachable` entry to Linux AppData. (#5210)
- Dev: Refactor `StreamerMode`. (#5216)

## 2.4.6

Expand Down
7 changes: 7 additions & 0 deletions benchmarks/src/RecentMessages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "controllers/accounts/AccountController.hpp"
#include "controllers/highlights/HighlightController.hpp"
#include "messages/Emote.hpp"
#include "mocks/DisabledStreamerMode.hpp"
#include "mocks/EmptyApplication.hpp"
#include "mocks/TwitchIrcServer.hpp"
#include "mocks/UserData.hpp"
Expand Down Expand Up @@ -93,6 +94,11 @@ class MockApplication : mock::EmptyApplication
return &this->seventvEmotes;
}

IStreamerMode *getStreamerMode() override
{
return &this->streamerMode;
}

AccountController accounts;
Emotes emotes;
mock::UserDataController userData;
Expand All @@ -105,6 +111,7 @@ class MockApplication : mock::EmptyApplication
BttvEmotes bttvEmotes;
FfzEmotes ffzEmotes;
SeventvEmotes seventvEmotes;
DisabledStreamerMode streamerMode;
};

std::optional<QJsonDocument> tryReadJsonFile(const QString &path)
Expand Down
12 changes: 12 additions & 0 deletions mocks/include/mocks/DisabledStreamerMode.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#include "singletons/StreamerMode.hpp"

class DisabledStreamerMode : public chatterino::IStreamerMode
{
public:
bool isEnabled() const override
{
return false;
}
};
7 changes: 7 additions & 0 deletions mocks/include/mocks/EmptyApplication.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,13 @@ class EmptyApplication : public IApplication
return nullptr;
}

IStreamerMode *getStreamerMode() override
{
assert(false && "EmptyApplication::getStreamerMode was called without "
"being initialized");
return nullptr;
}

private:
Paths paths_;
Args args_;
Expand Down
16 changes: 11 additions & 5 deletions src/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
#include "singletons/Logging.hpp"
#include "singletons/Paths.hpp"
#include "singletons/Settings.hpp"
#include "singletons/StreamerMode.hpp"
#include "singletons/Theme.hpp"
#include "singletons/Toasts.hpp"
#include "singletons/Updates.hpp"
Expand Down Expand Up @@ -144,6 +145,7 @@ Application::Application(Settings &_settings, const Paths &paths,
, seventvEmotes(new SeventvEmotes)
, logging(new Logging(_settings))
, linkResolver(new LinkResolver)
, streamerMode(new StreamerMode)
#ifdef CHATTERINO_HAVE_PLUGINS
, plugins(&this->emplace(new PluginController(paths)))
#endif
Expand Down Expand Up @@ -503,6 +505,11 @@ ILinkResolver *Application::getLinkResolver()
return this->linkResolver.get();
}

IStreamerMode *Application::getStreamerMode()
{
return this->streamerMode.get();
}

BttvEmotes *Application::getBttvEmotes()
{
assertInGuiThread();
Expand Down Expand Up @@ -707,7 +714,7 @@ void Application::initPubSub()
}

if (getSettings()->streamerModeHideModActions &&
isInStreamerMode())
this->getStreamerMode()->isEnabled())
{
return;
}
Expand Down Expand Up @@ -756,7 +763,7 @@ void Application::initPubSub()
}

if (getSettings()->streamerModeHideModActions &&
isInStreamerMode())
this->getStreamerMode()->isEnabled())
{
return;
}
Expand Down Expand Up @@ -892,9 +899,8 @@ void Application::initPubSub()

std::ignore = this->twitchPubSub->moderation.automodUserMessage.connect(
[&](const auto &action) {
// This condition has been set up to execute isInStreamerMode() as the last thing
// as it could end up being expensive.
if (getSettings()->streamerModeHideModActions && isInStreamerMode())
if (getSettings()->streamerModeHideModActions &&
this->getStreamerMode()->isEnabled())
{
return;
}
Expand Down
8 changes: 4 additions & 4 deletions src/Application.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
#include "debug/AssertInGuiThread.hpp"
#include "singletons/NativeMessaging.hpp"

#include <pajlada/signals.hpp>
#include <pajlada/signals/signal.hpp>
#include <QApplication>

#include <cassert>
Expand Down Expand Up @@ -55,6 +53,7 @@ class BttvEmotes;
class FfzEmotes;
class SeventvEmotes;
class ILinkResolver;
class IStreamerMode;

class IApplication
{
Expand Down Expand Up @@ -97,6 +96,7 @@ class IApplication
virtual FfzEmotes *getFfzEmotes() = 0;
virtual SeventvEmotes *getSeventvEmotes() = 0;
virtual ILinkResolver *getLinkResolver() = 0;
virtual IStreamerMode *getStreamerMode() = 0;
};

class Application : public IApplication
Expand Down Expand Up @@ -165,6 +165,7 @@ class Application : public IApplication
std::unique_ptr<SeventvEmotes> seventvEmotes;
const std::unique_ptr<Logging> logging;
std::unique_ptr<ILinkResolver> linkResolver;
std::unique_ptr<IStreamerMode> streamerMode;
#ifdef CHATTERINO_HAVE_PLUGINS
PluginController *const plugins{};
#endif
Expand Down Expand Up @@ -216,8 +217,7 @@ class Application : public IApplication
SeventvEmotes *getSeventvEmotes() override;

ILinkResolver *getLinkResolver() override;

pajlada::Signals::NoArgSignal streamerModeChanged;
IStreamerMode *getStreamerMode() override;

private:
void addSingleton(Singleton *singleton);
Expand Down
5 changes: 3 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,8 @@ set(SOURCE_FILES
singletons/Resources.hpp
singletons/Settings.cpp
singletons/Settings.hpp
singletons/StreamerMode.cpp
singletons/StreamerMode.hpp
singletons/Theme.cpp
singletons/Theme.hpp
singletons/Toasts.cpp
Expand Down Expand Up @@ -505,8 +507,6 @@ set(SOURCE_FILES
util/SplitCommand.hpp
util/StreamLink.cpp
util/StreamLink.hpp
util/StreamerMode.cpp
util/StreamerMode.hpp
util/ThreadGuard.hpp
util/Twitch.cpp
util/Twitch.hpp
Expand Down Expand Up @@ -770,6 +770,7 @@ target_link_libraries(${LIBRARY_PROJECT}
RapidJSON::RapidJSON
LRUCache
MagicEnum
$<$<BOOL:${WIN32}>:Wtsapi32>
)
if (CHATTERINO_PLUGINS)
target_link_libraries(${LIBRARY_PROJECT} PUBLIC lua)
Expand Down
3 changes: 2 additions & 1 deletion src/controllers/commands/builtin/twitch/SendWhisper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "providers/twitch/TwitchIrcServer.hpp"
#include "singletons/Emotes.hpp"
#include "singletons/Settings.hpp"
#include "singletons/StreamerMode.hpp"
#include "singletons/Theme.hpp"
#include "util/Twitch.hpp"

Expand Down Expand Up @@ -183,7 +184,7 @@ bool appendWhisperMessageWordsLocally(const QStringList &words)

if (getSettings()->inlineWhispers &&
!(getSettings()->streamerModeSuppressInlineWhispers &&
isInStreamerMode()))
getIApp()->getStreamerMode()->isEnabled()))
{
app->twitch->forEachChannel(
[&messagexD, overrideFlags](ChannelPtr _channel) {
Expand Down
6 changes: 4 additions & 2 deletions src/controllers/notifications/NotificationController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "providers/twitch/TwitchIrcServer.hpp"
#include "providers/twitch/TwitchMessageBuilder.hpp"
#include "singletons/Settings.hpp"
#include "singletons/StreamerMode.hpp"
#include "singletons/Toasts.hpp"
#include "singletons/WindowManager.hpp"
#include "util/Helpers.hpp"
Expand Down Expand Up @@ -186,14 +187,15 @@ void NotificationController::checkStream(bool live, QString channelName)
getIApp()->getToasts()->sendChannelNotification(channelName, QString(),
Platform::Twitch);
}
bool inStreamerMode = getIApp()->getStreamerMode()->isEnabled();
if (getSettings()->notificationPlaySound &&
!(isInStreamerMode() &&
!(inStreamerMode &&
getSettings()->streamerModeSuppressLiveNotifications))
{
getIApp()->getNotifications()->playSound();
}
if (getSettings()->notificationFlashTaskbar &&
!(isInStreamerMode() &&
!(inStreamerMode &&
getSettings()->streamerModeSuppressLiveNotifications))
{
getIApp()->getWindows()->sendAlert();
Expand Down
5 changes: 3 additions & 2 deletions src/messages/SharedMessageBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
#include "messages/MessageElement.hpp"
#include "providers/twitch/TwitchBadge.hpp"
#include "singletons/Settings.hpp"
#include "singletons/StreamerMode.hpp"
#include "singletons/WindowManager.hpp"
#include "util/Helpers.hpp"
#include "util/Qt.hpp"
#include "util/StreamerMode.hpp"

#include <QFileInfo>

Expand Down Expand Up @@ -204,7 +204,8 @@ void SharedMessageBuilder::triggerHighlights(
const QString &channelName, bool playSound,
const std::optional<QUrl> &customSoundUrl, bool windowAlert)
{
if (isInStreamerMode() && getSettings()->streamerModeMuteMentions)
if (getIApp()->getStreamerMode()->isEnabled() &&
getSettings()->streamerModeMuteMentions)
{
// We are in streamer mode with muting mention sounds enabled. Do nothing.
return;
Expand Down
6 changes: 2 additions & 4 deletions src/messages/layouts/MessageLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
#include "messages/Selection.hpp"
#include "providers/colors/ColorProvider.hpp"
#include "singletons/Settings.hpp"
#include "singletons/StreamerMode.hpp"
#include "singletons/WindowManager.hpp"
#include "util/DebugCount.hpp"
#include "util/StreamerMode.hpp"

#include <QApplication>
#include <QDebug>
Expand Down Expand Up @@ -160,11 +160,9 @@ void MessageLayout::actuallyLayout(int width, MessageElementFlags flags)
if (this->message_->flags.has(MessageFlag::Timeout) ||
this->message_->flags.has(MessageFlag::Untimeout))
{
// This condition has been set up to execute isInStreamerMode() as the last thing
// as it could end up being expensive.
if (hideModerationActions ||
(getSettings()->streamerModeHideModActions &&
isInStreamerMode()))
getIApp()->getStreamerMode()->isEnabled()))
{
continue;
}
Expand Down
4 changes: 2 additions & 2 deletions src/providers/twitch/IrcMessageHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
#include "providers/twitch/TwitchMessageBuilder.hpp"
#include "singletons/Resources.hpp"
#include "singletons/Settings.hpp"
#include "singletons/StreamerMode.hpp"
#include "singletons/WindowManager.hpp"
#include "util/ChannelHelpers.hpp"
#include "util/FormatTime.hpp"
#include "util/Helpers.hpp"
#include "util/IrcHelpers.hpp"
#include "util/StreamerMode.hpp"

#include <IrcMessage>
#include <QLocale>
Expand Down Expand Up @@ -931,7 +931,7 @@ void IrcMessageHandler::handleWhisperMessage(Communi::IrcMessage *ircMessage)

if (getSettings()->inlineWhispers &&
!(getSettings()->streamerModeSuppressInlineWhispers &&
isInStreamerMode()))
getIApp()->getStreamerMode()->isEnabled()))
{
getApp()->twitch->forEachChannel(
[&message, overrideFlags](ChannelPtr channel) {
Expand Down
1 change: 0 additions & 1 deletion src/providers/twitch/PubSubManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include "util/DebugCount.hpp"
#include "util/Helpers.hpp"
#include "util/RapidjsonHelpers.hpp"
#include "util/StreamerMode.hpp"

#include <QJsonArray>

Expand Down
3 changes: 2 additions & 1 deletion src/providers/twitch/TwitchChannel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "providers/twitch/TwitchMessageBuilder.hpp"
#include "singletons/Emotes.hpp"
#include "singletons/Settings.hpp"
#include "singletons/StreamerMode.hpp"
#include "singletons/Toasts.hpp"
#include "singletons/WindowManager.hpp"
#include "util/Helpers.hpp"
Expand Down Expand Up @@ -180,7 +181,7 @@ TwitchChannel::TwitchChannel(const QString &name)

// Notify on all channels with a ping sound
if (getSettings()->notificationOnAnyChannel &&
!(isInStreamerMode() &&
!(getIApp()->getStreamerMode()->isEnabled() &&
getSettings()->streamerModeSuppressLiveNotifications))
{
getIApp()->getNotifications()->playSound();
Expand Down
5 changes: 0 additions & 5 deletions src/singletons/Settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,6 @@ Settings::Settings(const QString &settingsDirectory)
},
false);
#endif
this->enableStreamerMode.connect(
[]() {
getApp()->streamerModeChanged.invoke();
},
false);
}

Settings::~Settings()
Expand Down
7 changes: 6 additions & 1 deletion src/singletons/Settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include "controllers/sound/ISoundController.hpp"
#include "singletons/Toasts.hpp"
#include "util/RapidJsonSerializeQString.hpp"
#include "util/StreamerMode.hpp"
#include "widgets/Notebook.hpp"

#include <pajlada/settings/setting.hpp>
Expand Down Expand Up @@ -68,6 +67,12 @@ enum class ChatSendProtocol : int {
Helix = 2,
};

enum StreamerModeSetting {
Disabled = 0,
Enabled = 1,
DetectStreamingSoftware = 2,
};

/// Settings which are availlable for reading and writing on the gui thread.
// These settings are still accessed concurrently in the code but it is bad practice.
class Settings
Expand Down
Loading

0 comments on commit c1fa512

Please sign in to comment.