refactor: Make Args less of a singleton (#5041)

This means it's no longer a singleton, and its lifetime is bound to our application.
This felt like a good small experiment to see how its changes would look
if we did this.
As a shortcut, `getApp` that is already a mega singleton keeps a
reference to Args, this means places that are a bit more difficult to
inject into call `getApp()->getArgs()` just like other things are
accessed.
This commit is contained in:
pajlada
2023-12-29 15:40:31 +01:00
committed by GitHub
parent c65ebd26bd
commit d085ab578f
20 changed files with 78 additions and 69 deletions
+6 -7
View File
@@ -82,10 +82,9 @@ std::optional<bool> readRecoverySettings(const Paths &paths)
return shouldRecover.toBool();
}
bool canRestart(const Paths &paths)
bool canRestart(const Paths &paths, const Args &args)
{
#ifdef NDEBUG
const auto &args = chatterino::getArgs();
if (args.isFramelessEmbed || args.shouldRunBrowserExtensionHost)
{
return false;
@@ -109,10 +108,10 @@ bool canRestart(const Paths &paths)
/// additional plus ('++' -> '+').
///
/// The decoding happens in crash-handler/src/CommandLine.cpp
std::string encodeArguments()
std::string encodeArguments(const Args &appArgs)
{
std::string args;
for (auto arg : getArgs().currentArguments())
for (auto arg : appArgs.currentArguments())
{
if (!args.empty())
{
@@ -161,7 +160,7 @@ void CrashHandler::saveShouldRecover(bool value)
}
#ifdef CHATTERINO_WITH_CRASHPAD
std::unique_ptr<crashpad::CrashpadClient> installCrashHandler()
std::unique_ptr<crashpad::CrashpadClient> installCrashHandler(const Args &args)
{
// Currently, the following directory layout is assumed:
// [applicationDirPath]
@@ -197,7 +196,7 @@ std::unique_ptr<crashpad::CrashpadClient> installCrashHandler()
std::map<std::string, std::string> annotations{
{
"canRestart"s,
canRestart(*getPaths()) ? "true"s : "false"s,
canRestart(*getPaths(), args) ? "true"s : "false"s,
},
{
"exePath"s,
@@ -209,7 +208,7 @@ std::unique_ptr<crashpad::CrashpadClient> installCrashHandler()
},
{
"exeArguments"s,
encodeArguments(),
encodeArguments(args),
},
};
+3 -1
View File
@@ -12,6 +12,8 @@
namespace chatterino {
class Args;
class CrashHandler : public Singleton
{
public:
@@ -30,7 +32,7 @@ private:
};
#ifdef CHATTERINO_WITH_CRASHPAD
std::unique_ptr<crashpad::CrashpadClient> installCrashHandler();
std::unique_ptr<crashpad::CrashpadClient> installCrashHandler(const Args &args);
#endif
} // namespace chatterino
+7 -7
View File
@@ -55,7 +55,7 @@ using SplitDirection = SplitContainer::Direction;
void WindowManager::showSettingsDialog(QWidget *parent,
SettingsDialogPreference preference)
{
if (getArgs().dontSaveSettings)
if (getApp()->getArgs().dontSaveSettings)
{
QMessageBox::critical(parent, "Chatterino - Editing Settings Forbidden",
"Settings cannot be edited when running with\n"
@@ -356,9 +356,9 @@ void WindowManager::initialize(Settings &settings, Paths &paths)
{
WindowLayout windowLayout;
if (getArgs().customChannelLayout)
if (getApp()->getArgs().customChannelLayout)
{
windowLayout = getArgs().customChannelLayout.value();
windowLayout = getApp()->getArgs().customChannelLayout.value();
}
else
{
@@ -370,7 +370,7 @@ void WindowManager::initialize(Settings &settings, Paths &paths)
this->applyWindowLayout(windowLayout);
}
if (getArgs().isFramelessEmbed)
if (getApp()->getArgs().isFramelessEmbed)
{
this->framelessEmbedWindow_.reset(new FramelessEmbedWindow);
this->framelessEmbedWindow_->show();
@@ -383,7 +383,7 @@ void WindowManager::initialize(Settings &settings, Paths &paths)
this->mainWindow_->getNotebook().addPage(true);
// TODO: don't create main window if it's a frameless embed
if (getArgs().isFramelessEmbed)
if (getApp()->getArgs().isFramelessEmbed)
{
this->mainWindow_->hide();
}
@@ -418,7 +418,7 @@ void WindowManager::initialize(Settings &settings, Paths &paths)
void WindowManager::save()
{
if (getArgs().dontSaveSettings)
if (getApp()->getArgs().dontSaveSettings)
{
return;
}
@@ -724,7 +724,7 @@ WindowLayout WindowManager::loadWindowLayoutFromFile() const
void WindowManager::applyWindowLayout(const WindowLayout &layout)
{
if (getArgs().dontLoadMainWindow)
if (getApp()->getArgs().dontLoadMainWindow)
{
return;
}