Skip to content

Commit

Permalink
Fallback to portal on Linux if QStyleHints::colorScheme is unknown
Browse files Browse the repository at this point in the history
  • Loading branch information
ilya-fedin authored and john-preston committed Oct 14, 2024
1 parent f2e0e48 commit a88f48c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 16 deletions.
47 changes: 39 additions & 8 deletions Telegram/SourceFiles/platform/linux/integration_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ For license and copyright information please follow this link:
#include "base/random.h"

#include <QtCore/QAbstractEventDispatcher>
#include <QtGui/QStyleHints>

#include <gio/gio.hpp>
#include <xdpinhibit/xdpinhibit.hpp>
Expand Down Expand Up @@ -189,24 +190,30 @@ class LinuxIntegration final : public Integration, public base::has_weak_ptr {

const gi::ref_ptr<Application> _application;
XdpInhibit::InhibitProxy _inhibitProxy;
#if QT_VERSION < QT_VERSION_CHECK(6, 5, 0)
rpl::variable<std::optional<bool>> _darkMode;
base::Platform::XDP::SettingWatcher _darkModeWatcher;
#endif // Qt < 6.5.0
rpl::lifetime _lifetime;
};

LinuxIntegration::LinuxIntegration()
: _application(MakeApplication())
#if QT_VERSION < QT_VERSION_CHECK(6, 5, 0)
, _darkMode([]() -> std::optional<bool> {
if (auto value = base::Platform::XDP::ReadSetting(
"org.freedesktop.appearance",
"color-scheme")) {
return value->get_uint32() == 1;
}
return std::nullopt;
})
, _darkModeWatcher(
"org.freedesktop.appearance",
"color-scheme",
[](GLib::Variant value) {
[=](GLib::Variant value) {
Core::Sandbox::Instance().customEnterFromEventLoop([&] {
Core::App().settings().setSystemDarkMode(value.get_uint32() == 1);
_darkMode = value.get_uint32() == 1;
});
})
#endif // Qt < 6.5.0
{
}
) {
LOG(("Icon theme: %1").arg(QIcon::themeName()));
LOG(("Fallback icon theme: %1").arg(QIcon::fallbackThemeName()));

Expand All @@ -230,6 +237,30 @@ void LinuxIntegration::init() {

initInhibit();
}));

_darkMode.value()
#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
| rpl::filter([] {
return QGuiApplication::styleHints()->colorScheme()
== Qt::ColorScheme::Unknown;
})
#endif // Qt >= 6.5.0
| rpl::start_with_next([](std::optional<bool> value) {
Core::App().settings().setSystemDarkMode(value);
}, _lifetime);

#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
Core::App().settings().systemDarkModeValue(
) | rpl::filter([=](std::optional<bool> value) {
return !value && _darkMode.current();
}) | rpl::start_with_next([=] {
crl::on_main(this, [=] {
if (!Core::App().settings().systemDarkMode()) {
Core::App().settings().setSystemDarkMode(_darkMode.current());
}
});
}, _lifetime);
#endif // Qt >= 6.5.0
}

void LinuxIntegration::initInhibit() {
Expand Down
8 changes: 1 addition & 7 deletions Telegram/SourceFiles/platform/linux/specific_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -536,13 +536,7 @@ QString SingleInstanceLocalServerName(const QString &hash) {

#if QT_VERSION < QT_VERSION_CHECK(6, 5, 0)
std::optional<bool> IsDarkMode() {
auto result = base::Platform::XDP::ReadSetting(
"org.freedesktop.appearance",
"color-scheme");

return result.has_value()
? std::make_optional(result->get_uint32() == 1)
: std::nullopt;
return std::nullopt;
}
#endif // Qt < 6.5.0

Expand Down
6 changes: 5 additions & 1 deletion Telegram/SourceFiles/window/themes/window_theme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,8 @@ void ChatBackground::start() {
#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
rpl::single(
QGuiApplication::styleHints()->colorScheme()
) | rpl::filter(
rpl::mappers::_1 != Qt::ColorScheme::Unknown
) | rpl::then(
base::qt_signal_producer(
QGuiApplication::styleHints(),
Expand All @@ -542,7 +544,9 @@ void ChatBackground::start() {
Core::App().settings().setSystemDarkMode(dark);
}, _lifetime);
#else // Qt >= 6.5.0
Core::App().settings().setSystemDarkMode(Platform::IsDarkMode());
if (const auto dark = Platform::IsDarkMode()) {
Core::App().settings().setSystemDarkMode(dark);
}
#endif // Qt < 6.5.0
}

Expand Down

0 comments on commit a88f48c

Please sign in to comment.