Skip to content

Commit

Permalink
Add contentBounds property on Windows (#187)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavou authored Aug 9, 2024
1 parent 3a9ebc6 commit e9f34ee
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 9 deletions.
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

0 comments on commit e9f34ee

Please sign in to comment.