Skip to content

(NT-77) Improve focus window implementation on Windows to deal with f… #178

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

Merged
merged 2 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
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
42 changes: 24 additions & 18 deletions src/win32/window_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ std::vector<WindowHandle> getWindows() {
}

WindowHandle getActiveWindow() {
HWND foregroundWindow = GetForegroundWindow();
auto foregroundWindow = GetForegroundWindow();
if (IsWindow(foregroundWindow)) {
return reinterpret_cast<WindowHandle>(foregroundWindow);
}
return -1;
}

MMRect getWindowRect(const WindowHandle windowHandle) {
HWND hWnd = reinterpret_cast<HWND>(windowHandle);
auto hWnd = reinterpret_cast<HWND>(windowHandle);
RECT windowRect;
if (IsWindow(hWnd) && GetWindowRect(hWnd, &windowRect)) {
return MMRectMake(windowRect.left, windowRect.top, windowRect.right - windowRect.left, windowRect.bottom - windowRect.top);
Expand All @@ -39,65 +39,71 @@ MMRect getWindowRect(const WindowHandle windowHandle) {
}

std::string getWindowTitle(const WindowHandle windowHandle) {
HWND hWnd = reinterpret_cast<HWND>(windowHandle);
auto hWnd = reinterpret_cast<HWND>(windowHandle);
if (IsWindow(hWnd)) {
auto BUFFER_SIZE = GetWindowTextLength(hWnd) + 1;
if (BUFFER_SIZE) {
LPSTR windowTitle = new CHAR[BUFFER_SIZE];
auto windowTitle = new CHAR[BUFFER_SIZE];
if (GetWindowText(hWnd, windowTitle, BUFFER_SIZE)) {
return std::string(windowTitle);
return {windowTitle};
}
}
}
return "";
}

bool focusWindow(const WindowHandle windowHandle) {
HWND hWnd = reinterpret_cast<HWND>(windowHandle);
auto hWnd = reinterpret_cast<HWND>(windowHandle);
if (IsWindow(hWnd)) {
// Restore the window if it's minimized
if (IsIconic(hWnd)) {
ShowWindow(hWnd, SW_RESTORE);
}


auto processId = GetCurrentProcessId();
// const auto allowSetForeground = AllowSetForegroundWindow(ASFW_ANY);
const auto allowSetForeground = AllowSetForegroundWindow(processId);
const auto setTopLevel = BringWindowToTop(hWnd);
const auto setForeground = SetForegroundWindow(hWnd);

// Try to set the window to the foreground
return SetForegroundWindow(hWnd);
return allowSetForeground && setTopLevel && setForeground;
}
return false;
}

bool resizeWindow(const WindowHandle windowHandle, const MMSize newSize) {
HWND hWnd = reinterpret_cast<HWND>(windowHandle);
auto hWnd = reinterpret_cast<HWND>(windowHandle);
if (IsWindow(hWnd)) {
//size
auto width = newSize.width;
auto height = newSize.height;
const auto width = newSize.width;
const auto height = newSize.height;

RECT currentPosition;
GetWindowRect(reinterpret_cast<HWND>(windowHandle), &currentPosition);

//origin
auto x = currentPosition.left;
auto y = currentPosition.top;
const auto x = currentPosition.left;
const auto y = currentPosition.top;

return MoveWindow(hWnd, x, y, width, height, TRUE);
}
return false;
}

bool moveWindow(const WindowHandle windowHandle, const MMPoint newOrigin) {
HWND hWnd = reinterpret_cast<HWND>(windowHandle);
auto hWnd = reinterpret_cast<HWND>(windowHandle);
if (IsWindow(hWnd)) {
// origin
auto x = newOrigin.x;
auto y = newOrigin.y;
const auto x = newOrigin.x;
const auto y = newOrigin.y;

RECT currentPosition;
GetWindowRect(reinterpret_cast<HWND>(windowHandle), &currentPosition);

// size
auto width = currentPosition.right - currentPosition.left;
auto height = currentPosition.bottom - currentPosition.top;
const auto width = currentPosition.right - currentPosition.left;
const auto height = currentPosition.bottom - currentPosition.top;

return MoveWindow(hWnd, x, y, width, height, TRUE);
}
Expand Down
8 changes: 8 additions & 0 deletions test/window-integration-tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,18 @@ describe("focusWindow", () => {
// WHEN
const secondApp = await electron.launch({args: ['second.js']});
const secondPage = await secondApp.firstWindow({timeout: APP_TIMEOUT});
const handle = await secondApp.browserWindow(secondPage);
await secondPage.waitForLoadState("domcontentloaded");
await handle.evaluate((win) => {
win.minimize();
win.restore();
win.focus();
});

const result = libnut.focusWindow(openWindowHandle);

// THEN
await sleep(1500);
const activeWindowHandle = libnut.getActiveWindow();
const activeWindowName = libnut.getWindowTitle(activeWindowHandle);
expect(activeWindowName).toBe(TITLE);
Expand Down
Loading