diff --git a/src/plugins/platformthemes/gtk3/qgtk3interface.cpp b/src/plugins/platformthemes/gtk3/qgtk3interface.cpp index d3195863af..4ce7d92ceb 100644 --- a/src/plugins/platformthemes/gtk3/qgtk3interface.cpp +++ b/src/plugins/platformthemes/gtk3/qgtk3interface.cpp @@ -400,6 +400,22 @@ const QString QGtk3Interface::themeName() const return QLatin1StringView(theme_name); } +Qt::Appearance QGtk3Interface::appearanceByColors() const +{ + const QColor background = color(widget(QGtkWidget::gtk_Default), + QGtkColorSource::Background, + GTK_STATE_FLAG_ACTIVE); + const QColor foreground = color(widget(QGtkWidget::gtk_Default), + QGtkColorSource::Foreground, + GTK_STATE_FLAG_ACTIVE); + + if (foreground.lightness() > background.lightness()) + return Qt::Appearance::Dark; + if (foreground.lightness() < background.lightness()) + return Qt::Appearance::Light; + return Qt::Appearance::Unknown; +} + inline constexpr QGtk3Interface::QGtkWidget QGtk3Interface::toWidgetType(QPlatformTheme::Font type) { switch (type) { diff --git a/src/plugins/platformthemes/gtk3/qgtk3interface_p.h b/src/plugins/platformthemes/gtk3/qgtk3interface_p.h index 1a44eed6cb..84119cc638 100644 --- a/src/plugins/platformthemes/gtk3/qgtk3interface_p.h +++ b/src/plugins/platformthemes/gtk3/qgtk3interface_p.h @@ -99,6 +99,9 @@ public: // Return current GTK theme name const QString themeName() const; + // Derive appearance from default colors + Qt::Appearance appearanceByColors() const; + // Convert GTK state to/from string static int toGtkState(const QString &state); static const QLatin1String fromGtkState(GtkStateFlags state); diff --git a/src/plugins/platformthemes/gtk3/qgtk3storage.cpp b/src/plugins/platformthemes/gtk3/qgtk3storage.cpp index 8c56d88898..de0fea4139 100644 --- a/src/plugins/platformthemes/gtk3/qgtk3storage.cpp +++ b/src/plugins/platformthemes/gtk3/qgtk3storage.cpp @@ -222,7 +222,7 @@ void QGtk3Storage::populateMap() // Derive appearance from theme name m_appearance = newThemeName.contains("dark"_L1, Qt::CaseInsensitive) - ? Qt::Appearance::Dark : Qt::Appearance::Light; + ? Qt::Appearance::Dark : m_interface->appearanceByColors(); if (m_themeName.isEmpty()) { qCDebug(lcQGtk3Interface) << "GTK theme initialized:" << newThemeName << m_appearance;