mirror of
https://github.com/AyuGram/AyuGramDesktop.git
synced 2025-06-05 06:33:57 +02:00
Add a way to get dark mode state on KDE
This commit is contained in:
parent
822c1cafd2
commit
81d052adfc
1 changed files with 92 additions and 23 deletions
|
@ -26,6 +26,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||||
|
|
||||||
#ifndef DESKTOP_APP_DISABLE_DBUS_INTEGRATION
|
#ifndef DESKTOP_APP_DISABLE_DBUS_INTEGRATION
|
||||||
#include "base/platform/linux/base_linux_dbus_utilities.h"
|
#include "base/platform/linux/base_linux_dbus_utilities.h"
|
||||||
|
#include "base/platform/linux/base_linux_xdp_utilities.h"
|
||||||
#include "platform/linux/linux_notification_service_watcher.h"
|
#include "platform/linux/linux_notification_service_watcher.h"
|
||||||
#include "platform/linux/linux_mpris_support.h"
|
#include "platform/linux/linux_mpris_support.h"
|
||||||
#include "platform/linux/linux_gsd_media_keys.h"
|
#include "platform/linux/linux_gsd_media_keys.h"
|
||||||
|
@ -36,6 +37,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||||
#endif // !DESKTOP_APP_DISABLE_X11_INTEGRATION
|
#endif // !DESKTOP_APP_DISABLE_X11_INTEGRATION
|
||||||
|
|
||||||
#include <QtWidgets/QApplication>
|
#include <QtWidgets/QApplication>
|
||||||
|
#include <QtWidgets/QStyle>
|
||||||
#include <QtWidgets/QDesktopWidget>
|
#include <QtWidgets/QDesktopWidget>
|
||||||
#include <QtCore/QStandardPaths>
|
#include <QtCore/QStandardPaths>
|
||||||
#include <QtCore/QProcess>
|
#include <QtCore/QProcess>
|
||||||
|
@ -67,6 +69,7 @@ namespace {
|
||||||
constexpr auto kDesktopFile = ":/misc/telegramdesktop.desktop"_cs;
|
constexpr auto kDesktopFile = ":/misc/telegramdesktop.desktop"_cs;
|
||||||
constexpr auto kIconName = "telegram"_cs;
|
constexpr auto kIconName = "telegram"_cs;
|
||||||
constexpr auto kHandlerTypeName = "x-scheme-handler/tg"_cs;
|
constexpr auto kHandlerTypeName = "x-scheme-handler/tg"_cs;
|
||||||
|
constexpr auto kDarkColorLimit = 192;
|
||||||
|
|
||||||
constexpr auto kXDGDesktopPortalService = "org.freedesktop.portal.Desktop"_cs;
|
constexpr auto kXDGDesktopPortalService = "org.freedesktop.portal.Desktop"_cs;
|
||||||
constexpr auto kXDGDesktopPortalObjectPath = "/org/freedesktop/portal/desktop"_cs;
|
constexpr auto kXDGDesktopPortalObjectPath = "/org/freedesktop/portal/desktop"_cs;
|
||||||
|
@ -503,15 +506,36 @@ QImage GetImageFromClipboard() {
|
||||||
}
|
}
|
||||||
|
|
||||||
std::optional<bool> IsDarkMode() {
|
std::optional<bool> IsDarkMode() {
|
||||||
|
std::optional<bool> failResult;
|
||||||
|
|
||||||
if (static auto Once = false; !std::exchange(Once, true)) {
|
if (static auto Once = false; !std::exchange(Once, true)) {
|
||||||
|
const auto onChanged = [] {
|
||||||
|
Core::Sandbox::Instance().customEnterFromEventLoop([] {
|
||||||
|
Core::App().settings().setSystemDarkMode(IsDarkMode());
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
QObject::connect(
|
||||||
|
qGuiApp,
|
||||||
|
&QGuiApplication::paletteChanged,
|
||||||
|
onChanged);
|
||||||
|
|
||||||
|
#ifndef DESKTOP_APP_DISABLE_DBUS_INTEGRATION
|
||||||
|
using XDPSettingWatcher = base::Platform::XDP::SettingWatcher;
|
||||||
|
static const XDPSettingWatcher KdeColorSchemeWatcher(
|
||||||
|
[=](
|
||||||
|
const Glib::ustring &group,
|
||||||
|
const Glib::ustring &key,
|
||||||
|
const Glib::VariantBase &value) {
|
||||||
|
if (group == "org.kde.kdeglobals.General"
|
||||||
|
&& key == "ColorScheme") {
|
||||||
|
onChanged();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
#endif // !DESKTOP_APP_DISABLE_DBUS_INTEGRATION
|
||||||
|
|
||||||
const auto integration = BaseGtkIntegration::Instance();
|
const auto integration = BaseGtkIntegration::Instance();
|
||||||
if (integration) {
|
if (integration) {
|
||||||
const auto onChanged = [] {
|
|
||||||
Core::Sandbox::Instance().customEnterFromEventLoop([] {
|
|
||||||
Core::App().settings().setSystemDarkMode(IsDarkMode());
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
integration->connectToSetting(
|
integration->connectToSetting(
|
||||||
"gtk-theme-name",
|
"gtk-theme-name",
|
||||||
onChanged);
|
onChanged);
|
||||||
|
@ -524,32 +548,77 @@ std::optional<bool> IsDarkMode() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const auto integration = BaseGtkIntegration::Instance();
|
const auto styleName = QApplication::style()->metaObject()->className();
|
||||||
if (!integration) {
|
if (styleName != qstr("QFusionStyle")
|
||||||
return std::nullopt;
|
&& styleName != qstr("QWindowsStyle")) {
|
||||||
}
|
failResult = false;
|
||||||
|
|
||||||
if (integration->checkVersion(3, 0, 0)) {
|
const auto paletteBackgroundGray = qGray(
|
||||||
const auto preferDarkTheme = integration->getBoolSetting(
|
QPalette().color(QPalette::Window).rgb());
|
||||||
qsl("gtk-application-prefer-dark-theme"));
|
|
||||||
|
|
||||||
if (!preferDarkTheme.has_value()) {
|
if (paletteBackgroundGray < kDarkColorLimit) {
|
||||||
return std::nullopt;
|
|
||||||
} else if (*preferDarkTheme) {
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const auto themeName = integration->getStringSetting(
|
#ifndef DESKTOP_APP_DISABLE_DBUS_INTEGRATION
|
||||||
qsl("gtk-theme-name"));
|
try {
|
||||||
|
using namespace base::Platform::XDP;
|
||||||
|
|
||||||
if (!themeName.has_value()) {
|
const auto kdeBackgroundColorOptional = ReadSetting(
|
||||||
return std::nullopt;
|
"org.kde.kdeglobals.Colors:Window",
|
||||||
} else if (themeName->contains(qsl("-dark"), Qt::CaseInsensitive)) {
|
"BackgroundNormal");
|
||||||
return true;
|
|
||||||
|
if (kdeBackgroundColorOptional.has_value()) {
|
||||||
|
const auto kdeBackgroundColorList = QString::fromStdString(
|
||||||
|
base::Platform::GlibVariantCast<Glib::ustring>(
|
||||||
|
*kdeBackgroundColorOptional)).split(',');
|
||||||
|
|
||||||
|
if (kdeBackgroundColorList.size() >= 3) {
|
||||||
|
failResult = false;
|
||||||
|
|
||||||
|
const auto kdeBackgroundGray = qGray(
|
||||||
|
kdeBackgroundColorList[0].toInt(),
|
||||||
|
kdeBackgroundColorList[1].toInt(),
|
||||||
|
kdeBackgroundColorList[2].toInt());
|
||||||
|
|
||||||
|
if (kdeBackgroundGray < kDarkColorLimit) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (...) {
|
||||||
|
}
|
||||||
|
#endif // !DESKTOP_APP_DISABLE_DBUS_INTEGRATION
|
||||||
|
|
||||||
|
const auto integration = BaseGtkIntegration::Instance();
|
||||||
|
if (integration) {
|
||||||
|
if (integration->checkVersion(3, 0, 0)) {
|
||||||
|
const auto preferDarkTheme = integration->getBoolSetting(
|
||||||
|
qsl("gtk-application-prefer-dark-theme"));
|
||||||
|
|
||||||
|
if (preferDarkTheme.has_value()) {
|
||||||
|
failResult = false;
|
||||||
|
|
||||||
|
if (*preferDarkTheme) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto themeName = integration->getStringSetting(
|
||||||
|
qsl("gtk-theme-name"));
|
||||||
|
|
||||||
|
if (themeName.has_value()) {
|
||||||
|
failResult = false;
|
||||||
|
|
||||||
|
if (themeName->contains(qsl("-dark"), Qt::CaseInsensitive)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return failResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool AutostartSupported() {
|
bool AutostartSupported() {
|
||||||
|
|
Loading…
Add table
Reference in a new issue