commit ffc6d14af52ef5ea810ee90cdaff2605301c9f30
parent e6f7e038b8bbca487e78ebfc6fe21d6852330623
Author: Justin M. Keyes <justinkz@gmail.com>
Date: Thu, 13 Oct 2022 15:08:38 +0200
fix(windows): set console icon later in startup
Problem:
Windows console icon is set early in startup, but there are some cases
where `os_exit` is called and we don't restore the original icon.
Solution:
- Move `os_icon_init()` later in the startup sequence, and only if
`use_builtin_ui==true`.
- Rename functions: use `os_` prefix for platform-specific code.
Diffstat:
1 file changed, 20 insertions(+), 25 deletions(-)
diff --git a/src/nvim/main.c b/src/nvim/main.c
@@ -211,17 +211,7 @@ HWND hWnd = NULL;
static HICON hOrigIconSmall = NULL;
static HICON hOrigIcon = NULL;
-/// Save Windows console icon to be reset later
-static void SaveWin32ConsoleIcon(void)
-{
- if ((hWnd = GetConsoleWindow()) == NULL) {
- return;
- }
- hOrigIconSmall = (HICON)SendMessage(hWnd, WM_GETICON, (WPARAM)ICON_SMALL, (LPARAM)0);
- hOrigIcon = (HICON)SendMessage(hWnd, WM_GETICON, (WPARAM)ICON_BIG, (LPARAM)0);
-}
-
-static void SetConsoleIcon(HWND hWindow, HICON hIconSmall, HICON hIcon)
+static void os_icon_set(HWND hWindow, HICON hIconSmall, HICON hIcon)
{
if (hWindow == NULL) {
return;
@@ -234,15 +224,18 @@ static void SetConsoleIcon(HWND hWindow, HICON hIconSmall, HICON hIcon)
}
}
-/// Reset Windows console icon to original
-static void ResetWin32ConsoleIcon(void)
+/// Sets Nvim logo as Windows console icon.
+///
+/// Saves the original icon so it can be restored at exit.
+static void os_icon_init(void)
{
- SetConsoleIcon(hWnd, hOrigIconSmall, hOrigIcon);
-}
+ if ((hWnd = GetConsoleWindow()) == NULL) {
+ return;
+ }
+ // Save Windows console icon to be restored later.
+ hOrigIconSmall = (HICON)SendMessage(hWnd, WM_GETICON, (WPARAM)ICON_SMALL, (LPARAM)0);
+ hOrigIcon = (HICON)SendMessage(hWnd, WM_GETICON, (WPARAM)ICON_BIG, (LPARAM)0);
-/// Set Neovim logo as Windows console icon
-static void SetWin32ConsoleIcon(void)
-{
const char *vimruntime = os_getenv("VIMRUNTIME");
if (vimruntime != NULL) {
snprintf(NameBuff, MAXPATHL, "%s" _PATHSEPSTR "neovim.ico", vimruntime);
@@ -251,7 +244,7 @@ static void SetWin32ConsoleIcon(void)
} else {
HICON hVimIcon = LoadImage(NULL, NameBuff, IMAGE_ICON, 64, 64,
LR_LOADFROMFILE | LR_LOADMAP3DCOLORS);
- SetConsoleIcon(hWnd, hVimIcon, hVimIcon);
+ os_icon_set(hWnd, hVimIcon, hVimIcon);
}
}
}
@@ -306,11 +299,6 @@ int main(int argc, char **argv)
early_init(¶ms);
-#ifdef MSWIN
- SaveWin32ConsoleIcon();
- SetWin32ConsoleIcon();
-#endif
-
set_argv_var(argv, argc); // set v:argv
// Check if we have an interactive window.
@@ -599,6 +587,12 @@ int main(int argc, char **argv)
TIME_MSG("UIEnter autocommands");
}
+#ifdef MSWIN
+ if (use_builtin_ui) {
+ os_icon_init();
+ }
+#endif
+
// Adjust default register name for "unnamed" in 'clipboard'. Can only be
// done after the clipboard is available and all initial commands that may
// modify the 'clipboard' setting have run; i.e. just before entering the
@@ -776,7 +770,8 @@ void getout(int exitval)
}
#ifdef MSWIN
- ResetWin32ConsoleIcon();
+ // Restore Windows console icon before exiting.
+ os_icon_set(hWnd, hOrigIconSmall, hOrigIcon);
#endif
os_exit(exitval);