Skip to content

Commit 8efc661

Browse files
lidaixingchenstd-microblock
authored andcommitted
refactor: address Copilot review feedback
1. Use MAKELPARAM instead of static_cast for WM_INITMENUPOPUP lParam to explicitly match the Windows message contract (LOWORD=position, HIWORD=system menu flag). 2. Add init_popup_lparam parameter to construct_with_hmenu to avoid sending WM_INITMENUPOPUP twice with conflicting lParam values. Previously, the submenu lambda sent WM_INITMENUPOPUP with the correct position index, but then construct_with_hmenu would immediately resend it with 0xFFFFFFFF, overriding the correct value. Now the position is passed through the parameter and sent only once inside construct_with_hmenu.
1 parent 43f8da3 commit 8efc661

File tree

2 files changed

+7
-10
lines changed

2 files changed

+7
-10
lines changed

src/shell/contextmenu/contextmenu.cc

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,8 @@ std::vector<std::string> extract_hotkeys(const std::string &name) {
135135

136136
menu menu::construct_with_hmenu(
137137
HMENU hMenu, HWND hWnd, bool is_top,
138-
std::function<void(int, WPARAM, LPARAM)> HandleMenuMsg) {
138+
std::function<void(int, WPARAM, LPARAM)> HandleMenuMsg,
139+
LPARAM init_popup_lparam) {
139140
menu m;
140141

141142
if (!HandleMenuMsg)
@@ -144,7 +145,7 @@ menu menu::construct_with_hmenu(
144145
};
145146

146147
HandleMenuMsg(WM_INITMENUPOPUP, reinterpret_cast<WPARAM>(hMenu),
147-
0xFFFFFFFF);
148+
init_popup_lparam);
148149
for (int i = 0; i < GetMenuItemCount(hMenu); i++) {
149150
menu_item item;
150151
wchar_t buffer[256];
@@ -180,11 +181,9 @@ menu menu::construct_with_hmenu(
180181
int submenu_pos = i;
181182
item.submenu = [=](std::shared_ptr<menu_widget> mw) {
182183
auto task = [&]() {
183-
HandleMenuMsg(WM_INITMENUPOPUP,
184-
reinterpret_cast<WPARAM>(info.hSubMenu),
185-
static_cast<LPARAM>(submenu_pos));
186184
mw->init_from_data(menu::construct_with_hmenu(
187-
info.hSubMenu, hWnd, false, HandleMenuMsg));
185+
info.hSubMenu, hWnd, false, HandleMenuMsg,
186+
MAKELPARAM(static_cast<WORD>(submenu_pos), 0)));
188187
};
189188
if (main_thread_id == GetCurrentThreadId())
190189
task();

src/shell/contextmenu/contextmenu.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,9 @@ struct menu {
2020

2121
static menu
2222
construct_with_hmenu(HMENU hMenu, HWND hWnd, bool is_top = true,
23-
// This is for handling submenus; messages are required
24-
// to be forwarded to IContextMenu2::HandleMenuMsg for
25-
// submenus and owner-draw menus to work properly
2623
std::function<void(int, WPARAM, LPARAM)>
27-
HandleMenuMsg = {});
24+
HandleMenuMsg = {},
25+
LPARAM init_popup_lparam = 0xFFFFFFFF);
2826
};
2927

3028
std::optional<int>

0 commit comments

Comments
 (0)