Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor focusWindow implementation on Windows #179

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/win32/window_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,32 @@ bool focusWindow(const WindowHandle windowHandle) {
const auto allowSetForeground = AllowSetForegroundWindow(processId);
const auto setTopLevel = BringWindowToTop(hWnd);
const auto setForeground = SetForegroundWindow(hWnd);
const auto setActive = SetActiveWindow(hWnd);

// Try to set the window to the foreground
return allowSetForeground && setTopLevel && setForeground;
if (allowSetForeground && setTopLevel && setForeground && setActive)
{
return true;
}

// get the current thread id
DWORD dwCurrentThread = GetCurrentThreadId();
DWORD dwFGThread = GetWindowThreadProcessId(GetForegroundWindow(), NULL);

// attach the input to the current thread
AttachThreadInput(dwCurrentThread, dwFGThread, TRUE);

SetForegroundWindow(hWnd);
SetCapture(hWnd);
SetFocus(hWnd);
SetActiveWindow(hWnd);
EnableWindow(hWnd, TRUE);

// detach the input from the current thread
AttachThreadInput(dwCurrentThread, dwFGThread, FALSE);

// check if the window is now in the foreground
return GetForegroundWindow() == hWnd;
}
return false;
}
Expand Down