Skip to content

Commit

Permalink
Use Qt 6.5 styleHint colorScheme to detect dark theme
Browse files Browse the repository at this point in the history
- keep linux compatibility

WE2-826

Signed-off-by: Raul Metsma <[email protected]>
  • Loading branch information
metsma committed Oct 12, 2023
1 parent e8e4916 commit 352b2bb
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 64 deletions.
6 changes: 0 additions & 6 deletions src/controller/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,6 @@ add_library(controller STATIC
writeresponse.hpp
)

if(APPLE)
target_sources(controller PRIVATE application.mm)
set_source_files_properties(application.mm PROPERTIES COMPILE_FLAGS "-fobjc-arc")
target_link_libraries(controller "-framework Cocoa" "-framework AppKit" "-fobjc-arc")
endif()

set_property(SOURCE application.cpp APPEND PROPERTY COMPILE_DEFINITIONS PROJECT_VERSION="${SEMVER_VERSION_STRING}")
target_include_directories(controller PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(controller
Expand Down
20 changes: 10 additions & 10 deletions src/controller/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <QPalette>
#include <QProcess>
#include <QSettings>
#include <QStyleHints>
#include <QTranslator>

inline CommandWithArguments::second_type parseArgumentJson(const QString& argumentStr)
Expand Down Expand Up @@ -70,23 +71,23 @@ Application::Application(int& argc, char** argv, const QString& name) :
menuBar = std::make_unique<QMenuBar>();
QAction* about = menuBar->addMenu(tr("&File"))->addAction(tr("&About"));
about->setMenuRole(QAction::AboutRole);
connect(about, &QAction::triggered, this, &Application::showAbout);
connect(about, &QAction::triggered, this, [] {
QProcess::startDetached(applicationFilePath());
});
#endif
}

#ifndef Q_OS_MAC
bool Application::isDarkTheme()
{
#ifdef Q_OS_WIN
QSettings settings(
"HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize",
QSettings::NativeFormat);
return settings.value("AppsUseLightTheme", 1).toInt() == 0;
#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
return styleHints()->colorScheme() == Qt::ColorScheme::Dark;
#else
// There is currently no straightforward way to detect dark mode in Linux, but this works for supported OS-s.
// There is currently no straightforward way to detect dark mode in Linux, but this works for
// supported OS-s.
static const bool isDarkTheme = [] {
QProcess p;
p.start(QStringLiteral("gsettings"), {"get", "org.gnome.desktop.interface", "color-scheme"});
p.start(QStringLiteral("gsettings"),
{"get", "org.gnome.desktop.interface", "color-scheme"});
if (p.waitForFinished()) {
return p.readAllStandardOutput().contains("dark");
}
Expand All @@ -97,7 +98,6 @@ bool Application::isDarkTheme()
return isDarkTheme;
#endif
}
#endif

void Application::loadTranslations(const QString& lang)
{
Expand Down
3 changes: 0 additions & 3 deletions src/controller/application.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@ class Application : public QApplication
// see class SafariApplication in src/mac/main.mm and WebEidDialog::showAboutPage().
virtual bool isSafariExtensionContainingApp() { return false; }
virtual void requestSafariExtensionState() {}
#ifdef Q_OS_MAC
void showAbout();
#endif
virtual void showSafariSettings() {}

signals:
Expand Down
45 changes: 0 additions & 45 deletions src/controller/application.mm

This file was deleted.

0 comments on commit 352b2bb

Please sign in to comment.