Commit 99c72b4b authored by Mikhail Zolotukhin's avatar Mikhail Zolotukhin
Browse files

Sync dark theme preference for GTK3 applications

Summary:
Previously preference of dark GTK3 theme was set by checkbox in GTK KCM.  Now, the checkbox is removed and preference is set according to color scheme, the user set in Colors KCMm, using the same heuristic, used for filter combobox in that KCM.

This makes sense, because, suppose the user set Adwaita GTK3 theme, and their color scheme is something dark. Therefore their overall theme is supposedly dark, so Adwaita needs to be dark too.

Note, however, that this preference is somewhat buggy. It does not work in flatpak apps, it can't be autoreloaded with xsettingsd, and also it is applied after some time has passed (this is hypothetical). You can check these statements, using the present checkbox in GTK KCM.

Depends on D25872

Test Plan:
{F7816371}
# Apply Adwaita GTK3 Theme
# Reload kded5
# Apply dark color scheme
# Wait some time
# (Re)open GTK3 app
# App should use Dark color scheme
# Apply light color scheme
# Wait some time
# Reopen GTK3 app
# App should use Light color scheme

Reviewers: apol, cblack, broulik, #vdg

Reviewed By: apol, cblack

Subscribers: ngraham, GB_2, plasma-devel

Tags: #plasma

Maniphest Tasks: T10611

