Skip to content

Commit

Permalink
Implement org.freedesktop.appearance.color-scheme
Browse files Browse the repository at this point in the history
  • Loading branch information
ilya-fedin committed Mar 23, 2022
1 parent bf3fbd4 commit a899b99
Showing 1 changed file with 14 additions and 118 deletions.
132 changes: 14 additions & 118 deletions Telegram/SourceFiles/platform/linux/specific_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,9 @@ For license and copyright information please follow this link:

#ifndef DESKTOP_APP_DISABLE_X11_INTEGRATION
#include "base/platform/linux/base_linux_xcb_utilities.h"
#include "base/platform/linux/base_linux_xsettings.h"
#endif // !DESKTOP_APP_DISABLE_X11_INTEGRATION

#include <QtWidgets/QApplication>
#include <QtWidgets/QStyle>
#include <QtCore/QStandardPaths>
#include <QtCore/QProcess>
#include <QtGui/QWindow>
Expand Down Expand Up @@ -68,7 +66,6 @@ namespace {

constexpr auto kDesktopFile = ":/misc/telegramdesktop.desktop"_cs;
constexpr auto kIconName = "telegram"_cs;
constexpr auto kDarkColorLimit = 192;

constexpr auto kXDGDesktopPortalService = "org.freedesktop.portal.Desktop"_cs;
constexpr auto kXDGDesktopPortalObjectPath = "/org/freedesktop/portal/desktop"_cs;
Expand Down Expand Up @@ -400,147 +397,46 @@ QString GetIconName() {
}

std::optional<bool> IsDarkMode() {
#ifndef DESKTOP_APP_DISABLE_DBUS_INTEGRATION
[[maybe_unused]] static const auto Inited = [] {
static const auto Setter = [] {
crl::on_main([] {
Core::App().settings().setSystemDarkMode(IsDarkMode());
});
};

QObject::connect(
qGuiApp,
&QGuiApplication::paletteChanged,
Setter);

#ifndef DESKTOP_APP_DISABLE_X11_INTEGRATION
using base::Platform::XCB::XSettings;
if (const auto xSettings = XSettings::Instance()) {
xSettings->registerCallbackForProperty("Net/ThemeName", [](
xcb_connection_t *,
const QByteArray &,
const QVariant &,
void *) {
Setter();
}, nullptr);
}
#endif // !DESKTOP_APP_DISABLE_X11_INTEGRATION

#ifndef DESKTOP_APP_DISABLE_DBUS_INTEGRATION
using XDPSettingWatcher = base::Platform::XDP::SettingWatcher;
static const XDPSettingWatcher GtkThemeWatcher(
static const XDPSettingWatcher Watcher(
[=](
const Glib::ustring &group,
const Glib::ustring &key,
const Glib::VariantBase &value) {
if (group == "org.gnome.desktop.interface"
&& key == "gtk-theme") {
if (group == "org.freedesktop.appearance"
&& key == "color-scheme") {
Setter();
}
});

static const XDPSettingWatcher KdeColorSchemeWatcher(
[=](
const Glib::ustring &group,
const Glib::ustring &key,
const Glib::VariantBase &value) {
if (group == "org.kde.kdeglobals.General"
&& key == "ColorScheme") {
Setter();
}
});
#endif // !DESKTOP_APP_DISABLE_DBUS_INTEGRATION

return true;
}();

std::optional<bool> result;

const auto styleName = QApplication::style()->metaObject()->className();
if (styleName != qstr("QFusionStyle")
&& styleName != qstr("QWindowsStyle")) {
result = false;

const auto paletteBackgroundGray = qGray(
QPalette().color(QPalette::Window).rgb());

if (paletteBackgroundGray < kDarkColorLimit) {
result = true;
return result;
}
}

#ifndef DESKTOP_APP_DISABLE_X11_INTEGRATION
using base::Platform::XCB::XSettings;
if (const auto xSettings = XSettings::Instance()) {
const auto gtkThemeX = xSettings->setting("Net/ThemeName");
if (gtkThemeX.isValid()) {
result = false;
if (gtkThemeX.toString().contains(
qsl("-dark"),
Qt::CaseInsensitive)) {
result = true;
return result;
}
}
}
#endif // !DESKTOP_APP_DISABLE_X11_INTEGRATION

#ifndef DESKTOP_APP_DISABLE_DBUS_INTEGRATION
try {
using namespace base::Platform::XDP;

const auto gtkThemePortal = ReadSetting(
"org.gnome.desktop.interface",
"gtk-theme");

if (gtkThemePortal.has_value()) {
const auto gtkThemePortalString = QString::fromStdString(
base::Platform::GlibVariantCast<Glib::ustring>(
*gtkThemePortal));

result = false;

if (gtkThemePortalString.contains(
qsl("-dark"),
Qt::CaseInsensitive)) {
result = true;
return result;
}
}
} catch (...) {
}

try {
using namespace base::Platform::XDP;

const auto kdeBackgroundColorOptional = ReadSetting(
"org.kde.kdeglobals.Colors:Window",
"BackgroundNormal");

if (kdeBackgroundColorOptional.has_value()) {
const auto kdeBackgroundColorList = QString::fromStdString(
base::Platform::GlibVariantCast<Glib::ustring>(
*kdeBackgroundColorOptional)).split(',');

if (kdeBackgroundColorList.size() >= 3) {
result = false;

const auto kdeBackgroundGray = qGray(
kdeBackgroundColorList[0].toInt(),
kdeBackgroundColorList[1].toInt(),
kdeBackgroundColorList[2].toInt());

if (kdeBackgroundGray < kDarkColorLimit) {
result = true;
return result;
}
const auto result = base::Platform::XDP::ReadSetting(
"org.freedesktop.appearance",
"color-scheme");

if (result.has_value()) {
const auto value = base::Platform::GlibVariantCast<uint>(*result);
if (value == 1) {
return true;
}
return false;
}
} catch (...) {
}
#endif // !DESKTOP_APP_DISABLE_DBUS_INTEGRATION

return result;
return std::nullopt;
}

bool AutostartSupported() {
Expand Down

0 comments on commit a899b99

Please sign in to comment.