diff --git a/src/core/Config.cpp b/src/core/Config.cpp index 41395214d..5965367b1 100644 --- a/src/core/Config.cpp +++ b/src/core/Config.cpp @@ -41,7 +41,7 @@ static const QMap deprecationMap = { {QStringLiteral("security/IconDownloadFallbackToGoogle"), QStringLiteral("security/IconDownloadFallback")}, }; -Config* Config::m_instance(nullptr); +QPointer Config::m_instance(nullptr); QVariant Config::get(const QString& key) { @@ -246,13 +246,17 @@ Config* Config::instance() void Config::createConfigFromFile(const QString& file) { - Q_ASSERT(!m_instance); + if (m_instance) { + delete m_instance; + } m_instance = new Config(file, qApp); } void Config::createTempFileInstance() { - Q_ASSERT(!m_instance); + if (m_instance) { + delete m_instance; + } auto* tmpFile = new QTemporaryFile(); bool openResult = tmpFile->open(); Q_ASSERT(openResult); diff --git a/src/core/Config.h b/src/core/Config.h index d65b3256b..ef6dd6af1 100644 --- a/src/core/Config.h +++ b/src/core/Config.h @@ -19,6 +19,7 @@ #ifndef KEEPASSX_CONFIG_H #define KEEPASSX_CONFIG_H +#include #include #include @@ -53,7 +54,7 @@ private: void init(const QString& fileName); void upgrade(); - static Config* m_instance; + static QPointer m_instance; QScopedPointer m_settings; QHash m_defaults; diff --git a/src/gui/Application.cpp b/src/gui/Application.cpp index b79f2c30a..7a2e956fc 100644 --- a/src/gui/Application.cpp +++ b/src/gui/Application.cpp @@ -18,8 +18,14 @@ */ #include "Application.h" -#include "MainWindow.h" + +#include "autotype/AutoType.h" #include "core/Config.h" +#include "core/Global.h" +#include "gui/MainWindow.h" +#include "gui/osutils/OSUtils.h" +#include "gui/styles/dark/DarkStyle.h" +#include "gui/styles/light/LightStyle.h" #include #include @@ -28,9 +34,6 @@ #include #include -#include "autotype/AutoType.h" -#include "core/Global.h" - #if defined(Q_OS_WIN) || (defined(Q_OS_UNIX) && !defined(Q_OS_MACOS)) #include "core/OSEventFilter.h" #endif @@ -65,6 +68,26 @@ Application::Application(int& argc, char** argv) registerUnixSignals(); #endif + QString appTheme = config()->get("GUI/ApplicationTheme").toString(); + if (appTheme == "auto") { + if (osUtils->isDarkMode()) { + setStyle(new DarkStyle); + m_darkTheme = true; + } else { + setStyle(new LightStyle); + } + } else if (appTheme == "light") { + setStyle(new LightStyle); + } else if (appTheme == "dark") { + setStyle(new DarkStyle); + m_darkTheme = true; + } else { + // Classic mode, only check for dark theme when not on Windows +#ifndef Q_OS_WIN + m_darkTheme = osUtils->isDarkMode(); +#endif + } + QString userName = qgetenv("USER"); if (userName.isEmpty()) { userName = qgetenv("USERNAME"); @@ -281,3 +304,8 @@ bool Application::sendFileNamesToRunningInstance(const QStringList& fileNames) const bool disconnected = client.waitForDisconnected(WaitTimeoutMSec); return writeOk && disconnected; } + +bool Application::isDarkTheme() const +{ + return m_darkTheme; +} diff --git a/src/gui/Application.h b/src/gui/Application.h index 9a3ef756b..b39fbe0e9 100644 --- a/src/gui/Application.h +++ b/src/gui/Application.h @@ -41,6 +41,7 @@ public: bool event(QEvent* event) override; bool isAlreadyRunning() const; + bool isDarkTheme() const; bool sendFileNamesToRunningInstance(const QStringList& fileNames); @@ -68,6 +69,7 @@ private: static int unixSignalSocket[2]; #endif bool m_alreadyRunning; + bool m_darkTheme = false; QLockFile* m_lockFile; QLocalServer m_lockServer; QString m_socketName; @@ -76,4 +78,6 @@ private: #endif }; +#define kpxcApp qobject_cast(Application::instance()) + #endif // KEEPASSX_APPLICATION_H diff --git a/src/main.cpp b/src/main.cpp index 524f112b3..c3494f02f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -29,9 +29,6 @@ #include "gui/Application.h" #include "gui/MainWindow.h" #include "gui/MessageBox.h" -#include "gui/osutils/OSUtils.h" -#include "gui/styles/dark/DarkStyle.h" -#include "gui/styles/light/LightStyle.h" #if defined(WITH_ASAN) && defined(WITH_LSAN) #include @@ -64,19 +61,6 @@ int main(int argc, char** argv) Application::setApplicationName("KeePassXC"); Application::setApplicationVersion(KEEPASSXC_VERSION); - QString appTheme = config()->get("GUI/ApplicationTheme").toString(); - if (appTheme == "auto") { - if (osUtils->isDarkMode()) { - QApplication::setStyle(new DarkStyle); - } else { - QApplication::setStyle(new LightStyle); - } - } else if (appTheme == "light") { - QApplication::setStyle(new LightStyle); - } else if (appTheme == "dark") { - QApplication::setStyle(new DarkStyle); - } - // don't set organizationName as that changes the return value of // QStandardPaths::writableLocation(QDesktopServices::DataLocation) Bootstrap::bootstrapApplication(); diff --git a/tests/gui/TestGui.cpp b/tests/gui/TestGui.cpp index 8ce9b0587..76766c8dc 100644 --- a/tests/gui/TestGui.cpp +++ b/tests/gui/TestGui.cpp @@ -74,16 +74,27 @@ #include "keys/FileKey.h" #include "keys/PasswordKey.h" -QTEST_MAIN(TestGui) +int main(int argc, char* argv[]) +{ +#if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0) + QApplication::setAttribute(Qt::AA_EnableHighDpiScaling); + QGuiApplication::setAttribute(Qt::AA_UseHighDpiPixmaps); +#endif + Application app(argc, argv); + app.setApplicationName("KeePassXC"); + app.setApplicationVersion(KEEPASSXC_VERSION); + app.setQuitOnLastWindowClosed(false); + app.setAttribute(Qt::AA_Use96Dpi, true); + QTEST_DISABLE_KEYPAD_NAVIGATION + TestGui tc; + QTEST_SET_MAIN_SOURCE_PATH + return QTest::qExec(&tc, argc, argv); +} static QString dbFileName = QStringLiteral(KEEPASSX_TEST_DATA_DIR).append("/NewDatabase.kdbx"); void TestGui::initTestCase() { - Application::setApplicationName("KeePassXC"); - Application::setApplicationVersion(KEEPASSXC_VERSION); - QApplication::setQuitOnLastWindowClosed(false); - QVERIFY(Crypto::init()); Config::createTempFileInstance(); // Disable autosave so we can test the modified file indicator diff --git a/tests/gui/TestGuiBrowser.cpp b/tests/gui/TestGuiBrowser.cpp index 834aea581..1750caa80 100644 --- a/tests/gui/TestGuiBrowser.cpp +++ b/tests/gui/TestGuiBrowser.cpp @@ -47,7 +47,22 @@ #include "gui/entry/EditEntryWidget.h" #include "gui/entry/EntryView.h" -QTEST_MAIN(TestGuiBrowser) +int main(int argc, char* argv[]) +{ +#if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0) + QApplication::setAttribute(Qt::AA_EnableHighDpiScaling); + QGuiApplication::setAttribute(Qt::AA_UseHighDpiPixmaps); +#endif + Application app(argc, argv); + app.setApplicationName("KeePassXC"); + app.setApplicationVersion(KEEPASSXC_VERSION); + app.setQuitOnLastWindowClosed(false); + app.setAttribute(Qt::AA_Use96Dpi, true); + QTEST_DISABLE_KEYPAD_NAVIGATION + TestGuiBrowser tc; + QTEST_SET_MAIN_SOURCE_PATH + return QTest::qExec(&tc, argc, argv); +} void TestGuiBrowser::initTestCase() {