Apply app theme CSS to menubar everywhere

This commit is contained in:
2026-03-22 10:00:27 -06:00
parent 1208449322
commit cd93fe3b0e
4 changed files with 43 additions and 83 deletions

View File

@@ -26,16 +26,29 @@
#include "theme-runtime.h" #include "theme-runtime.h"
#include "theme-gtk3.h" #include "theme-gtk3.h"
#include "../maingui.h" #include "../maingui.h"
#ifdef G_OS_WIN32
#include <gtk/gtk.h> #include <gtk/gtk.h>
#if defined(__GNUC__) || defined(__clang__)
extern char *theme_css_build_toplevel_classes (void) __attribute__ ((weak));
#else
extern char *theme_css_build_toplevel_classes (void);
#endif
static char *
theme_application_build_toplevel_css (void)
{
if (theme_css_build_toplevel_classes)
return theme_css_build_toplevel_classes ();
return g_strdup ("");
}
static void static void
theme_application_apply_windows_theme (gboolean dark) theme_application_apply_toplevel_theme (gboolean dark)
{ {
GtkSettings *settings = gtk_settings_get_default (); GtkSettings *settings = gtk_settings_get_default ();
static GtkCssProvider *win_theme_provider = NULL; static GtkCssProvider *theme_provider = NULL;
static gboolean win_theme_provider_installed = FALSE; static gboolean theme_provider_installed = FALSE;
GdkScreen *screen; GdkScreen *screen;
gboolean prefer_dark = dark; gboolean prefer_dark = dark;
char *css; char *css;
@@ -56,33 +69,21 @@ theme_application_apply_windows_theme (gboolean dark)
if (!screen) if (!screen)
return; return;
if (theme_gtk3_is_active ()) if (!theme_provider)
{ theme_provider = gtk_css_provider_new ();
if (win_theme_provider_installed && win_theme_provider)
{
gtk_style_context_remove_provider_for_screen (screen,
GTK_STYLE_PROVIDER (win_theme_provider));
win_theme_provider_installed = FALSE;
}
return;
}
if (!win_theme_provider) css = theme_application_build_toplevel_css ();
win_theme_provider = gtk_css_provider_new (); gtk_css_provider_load_from_data (theme_provider, css, -1, NULL);
css = theme_css_build_toplevel_classes ();
gtk_css_provider_load_from_data (win_theme_provider, css, -1, NULL);
g_free (css); g_free (css);
if (!win_theme_provider_installed) if (!theme_provider_installed)
{ {
gtk_style_context_add_provider_for_screen (screen, gtk_style_context_add_provider_for_screen (screen,
GTK_STYLE_PROVIDER (win_theme_provider), GTK_STYLE_PROVIDER (theme_provider),
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION + 1); GTK_STYLE_PROVIDER_PRIORITY_APPLICATION + 1);
win_theme_provider_installed = TRUE; theme_provider_installed = TRUE;
} }
} }
#endif
gboolean gboolean
theme_application_apply_mode (unsigned int mode, gboolean *palette_changed) theme_application_apply_mode (unsigned int mode, gboolean *palette_changed)
@@ -91,10 +92,7 @@ theme_application_apply_mode (unsigned int mode, gboolean *palette_changed)
theme_runtime_load (); theme_runtime_load ();
dark = theme_runtime_apply_mode (mode, palette_changed); dark = theme_runtime_apply_mode (mode, palette_changed);
theme_application_apply_toplevel_theme (dark);
#ifdef G_OS_WIN32
theme_application_apply_windows_theme (dark);
#endif
theme_application_reload_input_style (); theme_application_reload_input_style ();

View File

@@ -361,20 +361,22 @@ theme_css_build_toplevel_classes (void)
"color: #f0f0f0;" "color: #f0f0f0;"
"border-color: #202020;" "border-color: #202020;"
"}" "}"
"window.%s menubar, window.%s menubar:backdrop, window.%s menuitem, window.%s menuitem:backdrop {" "window.%s menubar, window.%s menubar:backdrop, window.%s menubar box, window.%s menubar box:backdrop, window.%s menuitem, window.%s menuitem:backdrop {"
"background-color: #202020;" "background-color: @theme_bg_color;"
"color: #f0f0f0;" "background-image: none;"
"border-color: #202020;" "color: @theme_fg_color;"
"border-color: @theme_bg_color;"
"}" "}"
"window.%s, window.%s:backdrop, .%s {" "window.%s, window.%s:backdrop, .%s {"
"background-color: #f6f6f6;" "background-color: #f6f6f6;"
"color: #101010;" "color: #101010;"
"border-color: #f6f6f6;" "border-color: #f6f6f6;"
"}" "}"
"window.%s menubar, window.%s menubar:backdrop, window.%s menuitem, window.%s menuitem:backdrop {" "window.%s menubar, window.%s menubar:backdrop, window.%s menubar box, window.%s menubar box:backdrop, window.%s menuitem, window.%s menuitem:backdrop {"
"background-color: #f6f6f6;" "background-color: @theme_bg_color;"
"color: #101010;" "background-image: none;"
"border-color: #f6f6f6;" "color: @theme_fg_color;"
"border-color: @theme_bg_color;"
"}", "}",
theme_css_selector_dark_class, theme_css_selector_dark_class,
theme_css_selector_dark_class, theme_css_selector_dark_class,
@@ -383,6 +385,10 @@ theme_css_build_toplevel_classes (void)
theme_css_selector_dark_class, theme_css_selector_dark_class,
theme_css_selector_dark_class, theme_css_selector_dark_class,
theme_css_selector_dark_class, theme_css_selector_dark_class,
theme_css_selector_dark_class,
theme_css_selector_dark_class,
theme_css_selector_light_class,
theme_css_selector_light_class,
theme_css_selector_light_class, theme_css_selector_light_class,
theme_css_selector_light_class, theme_css_selector_light_class,
theme_css_selector_light_class, theme_css_selector_light_class,

