Skip to content

Commit fedf45c

Browse files
committed
Backends: Win32 + Viewports: tweak ImGui_ImplWin32_WndProcHandler_PlatformWindow() to be easier to rework in a parallal friendly way. (#8069)
1 parent 646df39 commit fedf45c

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

backends/imgui_impl_win32.cpp

+17-3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
// CHANGELOG
2424
// (minor and older changes stripped away, please see git history for details)
2525
// 2024-XX-XX: Platform: Added support for multiple windows via the ImGuiPlatformIO interface.
26+
// 2024-10-28: [Docking] Rely on property stored inside HWND to retrieve context/viewport, should facilitate attempt to use this for parallel contexts. (#8069)
2627
// 2024-09-16: [Docking] Inputs: fixed an issue where a viewport destroyed while clicking would hog mouse tracking and temporary lead to incorrect update of HoveredWindow. (#7971)
2728
// 2024-07-08: Inputs: Fixed ImGuiMod_Super being mapped to VK_APPS instead of VK_LWIN||VK_RWIN. (#7768)
2829
// 2023-10-05: Inputs: Added support for extra ImGuiKey values: F13 to F24 function keys, app back/forward keys.
@@ -187,6 +188,8 @@ static bool ImGui_ImplWin32_InitEx(void* hwnd, bool platform_has_own_dc)
187188
// Our mouse update function expect PlatformHandle to be filled for the main viewport
188189
ImGuiViewport* main_viewport = ImGui::GetMainViewport();
189190
main_viewport->PlatformHandle = main_viewport->PlatformHandleRaw = (void*)bd->hWnd;
191+
::SetPropA(bd->hWnd, "IMGUI_CONTEXT", ImGui::GetCurrentContext());
192+
::SetPropA(bd->hWnd, "IMGUI_VIEWPORT", main_viewport);
190193
ImGui_ImplWin32_InitMultiViewportSupport(platform_has_own_dc);
191194

192195
// Dynamically load XInput library
@@ -230,6 +233,8 @@ void ImGui_ImplWin32_Shutdown()
230233
IM_ASSERT(bd != nullptr && "No platform backend to shutdown, or already shutdown?");
231234
ImGuiIO& io = ImGui::GetIO();
232235

236+
::SetPropA(bd->hWnd, "IMGUI_CONTEXT", nullptr);
237+
::SetPropA(bd->hWnd, "IMGUI_VIEWPORT", nullptr);
233238
ImGui_ImplWin32_ShutdownMultiViewportSupport();
234239

235240
// Unload XInput library
@@ -314,6 +319,14 @@ static void ImGui_ImplWin32_UpdateKeyModifiers()
314319
io.AddKeyEvent(ImGuiMod_Super, IsVkDown(VK_LWIN) || IsVkDown(VK_RWIN));
315320
}
316321

322+
static ImGuiViewport* ImGui_ImplWin32_FindViewportByPlatformHandle(HWND hwnd)
323+
{
324+
// We could use ImGui::FindViewportByPlatformHandle() but GetPropA() gets us closer to parallel multi-contexts,
325+
// until we can pass an explicit context to FindViewportByPlatformHandle().
326+
//return ImGui::FindViewportByPlatformHandle((void*)hwnd);
327+
return (ImGuiViewport*)::GetPropA(hwnd, "IMGUI_VIEWPORT");
328+
}
329+
317330
// This code supports multi-viewports (multiple OS Windows mapped into different Dear ImGui viewports)
318331
// Because of that, it is a little more complicated than your typical single-viewport binding code!
319332
static void ImGui_ImplWin32_UpdateMouseData()
@@ -326,7 +339,7 @@ static void ImGui_ImplWin32_UpdateMouseData()
326339
bool has_mouse_screen_pos = ::GetCursorPos(&mouse_screen_pos) != 0;
327340

328341
HWND focused_window = ::GetForegroundWindow();
329-
const bool is_app_focused = (focused_window && (focused_window == bd->hWnd || ::IsChild(focused_window, bd->hWnd) || ImGui::FindViewportByPlatformHandle((void*)focused_window)));
342+
const bool is_app_focused = (focused_window && (focused_window == bd->hWnd || ::IsChild(focused_window, bd->hWnd) || ImGui_ImplWin32_FindViewportByPlatformHandle(focused_window)));
330343
if (is_app_focused)
331344
{
332345
// (Optional) Set OS mouse position from Dear ImGui if requested (rarely used, only when io.ConfigNavMoveSetMousePos is enabled by user)
@@ -364,7 +377,7 @@ static void ImGui_ImplWin32_UpdateMouseData()
364377
ImGuiID mouse_viewport_id = 0;
365378
if (has_mouse_screen_pos)
366379
if (HWND hovered_hwnd = ::WindowFromPoint(mouse_screen_pos))
367-
if (ImGuiViewport* viewport = ImGui::FindViewportByPlatformHandle((void*)hovered_hwnd))
380+
if (ImGuiViewport* viewport = ImGui_ImplWin32_FindViewportByPlatformHandle(hovered_hwnd))
368381
mouse_viewport_id = viewport->ID;
369382
io.AddMouseViewportEvent(mouse_viewport_id);
370383
}
@@ -1072,6 +1085,7 @@ static void ImGui_ImplWin32_CreateWindow(ImGuiViewport* viewport)
10721085

10731086
// Secondary viewports store their imgui context
10741087
::SetPropA(vd->Hwnd, "IMGUI_CONTEXT", ImGui::GetCurrentContext());
1088+
::SetPropA(vd->Hwnd, "IMGUI_VIEWPORT", viewport);
10751089
}
10761090

10771091
static void ImGui_ImplWin32_DestroyWindow(ImGuiViewport* viewport)
@@ -1294,7 +1308,7 @@ static LRESULT CALLBACK ImGui_ImplWin32_WndProcHandler_PlatformWindow(HWND hWnd,
12941308
LRESULT result = 0;
12951309
if (ImGui_ImplWin32_WndProcHandler(hWnd, msg, wParam, lParam))
12961310
result = true;
1297-
else if (ImGuiViewport* viewport = ImGui::FindViewportByPlatformHandle((void*)hWnd))
1311+
else if (ImGuiViewport* viewport = ImGui_ImplWin32_FindViewportByPlatformHandle(hWnd))
12981312
{
12991313
switch (msg)
13001314
{

0 commit comments

Comments
 (0)