From 0db78d6877762815ac1e15b0cf6c4d5d2eeb0851 Mon Sep 17 00:00:00 2001 From: Christopher Wang Date: Thu, 20 Feb 2020 00:53:49 -0500 Subject: [PATCH 1/4] Add support for drawinTitleBar=true --- dll/dllmain.cpp | 79 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 52 insertions(+), 27 deletions(-) diff --git a/dll/dllmain.cpp b/dll/dllmain.cpp index 0baccad..ef25f61 100644 --- a/dll/dllmain.cpp +++ b/dll/dllmain.cpp @@ -38,11 +38,9 @@ #define dprintf(...) #endif -/* Our GetMessage hook */ -HHOOK hMessageHook; - static HWND mainHwnd = nullptr; static NOTIFYICONDATA nid = { 0 }; +static HWND trayHwnd = nullptr; enum CommandID { @@ -50,12 +48,34 @@ enum CommandID ID_CLOSE }; +static void ShowTray() +{ + // Show tray icon + nid.cbSize = sizeof(nid); + nid.hWnd = trayHwnd; + nid.uID = 1337; + nid.uFlags = NIF_ICON | NIF_TIP | NIF_SHOWTIP | NIF_MESSAGE; + nid.uCallbackMessage = WM_USER + 1337; + nid.hIcon = (HICON)GetClassLongPtr(mainHwnd, GCLP_HICON); + lstrcpy(nid.szTip, _T("Mozilla Thunderbird")); + nid.dwState = 0; + nid.dwStateMask = 0; + lstrcpy(nid.szInfo, _T("")); + nid.uVersion = NOTIFYICON_VERSION_4; + lstrcpy(nid.szInfoTitle, _T("")); + nid.dwInfoFlags = 0; + nid.guidItem = {}; + nid.hBalloonIcon = nid.hIcon; + Shell_NotifyIcon(NIM_ADD, &nid); + Shell_NotifyIcon(NIM_SETVERSION, &nid); +} + static LRESULT CALLBACK TrayIconProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { if((uMsg == WM_USER + 1337 && LOWORD(lParam) == NIN_SELECT) || uMsg == WM_COMMAND) { // Restore main window - ShowWindow(mainHwnd, SW_SHOW); + ShowWindow(mainHwnd, SW_RESTORE); Shell_NotifyIcon(NIM_DELETE, &nid); if (uMsg == WM_COMMAND && wParam == ID_CLOSE) SendMessage(mainHwnd, WM_SYSCOMMAND, SC_CLOSE, 0); @@ -78,7 +98,6 @@ static LRESULT CALLBACK TrayIconProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM LRESULT CALLBACK MessageHook(int nCode, WPARAM wParam, LPARAM lParam) { - static HWND trayHwnd = nullptr; MSG &msg = *(MSG *)lParam; if(mainHwnd == nullptr) @@ -125,37 +144,42 @@ LRESULT CALLBACK MessageHook(int nCode, WPARAM wParam, LPARAM lParam) if(msg.hwnd == mainHwnd && ( - (msg.message == WM_NCLBUTTONDOWN && (msg.wParam == HTCLOSE || msg.wParam == HTMINBUTTON)) + (msg.message == WM_NCLBUTTONDOWN && msg.wParam == HTCLOSE) || - (msg.message == WM_SYSCOMMAND && (msg.wParam == SC_CLOSE || msg.wParam == SC_MINIMIZE)) + (msg.message == WM_SYSCOMMAND && msg.wParam == SC_CLOSE) )) { ShowWindow(msg.hwnd, SW_HIDE); - // Show tray icon - nid.cbSize = sizeof(nid); - nid.hWnd = trayHwnd; - nid.uID = 1337; - nid.uFlags = NIF_ICON | NIF_TIP | NIF_SHOWTIP | NIF_MESSAGE; - nid.uCallbackMessage = WM_USER + 1337; - nid.hIcon = (HICON)GetClassLongPtr(mainHwnd, GCLP_HICON); - lstrcpy(nid.szTip, _T("Mozilla Thunderbird")); - nid.dwState = 0; - nid.dwStateMask = 0; - lstrcpy(nid.szInfo, _T("")); - nid.uVersion = NOTIFYICON_VERSION_4; - lstrcpy(nid.szInfoTitle, _T("")); - nid.dwInfoFlags = 0; - nid.guidItem = {}; - nid.hBalloonIcon = nid.hIcon; - Shell_NotifyIcon(NIM_ADD, &nid); - Shell_NotifyIcon(NIM_SETVERSION, &nid); - // Ignore this message + ShowTray(); + msg.message = WM_NULL; } return CallNextHookEx(NULL, nCode, wParam, lParam); } +LRESULT CALLBACK WindowHook(int nCode, WPARAM wParam, LPARAM lParam) +{ + CWPSTRUCT &msg = *(CWPSTRUCT *)lParam; + + if (msg.hwnd == mainHwnd) + { + if (msg.message == WM_WINDOWPOSCHANGED) + { + // we won't receive WM_SIZE messages unless we DefWindowProc the WM_WINDOWPOSCHANGED message + DefWindowProc(msg.hwnd, msg.message, msg.wParam, msg.lParam); + } + + if (msg.message == WM_SIZE && msg.wParam == SIZE_MINIMIZED) + { + ShowWindow(msg.hwnd, SW_HIDE); + ShowTray(); + } + } + + return CallNextHookEx(NULL, nCode, wParam, lParam); +} + HMODULE hDLL; /* @@ -182,7 +206,8 @@ extern "C" __declspec(dllexport) LRESULT CALLBACK EntryHook(int nCode, WPARAM wP GetModuleFileName(hDLL, dllName, _countof(dllName)); LoadLibrary(dllName); #endif - hMessageHook = SetWindowsHookEx(WH_GETMESSAGE, MessageHook, NULL, GetCurrentThreadId()); + SetWindowsHookEx(WH_GETMESSAGE, MessageHook, NULL, GetCurrentThreadId()); + SetWindowsHookEx(WH_CALLWNDPROC, WindowHook, NULL, GetCurrentThreadId()); firstTime = false; } From ca85065d109054fdc3698375cefc700d80f80d04 Mon Sep 17 00:00:00 2001 From: Christopher Wang Date: Thu, 20 Feb 2020 01:13:51 -0500 Subject: [PATCH 2/4] Remove tray icon on close --- dll/dllmain.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/dll/dllmain.cpp b/dll/dllmain.cpp index ef25f61..a78f896 100644 --- a/dll/dllmain.cpp +++ b/dll/dllmain.cpp @@ -175,6 +175,10 @@ LRESULT CALLBACK WindowHook(int nCode, WPARAM wParam, LPARAM lParam) ShowWindow(msg.hwnd, SW_HIDE); ShowTray(); } + + if (msg.message == WM_DESTROY) { + Shell_NotifyIcon(NIM_DELETE, &nid); + } } return CallNextHookEx(NULL, nCode, wParam, lParam); From 8414bae682ae1511fe656f8e7521f9bfa44213bc Mon Sep 17 00:00:00 2001 From: Christopher Wang Date: Thu, 20 Feb 2020 17:56:28 -0500 Subject: [PATCH 3/4] Update readme about drawInTitlebar --- README.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 3318566..ac65299 100644 --- a/README.md +++ b/README.md @@ -48,14 +48,13 @@ it finds the window, injects a library into the Thunderbird process to hook into the message queue. TBTray keeps running in the background, in case you want to restart Thunderbird at some point. -It doesn't work for me! +The close button just closes Thunderbird! ----------------------- The most likely cause is that you have `mail.tabs.drawInTitlebar` set to `true` -(which is the default value) - setting it to `false` should solve that problem. +(which is the default value). -Note: If you want to get this fixed, consider submitting a pull request - I do -not have the time required to debug and fix a feature I am not using. +TBTray intercepts the close button by intercepting clicks in the non-client area (the title bar). This doesn't work when `drawInTitlebar` is `true`. A workaround for this is to install the [Minimize on Close](https://addons.thunderbird.net/en-us/thunderbird/addon/minimize-on-close/) extension, which uses client-side javascript to intercept the close event and minimize instead. Halp, how do I quit Thunderbird? -------------------------------- From 1490ba47a280d4d3bf8b3cd21c0c2406230bb6f1 Mon Sep 17 00:00:00 2001 From: Christopher Wang Date: Thu, 20 Feb 2020 18:25:11 -0500 Subject: [PATCH 4/4] Update readme about issue with Minimize on Close extension --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index ac65299..0136064 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,8 @@ Halp, how do I quit Thunderbird? Through the File menu, or the context menu of the tray icon. +Note that the context menu exit option will not work if you have the Minimize on Close extension installed, in which case you will have to exit through the file menu. + Is there any sort of configuration? -----------------------------------