View File

@@ -216,8 +216,6 @@ theme_manager_queue_auto_refresh (GtkSettings *settings, GParamSpec *pspec, gpoi
void void
theme_manager_init (void) theme_manager_init (void)
{ {
GtkSettings *settings;
if (!theme_manager_listeners) if (!theme_manager_listeners)
theme_manager_listeners = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, theme_manager_listeners = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL,
theme_listener_free); theme_listener_free);
@@ -225,21 +223,10 @@ theme_manager_init (void)
if (!theme_manager_setup_listener_id) if (!theme_manager_setup_listener_id)
theme_manager_setup_listener_id = theme_listener_register ("setup.apply", theme_manager_setup_apply_listener, NULL); theme_manager_setup_listener_id = theme_listener_register ("setup.apply", theme_manager_setup_apply_listener, NULL);
settings = gtk_settings_get_default (); fe_set_auto_dark_mode_state (FALSE);
if (settings)
fe_set_auto_dark_mode_state (theme_policy_system_prefers_dark ());
theme_application_apply_mode (prefs.hex_gui_dark_mode, NULL); theme_application_apply_mode (prefs.hex_gui_dark_mode, NULL);
theme_gtk3_init (); theme_gtk3_init ();
zoitechat_set_theme_post_apply_callback (theme_manager_handle_theme_applied); zoitechat_set_theme_post_apply_callback (theme_manager_handle_theme_applied);
if (settings)
{
g_signal_connect (settings, "notify::gtk-application-prefer-dark-theme",
G_CALLBACK (theme_manager_queue_auto_refresh), NULL);
g_signal_connect (settings, "notify::gtk-theme-name",
G_CALLBACK (theme_manager_queue_auto_refresh), NULL);
}
} }
gboolean gboolean
@@ -494,7 +481,6 @@ theme_manager_apply_wayland_kde_csd (GtkWidget *window)
static void static void
theme_manager_apply_platform_window_theme (GtkWidget *window) theme_manager_apply_platform_window_theme (GtkWidget *window)
{ {
#ifdef G_OS_WIN32
GtkStyleContext *context; GtkStyleContext *context;
gboolean dark; gboolean dark;
@@ -516,6 +502,7 @@ theme_manager_apply_platform_window_theme (GtkWidget *window)
gtk_style_context_remove_class (context, "zoitechat-light"); gtk_style_context_remove_class (context, "zoitechat-light");
gtk_style_context_add_class (context, dark ? "zoitechat-dark" : "zoitechat-light"); gtk_style_context_add_class (context, dark ? "zoitechat-dark" : "zoitechat-light");
} }
#ifdef G_OS_WIN32
fe_win32_apply_native_titlebar (window, dark); fe_win32_apply_native_titlebar (window, dark);
#else #else
theme_manager_apply_wayland_kde_csd (window); theme_manager_apply_wayland_kde_csd (window);

View File

@@ -27,38 +27,7 @@
gboolean gboolean
theme_policy_system_prefers_dark (void) theme_policy_system_prefers_dark (void)
{ {
GtkSettings *settings = gtk_settings_get_default (); return FALSE;
gboolean prefer_dark = FALSE;
char *theme_name = NULL;
#ifdef G_OS_WIN32
gboolean have_win_pref = FALSE;
if (fe_win32_high_contrast_is_enabled ())
return FALSE;
have_win_pref = fe_win32_try_get_system_dark (&prefer_dark);
if (!have_win_pref)
#endif
if (settings && g_object_class_find_property (G_OBJECT_GET_CLASS (settings),
"gtk-application-prefer-dark-theme"))
{
g_object_get (settings, "gtk-application-prefer-dark-theme", &prefer_dark, NULL);
}
if (settings && !prefer_dark)
{
g_object_get (settings, "gtk-theme-name", &theme_name, NULL);
if (theme_name)
{
char *lower = g_ascii_strdown (theme_name, -1);
if (g_str_has_suffix (lower, "-dark") || g_strrstr (lower, "dark"))
prefer_dark = TRUE;
g_free (lower);
g_free (theme_name);
}
}
return prefer_dark;
} }
gboolean gboolean