Differential Revision: https://phabricator.kde.org/D25945
parent 13c4eee2
......@@ -7,7 +7,6 @@ set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules" ${ECM_MODULE_P
find_package(Qt5 REQUIRED NO_MODULE COMPONENTS Widgets Svg Test)
find_package(KF5 REQUIRED COMPONENTS I18n KIO ConfigWidgets NewStuff Archive KCMUtils IconThemes DBusAddons)
find_package(X11 REQUIRED)
find_package(GTK3 REQUIRED)
find_package(GSettingSchemas REQUIRED)
......
......@@ -20,6 +20,7 @@
#include <QFont>
#include <QString>
#include <QColor>
#include <KConfig>
#include <KIconTheme>
......@@ -110,6 +111,21 @@ QString ConfigValueProvider::scrollbarBehavior() const
}
}
QString ConfigValueProvider::preferDarkTheme() const
{
kdeglobalsConfig->reparseConfiguration();
KConfigGroup colorsConfigGroup = kdeglobalsConfig->group(QStringLiteral("Colors:Window"));
QColor windowBackgroundColor = colorsConfigGroup.readEntry(QStringLiteral("BackgroundNormal"), QColor(239, 240, 241));
const int windowBackgroundGray = qGray(windowBackgroundColor.rgb());
// We use heuristic to determine if current color scheme is dark or not
if (windowBackgroundGray >= 192) {
return QStringLiteral("0");
} else {
return QStringLiteral("1");
}
}
QString ConfigValueProvider::toolbarStyleInDesiredNotation(const QString &kdeConfigValue, ConfigValueProvider::ToolbarStyleNotation notation) const
{
QStringList toolbarStyles {};
......
......@@ -43,6 +43,7 @@ public:
QString iconsInMenus() const;
QString toolbarStyle(ToolbarStyleNotation notation) const;
QString scrollbarBehavior() const;
QString preferDarkTheme() const;
private:
QString toolbarStyleInDesiredNotation(const QString &kdeConfigValue, ToolbarStyleNotation notation) const;
......
......@@ -115,6 +115,12 @@ void GtkConfig::setScrollbarBehavior() const
ConfigEditor::setGtk3ConfigValueXSettingsd(QStringLiteral("Gtk/PrimaryButtonWarpsSlider"), scrollbarBehavior);
}
void GtkConfig::setDarkThemePreference() const
{
const QString preferDarkTheme = configValueProvider->preferDarkTheme();
ConfigEditor::setGtk3ConfigValueSettingsIni(QStringLiteral("gtk-application-prefer-dark-theme"), preferDarkTheme);
}
void GtkConfig::applyAllSettings() const
{
setFont();
......@@ -124,6 +130,7 @@ void GtkConfig::applyAllSettings() const
setIconsInMenus();
setToolbarStyle();
setScrollbarBehavior();
setDarkThemePreference();
}
void GtkConfig::onGlobalSettingsChange(int settingsChangeType, int arg) const
......@@ -139,6 +146,8 @@ void GtkConfig::onGlobalSettingsChange(int settingsChangeType, int arg) const
setToolbarStyle();
} else if (changeType == SettingsChangeType::Settings && settingsCategory == SettingsCategory::Mouse) {
setScrollbarBehavior();
} else if (changeType == SettingsChangeType::Palette) {
setDarkThemePreference();
}
}
......
......@@ -63,6 +63,7 @@ public:
void setIconsInMenus() const;
void setToolbarStyle() const;
void setScrollbarBehavior() const;
void setDarkThemePreference() const;
void applyAllSettings() const;
......
......@@ -29,7 +29,6 @@
#include <KConfigGroup>
#include <gio/gio.h>
#include <gtk/gtk.h>
#include "appearancegtk3.h"
......@@ -64,7 +63,6 @@ bool AppearanceGTK3::saveSettings(const KSharedConfig::Ptr& file) const
KConfigGroup group(file, QStringLiteral("Settings"));
group.writeEntry(QStringLiteral("gtk-theme-name"), m_settings["theme"]);
group.writeEntry(QStringLiteral("gtk-application-prefer-dark-theme"), m_settings[QStringLiteral("application_prefer_dark_theme")]);
const bool sync = group.sync();
Q_ASSERT(sync);
......@@ -80,12 +78,9 @@ bool AppearanceGTK3::loadSettings(const KSharedConfig::Ptr& file)
return false;
}
m_settings = QMap<QString, QString> {
{QStringLiteral("application_prefer_dark_theme"), QStringLiteral("false")}
};
m_settings.clear();
m_settings[QStringLiteral("theme")] = group.readEntry(QStringLiteral("gtk-theme-name"));
m_settings[QStringLiteral("application_prefer_dark_theme")] = group.readEntry(QStringLiteral("gtk-application-prefer-dark-theme"));
for(auto it = m_settings.begin(); it != m_settings.end(); ) {
if (it.value().isEmpty()) {
it = m_settings.erase(it);
......@@ -111,16 +106,6 @@ QString AppearanceGTK3::defaultConfigFile() const
return root + '/' + configFileName();
}
bool AppearanceGTK3::getApplicationPreferDarkTheme() const
{
return m_settings[QStringLiteral("application_prefer_dark_theme")] == QStringLiteral("1") || m_settings[QStringLiteral("application_prefer_dark_theme")] == QStringLiteral("true");
}
void AppearanceGTK3::setApplicationPreferDarkTheme(bool enable)
{
m_settings[QStringLiteral("application_prefer_dark_theme")] = enable ? QStringLiteral("true") : QStringLiteral("false");
}
bool AppearanceGTK3::saveSettings(const QString& file) const
{
auto cfg = KSharedConfig::openConfig(file, KConfig::NoGlobals);
......@@ -148,8 +133,6 @@ bool AppearanceGTK3::saveSettings() const
g_autoptr(GSettings) gsettings = g_settings_new("org.gnome.desktop.interface");
g_settings_set_string(gsettings, "gtk-theme", m_settings["theme"].toUtf8().constData());
g_object_set(gtk_settings_get_default(), "gtk-application-prefer-dark-theme", getApplicationPreferDarkTheme(), nullptr);
auto cfg = KSharedConfig::openConfig(configFileName(), KConfig::NoGlobals);
return saveSettings(cfg);
}
......@@ -36,8 +36,6 @@ public:
bool loadSettings() override;
bool saveSettings(const QString &file) const override;
bool loadSettings(const QString &path) override;
bool getApplicationPreferDarkTheme() const;
void setApplicationPreferDarkTheme(bool enable);
private:
QString defaultConfigFile() const;
......
......@@ -58,16 +58,6 @@ void AppearenceGTK::setThemeGtk3(const QString& name)
return gtk3Appearance()->setTheme(name);
}
bool AppearenceGTK::getApplicationPreferDarkTheme() const
{
return ((AppearanceGTK3*)gtk3Appearance())->getApplicationPreferDarkTheme();
}
void AppearenceGTK::setApplicationPreferDarkTheme(bool enable)
{
return ((AppearanceGTK3*)gtk3Appearance())->setApplicationPreferDarkTheme(enable);
}
bool AppearenceGTK::loadFileConfig()
{
bool correct = false;
......
......@@ -41,11 +41,9 @@ public:
void setTheme(const QString &);
void setThemeGtk3(const QString &theme);
void setApplicationPreferDarkTheme(bool enable);
QString getTheme() const;
QString getThemeGtk3() const;
bool getApplicationPreferDarkTheme() const;
bool loadFileConfig();
bool saveFileConfig();
......
......@@ -93,7 +93,6 @@ GTKConfigKCModule::GTKConfigKCModule(QWidget* parent, const QVariantList& args )
// UI changes
connect(ui->cb_theme, SIGNAL(currentIndexChanged(int)), this, SLOT(appChanged()));
connect(ui->cb_theme_gtk3, SIGNAL(currentIndexChanged(int)), this, SLOT(appChanged()));
connect(ui->checkBox_theme_gtk3_prefer_dark, &QAbstractButton::clicked, this, &GTKConfigKCModule::appChanged);
// Preview updates
connect(ui->gtk2Preview, &QAbstractButton::clicked, this, &GTKConfigKCModule::runGtk2IfNecessary);
......@@ -126,7 +125,6 @@ void GTKConfigKCModule::syncUI()
{
appareance->setThemeGtk3(ui->cb_theme_gtk3->currentText());
appareance->setTheme(ui->cb_theme->currentText());
appareance->setApplicationPreferDarkTheme(ui->checkBox_theme_gtk3_prefer_dark->isChecked());
}
void GTKConfigKCModule::showThemeGHNS()
......@@ -348,10 +346,6 @@ void GTKConfigKCModule::refreshThemesUi(bool useConfig)
useConfig ? appareance->getThemeGtk3() : ui->cb_theme_gtk3->currentText(),
appareance->gtk3Appearance()->installedThemesNames());
// Dark theme for gtk3
ui->checkBox_theme_gtk3_prefer_dark->setChecked(appareance->getApplicationPreferDarkTheme());
m_saveEnabled = wasenabled;
emit changed(true);
}
......
......@@ -90,13 +90,6 @@
</item>
</layout>
</item>
<item row="2" column="1">
<widget class="QCheckBox" name="checkBox_theme_gtk3_prefer_dark">
<property name="text">
<string>Prefer dark theme</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
......@@ -162,7 +155,4 @@
</widget>
<resources/>
<connections/>
<buttongroups>
<buttongroup name="buttonGroup_primary_button_warps_slider"/>
</buttongroups>
</ui>
......@@ -17,11 +17,6 @@ ConfigSaveTest::ConfigSaveTest()
static void fillValues(QScopedPointer<AbstractAppearance>& a)
{
a->setTheme("a");
auto a3 = dynamic_cast<AppearanceGTK3*>(a.data());
if (a3) {
a3->setApplicationPreferDarkTheme(false);
}
}
void compareAppearances(QScopedPointer<AbstractAppearance>& reloaded, QScopedPointer<AbstractAppearance>& instance)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment