From 456065f508b482d1ad92ce9d3f1a22f6dd188849 Mon Sep 17 00:00:00 2001 From: fourtf Date: Sun, 22 Sep 2019 10:53:39 +0200 Subject: [PATCH] moved resources out of Application --- src/Application.cpp | 8 +++---- src/Application.hpp | 9 +++----- src/RunGui.cpp | 3 +++ src/autogenerated/ResourcesAutogen.cpp | 2 +- src/autogenerated/ResourcesAutogen.hpp | 3 ++- .../moderationactions/ModerationAction.cpp | 6 ++--- src/messages/MessageBuilder.cpp | 2 +- src/providers/twitch/TwitchMessageBuilder.cpp | 20 ++++++++-------- src/singletons/Resources.cpp | 23 +++++++++++++++++++ src/singletons/Resources.hpp | 9 ++++++++ src/widgets/dialogs/UserInfoPopup.cpp | 10 ++++---- src/widgets/splits/SplitHeader.cpp | 14 +++++------ src/widgets/splits/SplitOverlay.cpp | 10 ++++---- 13 files changed, 75 insertions(+), 44 deletions(-) diff --git a/src/Application.cpp b/src/Application.cpp index c0749037e..a5c7e83ef 100644 --- a/src/Application.cpp +++ b/src/Application.cpp @@ -1,5 +1,7 @@ #include "Application.hpp" +#include + #include "controllers/accounts/AccountController.hpp" #include "controllers/commands/CommandController.hpp" #include "controllers/highlights/HighlightController.hpp" @@ -31,8 +33,6 @@ #include "util/PostToThread.hpp" #include "widgets/Window.hpp" -#include - namespace chatterino { static std::atomic isAppInitialized{false}; @@ -44,9 +44,7 @@ Application *Application::instance = nullptr; // to each other Application::Application(Settings &_settings, Paths &_paths) - : resources(&this->emplace()) - - , themes(&this->emplace()) + : themes(&this->emplace()) , fonts(&this->emplace()) , emotes(&this->emplace()) , windows(&this->emplace()) diff --git a/src/Application.hpp b/src/Application.hpp index 88cea1aba..fac8ec0f6 100644 --- a/src/Application.hpp +++ b/src/Application.hpp @@ -1,11 +1,11 @@ #pragma once -#include "common/Singleton.hpp" -#include "singletons/NativeMessaging.hpp" - #include #include +#include "common/Singleton.hpp" +#include "singletons/NativeMessaging.hpp" + namespace chatterino { class TwitchIrcServer; @@ -28,7 +28,6 @@ class AccountManager; class Emotes; class Settings; class Fonts; -class Resources2; class Toasts; class ChatterinoBadges; @@ -51,8 +50,6 @@ public: friend void test(); - Resources2 *const resources; - Theme *const themes{}; Fonts *const fonts{}; Emotes *const emotes{}; diff --git a/src/RunGui.cpp b/src/RunGui.cpp index 5f86a6a4a..190df9d5a 100644 --- a/src/RunGui.cpp +++ b/src/RunGui.cpp @@ -8,6 +8,7 @@ #include "Application.hpp" #include "common/NetworkManager.hpp" #include "singletons/Paths.hpp" +#include "singletons/Resources.hpp" #include "singletons/Updates.hpp" #include "util/CombinePath.hpp" #include "widgets/dialogs/LastRunCrashDialog.hpp" @@ -114,6 +115,8 @@ void runGui(QApplication &a, Paths &paths, Settings &settings) { initQt(); + initResources(); + auto thread = std::thread([dir = paths.miscDirectory] { { auto path = combinePath(dir, "Update.exe"); diff --git a/src/autogenerated/ResourcesAutogen.cpp b/src/autogenerated/ResourcesAutogen.cpp index 9629a8f02..e7aab5d55 100644 --- a/src/autogenerated/ResourcesAutogen.cpp +++ b/src/autogenerated/ResourcesAutogen.cpp @@ -50,4 +50,4 @@ Resources2::Resources2() this->twitch.vip = QPixmap(":/twitch/vip.png"); } -} // namespace chatterino \ No newline at end of file +} // namespace chatterino diff --git a/src/autogenerated/ResourcesAutogen.hpp b/src/autogenerated/ResourcesAutogen.hpp index 51a9a5734..cb3423631 100644 --- a/src/autogenerated/ResourcesAutogen.hpp +++ b/src/autogenerated/ResourcesAutogen.hpp @@ -1,4 +1,5 @@ #include + #include "common/Singleton.hpp" namespace chatterino { @@ -64,4 +65,4 @@ public: } twitch; }; -} // namespace chatterino \ No newline at end of file +} // namespace chatterino diff --git a/src/controllers/moderationactions/ModerationAction.cpp b/src/controllers/moderationactions/ModerationAction.cpp index 1bba03d2c..dce01904c 100644 --- a/src/controllers/moderationactions/ModerationAction.cpp +++ b/src/controllers/moderationactions/ModerationAction.cpp @@ -65,17 +65,17 @@ ModerationAction::ModerationAction(const QString &action) // line1 = this->line1_; // line2 = this->line2_; // } else { - // this->_moderationActions.emplace_back(app->resources->buttonTimeout, + // this->_moderationActions.emplace_back(getResources().buttonTimeout, // str); // } } else if (action.startsWith("/ban ")) { - this->image_ = Image::fromPixmap(getApp()->resources->buttons.ban); + this->image_ = Image::fromPixmap(getResources().buttons.ban); } else if (action.startsWith("/delete ")) { - this->image_ = Image::fromPixmap(getApp()->resources->buttons.trashCan); + this->image_ = Image::fromPixmap(getResources().buttons.trashCan); } else { diff --git a/src/messages/MessageBuilder.cpp b/src/messages/MessageBuilder.cpp index c38bf024c..e24b22f9e 100644 --- a/src/messages/MessageBuilder.cpp +++ b/src/messages/MessageBuilder.cpp @@ -33,7 +33,7 @@ std::pair makeAutomodMessage( builder .emplace( - Image::fromPixmap(getApp()->resources->twitch.automod), + Image::fromPixmap(getResources().twitch.automod), MessageElementFlag::BadgeChannelAuthority) ->setTooltip("AutoMod"); builder.emplace("AutoMod:", MessageElementFlag::BoldUsername, diff --git a/src/providers/twitch/TwitchMessageBuilder.cpp b/src/providers/twitch/TwitchMessageBuilder.cpp index 3f94877a1..5caa56fbd 100644 --- a/src/providers/twitch/TwitchMessageBuilder.cpp +++ b/src/providers/twitch/TwitchMessageBuilder.cpp @@ -1174,21 +1174,21 @@ void TwitchMessageBuilder::appendTwitchBadges() else if (badge == "staff/1") { this->emplace( - Image::fromPixmap(app->resources->twitch.staff), + Image::fromPixmap(getResources().twitch.staff), MessageElementFlag::BadgeGlobalAuthority) ->setTooltip("Twitch Staff"); } else if (badge == "admin/1") { this->emplace( - Image::fromPixmap(app->resources->twitch.admin), + Image::fromPixmap(getResources().twitch.admin), MessageElementFlag::BadgeGlobalAuthority) ->setTooltip("Twitch Admin"); } else if (badge == "global_mod/1") { this->emplace( - Image::fromPixmap(app->resources->twitch.globalmod), + Image::fromPixmap(getResources().twitch.globalmod), MessageElementFlag::BadgeGlobalAuthority) ->setTooltip("Twitch Global Moderator"); } @@ -1203,35 +1203,35 @@ void TwitchMessageBuilder::appendTwitchBadges() continue; } this->emplace( - Image::fromPixmap(app->resources->twitch.moderator), + Image::fromPixmap(getResources().twitch.moderator), MessageElementFlag::BadgeChannelAuthority) ->setTooltip("Twitch Channel Moderator"); } else if (badge == "vip/1") { this->emplace( - Image::fromPixmap(app->resources->twitch.vip), + Image::fromPixmap(getResources().twitch.vip), MessageElementFlag::BadgeChannelAuthority) ->setTooltip("VIP"); } else if (badge == "broadcaster/1") { this->emplace( - Image::fromPixmap(app->resources->twitch.broadcaster), + Image::fromPixmap(getResources().twitch.broadcaster), MessageElementFlag::BadgeChannelAuthority) ->setTooltip("Twitch Broadcaster"); } else if (badge == "turbo/1") { this->emplace( - Image::fromPixmap(app->resources->twitch.turbo), + Image::fromPixmap(getResources().twitch.turbo), MessageElementFlag::BadgeVanity) ->setTooltip("Twitch Turbo Subscriber"); } else if (badge == "premium/1") { this->emplace( - Image::fromPixmap(app->resources->twitch.prime), + Image::fromPixmap(getResources().twitch.prime), MessageElementFlag::BadgeVanity) ->setTooltip("Twitch Prime Subscriber"); } @@ -1243,7 +1243,7 @@ void TwitchMessageBuilder::appendTwitchBadges() case 1: { this->emplace( - Image::fromPixmap(app->resources->twitch.verified, + Image::fromPixmap(getResources().twitch.verified, 0.25), MessageElementFlag::BadgeVanity) ->setTooltip("Twitch Verified"); @@ -1271,7 +1271,7 @@ void TwitchMessageBuilder::appendTwitchBadges() // use default subscriber badge if custom one not found this->emplace( - Image::fromPixmap(app->resources->twitch.subscriber, 0.25), + Image::fromPixmap(getResources().twitch.subscriber, 0.25), MessageElementFlag::BadgeSubscription) ->setTooltip("Twitch Subscriber"); } diff --git a/src/singletons/Resources.cpp b/src/singletons/Resources.cpp index daec74a5f..18995dc31 100644 --- a/src/singletons/Resources.cpp +++ b/src/singletons/Resources.cpp @@ -1 +1,24 @@ #include "singletons/Resources.hpp" + +#include "debug/AssertInGuiThread.hpp" + +namespace chatterino { +namespace { + static Resources2 *resources = nullptr; +} + +Resources2 &getResources() +{ + assert(resources); + + return *resources; +} + +void initResources() +{ + assertInGuiThread(); + + resources = new Resources2; +} + +} // namespace chatterino diff --git a/src/singletons/Resources.hpp b/src/singletons/Resources.hpp index 57a1b79ad..f4217ca8f 100644 --- a/src/singletons/Resources.hpp +++ b/src/singletons/Resources.hpp @@ -1,3 +1,12 @@ #pragma once #include "autogenerated/ResourcesAutogen.hpp" + +namespace chatterino { + +/// This class in thread safe but needs to be initialized from the gui thread +/// first. +Resources2 &getResources(); +void initResources(); + +} // namespace chatterino diff --git a/src/widgets/dialogs/UserInfoPopup.cpp b/src/widgets/dialogs/UserInfoPopup.cpp index 40beaedfa..4a7d4f681 100644 --- a/src/widgets/dialogs/UserInfoPopup.cpp +++ b/src/widgets/dialogs/UserInfoPopup.cpp @@ -32,7 +32,7 @@ namespace { { auto label = box.emplace