@b4n commented on this pull request.
- const gchar *reg_path = "Software\Microsoft\Windows\CurrentVersion\Themes\Personalize";
+ gboolean is_light_theme = TRUE; + DWORD val; + DWORD data_size = sizeof(val); + + g_return_if_fail(GTK_IS_WINDOW(window)); + + if (RegGetValueA(HKEY_CURRENT_USER, reg_path, "AppsUseLightTheme", RRF_RT_DWORD, NULL, &val, &data_size) == ERROR_SUCCESS) + is_light_theme = val; + + if (!is_light_theme) + { + GdkWindow *gdk_window; + + /* make sure the window is realized so the underlying GdkWindow is created */ + gtk_widget_realize(window);
…as reading #4200 showed me you're not testing more than I am, I'm suggesting something like the following, which is entirely untested, so probably works :grin:. It avoids manually realizing the window, and tries to hook all Glade-created windows into it (which should include at least main & prefs windows). I also moved the manual hooking into the `create_*()` functions to make it clear it has to be called exactly once per window (more would connect more handlers than necessary), although it was already the case.
```diff diff --git a/src/libmain.c b/src/libmain.c index fa912616a..3e860e73e 100644 --- a/src/libmain.c +++ b/src/libmain.c @@ -1259,9 +1259,6 @@ gint main_lib(gint argc, gchar **argv)
/* finally show the window */ document_grab_focus(doc); -#ifdef G_OS_WIN32 - win32_update_titlebar_theme(main_widgets.window); -#endif gtk_widget_show(main_widgets.window); main_status.main_window_realized = TRUE;
diff --git a/src/search.c b/src/search.c index 569fe204e..19dfc9c2f 100644 --- a/src/search.c +++ b/src/search.c @@ -548,6 +548,10 @@ static void create_find_dialog(void) GTK_BUTTON_BOX(bbox)); gtk_container_add(GTK_CONTAINER(exp), bbox); gtk_container_add(GTK_CONTAINER(vbox), exp); + +#ifdef G_OS_WIN32 + win32_update_titlebar_theme(find_dlg.dialog); +#endif }
@@ -575,9 +579,6 @@ void search_show_find_dialog(void) gtk_entry_set_text(GTK_ENTRY(find_dlg.entry), sel);
set_dialog_position(find_dlg.dialog, find_dlg.position); -#ifdef G_OS_WIN32 - win32_update_titlebar_theme(find_dlg.dialog); -#endif gtk_widget_show_all(find_dlg.dialog); } else @@ -735,6 +736,10 @@ static void create_replace_dialog(void) GTK_BUTTON_BOX(bbox)); gtk_container_add(GTK_CONTAINER(exp), bbox); gtk_container_add(GTK_CONTAINER(vbox), exp); + +#ifdef G_OS_WIN32 + win32_update_titlebar_theme(replace_dlg.dialog); +#endif }
@@ -756,9 +761,6 @@ void search_show_replace_dialog(void) gtk_entry_set_text(GTK_ENTRY(replace_dlg.find_entry), sel);
set_dialog_position(replace_dlg.dialog, replace_dlg.position); -#ifdef G_OS_WIN32 - win32_update_titlebar_theme(replace_dlg.dialog); -#endif gtk_widget_show_all(replace_dlg.dialog); } else @@ -1036,6 +1038,10 @@ static void create_fif_dialog(void) G_CALLBACK(on_find_in_files_dialog_response), NULL); g_signal_connect(fif_dlg.dialog, "delete-event", G_CALLBACK(gtk_widget_hide_on_delete), NULL); + +#ifdef G_OS_WIN32 + win32_update_titlebar_theme(fif_dlg.dialog); +#endif }
@@ -1065,9 +1071,6 @@ void search_show_find_in_files_dialog_full(const gchar *text, const gchar *dir) if (fif_dlg.dialog == NULL) { create_fif_dialog(); -#ifdef G_OS_WIN32 - win32_update_titlebar_theme(fif_dlg.dialog); -#endif gtk_widget_show_all(fif_dlg.dialog); if (doc && !text) sel = editor_get_default_selection(doc->editor, search_prefs.use_current_word, NULL); diff --git a/src/ui_utils.c b/src/ui_utils.c index b99338e4e..ccdfe3c66 100644 --- a/src/ui_utils.c +++ b/src/ui_utils.c @@ -2521,6 +2521,11 @@ void ui_init_builder(void)
widget = GTK_WIDGET(iter->data);
+#ifdef G_OS_WIN32 + if (GTK_IS_WINDOW(widget)) + win32_update_titlebar_theme(widget); +#endif + name = ui_guess_object_name(G_OBJECT(widget)); if (! name) { diff --git a/src/win32.c b/src/win32.c index a53827aec..44a881734 100644 --- a/src/win32.c +++ b/src/win32.c @@ -333,6 +333,20 @@ gchar *win32_get_user_config_dir(void) }
+static void on_update_titlebar_theme_realized(GtkWidget *widget, gpointer data) +{ + GdkWindow *gdk_window = gtk_widget_get_window(widget); + + g_return_if_fail(gdk_window != NULL); + + HWND hwnd = (HWND)gdk_win32_window_get_handle(gdk_window); + if (hwnd) + { + BOOL use_dark_mode = TRUE; + DwmSetWindowAttribute(hwnd, DWMWA_USE_IMMERSIVE_DARK_MODE, &use_dark_mode, sizeof(use_dark_mode)); + } +} + /* Makes titlebar dark when using dark theme; call this before * gtk_widget_show(window) to avoid titlebar color change */ void win32_update_titlebar_theme(GtkWidget *window) @@ -348,27 +362,7 @@ void win32_update_titlebar_theme(GtkWidget *window) is_light_theme = val;
if (!is_light_theme) - { - GdkWindow *gdk_window; - - /* make sure the window is realized so the underlying GdkWindow is created */ - gtk_widget_realize(window); - - gdk_window = gtk_widget_get_window(window); - if (gdk_window) - { - HWND hwnd = (HWND)gdk_win32_window_get_handle(gdk_window); - if (hwnd) - { - BOOL use_dark_mode = TRUE; - DwmSetWindowAttribute(hwnd, DWMWA_USE_IMMERSIVE_DARK_MODE, &use_dark_mode, sizeof(use_dark_mode)); - } - else - g_warning("Failed to get HWND from GdkWindow."); - } - else - g_warning("Failed to get GdkWindow from GtkWidget."); - } + g_signal_connect(window, "realize", G_CALLBACK(on_update_titlebar_theme_realized), NULL); }
#endif ```