Issue #181: where is the dark mode?

This commit is contained in:
Jakub Melka 2024-06-27 14:15:05 +02:00
parent 625a4307d0
commit 3a57bc6de5
2 changed files with 19 additions and 6 deletions

View File

@ -20,6 +20,7 @@
#include <map> #include <map>
#include <set> #include <set>
#include <optional>
#include <QMenu> #include <QMenu>
#include <QAction> #include <QAction>
@ -30,6 +31,7 @@
#include <QGroupBox> #include <QGroupBox>
#include <QMessageBox> #include <QMessageBox>
#include <QApplication> #include <QApplication>
#include <QStyleHints>
#include "pdfdbgheap.h" #include "pdfdbgheap.h"
@ -44,6 +46,8 @@ int qt_default_dpi_y() { return 96; }
namespace pdf namespace pdf
{ {
std::optional<bool> s_isDarkThemeOverride;
static constexpr bool isScalingNeeded() static constexpr bool isScalingNeeded()
{ {
return QT_VERSION_MAJOR < 6; 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() bool PDFWidgetUtils::isDarkTheme()
{ {
QPalette palette = QApplication::palette(); if (s_isDarkThemeOverride.has_value())
QColor backgroundColor = palette.color(QPalette::Window); {
QColor textColor = palette.color(QPalette::WindowText); 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) void PDFWidgetUtils::convertActionForDarkTheme(QAction* action, QSize iconSize, qreal devicePixelRatioF)

View File

@ -60,8 +60,10 @@ public:
/// Apply style to the widget /// Apply style to the widget
static void style(QWidget* widget); static void style(QWidget* widget);
/// Vrátí true, pokud je aktuálně nastavená dark theme /// Overrides automatically detected dark theme / light theme settings
/// pro aplikaci. static void overrideDarkTheme(bool isDarkTheme);
/// Returns true if the dark theme is currently set for the application.
static bool isDarkTheme(); static bool isDarkTheme();
/// Converts an action's icon for use in a dark theme. /// Converts an action's icon for use in a dark theme.