2019-09-05 00:26:26 +08:00
|
|
|
#include "pch.h"
|
2020-09-10 21:57:16 +08:00
|
|
|
#include "Generated files/resource.h"
|
2019-09-05 00:26:26 +08:00
|
|
|
#include "settings_window.h"
|
|
|
|
#include "tray_icon.h"
|
2021-05-20 20:07:34 +08:00
|
|
|
#include "CentralizedHotkeys.h"
|
2019-09-05 00:26:26 +08:00
|
|
|
#include <Windows.h>
|
|
|
|
|
2021-01-21 16:55:58 +08:00
|
|
|
#include <common/utils/process_path.h>
|
2020-12-15 20:16:09 +08:00
|
|
|
#include <common/utils/resources.h>
|
|
|
|
#include <common/version/version.h>
|
2021-05-20 20:07:34 +08:00
|
|
|
#include <common/logger/logger.h>
|
2019-09-05 00:26:26 +08:00
|
|
|
|
2019-12-27 00:26:11 +08:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
HWND tray_icon_hwnd = NULL;
|
2019-09-05 00:26:26 +08:00
|
|
|
|
2019-12-27 00:26:11 +08:00
|
|
|
// Message code that Windows will use for tray icon notifications.
|
|
|
|
UINT wm_icon_notify = 0;
|
2019-09-05 00:26:26 +08:00
|
|
|
|
2019-12-27 00:26:11 +08:00
|
|
|
// Contains the Windows Message for taskbar creation.
|
|
|
|
UINT wm_taskbar_restart = 0;
|
|
|
|
UINT wm_run_on_main_ui_thread = 0;
|
2019-09-05 00:26:26 +08:00
|
|
|
|
2019-12-27 00:26:11 +08:00
|
|
|
NOTIFYICONDATAW tray_icon_data;
|
|
|
|
bool tray_icon_created = false;
|
2019-09-05 00:26:26 +08:00
|
|
|
|
2019-12-27 00:26:11 +08:00
|
|
|
bool about_box_shown = false;
|
2019-11-27 22:19:10 +08:00
|
|
|
|
2019-12-27 00:26:11 +08:00
|
|
|
HMENU h_menu = nullptr;
|
|
|
|
HMENU h_sub_menu = nullptr;
|
2019-11-27 22:19:10 +08:00
|
|
|
}
|
2019-09-05 00:26:26 +08:00
|
|
|
|
|
|
|
// Struct to fill with callback and the data. The window_proc is responsible for cleaning it.
|
2019-12-27 00:26:11 +08:00
|
|
|
struct run_on_main_ui_thread_msg
|
|
|
|
{
|
|
|
|
main_loop_callback_function _callback;
|
|
|
|
PVOID data;
|
2019-09-05 00:26:26 +08:00
|
|
|
};
|
|
|
|
|
2019-12-27 00:26:11 +08:00
|
|
|
bool dispatch_run_on_main_ui_thread(main_loop_callback_function _callback, PVOID data)
|
|
|
|
{
|
|
|
|
if (tray_icon_hwnd == NULL)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
struct run_on_main_ui_thread_msg* wnd_msg = new struct run_on_main_ui_thread_msg();
|
|
|
|
wnd_msg->_callback = _callback;
|
|
|
|
wnd_msg->data = data;
|
2019-09-05 00:26:26 +08:00
|
|
|
|
2019-12-27 00:26:11 +08:00
|
|
|
PostMessage(tray_icon_hwnd, wm_run_on_main_ui_thread, 0, (LPARAM)wnd_msg);
|
2019-09-05 00:26:26 +08:00
|
|
|
|
2019-12-27 00:26:11 +08:00
|
|
|
return true;
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
|
|
|
|
2020-11-10 20:56:05 +08:00
|
|
|
void change_menu_item_text(const UINT item_id, wchar_t* new_text)
|
|
|
|
{
|
|
|
|
MENUITEMINFOW menuitem = { .cbSize = sizeof(MENUITEMINFOW), .fMask = MIIM_TYPE | MIIM_DATA };
|
|
|
|
GetMenuItemInfoW(h_menu, item_id, false, &menuitem);
|
|
|
|
menuitem.dwTypeData = new_text;
|
|
|
|
SetMenuItemInfoW(h_menu, item_id, false, &menuitem);
|
|
|
|
}
|
|
|
|
|
2021-01-21 16:55:58 +08:00
|
|
|
void handle_tray_command(HWND window, const WPARAM command_id)
|
|
|
|
{
|
|
|
|
switch (command_id)
|
|
|
|
{
|
|
|
|
case ID_SETTINGS_MENU_COMMAND:
|
|
|
|
open_settings_window();
|
|
|
|
break;
|
|
|
|
case ID_EXIT_MENU_COMMAND:
|
|
|
|
if (h_menu)
|
|
|
|
{
|
|
|
|
DestroyMenu(h_menu);
|
|
|
|
}
|
|
|
|
DestroyWindow(window);
|
|
|
|
break;
|
|
|
|
case ID_ABOUT_MENU_COMMAND:
|
|
|
|
if (!about_box_shown)
|
|
|
|
{
|
|
|
|
about_box_shown = true;
|
|
|
|
std::wstring about_msg = L"PowerToys\nVersion " + get_product_version() + L"\n\xa9 2019 Microsoft Corporation";
|
|
|
|
MessageBoxW(nullptr, about_msg.c_str(), L"About PowerToys", MB_OK);
|
|
|
|
about_box_shown = false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ID_REPORT_BUG_COMMAND:
|
|
|
|
std::wstring bug_report_path = get_module_folderpath();
|
|
|
|
bug_report_path += L"\\Tools\\BugReportTool.exe";
|
|
|
|
SHELLEXECUTEINFOW sei{ sizeof(sei) };
|
|
|
|
sei.fMask = { SEE_MASK_FLAG_NO_UI | SEE_MASK_NOASYNC | SEE_MASK_NOCLOSEPROCESS | SEE_MASK_NO_CONSOLE };
|
|
|
|
sei.lpFile = bug_report_path.c_str();
|
|
|
|
sei.nShow = SW_HIDE;
|
|
|
|
if (ShellExecuteExW(&sei))
|
|
|
|
{
|
|
|
|
WaitForSingleObject(sei.hProcess, INFINITE);
|
|
|
|
CloseHandle(sei.hProcess);
|
|
|
|
static const std::wstring bugreport_success = GET_RESOURCE_STRING(IDS_BUGREPORT_SUCCESS);
|
|
|
|
MessageBoxW(nullptr, bugreport_success.c_str(), L"PowerToys", MB_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-27 00:26:11 +08:00
|
|
|
LRESULT __stdcall tray_icon_window_proc(HWND window, UINT message, WPARAM wparam, LPARAM lparam)
|
|
|
|
{
|
|
|
|
switch (message)
|
|
|
|
{
|
2021-05-20 20:07:34 +08:00
|
|
|
case WM_HOTKEY:
|
|
|
|
{
|
|
|
|
// We use the tray icon WndProc to avoid creating a dedicated window just for this message.
|
|
|
|
const auto modifiersMask = LOWORD(lparam);
|
|
|
|
const auto vkCode = HIWORD(lparam);
|
|
|
|
Logger::trace(L"On {} hotkey", CentralizedHotkeys::ToWstring({ modifiersMask, vkCode }));
|
|
|
|
CentralizedHotkeys::PopulateHotkey({ modifiersMask, vkCode });
|
|
|
|
break;
|
|
|
|
}
|
2019-12-27 00:26:11 +08:00
|
|
|
case WM_CREATE:
|
|
|
|
if (wm_taskbar_restart == 0)
|
|
|
|
{
|
|
|
|
tray_icon_hwnd = window;
|
|
|
|
wm_taskbar_restart = RegisterWindowMessageW(L"TaskbarCreated");
|
|
|
|
wm_run_on_main_ui_thread = RegisterWindowMessage(L"RunOnMainThreadCallback");
|
|
|
|
}
|
2019-09-05 00:26:26 +08:00
|
|
|
break;
|
2019-12-27 00:26:11 +08:00
|
|
|
case WM_DESTROY:
|
|
|
|
if (tray_icon_created)
|
|
|
|
{
|
|
|
|
Shell_NotifyIcon(NIM_DELETE, &tray_icon_data);
|
|
|
|
tray_icon_created = false;
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
2020-05-06 05:13:52 +08:00
|
|
|
close_settings_window();
|
2019-12-27 00:26:11 +08:00
|
|
|
PostQuitMessage(0);
|
|
|
|
break;
|
|
|
|
case WM_CLOSE:
|
2019-09-05 00:26:26 +08:00
|
|
|
DestroyWindow(window);
|
|
|
|
break;
|
2019-12-27 00:26:11 +08:00
|
|
|
case WM_COMMAND:
|
2021-01-21 16:55:58 +08:00
|
|
|
handle_tray_command(window, wparam);
|
2019-12-27 00:26:11 +08:00
|
|
|
break;
|
|
|
|
// Shell_NotifyIcon can fail when we invoke it during the time explorer.exe isn't present/ready to handle it.
|
|
|
|
// We'll also never receive wm_taskbar_restart message if the first call to Shell_NotifyIcon failed, so we use
|
|
|
|
// WM_WINDOWPOSCHANGING which is always received on explorer startup sequence.
|
2020-03-05 18:07:06 +08:00
|
|
|
case WM_WINDOWPOSCHANGING:
|
|
|
|
{
|
2019-12-27 00:26:11 +08:00
|
|
|
if (!tray_icon_created)
|
|
|
|
{
|
|
|
|
tray_icon_created = Shell_NotifyIcon(NIM_ADD, &tray_icon_data) == TRUE;
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2019-12-27 00:26:11 +08:00
|
|
|
default:
|
|
|
|
if (message == wm_icon_notify)
|
2019-09-05 00:26:26 +08:00
|
|
|
{
|
2019-12-27 00:26:11 +08:00
|
|
|
switch (lparam)
|
|
|
|
{
|
2020-03-05 18:07:06 +08:00
|
|
|
case WM_LBUTTONUP:
|
|
|
|
{
|
2019-12-27 00:26:11 +08:00
|
|
|
open_settings_window();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case WM_RBUTTONUP:
|
2020-03-05 18:07:06 +08:00
|
|
|
case WM_CONTEXTMENU:
|
|
|
|
{
|
2019-12-27 00:26:11 +08:00
|
|
|
if (!h_menu)
|
|
|
|
{
|
|
|
|
h_menu = LoadMenu(reinterpret_cast<HINSTANCE>(&__ImageBase), MAKEINTRESOURCE(ID_TRAY_MENU));
|
|
|
|
}
|
2020-11-10 20:56:05 +08:00
|
|
|
if (h_menu)
|
|
|
|
{
|
|
|
|
static std::wstring settings_menuitem_label = GET_RESOURCE_STRING(IDS_SETTINGS_MENU_TEXT);
|
|
|
|
static std::wstring exit_menuitem_label = GET_RESOURCE_STRING(IDS_EXIT_MENU_TEXT);
|
2021-01-21 16:55:58 +08:00
|
|
|
static std::wstring submit_bug_menuitem_label = GET_RESOURCE_STRING(IDS_SUBMIT_BUG_TEXT);
|
|
|
|
|
2020-11-10 20:56:05 +08:00
|
|
|
change_menu_item_text(ID_SETTINGS_MENU_COMMAND, settings_menuitem_label.data());
|
|
|
|
change_menu_item_text(ID_EXIT_MENU_COMMAND, exit_menuitem_label.data());
|
2021-01-21 16:55:58 +08:00
|
|
|
change_menu_item_text(ID_REPORT_BUG_COMMAND, submit_bug_menuitem_label.data());
|
2020-11-10 20:56:05 +08:00
|
|
|
}
|
2019-12-27 00:26:11 +08:00
|
|
|
if (!h_sub_menu)
|
|
|
|
{
|
|
|
|
h_sub_menu = GetSubMenu(h_menu, 0);
|
|
|
|
}
|
|
|
|
POINT mouse_pointer;
|
|
|
|
GetCursorPos(&mouse_pointer);
|
|
|
|
SetForegroundWindow(window); // Needed for the context menu to disappear.
|
|
|
|
TrackPopupMenu(h_sub_menu, TPM_CENTERALIGN | TPM_BOTTOMALIGN, mouse_pointer.x, mouse_pointer.y, 0, window, nullptr);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
2019-12-27 00:26:11 +08:00
|
|
|
else if (message == wm_run_on_main_ui_thread)
|
2019-09-05 00:26:26 +08:00
|
|
|
{
|
2019-12-27 00:26:11 +08:00
|
|
|
if (lparam != NULL)
|
|
|
|
{
|
|
|
|
struct run_on_main_ui_thread_msg* msg = (struct run_on_main_ui_thread_msg*)lparam;
|
|
|
|
msg->_callback(msg->data);
|
|
|
|
delete msg;
|
|
|
|
lparam = NULL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (message == wm_taskbar_restart)
|
|
|
|
{
|
|
|
|
tray_icon_created = Shell_NotifyIcon(NIM_ADD, &tray_icon_data) == TRUE;
|
|
|
|
break;
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
|
|
|
}
|
2019-12-27 00:26:11 +08:00
|
|
|
return DefWindowProc(window, message, wparam, lparam);
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
|
|
|
|
2019-12-27 00:26:11 +08:00
|
|
|
void start_tray_icon()
|
|
|
|
{
|
|
|
|
auto h_instance = reinterpret_cast<HINSTANCE>(&__ImageBase);
|
|
|
|
auto icon = LoadIcon(h_instance, MAKEINTRESOURCE(APPICON));
|
|
|
|
if (icon)
|
|
|
|
{
|
|
|
|
UINT id_tray_icon = wm_icon_notify = RegisterWindowMessageW(L"WM_PowerToysIconNotify");
|
|
|
|
|
|
|
|
WNDCLASS wc = {};
|
|
|
|
wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
|
|
|
|
wc.hInstance = h_instance;
|
2020-04-21 15:30:12 +08:00
|
|
|
wc.lpszClassName = pt_tray_icon_window_class;
|
2019-12-27 00:26:11 +08:00
|
|
|
wc.style = CS_HREDRAW | CS_VREDRAW;
|
|
|
|
wc.lpfnWndProc = tray_icon_window_proc;
|
|
|
|
wc.hIcon = icon;
|
|
|
|
RegisterClass(&wc);
|
|
|
|
auto hwnd = CreateWindowW(wc.lpszClassName,
|
2020-04-21 15:30:12 +08:00
|
|
|
pt_tray_icon_window_class,
|
2019-12-27 00:26:11 +08:00
|
|
|
WS_OVERLAPPEDWINDOW | WS_POPUP,
|
|
|
|
CW_USEDEFAULT,
|
|
|
|
CW_USEDEFAULT,
|
|
|
|
CW_USEDEFAULT,
|
|
|
|
CW_USEDEFAULT,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
wc.hInstance,
|
|
|
|
nullptr);
|
|
|
|
WINRT_VERIFY(hwnd);
|
2021-05-20 20:07:34 +08:00
|
|
|
CentralizedHotkeys::RegisterWindow(hwnd);
|
2019-12-27 00:26:11 +08:00
|
|
|
memset(&tray_icon_data, 0, sizeof(tray_icon_data));
|
|
|
|
tray_icon_data.cbSize = sizeof(tray_icon_data);
|
|
|
|
tray_icon_data.hIcon = icon;
|
|
|
|
tray_icon_data.hWnd = hwnd;
|
|
|
|
tray_icon_data.uID = id_tray_icon;
|
|
|
|
tray_icon_data.uCallbackMessage = wm_icon_notify;
|
2020-04-17 16:47:24 +08:00
|
|
|
std::wstring about_msg_pt_version = L"PowerToys\n" + get_product_version();
|
2020-11-10 20:56:05 +08:00
|
|
|
wcscpy_s(tray_icon_data.szTip, sizeof(tray_icon_data.szTip) / sizeof(WCHAR), about_msg_pt_version.c_str());
|
2019-12-27 00:26:11 +08:00
|
|
|
tray_icon_data.uFlags = NIF_ICON | NIF_TIP | NIF_MESSAGE;
|
|
|
|
|
|
|
|
tray_icon_created = Shell_NotifyIcon(NIM_ADD, &tray_icon_data) == TRUE;
|
|
|
|
}
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
2019-12-17 01:36:52 +08:00
|
|
|
|
2019-12-27 00:26:11 +08:00
|
|
|
void stop_tray_icon()
|
|
|
|
{
|
|
|
|
if (tray_icon_created)
|
|
|
|
{
|
|
|
|
SendMessage(tray_icon_hwnd, WM_CLOSE, 0, 0);
|
|
|
|
}
|
2019-12-17 01:36:52 +08:00
|
|
|
}
|