From 352b2bbe5f738b41dcea3394183e30aa9680c079 Mon Sep 17 00:00:00 2001 From: Raul Metsma Date: Thu, 12 Oct 2023 11:47:13 +0300 Subject: [PATCH] Use Qt 6.5 styleHint colorScheme to detect dark theme - keep linux compatibility WE2-826 Signed-off-by: Raul Metsma --- src/controller/CMakeLists.txt | 6 ----- src/controller/application.cpp | 20 +++++++-------- src/controller/application.hpp | 3 --- src/controller/application.mm | 45 ---------------------------------- 4 files changed, 10 insertions(+), 64 deletions(-) delete mode 100644 src/controller/application.mm diff --git a/src/controller/CMakeLists.txt b/src/controller/CMakeLists.txt index 0748a9d4..3b9f36bf 100644 --- a/src/controller/CMakeLists.txt +++ b/src/controller/CMakeLists.txt @@ -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 diff --git a/src/controller/application.cpp b/src/controller/application.cpp index c8a56f37..1270b4c6 100644 --- a/src/controller/application.cpp +++ b/src/controller/application.cpp @@ -33,6 +33,7 @@ #include #include #include +#include #include inline CommandWithArguments::second_type parseArgumentJson(const QString& argumentStr) @@ -70,23 +71,23 @@ Application::Application(int& argc, char** argv, const QString& name) : menuBar = std::make_unique(); 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"); } @@ -97,7 +98,6 @@ bool Application::isDarkTheme() return isDarkTheme; #endif } -#endif void Application::loadTranslations(const QString& lang) { diff --git a/src/controller/application.hpp b/src/controller/application.hpp index 2e05f737..fda67622 100644 --- a/src/controller/application.hpp +++ b/src/controller/application.hpp @@ -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: diff --git a/src/controller/application.mm b/src/controller/application.mm deleted file mode 100644 index eb9c2478..00000000 --- a/src/controller/application.mm +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright (c) 2022-2023 The Web eID Project - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -#include "application.hpp" - -#include - -#import -#import - -bool Application::isDarkTheme() -{ - auto appearance = [NSApp.effectiveAppearance bestMatchFromAppearancesWithNames: - @[ NSAppearanceNameAqua, NSAppearanceNameDarkAqua ]]; - return [appearance isEqualToString:NSAppearanceNameDarkAqua]; -} - -void Application::showAbout() -{ - NSLog(@"web-eid-app: starting app"); - [NSWorkspace.sharedWorkspace openApplicationAtURL:QUrl::fromLocalFile(qApp->applicationFilePath()).toNSURL() configuration:NSWorkspaceOpenConfiguration.configuration completionHandler:^(NSRunningApplication *app, NSError *error) { - if (app == nil) { - NSLog(@"web-eid-app: failed to start app: %@", error); - } - }]; -}