See #4200
You can view, comment on, or merge this pull request online at:
https://github.com/geany/geany/pull/4205
-- Commit Summary --
* Attempt to use themed title bar on Windows
-- File Changes --
M src/libmain.c (3) M src/search.c (10) M src/win32.c (38) M src/win32.h (2)
-- Patch Links --
https://github.com/geany/geany/pull/4205.patch https://github.com/geany/geany/pull/4205.diff
Just a note: I've also attempted to update the titlebar color of various search dialogs here. There are many more dialogs in Geany but I think we don't have to be too crazy about updating every single dialog and clutter Geany with Windows-specific code - the search ones are probably the most used of them and most frequently displayed so for them I think it makes most sense.
@techee pushed 1 commit.
71a48f5f8180f41767dc3dd6573ea0b56815e131 Update titlebar before showing the window
@techee pushed 1 commit.
b2b8ff6fc0da043838f23d33879cf1616e1f5d46 Add <winreg.h> include
@techee pushed 1 commit.
69c8492a4a096c0b9f8530abb56f3b27ee72208a Test if the codeql problem persists also after removing the register check
@techee pushed 1 commit.
bb262fe886662de03df915b22eb941fcb3e52a20 try to remove all the added code
@techee pushed 1 commit.
6fef8592975700ed6f54964cf10db0e13699c93e Coment-out all the remaining code from this PR
@techee pushed 0 commits.
@techee pushed 1 commit.
27e439becca476e2ccbd1d6c7d606eb695dddf0d Ignore tests/ctags for codeql
@techee pushed 0 commits.
The CodeQL CI failure doesn't seem to be related to anything in this PR - I tried to comment-out all the added code and it still fails.
Unfortunately no idea what's going on :-(
@techee pushed 1 commit.
bb2860179757a6e62dea9d7b5cd026cbf79d36ba Use themed title bar on Windows
@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);
This seems overly risky to me, can't this simply be done in GtkWidget:realize? Or is this the hack to "avoid color change" mentioned above?
@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 ```
@techee 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);
This seems overly risky to me, can't this simply be done in GtkWidget:realize? Or is this the hack to "avoid color change" mentioned above?
…as reading https://github.com/geany/geany/issues/4200 showed me you're not testing more than I am
I actually tested it and it worked, I just don't have access to the Windows machine all the time (which is the case right now, I'll only be able to test it the next week).
In any case, I didn't like the manual `realize()` either and connecting the signal is a good idea and if it works, it's a better solution.
@techee pushed 1 commit.
d842f84e3f016324d05e4f7f9d4d62aba5292638 Add patch from Colomban
@b4n I've just tested your patch and it works as expected. Also a nice side-effect is having all the glade Windows titlebars themed now. Thanks!
I've squashed your commit to the original commit.
@b4n approved this pull request.
LGTM now (but no need to mention me twice in the commit message :wink: )
(but no need to mention me twice in the commit message 😉 )
Yeah, leftover of previous commit message before squashing. Removed now.
@b4n approved this pull request.
Untested but I trust you, so LGTM
Merged #4205 into master.
github-comments@lists.geany.org