QGtk3Theme: implement appearance function to detect dark themes

This allows Qt Quick Controls to detect if a dark theme is in use,
and if so, use a dark theme of the current style (if available).

Fixes: QTBUG-93955
Pick-to: 6.2
Change-Id: I15fc8a2271acf9ba27232359056d5a681d4cffca
Reviewed-by: Dmitry Shachnev <mitya57@gmail.com>
Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
This commit is contained in:
Mitch Curtis 2021-10-06 16:48:58 +02:00
parent 04419c2852
commit 2b77e779ce
2 changed files with 44 additions and 0 deletions

View File

@ -41,6 +41,7 @@
#include "qgtk3dialoghelpers.h"
#include "qgtk3menu.h"
#include <QVariant>
#include <QtCore/qregularexpression.h>
#undef signals
#include <gtk/gtk.h>
@ -147,6 +148,47 @@ QString QGtk3Theme::gtkFontName() const
return QGnomeTheme::gtkFontName();
}
QPlatformTheme::Appearance QGtk3Theme::appearance() const
{
/*
https://docs.gtk.org/gtk3/running.html
It's possible to set a theme variant after the theme name when using GTK_THEME:
GTK_THEME=Adwaita:dark
Some themes also have "-dark" as part of their name.
We test this environment variable first because the documentation says
it's mainly used for easy debugging, so it should be possible to use it
to override any other settings.
*/
QString themeName = qEnvironmentVariable("GTK_THEME");
const QRegularExpression darkRegex(QStringLiteral("[:-]dark"), QRegularExpression::CaseInsensitiveOption);
if (!themeName.isEmpty())
return darkRegex.match(themeName).hasMatch() ? Appearance::Dark : Appearance::Light;
/*
https://docs.gtk.org/gtk3/property.Settings.gtk-application-prefer-dark-theme.html
This setting controls which theme is used when the theme specified by
gtk-theme-name provides both light and dark variants. We can save a
regex check by testing this property first.
*/
const auto preferDark = gtkSetting<bool>("gtk-application-prefer-dark-theme");
if (preferDark)
return Appearance::Dark;
/*
https://docs.gtk.org/gtk3/property.Settings.gtk-theme-name.html
*/
themeName = gtkSetting("gtk-theme-name");
if (!themeName.isEmpty())
return darkRegex.match(themeName).hasMatch() ? Appearance::Dark : Appearance::Light;
return Appearance::Unknown;
}
bool QGtk3Theme::usePlatformNativeDialog(DialogType type) const
{
switch (type) {

View File

@ -52,6 +52,8 @@ public:
virtual QVariant themeHint(ThemeHint hint) const override;
virtual QString gtkFontName() const override;
Appearance appearance() const override;
bool usePlatformNativeDialog(DialogType type) const override;
QPlatformDialogHelper *createPlatformDialogHelper(DialogType type) const override;