Move theme detection into Application

* Add function to Application to quickly determine if in light or dark theme
* Add kpxcApp symbol
* Explicitly define main function for GUI tests to improve performance and use custom Application.
This commit is contained in:
Jonathan White 2020-03-08 22:45:51 -04:00
parent a8c02fdc3c
commit 1d7ef5d4eb
7 changed files with 77 additions and 30 deletions

View File

@ -41,7 +41,7 @@ static const QMap<QString, QString> deprecationMap = {
{QStringLiteral("security/IconDownloadFallbackToGoogle"), QStringLiteral("security/IconDownloadFallback")}, {QStringLiteral("security/IconDownloadFallbackToGoogle"), QStringLiteral("security/IconDownloadFallback")},
}; };
Config* Config::m_instance(nullptr); QPointer<Config> Config::m_instance(nullptr);
QVariant Config::get(const QString& key) QVariant Config::get(const QString& key)
{ {
@ -246,13 +246,17 @@ Config* Config::instance()
void Config::createConfigFromFile(const QString& file) void Config::createConfigFromFile(const QString& file)
{ {
Q_ASSERT(!m_instance); if (m_instance) {
delete m_instance;
}
m_instance = new Config(file, qApp); m_instance = new Config(file, qApp);
} }
void Config::createTempFileInstance() void Config::createTempFileInstance()
{ {
Q_ASSERT(!m_instance); if (m_instance) {
delete m_instance;
}
auto* tmpFile = new QTemporaryFile(); auto* tmpFile = new QTemporaryFile();
bool openResult = tmpFile->open(); bool openResult = tmpFile->open();
Q_ASSERT(openResult); Q_ASSERT(openResult);

View File

@ -19,6 +19,7 @@
#ifndef KEEPASSX_CONFIG_H #ifndef KEEPASSX_CONFIG_H
#define KEEPASSX_CONFIG_H #define KEEPASSX_CONFIG_H
#include <QPointer>
#include <QScopedPointer> #include <QScopedPointer>
#include <QVariant> #include <QVariant>
@ -53,7 +54,7 @@ private:
void init(const QString& fileName); void init(const QString& fileName);
void upgrade(); void upgrade();
static Config* m_instance; static QPointer<Config> m_instance;
QScopedPointer<QSettings> m_settings; QScopedPointer<QSettings> m_settings;
QHash<QString, QVariant> m_defaults; QHash<QString, QVariant> m_defaults;

View File

@ -18,8 +18,14 @@
*/ */
#include "Application.h" #include "Application.h"
#include "MainWindow.h"
#include "autotype/AutoType.h"
#include "core/Config.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 <QFileInfo> #include <QFileInfo>
#include <QFileOpenEvent> #include <QFileOpenEvent>
@ -28,9 +34,6 @@
#include <QStandardPaths> #include <QStandardPaths>
#include <QtNetwork/QLocalSocket> #include <QtNetwork/QLocalSocket>
#include "autotype/AutoType.h"
#include "core/Global.h"
#if defined(Q_OS_WIN) || (defined(Q_OS_UNIX) && !defined(Q_OS_MACOS)) #if defined(Q_OS_WIN) || (defined(Q_OS_UNIX) && !defined(Q_OS_MACOS))
#include "core/OSEventFilter.h" #include "core/OSEventFilter.h"
#endif #endif
@ -65,6 +68,26 @@ Application::Application(int& argc, char** argv)
registerUnixSignals(); registerUnixSignals();
#endif #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"); QString userName = qgetenv("USER");
if (userName.isEmpty()) { if (userName.isEmpty()) {
userName = qgetenv("USERNAME"); userName = qgetenv("USERNAME");
@ -281,3 +304,8 @@ bool Application::sendFileNamesToRunningInstance(const QStringList& fileNames)
const bool disconnected = client.waitForDisconnected(WaitTimeoutMSec); const bool disconnected = client.waitForDisconnected(WaitTimeoutMSec);
return writeOk && disconnected; return writeOk && disconnected;
} }
bool Application::isDarkTheme() const
{
return m_darkTheme;
}

View File

@ -41,6 +41,7 @@ public:
bool event(QEvent* event) override; bool event(QEvent* event) override;
bool isAlreadyRunning() const; bool isAlreadyRunning() const;
bool isDarkTheme() const;
bool sendFileNamesToRunningInstance(const QStringList& fileNames); bool sendFileNamesToRunningInstance(const QStringList& fileNames);
@ -68,6 +69,7 @@ private:
static int unixSignalSocket[2]; static int unixSignalSocket[2];
#endif #endif
bool m_alreadyRunning; bool m_alreadyRunning;
bool m_darkTheme = false;
QLockFile* m_lockFile; QLockFile* m_lockFile;
QLocalServer m_lockServer; QLocalServer m_lockServer;
QString m_socketName; QString m_socketName;
@ -76,4 +78,6 @@ private:
#endif #endif
}; };
#define kpxcApp qobject_cast<Application*>(Application::instance())
#endif // KEEPASSX_APPLICATION_H #endif // KEEPASSX_APPLICATION_H

View File

@ -29,9 +29,6 @@
#include "gui/Application.h" #include "gui/Application.h"
#include "gui/MainWindow.h" #include "gui/MainWindow.h"
#include "gui/MessageBox.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) #if defined(WITH_ASAN) && defined(WITH_LSAN)
#include <sanitizer/lsan_interface.h> #include <sanitizer/lsan_interface.h>
@ -64,19 +61,6 @@ int main(int argc, char** argv)
Application::setApplicationName("KeePassXC"); Application::setApplicationName("KeePassXC");
Application::setApplicationVersion(KEEPASSXC_VERSION); 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 // don't set organizationName as that changes the return value of
// QStandardPaths::writableLocation(QDesktopServices::DataLocation) // QStandardPaths::writableLocation(QDesktopServices::DataLocation)
Bootstrap::bootstrapApplication(); Bootstrap::bootstrapApplication();

View File

@ -74,16 +74,27 @@
#include "keys/FileKey.h" #include "keys/FileKey.h"
#include "keys/PasswordKey.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"); static QString dbFileName = QStringLiteral(KEEPASSX_TEST_DATA_DIR).append("/NewDatabase.kdbx");
void TestGui::initTestCase() void TestGui::initTestCase()
{ {
Application::setApplicationName("KeePassXC");
Application::setApplicationVersion(KEEPASSXC_VERSION);
QApplication::setQuitOnLastWindowClosed(false);
QVERIFY(Crypto::init()); QVERIFY(Crypto::init());
Config::createTempFileInstance(); Config::createTempFileInstance();
// Disable autosave so we can test the modified file indicator // Disable autosave so we can test the modified file indicator

View File

@ -47,7 +47,22 @@
#include "gui/entry/EditEntryWidget.h" #include "gui/entry/EditEntryWidget.h"
#include "gui/entry/EntryView.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() void TestGuiBrowser::initTestCase()
{ {