From 40d6ee6ad5f920e450160c26bad1dfe2136a529e Mon Sep 17 00:00:00 2001 From: liuxinhao Date: Fri, 27 Sep 2024 15:52:39 +0800 Subject: [PATCH] fix(gnome theme): Fix the issue where switching between light and dark themes does not work in Gnome 41 and later versions. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修复切换深浅色主题对gnome41之后的应用失效的问题 #35120 --- plugins/appearance/theme/appearance-theme.cpp | 41 ++++++++++++++++++- plugins/appearance/theme/appearance-theme.h | 2 + 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/plugins/appearance/theme/appearance-theme.cpp b/plugins/appearance/theme/appearance-theme.cpp index 383e59d..186e156 100644 --- a/plugins/appearance/theme/appearance-theme.cpp +++ b/plugins/appearance/theme/appearance-theme.cpp @@ -24,6 +24,16 @@ #define MOUSE_SCHEMA_KEY_CURSOR_THEME "cursor-theme" #define MOUSE_SCHEMA_KEY_CURSOR_SIZE "cursor-size" +#define GNOME_DESKTOP_SCHEMA_ID "org.gnome.desktop.interface" +#define GNOME_DESKTOP_SCHEMA_KEY_COLOR_SCHEMA "color-scheme" +enum GnomeDesktopColorScheme +{ + GNOME_COLOR_SCHEME_DEFAULT, + GNOME_COLOR_SCHEME_PREFER_DARK, + GNOME_COLOR_SCHEME_PREFER_LIGHT, + GNOME_COLOR_SCHEME_LAST +}; + namespace Kiran { AppearanceTheme::AppearanceTheme() @@ -40,6 +50,11 @@ AppearanceTheme::AppearanceTheme() { this->mouse_settings_ = Gio::Settings::create(MOUSE_SCHEMA_ID); } + + if (std::find(schemas.begin(), schemas.end(), GNOME_DESKTOP_SCHEMA_ID) != schemas.end()) + { + this->gnome_desktop_settigns_ = Gio::Settings::create(GNOME_DESKTOP_SCHEMA_ID); + } } void AppearanceTheme::init() @@ -54,10 +69,12 @@ void AppearanceTheme::init() { this->add_theme(theme); } - } + } this->xsettings_settings_->signal_changed().connect(sigc::mem_fun(this, &AppearanceTheme::on_xsettings_settings_changed)); this->theme_monitor_.signal_monitor_event().connect(sigc::mem_fun(this, &AppearanceTheme::on_monitor_event_changed)); + + this->try_sync_gnome_color_schema(); } ThemeInfoVec AppearanceTheme::get_themes_by_type(AppearanceThemeType type) @@ -111,6 +128,7 @@ bool AppearanceTheme::set_theme(ThemeKey key, CCErrorCode& error_code) break; case AppearanceThemeType::APPEARANCE_THEME_TYPE_GTK: this->set_gtk_theme(theme->name); + this->try_sync_gnome_color_schema(); break; case AppearanceThemeType::APPEARANCE_THEME_TYPE_METACITY: this->set_metacity_theme(theme->name); @@ -289,4 +307,25 @@ void AppearanceTheme::on_monitor_event_changed(std::shared_ptr this->themes_changed_.emit(key, ThemeEventType::THEME_EVENT_TYPE_CHG); } } + +void AppearanceTheme::try_sync_gnome_color_schema() +{ + if (!this->gnome_desktop_settigns_) + { + return; + } + + auto theme_name = this->get_theme(APPEARANCE_THEME_TYPE_GTK); + if (theme_name == APPEARANCE_DEFAULT_DARK_GTK_THEME) + { + this->gnome_desktop_settigns_->set_enum(GNOME_DESKTOP_SCHEMA_KEY_COLOR_SCHEMA, + GNOME_COLOR_SCHEME_PREFER_DARK); + } + else + { + this->gnome_desktop_settigns_->set_enum(GNOME_DESKTOP_SCHEMA_KEY_COLOR_SCHEMA, + GNOME_COLOR_SCHEME_PREFER_LIGHT); + } +} + } // namespace Kiran \ No newline at end of file diff --git a/plugins/appearance/theme/appearance-theme.h b/plugins/appearance/theme/appearance-theme.h index 77d5149..b812128 100644 --- a/plugins/appearance/theme/appearance-theme.h +++ b/plugins/appearance/theme/appearance-theme.h @@ -74,6 +74,7 @@ private: void on_xsettings_settings_changed(const Glib::ustring& key); void on_monitor_event_changed(std::shared_ptr monitor_info, ThemeMonitorEventType event_type); + void try_sync_gnome_color_schema(); private: static AppearanceTheme* instance_; @@ -89,5 +90,6 @@ private: Glib::RefPtr xsettings_settings_; Glib::RefPtr marco_settings_; Glib::RefPtr mouse_settings_; + Glib::RefPtr gnome_desktop_settigns_; }; } // namespace Kiran \ No newline at end of file -- 2.27.0