From c609a9fd373ddec06b3cb61b8d6491fc44500831 Mon Sep 17 00:00:00 2001 From: fourtf Date: Sat, 7 Jul 2018 11:41:01 +0200 Subject: [PATCH] started to refactor Application --- src/Application.cpp | 67 +++++++++---------- src/Application.hpp | 16 +++-- src/common/Singleton.hpp | 9 ++- .../accounts/AccountController.cpp | 2 +- .../accounts/AccountController.hpp | 6 +- .../commands/CommandController.cpp | 5 ++ .../commands/CommandController.hpp | 10 ++- .../highlights/HighlightController.cpp | 2 +- .../highlights/HighlightController.hpp | 6 +- src/controllers/ignores/IgnoreController.cpp | 2 +- src/controllers/ignores/IgnoreController.hpp | 6 +- .../moderationactions/ModerationActions.cpp | 2 +- .../moderationactions/ModerationActions.hpp | 6 +- .../taggedusers/TaggedUsersController.hpp | 4 +- src/providers/twitch/TwitchServer.cpp | 12 +++- src/providers/twitch/TwitchServer.hpp | 12 +++- src/singletons/Emotes.cpp | 6 +- src/singletons/Emotes.hpp | 8 +-- src/singletons/Fonts.cpp | 57 ++++++++-------- src/singletons/Fonts.hpp | 6 +- src/singletons/Logging.cpp | 9 +-- src/singletons/Logging.hpp | 8 +-- src/singletons/NativeMessaging.hpp | 7 +- src/singletons/Paths.cpp | 5 ++ src/singletons/Paths.hpp | 2 + src/singletons/Resources.cpp | 2 +- src/singletons/Resources.hpp | 6 +- src/singletons/Settings.cpp | 17 +---- src/singletons/Theme.hpp | 5 +- src/singletons/WindowManager.cpp | 26 +++++-- src/singletons/WindowManager.hpp | 9 +-- src/widgets/helper/ChannelView.cpp | 2 +- 32 files changed, 195 insertions(+), 147 deletions(-) diff --git a/src/Application.cpp b/src/Application.cpp index 6f59e012..9068bf9a 100644 --- a/src/Application.cpp +++ b/src/Application.cpp @@ -36,6 +36,8 @@ Application::Application(int _argc, char **_argv) : argc_(_argc) , argv_(_argv) { + getSettings()->initialize(); + getSettings()->load(); } void Application::construct() @@ -45,23 +47,24 @@ void Application::construct() // 1. Instantiate all classes this->settings = getSettings(); - this->paths = Paths::getInstance(); + this->paths = getPaths(); - this->themes = new Theme; - this->windows = new WindowManager; - this->logging = new Logging; - this->commands = new CommandController; - this->highlights = new HighlightController; - this->ignores = new IgnoreController; - this->taggedUsers = new TaggedUsersController; - this->accounts = new AccountController; - this->emotes = new Emotes; - this->fonts = new Fonts; - this->resources = new Resources; - this->moderationActions = new ModerationActions; + this->addSingleton(this->themes = new Theme); + this->addSingleton(this->windows = new WindowManager); + this->addSingleton(this->logging = new Logging); + this->addSingleton(this->commands = new CommandController); + this->addSingleton(this->highlights = new HighlightController); + this->addSingleton(this->ignores = new IgnoreController); + this->addSingleton(this->taggedUsers = new TaggedUsersController); + this->addSingleton(this->accounts = new AccountController); + this->addSingleton(this->emotes = new Emotes); + this->addSingleton(this->fonts = new Fonts); + this->addSingleton(this->resources = new Resources); + this->addSingleton(this->moderationActions = new ModerationActions); - this->twitch.server = new TwitchServer; - this->twitch.pubsub = new PubSub; + this->addSingleton(this->twitch2 = new TwitchServer); + this->twitch.server = this->twitch2; + this->twitch.pubsub = this->twitch2->pubsub; } void Application::instantiate(int argc, char **argv) @@ -77,24 +80,9 @@ void Application::initialize() isAppInitialized = true; // 2. Initialize/load classes - this->settings->initialize(); - - this->settings->load(); - this->commands->load(); - this->logging->initialize(); - this->windows->initialize(); - - this->resources->initialize(); - - this->highlights->initialize(); - this->ignores->initialize(); - - this->emotes->initialize(); - - this->accounts->load(); - - this->twitch.server->initialize(); - this->moderationActions->initialize(); + for (Singleton *singleton : this->singletons_) { + singleton->initialize(*this); + } // XXX this->windows->updateWordTypeMask(); @@ -197,11 +185,11 @@ void Application::initialize() this->twitch.pubsub->start(); auto RequestModerationActions = [=]() { - this->twitch.pubsub->unlistenAllModerationActions(); + this->twitch.server->pubsub->unlistenAllModerationActions(); // TODO(pajlada): Unlisten to all authed topics instead of only moderation topics // this->twitch.pubsub->UnlistenAllAuthedTopics(); - this->twitch.pubsub->listenToWhispers(this->accounts->twitch.getCurrent()); // + this->twitch.server->pubsub->listenToWhispers(this->accounts->twitch.getCurrent()); // }; this->accounts->twitch.currentUserChanged.connect(RequestModerationActions); @@ -222,9 +210,14 @@ int Application::run(QApplication &qtApp) void Application::save() { - this->windows->save(); + for (Singleton *singleton : this->singletons_) { + singleton->save(); + } +} - this->commands->save(); +void Application::addSingleton(Singleton *singleton) +{ + this->singletons_.push_back(singleton); } Application *getApp() diff --git a/src/Application.hpp b/src/Application.hpp index f792bc57..8fda2ebb 100644 --- a/src/Application.hpp +++ b/src/Application.hpp @@ -6,6 +6,8 @@ namespace chatterino { +class Singleton; + class TwitchServer; class PubSub; @@ -44,7 +46,9 @@ public: friend void test(); - Paths *paths = nullptr; + [[deprecated("use getSettings() instead")]] Settings *settings = nullptr; + [[deprecated("use getPaths() instead")]] Paths *paths = nullptr; + Theme *themes = nullptr; WindowManager *windows = nullptr; Logging *logging = nullptr; @@ -55,15 +59,15 @@ public: AccountController *accounts = nullptr; Emotes *emotes = nullptr; NativeMessaging *nativeMessaging = nullptr; - Settings *settings = nullptr; Fonts *fonts = nullptr; Resources *resources = nullptr; ModerationActions *moderationActions = nullptr; + TwitchServer *twitch2 = nullptr; /// Provider-specific struct { - TwitchServer *server = nullptr; - PubSub *pubsub = nullptr; + [[deprecated("use twitch2 instead")]] TwitchServer *server = nullptr; + [[deprecated("use twitch2->pubsub instead")]] PubSub *pubsub = nullptr; } twitch; void save(); @@ -72,8 +76,12 @@ public: static void runNativeMessagingHost(); private: + void addSingleton(Singleton *singleton); + int argc_; char **argv_; + + std::vector singletons_; }; Application *getApp(); diff --git a/src/common/Singleton.hpp b/src/common/Singleton.hpp index 9a1b52c7..474c31f0 100644 --- a/src/common/Singleton.hpp +++ b/src/common/Singleton.hpp @@ -8,14 +8,13 @@ class Application; class Singleton : boost::noncopyable { - virtual ~Singleton() = default; - - virtual void initialize(Application &application) +public: + virtual void initialize(Application &app) { - (void)(application); + (void)(app); } - virtual void finalize() + virtual void save() { } }; diff --git a/src/controllers/accounts/AccountController.cpp b/src/controllers/accounts/AccountController.cpp index 17dca956..0341a06a 100644 --- a/src/controllers/accounts/AccountController.cpp +++ b/src/controllers/accounts/AccountController.cpp @@ -33,7 +33,7 @@ AccountController::AccountController() }); } -void AccountController::load() +void AccountController::initialize(Application &app) { this->twitch.load(); } diff --git a/src/controllers/accounts/AccountController.hpp b/src/controllers/accounts/AccountController.hpp index 2b273bfc..69fc7f09 100644 --- a/src/controllers/accounts/AccountController.hpp +++ b/src/controllers/accounts/AccountController.hpp @@ -1,5 +1,7 @@ #pragma once +#include "common/Singleton.hpp" + #include #include "common/SignalVector.hpp" @@ -11,14 +13,14 @@ namespace chatterino { class AccountModel; -class AccountController +class AccountController : public Singleton { public: AccountController(); AccountModel *createModel(QObject *parent); - void load(); + virtual void initialize(Application &app) override; TwitchAccountManager twitch; diff --git a/src/controllers/commands/CommandController.cpp b/src/controllers/commands/CommandController.cpp index f8650649..8d33391f 100644 --- a/src/controllers/commands/CommandController.cpp +++ b/src/controllers/commands/CommandController.cpp @@ -43,6 +43,11 @@ CommandController::CommandController() this->items.itemRemoved.connect(addFirstMatchToMap); } +void CommandController::initialize(Application &app) +{ + this->load(); +} + void CommandController::load() { auto app = getApp(); diff --git a/src/controllers/commands/CommandController.hpp b/src/controllers/commands/CommandController.hpp index 4f500537..c1cbfd85 100644 --- a/src/controllers/commands/CommandController.hpp +++ b/src/controllers/commands/CommandController.hpp @@ -1,5 +1,7 @@ #pragma once +#include "common/Singleton.hpp" + #include #include #include @@ -12,7 +14,7 @@ class Channel; class CommandModel; -class CommandController +class CommandController : public Singleton { public: CommandController(); @@ -20,14 +22,16 @@ public: QString execCommand(const QString &text, std::shared_ptr channel, bool dryRun); QStringList getDefaultTwitchCommandList(); - void load(); - void save(); + virtual void initialize(Application &app) override; + virtual void save() override; CommandModel *createModel(QObject *parent); UnsortedSignalVector items; private: + void load(); + QMap commandsMap_; std::mutex mutex_; diff --git a/src/controllers/highlights/HighlightController.cpp b/src/controllers/highlights/HighlightController.cpp index 32327d8f..02c420c7 100644 --- a/src/controllers/highlights/HighlightController.cpp +++ b/src/controllers/highlights/HighlightController.cpp @@ -12,7 +12,7 @@ HighlightController::HighlightController() { } -void HighlightController::initialize() +void HighlightController::initialize(Application &app) { assert(!this->initialized_); this->initialized_ = true; diff --git a/src/controllers/highlights/HighlightController.hpp b/src/controllers/highlights/HighlightController.hpp index f9db887b..a881499c 100644 --- a/src/controllers/highlights/HighlightController.hpp +++ b/src/controllers/highlights/HighlightController.hpp @@ -1,5 +1,7 @@ #pragma once +#include "common/Singleton.hpp" + #include "common/SignalVector.hpp" #include "controllers/highlights/HighlightBlacklistUser.hpp" #include "controllers/highlights/HighlightPhrase.hpp" @@ -12,12 +14,12 @@ class UserHighlightModel; class HighlightModel; class HighlightBlacklistModel; -class HighlightController +class HighlightController : public Singleton { public: HighlightController(); - void initialize(); + virtual void initialize(Application &app) override; UnsortedSignalVector phrases; UnsortedSignalVector blacklistedUsers; diff --git a/src/controllers/ignores/IgnoreController.cpp b/src/controllers/ignores/IgnoreController.cpp index 3c721a1c..5aa271cb 100644 --- a/src/controllers/ignores/IgnoreController.cpp +++ b/src/controllers/ignores/IgnoreController.cpp @@ -7,7 +7,7 @@ namespace chatterino { -void IgnoreController::initialize() +void IgnoreController::initialize(Application &) { assert(!this->initialized_); this->initialized_ = true; diff --git a/src/controllers/ignores/IgnoreController.hpp b/src/controllers/ignores/IgnoreController.hpp index 0a9ed277..f0346013 100644 --- a/src/controllers/ignores/IgnoreController.hpp +++ b/src/controllers/ignores/IgnoreController.hpp @@ -1,5 +1,7 @@ #pragma once +#include "common/Singleton.hpp" + #include "common/SignalVector.hpp" #include "controllers/ignores/IgnorePhrase.hpp" #include "singletons/Settings.hpp" @@ -8,10 +10,10 @@ namespace chatterino { class IgnoreModel; -class IgnoreController +class IgnoreController : public Singleton { public: - void initialize(); + virtual void initialize(Application &app) override; UnsortedSignalVector phrases; diff --git a/src/controllers/moderationactions/ModerationActions.cpp b/src/controllers/moderationactions/ModerationActions.cpp index 7cef80fa..c130e65a 100644 --- a/src/controllers/moderationactions/ModerationActions.cpp +++ b/src/controllers/moderationactions/ModerationActions.cpp @@ -12,7 +12,7 @@ ModerationActions::ModerationActions() { } -void ModerationActions::initialize() +void ModerationActions::initialize(Application &app) { assert(!this->initialized_); this->initialized_ = true; diff --git a/src/controllers/moderationactions/ModerationActions.hpp b/src/controllers/moderationactions/ModerationActions.hpp index 935567c3..934e2f95 100644 --- a/src/controllers/moderationactions/ModerationActions.hpp +++ b/src/controllers/moderationactions/ModerationActions.hpp @@ -1,5 +1,7 @@ #pragma once +#include "common/Singleton.hpp" + #include "common/ChatterinoSetting.hpp" #include "common/SignalVector.hpp" #include "controllers/moderationactions/ModerationAction.hpp" @@ -8,12 +10,12 @@ namespace chatterino { class ModerationActionModel; -class ModerationActions +class ModerationActions final : public Singleton { public: ModerationActions(); - void initialize(); + virtual void initialize(Application &app) override; UnsortedSignalVector items; diff --git a/src/controllers/taggedusers/TaggedUsersController.hpp b/src/controllers/taggedusers/TaggedUsersController.hpp index b09cea72..cfd98b62 100644 --- a/src/controllers/taggedusers/TaggedUsersController.hpp +++ b/src/controllers/taggedusers/TaggedUsersController.hpp @@ -1,5 +1,7 @@ #pragma once +#include "common/Singleton.hpp" + #include "common/SignalVector.hpp" #include "controllers/taggedusers/TaggedUser.hpp" @@ -7,7 +9,7 @@ namespace chatterino { class TaggedUsersModel; -class TaggedUsersController +class TaggedUsersController : public Singleton { public: TaggedUsersController(); diff --git a/src/providers/twitch/TwitchServer.cpp b/src/providers/twitch/TwitchServer.cpp index 159f5414..247d28fe 100644 --- a/src/providers/twitch/TwitchServer.cpp +++ b/src/providers/twitch/TwitchServer.cpp @@ -1,9 +1,11 @@ #include "TwitchServer.hpp" #include "Application.hpp" +#include "common/Common.hpp" #include "controllers/accounts/AccountController.hpp" #include "controllers/highlights/HighlightController.hpp" #include "providers/twitch/IrcMessageHandler.hpp" +#include "providers/twitch/PubsubClient.hpp" #include "providers/twitch/TwitchAccount.hpp" #include "providers/twitch/TwitchHelpers.hpp" #include "providers/twitch/TwitchMessageBuilder.hpp" @@ -24,16 +26,22 @@ TwitchServer::TwitchServer() , watchingChannel(Channel::getEmpty(), Channel::Type::TwitchWatching) { qDebug() << "init TwitchServer"; + + this->pubsub = new PubSub; } -void TwitchServer::initialize() +void TwitchServer::initialize(Application &app) { - getApp()->accounts->twitch.currentUserChanged.connect( + this->app = &app; + + app.accounts->twitch.currentUserChanged.connect( [this]() { postToThread([this] { this->connect(); }); }); } void TwitchServer::initializeConnection(IrcConnection *connection, bool isRead, bool isWrite) { + assert(this->app); + std::shared_ptr account = getApp()->accounts->twitch.getCurrent(); qDebug() << "logging in as" << account->getUserName(); diff --git a/src/providers/twitch/TwitchServer.hpp b/src/providers/twitch/TwitchServer.hpp index 6ff5c4fe..4587d0d8 100644 --- a/src/providers/twitch/TwitchServer.hpp +++ b/src/providers/twitch/TwitchServer.hpp @@ -1,6 +1,7 @@ #pragma once #include "common/MutexValue.hpp" +#include "common/Singleton.hpp" #include "providers/irc/AbstractIrcServer.hpp" #include "providers/twitch/TwitchAccount.hpp" #include "providers/twitch/TwitchChannel.hpp" @@ -11,12 +12,15 @@ namespace chatterino { -class TwitchServer final : public AbstractIrcServer +class PubSub; + +class TwitchServer : public AbstractIrcServer, public Singleton { public: TwitchServer(); + virtual ~TwitchServer() override = default; - void initialize(); + virtual void initialize(Application &app) override; // fourtf: ugh void forEachChannelAndSpecialChannels(std::function func); @@ -29,6 +33,8 @@ public: const ChannelPtr mentionsChannel; IndirectChannel watchingChannel; + PubSub *pubsub; + protected: void initializeConnection(IrcConnection *connection, bool isRead, bool isWrite) override; std::shared_ptr createChannel(const QString &channelName) override; @@ -44,6 +50,8 @@ protected: private: void onMessageSendRequested(TwitchChannel *channel, const QString &message, bool &sent); + Application *app = nullptr; + std::mutex lastMessageMutex_; std::queue lastMessagePleb_; std::queue lastMessageMod_; diff --git a/src/singletons/Emotes.cpp b/src/singletons/Emotes.cpp index f8f68348..df029c76 100644 --- a/src/singletons/Emotes.cpp +++ b/src/singletons/Emotes.cpp @@ -5,10 +5,10 @@ namespace chatterino { -void Emotes::initialize() +void Emotes::initialize(Application &app) { - getApp()->accounts->twitch.currentUserChanged.connect([this] { - auto currentUser = getApp()->accounts->twitch.getCurrent(); + app.accounts->twitch.currentUserChanged.connect([this, &app] { + auto currentUser = app.accounts->twitch.getCurrent(); assert(currentUser); this->twitch.refresh(currentUser); }); diff --git a/src/singletons/Emotes.hpp b/src/singletons/Emotes.hpp index 7500dba9..27999243 100644 --- a/src/singletons/Emotes.hpp +++ b/src/singletons/Emotes.hpp @@ -1,5 +1,7 @@ #pragma once +#include "common/Singleton.hpp" + #define GIF_FRAME_LENGTH 33 #include "providers/bttv/BttvEmotes.hpp" @@ -10,12 +12,10 @@ namespace chatterino { -class Emotes +class Emotes final : public Singleton { public: - ~Emotes() = delete; - - void initialize(); + virtual void initialize(Application &app) override; bool isIgnoredEmote(const QString &emote); diff --git a/src/singletons/Fonts.cpp b/src/singletons/Fonts.cpp index e557b40d..2760723e 100644 --- a/src/singletons/Fonts.cpp +++ b/src/singletons/Fonts.cpp @@ -26,37 +26,38 @@ Fonts::Fonts() : chatFontFamily("/appearance/currentFontFamily", DEFAULT_FONT_FAMILY) , chatFontSize("/appearance/currentFontSize", DEFAULT_FONT_SIZE) { - qDebug() << "init FontManager"; - - this->chatFontFamily.connect([this](const std::string &, auto) { - assertInGuiThread(); - - if (getApp()->windows) { - getApp()->windows->incGeneration(); - } - - for (auto &map : this->fontsByType_) { - map.clear(); - } - this->fontChanged.invoke(); - }); - - this->chatFontSize.connect([this](const int &, auto) { - assertInGuiThread(); - - if (getApp()->windows) { - getApp()->windows->incGeneration(); - } - - for (auto &map : this->fontsByType_) { - map.clear(); - } - this->fontChanged.invoke(); - }); - this->fontsByType_.resize(size_t(EndType)); } +void Fonts::initialize(Application &app) +{ + this->chatFontFamily.connect([this, &app](const std::string &, auto) { + assertInGuiThread(); + + if (app.windows) { + app.windows->incGeneration(); + } + + for (auto &map : this->fontsByType_) { + map.clear(); + } + this->fontChanged.invoke(); + }); + + this->chatFontSize.connect([this, &app](const int &, auto) { + assertInGuiThread(); + + if (app.windows) { + app.windows->incGeneration(); + } + + for (auto &map : this->fontsByType_) { + map.clear(); + } + this->fontChanged.invoke(); + }); +} + QFont Fonts::getFont(Fonts::Type type, float scale) { return this->getOrCreateFontData(type, scale).font; diff --git a/src/singletons/Fonts.hpp b/src/singletons/Fonts.hpp index c3f633c5..5a719229 100644 --- a/src/singletons/Fonts.hpp +++ b/src/singletons/Fonts.hpp @@ -1,5 +1,7 @@ #pragma once +#include "common/Singleton.hpp" + #include #include #include @@ -11,11 +13,13 @@ namespace chatterino { -class Fonts : boost::noncopyable +class Fonts final : public Singleton { public: Fonts(); + virtual void initialize(Application &app) override; + // font data gets set in createFontData(...) enum Type : uint8_t { Tiny, diff --git a/src/singletons/Logging.cpp b/src/singletons/Logging.cpp index 5ef531a3..711e64a9 100644 --- a/src/singletons/Logging.cpp +++ b/src/singletons/Logging.cpp @@ -12,16 +12,13 @@ namespace chatterino { -void Logging::initialize() +void Logging::initialize(Application &app) { - this->pathManager = getApp()->paths; } void Logging::addMessage(const QString &channelName, MessagePtr message) { - auto app = getApp(); - - if (!app->settings->enableLogging) { + if (!getSettings()->enableLogging) { return; } @@ -30,7 +27,7 @@ void Logging::addMessage(const QString &channelName, MessagePtr message) auto channel = new LoggingChannel(channelName); channel->addMessage(message); this->loggingChannels_.emplace(channelName, - std::unique_ptr(std::move(channel))); + std::unique_ptr(std::move(channel))); } else { it->second->addMessage(message); } diff --git a/src/singletons/Logging.hpp b/src/singletons/Logging.hpp index 61e261ee..f23d8c0e 100644 --- a/src/singletons/Logging.hpp +++ b/src/singletons/Logging.hpp @@ -1,5 +1,7 @@ #pragma once +#include "common/Singleton.hpp" + #include "messages/Message.hpp" #include "singletons/helper/LoggingChannel.hpp" @@ -9,16 +11,14 @@ namespace chatterino { class Paths; -class Logging +class Logging : public Singleton { Paths *pathManager = nullptr; public: Logging() = default; - ~Logging() = delete; - - void initialize(); + virtual void initialize(Application &app) override; void addMessage(const QString &channelName, MessagePtr message); diff --git a/src/singletons/NativeMessaging.hpp b/src/singletons/NativeMessaging.hpp index 9906f833..b9fa7ce6 100644 --- a/src/singletons/NativeMessaging.hpp +++ b/src/singletons/NativeMessaging.hpp @@ -1,17 +1,16 @@ #pragma once +#include "common/Singleton.hpp" + #include namespace chatterino { - -class NativeMessaging +class NativeMessaging final { public: // fourtf: don't add this class to the application class NativeMessaging(); - ~NativeMessaging() = delete; - class ReceiverThread : public QThread { public: diff --git a/src/singletons/Paths.cpp b/src/singletons/Paths.cpp index 7f6123dc..c695e046 100644 --- a/src/singletons/Paths.cpp +++ b/src/singletons/Paths.cpp @@ -114,4 +114,9 @@ void Paths::initSubDirectories() this->miscDirectory = makePath("Misc"); } +Paths *getPaths() +{ + return Paths::getInstance(); +} + } // namespace chatterino diff --git a/src/singletons/Paths.hpp b/src/singletons/Paths.hpp index 53dba36b..e1df4676 100644 --- a/src/singletons/Paths.hpp +++ b/src/singletons/Paths.hpp @@ -45,4 +45,6 @@ private: boost::optional portable_; }; +Paths *getPaths(); + } // namespace chatterino diff --git a/src/singletons/Resources.cpp b/src/singletons/Resources.cpp index db734ce2..0180c581 100644 --- a/src/singletons/Resources.cpp +++ b/src/singletons/Resources.cpp @@ -302,7 +302,7 @@ Resources::Resources() qDebug() << "init ResourceManager"; } -void Resources::initialize() +void Resources::initialize(Application &app) { this->loadDynamicTwitchBadges(); diff --git a/src/singletons/Resources.hpp b/src/singletons/Resources.hpp index 0a521267..7327c4da 100644 --- a/src/singletons/Resources.hpp +++ b/src/singletons/Resources.hpp @@ -1,5 +1,7 @@ #pragma once +#include "common/Singleton.hpp" + #include "common/Emotemap.hpp" #include @@ -11,14 +13,14 @@ namespace chatterino { -class Resources +class Resources : public Singleton { public: Resources(); ~Resources() = delete; - void initialize(); + virtual void initialize(Application &app) override; struct { QIcon left; diff --git a/src/singletons/Settings.cpp b/src/singletons/Settings.cpp index 16165ec1..d2c1718c 100644 --- a/src/singletons/Settings.cpp +++ b/src/singletons/Settings.cpp @@ -29,26 +29,11 @@ Settings &Settings::getInstance() void Settings::initialize() { - this->timestampFormat.connect([](auto, auto) { - auto app = getApp(); - app->windows->layoutChannelViews(); - }); - - this->emoteScale.connect([](auto, auto) { getApp()->windows->forceLayoutChannelViews(); }); - - this->timestampFormat.connect([](auto, auto) { getApp()->windows->forceLayoutChannelViews(); }); - this->alternateMessageBackground.connect( - [](auto, auto) { getApp()->windows->forceLayoutChannelViews(); }); - this->separateMessages.connect( - [](auto, auto) { getApp()->windows->forceLayoutChannelViews(); }); - this->collpseMessagesMinLines.connect( - [](auto, auto) { getApp()->windows->forceLayoutChannelViews(); }); } void Settings::load() { - auto app = getApp(); - QString settingsPath = app->paths->settingsDirectory + "/settings.json"; + QString settingsPath = getPaths()->settingsDirectory + "/settings.json"; pajlada::Settings::SettingManager::load(qPrintable(settingsPath)); } diff --git a/src/singletons/Theme.hpp b/src/singletons/Theme.hpp index 973aea86..25a97827 100644 --- a/src/singletons/Theme.hpp +++ b/src/singletons/Theme.hpp @@ -1,6 +1,7 @@ #pragma once #include "common/SerializeCustom.hpp" +#include "common/Singleton.hpp" #include #include @@ -10,13 +11,11 @@ namespace chatterino { class WindowManager; -class Theme +class Theme final : public Singleton { public: Theme(); - ~Theme() = delete; - inline bool isLightTheme() const { return this->isLight_; diff --git a/src/singletons/WindowManager.cpp b/src/singletons/WindowManager.cpp index 1b56b7de..4077200d 100644 --- a/src/singletons/WindowManager.cpp +++ b/src/singletons/WindowManager.cpp @@ -206,17 +206,16 @@ Window *WindowManager::windowAt(int index) return this->windows_.at(index); } -void WindowManager::initialize() +void WindowManager::initialize(Application &app) { assertInGuiThread(); - auto app = getApp(); - app->themes->repaintVisibleChatWidgets_.connect([this] { this->repaintVisibleChatWidgets(); }); + app.themes->repaintVisibleChatWidgets_.connect([this] { this->repaintVisibleChatWidgets(); }); assert(!this->initialized_); // load file - QString settingsPath = app->paths->settingsDirectory + SETTINGS_FILENAME; + QString settingsPath = getPaths()->settingsDirectory + SETTINGS_FILENAME; QFile file(settingsPath); file.open(QIODevice::ReadOnly); QByteArray data = file.readAll(); @@ -301,6 +300,22 @@ void WindowManager::initialize() mainWindow_->getNotebook().addPage(true); } + auto settings = getSettings(); + + settings->timestampFormat.connect([this](auto, auto) { + auto app = getApp(); + this->layoutChannelViews(); + }); + + settings->emoteScale.connect([this](auto, auto) { this->forceLayoutChannelViews(); }); + + settings->timestampFormat.connect([this](auto, auto) { this->forceLayoutChannelViews(); }); + settings->alternateMessageBackground.connect( + [this](auto, auto) { this->forceLayoutChannelViews(); }); + settings->separateMessages.connect([this](auto, auto) { this->forceLayoutChannelViews(); }); + settings->collpseMessagesMinLines.connect( + [this](auto, auto) { this->forceLayoutChannelViews(); }); + this->initialized_ = true; } @@ -321,9 +336,12 @@ void WindowManager::save() case Window::Type::Main: window_obj.insert("type", "main"); break; + case Window::Type::Popup: window_obj.insert("type", "popup"); break; + + case Window::Type::Attached:; } // window geometry diff --git a/src/singletons/WindowManager.hpp b/src/singletons/WindowManager.hpp index d000349d..217bf737 100644 --- a/src/singletons/WindowManager.hpp +++ b/src/singletons/WindowManager.hpp @@ -1,15 +1,16 @@ #pragma once +#include + #include "widgets/Window.hpp" #include "widgets/splits/SplitContainer.hpp" namespace chatterino { -class WindowManager +class WindowManager : public Singleton { public: WindowManager(); - ~WindowManager() = delete; static void encodeChannel(IndirectChannel channel, QJsonObject &obj); static IndirectChannel decodeChannel(const QJsonObject &obj); @@ -36,8 +37,8 @@ public: int windowCount(); Window *windowAt(int index); - void save(); - void initialize(); + virtual void initialize(Application &app) override; + virtual void save() override; void closeAll(); int getGeneration() const; diff --git a/src/widgets/helper/ChannelView.cpp b/src/widgets/helper/ChannelView.cpp index 8442e34f..ef377300 100644 --- a/src/widgets/helper/ChannelView.cpp +++ b/src/widgets/helper/ChannelView.cpp @@ -676,7 +676,7 @@ void ChannelView::wheelEvent(QWheelEvent *event) auto snapshot = this->getMessagesSnapshot(); int snapshotLength = int(snapshot.getLength()); - int i = std::min(int(desired), snapshotLength); + int i = std::min(int(desired), snapshotLength); if (delta > 0) { qreal scrollFactor = fmod(desired, 1);