From 5acb90025f50c3d469978c25a21fc22d25a72a1e Mon Sep 17 00:00:00 2001 From: deepend-tildeclub Date: Tue, 9 Jun 2026 11:23:38 -0600 Subject: [PATCH] Fix GTK3 theme background refresh/saving --- .../test-theme-preferences-gtk3-populate.c | 11 ++++ src/fe-gtk/theme/theme-application.c | 8 ++- src/fe-gtk/theme/theme-preferences.c | 64 +++++++++++++++---- src/fe-gtk/theme/theme-runtime.c | 14 ++++ src/fe-gtk/theme/theme-runtime.h | 1 + 5 files changed, 85 insertions(+), 13 deletions(-) diff --git a/src/fe-gtk/theme/tests/test-theme-preferences-gtk3-populate.c b/src/fe-gtk/theme/tests/test-theme-preferences-gtk3-populate.c index f6d230f5..78f94e3d 100644 --- a/src/fe-gtk/theme/tests/test-theme-preferences-gtk3-populate.c +++ b/src/fe-gtk/theme/tests/test-theme-preferences-gtk3-populate.c @@ -107,12 +107,23 @@ theme_manager_reset_mode_colors (unsigned int mode, gboolean *palette_changed) *palette_changed = FALSE; } +void +theme_runtime_clear_gtk_mapped_custom_tokens (void) +{ +} + gboolean theme_manager_save_preferences (void) { return TRUE; } +void +theme_manager_apply_to_window (GtkWidget *window) +{ + (void)window; +} + void theme_manager_dispatch_changed (ThemeChangedReason reasons) { diff --git a/src/fe-gtk/theme/theme-application.c b/src/fe-gtk/theme/theme-application.c index e631a6c2..6a08c5a9 100644 --- a/src/fe-gtk/theme/theme-application.c +++ b/src/fe-gtk/theme/theme-application.c @@ -88,9 +88,15 @@ theme_application_apply_toplevel_theme (gboolean dark) gboolean theme_application_apply_mode (unsigned int mode, gboolean *palette_changed) { + static gboolean runtime_loaded = FALSE; gboolean dark; - theme_runtime_load (); + if (!runtime_loaded) + { + theme_runtime_load (); + runtime_loaded = TRUE; + } + dark = theme_runtime_apply_mode (mode, palette_changed); theme_application_apply_toplevel_theme (dark); diff --git a/src/fe-gtk/theme/theme-preferences.c b/src/fe-gtk/theme/theme-preferences.c index 51adafba..efcc9975 100644 --- a/src/fe-gtk/theme/theme-preferences.c +++ b/src/fe-gtk/theme/theme-preferences.c @@ -35,6 +35,7 @@ #include "theme-gtk3.h" #include "theme-manager.h" #include "theme-preferences.h" +#include "theme-runtime.h" extern void load_text_events (void); @@ -1417,17 +1418,56 @@ theme_preferences_gtk3_sync_remove_state (theme_preferences_ui *ui) gtk_widget_set_sensitive (ui->gtk3_remove, source == ZOITECHAT_GTK3_THEME_SOURCE_USER); } -static gboolean -theme_preferences_gtk3_apply_and_refresh (GError **error) +static void +theme_preferences_gtk3_sync_runtime_palette (theme_preferences_ui *ui) { - if (!theme_gtk3_apply_current (error)) - return FALSE; - theme_manager_dispatch_changed (THEME_CHANGED_REASON_THEME_PACK | - THEME_CHANGED_REASON_PALETTE | - THEME_CHANGED_REASON_WIDGET_STYLE | - THEME_CHANGED_REASON_USERLIST | - THEME_CHANGED_REASON_MODE); - return TRUE; + ThemeWidgetStyleValues style_values; + GtkWidget *style_source = NULL; + + if (ui && ui->parent) + style_source = GTK_WIDGET (ui->parent); + else if (ui && ui->gtk3_combo) + style_source = ui->gtk3_combo; + + theme_runtime_clear_gtk_mapped_custom_tokens (); + + theme_get_widget_style_values_for_widget (style_source, &style_values); + + theme_preferences_staged_set_color (THEME_TOKEN_TEXT_FOREGROUND, + &style_values.foreground, + NULL, + TRUE); + theme_preferences_staged_set_color (THEME_TOKEN_TEXT_BACKGROUND, + &style_values.background, + NULL, + TRUE); + theme_preferences_staged_set_color (THEME_TOKEN_SELECTION_FOREGROUND, + &style_values.selection_foreground, + NULL, + TRUE); + theme_preferences_staged_set_color (THEME_TOKEN_SELECTION_BACKGROUND, + &style_values.selection_background, + NULL, + TRUE); +} + +static gboolean +theme_preferences_gtk3_apply_and_refresh (theme_preferences_ui *ui, GError **error) +{ + if (!theme_gtk3_apply_current (error)) + return FALSE; + + if (ui && ui->parent) + theme_manager_apply_to_window (GTK_WIDGET (ui->parent)); + + theme_preferences_gtk3_sync_runtime_palette (ui); + + theme_manager_dispatch_changed (THEME_CHANGED_REASON_THEME_PACK | + THEME_CHANGED_REASON_PALETTE | + THEME_CHANGED_REASON_WIDGET_STYLE | + THEME_CHANGED_REASON_USERLIST | + THEME_CHANGED_REASON_MODE); + return TRUE; } static void @@ -1463,7 +1503,7 @@ theme_preferences_gtk3_changed_cb (GtkComboBox *combo, gpointer user_data) ui->setup_prefs->hex_gui_gtk3_variant = prefs.hex_gui_gtk3_variant; } - if (selection_changed && !theme_preferences_gtk3_apply_and_refresh (&error)) + if (selection_changed && !theme_preferences_gtk3_apply_and_refresh (ui, &error)) { theme_preferences_show_message (ui, GTK_MESSAGE_ERROR, error ? error->message : _("Failed to apply GTK3 theme.")); @@ -1552,7 +1592,7 @@ theme_preferences_populate_gtk3 (theme_preferences_ui *ui) g_free (final_id); } - if (should_apply && !theme_preferences_gtk3_apply_and_refresh (&error)) + if (should_apply && !theme_preferences_gtk3_apply_and_refresh (ui, &error)) { theme_preferences_show_message (ui, GTK_MESSAGE_ERROR, error ? error->message : _("Failed to apply GTK3 theme.")); diff --git a/src/fe-gtk/theme/theme-runtime.c b/src/fe-gtk/theme/theme-runtime.c index e8870762..15e13e11 100644 --- a/src/fe-gtk/theme/theme-runtime.c +++ b/src/fe-gtk/theme/theme-runtime.c @@ -410,6 +410,20 @@ theme_runtime_reset_mode_colors (gboolean dark_mode) dark_mode_active = FALSE; } +void +theme_runtime_clear_gtk_mapped_custom_tokens (void) +{ + light_custom_tokens[THEME_TOKEN_TEXT_FOREGROUND] = FALSE; + light_custom_tokens[THEME_TOKEN_TEXT_BACKGROUND] = FALSE; + light_custom_tokens[THEME_TOKEN_SELECTION_FOREGROUND] = FALSE; + light_custom_tokens[THEME_TOKEN_SELECTION_BACKGROUND] = FALSE; + + dark_custom_tokens[THEME_TOKEN_TEXT_FOREGROUND] = FALSE; + dark_custom_tokens[THEME_TOKEN_TEXT_BACKGROUND] = FALSE; + dark_custom_tokens[THEME_TOKEN_SELECTION_FOREGROUND] = FALSE; + dark_custom_tokens[THEME_TOKEN_SELECTION_BACKGROUND] = FALSE; +} + void theme_runtime_load (void) { diff --git a/src/fe-gtk/theme/theme-runtime.h b/src/fe-gtk/theme/theme-runtime.h index 4aa333a5..12923708 100644 --- a/src/fe-gtk/theme/theme-runtime.h +++ b/src/fe-gtk/theme/theme-runtime.h @@ -47,6 +47,7 @@ gboolean theme_runtime_apply_dark_mode (gboolean enable); void theme_runtime_user_set_color (ThemeSemanticToken token, const GdkRGBA *col); void theme_runtime_dark_set_color (ThemeSemanticToken token, const GdkRGBA *col); void theme_runtime_reset_mode_colors (gboolean dark_mode); +void theme_runtime_clear_gtk_mapped_custom_tokens (void); gboolean theme_runtime_get_color (ThemeSemanticToken token, GdkRGBA *out_rgba); gboolean theme_runtime_mode_has_user_colors (gboolean dark_mode); void theme_runtime_get_widget_style_values (ThemeWidgetStyleValues *out_values);