From 3a57bc6de52cc5c9551d8c92fc995d7f9ab44607 Mon Sep 17 00:00:00 2001 From: Jakub Melka Date: Thu, 27 Jun 2024 14:15:05 +0200 Subject: [PATCH] Issue #181: where is the dark mode? --- Pdf4QtLibWidgets/sources/pdfwidgetutils.cpp | 19 +++++++++++++++---- Pdf4QtLibWidgets/sources/pdfwidgetutils.h | 6 ++++-- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/Pdf4QtLibWidgets/sources/pdfwidgetutils.cpp b/Pdf4QtLibWidgets/sources/pdfwidgetutils.cpp index 333b846..7b37898 100644 --- a/Pdf4QtLibWidgets/sources/pdfwidgetutils.cpp +++ b/Pdf4QtLibWidgets/sources/pdfwidgetutils.cpp @@ -20,6 +20,7 @@ #include #include +#include #include #include @@ -30,6 +31,7 @@ #include #include #include +#include #include "pdfdbgheap.h" @@ -44,6 +46,8 @@ int qt_default_dpi_y() { return 96; } namespace pdf { +std::optional s_isDarkThemeOverride; + static constexpr bool isScalingNeeded() { return QT_VERSION_MAJOR < 6; @@ -172,13 +176,20 @@ void PDFWidgetUtils::style(QWidget* widget) } } +void PDFWidgetUtils::overrideDarkTheme(bool isDarkTheme) +{ + s_isDarkThemeOverride = isDarkTheme; +} + bool PDFWidgetUtils::isDarkTheme() { - QPalette palette = QApplication::palette(); - QColor backgroundColor = palette.color(QPalette::Window); - QColor textColor = palette.color(QPalette::WindowText); + if (s_isDarkThemeOverride.has_value()) + { + return s_isDarkThemeOverride.value(); + } - return backgroundColor.lightness() < textColor.lightness(); + Qt::ColorScheme colorScheme = QApplication::styleHints()->colorScheme(); + return colorScheme == Qt::ColorScheme::Dark; } void PDFWidgetUtils::convertActionForDarkTheme(QAction* action, QSize iconSize, qreal devicePixelRatioF) diff --git a/Pdf4QtLibWidgets/sources/pdfwidgetutils.h b/Pdf4QtLibWidgets/sources/pdfwidgetutils.h index 186cf56..b539b6f 100644 --- a/Pdf4QtLibWidgets/sources/pdfwidgetutils.h +++ b/Pdf4QtLibWidgets/sources/pdfwidgetutils.h @@ -60,8 +60,10 @@ public: /// Apply style to the widget static void style(QWidget* widget); - /// Vrátí true, pokud je aktuálně nastavená dark theme - /// pro aplikaci. + /// Overrides automatically detected dark theme / light theme settings + static void overrideDarkTheme(bool isDarkTheme); + + /// Returns true if the dark theme is currently set for the application. static bool isDarkTheme(); /// Converts an action's icon for use in a dark theme.