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

Add support for client bounds without NonClient Area on Windows #187

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
32 changes: 25 additions & 7 deletions Sources/windows/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,13 @@ Napi::Value getWindowInformation(const HWND &hwnd, const Napi::CallbackInfo &inf
return env.Null();
}

RECT lpRect;
BOOL rectResult = GetWindowRect(hwnd, &lpRect);
RECT lpWinRect;
BOOL rectWinResult = GetWindowRect(hwnd, &lpWinRect);

if (rectResult == 0) {
RECT lpClientRect;
BOOL rectClientResult = GetClientRect(hwnd, &lpClientRect);

if (rectWinResult == 0 || rectClientResult == 0 ) {
return env.Null();
}

Expand All @@ -194,12 +197,26 @@ Napi::Value getWindowInformation(const HWND &hwnd, const Napi::CallbackInfo &inf
owner.Set(Napi::String::New(env, "path"), ownerInfo.path);
owner.Set(Napi::String::New(env, "name"), ownerInfo.name);

// bounds window
Napi::Object bounds = Napi::Object::New(env);

bounds.Set(Napi::String::New(env, "x"), lpRect.left);
bounds.Set(Napi::String::New(env, "y"), lpRect.top);
bounds.Set(Napi::String::New(env, "width"), lpRect.right - lpRect.left);
bounds.Set(Napi::String::New(env, "height"), lpRect.bottom - lpRect.top);
bounds.Set(Napi::String::New(env, "x"), lpWinRect.left);
bounds.Set(Napi::String::New(env, "y"), lpWinRect.top);
bounds.Set(Napi::String::New(env, "width"), lpWinRect.right - lpWinRect.left);
bounds.Set(Napi::String::New(env, "height"), lpWinRect.bottom - lpWinRect.top);

// bounds content
POINT rectTopLeft = {lpClientRect.left, lpClientRect.top};
ClientToScreen(hwnd, &rectTopLeft);
POINT rectBottomRight = {lpClientRect.right, lpClientRect.bottom};
ClientToScreen(hwnd, &rectBottomRight);

Napi::Object contentBounds = Napi::Object::New(env);

contentBounds.Set(Napi::String::New(env, "x"), rectTopLeft.x);
contentBounds.Set(Napi::String::New(env, "y"), rectTopLeft.y);
contentBounds.Set(Napi::String::New(env, "width"), rectBottomRight.x - rectTopLeft.x);
contentBounds.Set(Napi::String::New(env, "height"), rectBottomRight.y - rectTopLeft.y);

Napi::Object activeWinObj = Napi::Object::New(env);

Expand All @@ -208,6 +225,7 @@ Napi::Value getWindowInformation(const HWND &hwnd, const Napi::CallbackInfo &inf
activeWinObj.Set(Napi::String::New(env, "title"), getWindowTitle(hwnd));
activeWinObj.Set(Napi::String::New(env, "owner"), owner);
activeWinObj.Set(Napi::String::New(env, "bounds"), bounds);
activeWinObj.Set(Napi::String::New(env, "contentBounds"), contentBounds);
activeWinObj.Set(Napi::String::New(env, "memoryUsage"), memoryCounter.WorkingSetSize);

return activeWinObj;
Expand Down
10 changes: 10 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@ export type LinuxResult = {

export type WindowsResult = {
platform: 'windows';

/**
Window content position and size, which excludes the title bar, menu bar, and frame.
*/
contentBounds: {
x: number;
y: number;
width: number;
height: number;
};
} & BaseResult;

export type Result = MacOSResult | LinuxResult | WindowsResult;
Expand Down
6 changes: 4 additions & 2 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ if (result) {
expectType<string | undefined>(result.url);
} else if (result.platform === 'linux') {
expectType<LinuxResult>(result);
expectError(result.owner.bundleId);
} else {
expectType<WindowsResult>(result);
expectError(result.owner.bundleId);
expectType<number>(result.contentBounds.x);
expectType<number>(result.contentBounds.y);
expectType<number>(result.contentBounds.width);
expectType<number>(result.contentBounds.height);
}
}
5 changes: 5 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ Returns a `Promise<object>` with the result, or `Promise<undefined>` if there is
- `y` *(number)*
- `width` *(number)*
- `height` *(number)*
- `contentBounds` *(Object)* - Window content position and size, which excludes the title bar, menu bar, and frame *(Windows only)*
- `x` *(number)*
- `y` *(number)*
- `width` *(number)*
- `height` *(number)*
- `owner` *(Object)* - App that owns the window
- `name` *(string)* - Name of the app
- `processId` *(number)* - Process identifier
Expand Down