2
0
mirror of https://github.com/kotatogram/kotatogram-desktop synced 2025-08-31 22:55:11 +00:00

Implement system-based dark mode for Windows and Linux

This commit is contained in:
Ilya Fedin
2020-07-14 20:36:55 +04:00
committed by John Preston
parent fc3a9d98c0
commit 47a237c924
12 changed files with 185 additions and 20 deletions

View File

@@ -13,6 +13,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "platform/linux/linux_desktop_environment.h"
#include "platform/linux/specific_linux.h"
#include "core/sandbox.h"
#include "core/core_settings.h"
#include "core/application.h"
#include "main/main_domain.h"
#include "mainwindow.h"
@@ -22,6 +23,7 @@ namespace Libs {
namespace {
bool gtkTriedToInit = false;
bool gtkLoaded = false;
bool loadLibrary(QLibrary &lib, const char *name, int version) {
#if defined DESKTOP_APP_USE_PACKAGED && !defined DESKTOP_APP_USE_PACKAGED_LAZY
@@ -44,21 +46,6 @@ bool loadLibrary(QLibrary &lib, const char *name, int version) {
}
#ifndef TDESKTOP_DISABLE_GTK_INTEGRATION
template <typename T>
T gtkSetting(const gchar *propertyName) {
GtkSettings *settings = gtk_settings_get_default();
T value;
g_object_get(settings, propertyName, &value, nullptr);
return value;
}
QString gtkSetting(const gchar *propertyName) {
gchararray value = gtkSetting<gchararray>(propertyName);
QString str = QString::fromUtf8(value);
g_free(value);
return str;
}
void gtkMessageHandler(
const gchar *log_domain,
GLogLevelFlags log_level,
@@ -75,6 +62,7 @@ void gtkMessageHandler(
bool setupGtkBase(QLibrary &lib_gtk) {
if (!LOAD_SYMBOL(lib_gtk, "gtk_init_check", gtk_init_check)) return false;
if (!LOAD_SYMBOL(lib_gtk, "gtk_check_version", gtk_check_version)) return false;
if (!LOAD_SYMBOL(lib_gtk, "gtk_settings_get_default", gtk_settings_get_default)) return false;
if (!LOAD_SYMBOL(lib_gtk, "gtk_widget_show", gtk_widget_show)) return false;
@@ -173,10 +161,12 @@ bool IconThemeShouldBeSet() {
void SetIconTheme() {
Core::Sandbox::Instance().customEnterFromEventLoop([] {
if (IconThemeShouldBeSet()) {
if (GtkSettingSupported()
&& GtkLoaded()
&& IconThemeShouldBeSet()) {
DEBUG_LOG(("Set GTK icon theme"));
QIcon::setThemeName(gtkSetting("gtk-icon-theme-name"));
QIcon::setFallbackThemeName(gtkSetting("gtk-fallback-icon-theme"));
QIcon::setThemeName(GtkSetting("gtk-icon-theme-name"));
QIcon::setFallbackThemeName(GtkSetting("gtk-fallback-icon-theme"));
Platform::SetApplicationIcon(Window::CreateIcon());
if (App::wnd()) {
App::wnd()->setWindowIcon(Window::CreateIcon());
@@ -185,12 +175,19 @@ void SetIconTheme() {
}
});
}
void DarkModeChanged() {
Core::Sandbox::Instance().customEnterFromEventLoop([] {
Core::App().settings().setSystemDarkMode(Platform::IsDarkMode());
});
}
#endif // !TDESKTOP_DISABLE_GTK_INTEGRATION
} // namespace
#ifndef TDESKTOP_DISABLE_GTK_INTEGRATION
f_gtk_init_check gtk_init_check = nullptr;
f_gtk_check_version gtk_check_version = nullptr;
f_gtk_settings_get_default gtk_settings_get_default = nullptr;
f_gtk_widget_show gtk_widget_show = nullptr;
f_gtk_widget_hide gtk_widget_hide = nullptr;
@@ -245,6 +242,10 @@ f_gdk_pixbuf_get_pixels gdk_pixbuf_get_pixels = nullptr;
f_gdk_pixbuf_get_width gdk_pixbuf_get_width = nullptr;
f_gdk_pixbuf_get_height gdk_pixbuf_get_height = nullptr;
f_gdk_pixbuf_get_rowstride gdk_pixbuf_get_rowstride = nullptr;
bool GtkLoaded() {
return gtkLoaded;
}
#endif // !TDESKTOP_DISABLE_GTK_INTEGRATION
void start() {
@@ -255,7 +256,6 @@ void start() {
DEBUG_LOG(("Loading libraries"));
bool gtkLoaded = false;
QLibrary lib_gtk;
lib_gtk.setLoadHints(QLibrary::DeepBindHint);
@@ -284,6 +284,11 @@ void start() {
const auto settings = gtk_settings_get_default();
g_signal_connect(settings, "notify::gtk-icon-theme-name", G_CALLBACK(SetIconTheme), nullptr);
g_signal_connect(settings, "notify::gtk-theme-name", G_CALLBACK(DarkModeChanged), nullptr);
if (!gtk_check_version(3, 0, 0)) {
g_signal_connect(settings, "notify::gtk-application-prefer-dark-theme", G_CALLBACK(DarkModeChanged), nullptr);
}
} else {
LOG(("Could not load gtk-3 or gtk-x11-2.0!"));
}