Skip to content

Commit e9f34ee

Browse files
authored
Add contentBounds property on Windows (#187)
1 parent 3a9ebc6 commit e9f34ee

File tree

4 files changed

+44
-9
lines changed

4 files changed

+44
-9
lines changed

Sources/windows/main.cc

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,13 @@ Napi::Value getWindowInformation(const HWND &hwnd, const Napi::CallbackInfo &inf
181181
return env.Null();
182182
}
183183

184-
RECT lpRect;
185-
BOOL rectResult = GetWindowRect(hwnd, &lpRect);
184+
RECT lpWinRect;
185+
BOOL rectWinResult = GetWindowRect(hwnd, &lpWinRect);
186186

187-
if (rectResult == 0) {
187+
RECT lpClientRect;
188+
BOOL rectClientResult = GetClientRect(hwnd, &lpClientRect);
189+
190+
if (rectWinResult == 0 || rectClientResult == 0 ) {
188191
return env.Null();
189192
}
190193

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

200+
// bounds window
197201
Napi::Object bounds = Napi::Object::New(env);
198202

199-
bounds.Set(Napi::String::New(env, "x"), lpRect.left);
200-
bounds.Set(Napi::String::New(env, "y"), lpRect.top);
201-
bounds.Set(Napi::String::New(env, "width"), lpRect.right - lpRect.left);
202-
bounds.Set(Napi::String::New(env, "height"), lpRect.bottom - lpRect.top);
203+
bounds.Set(Napi::String::New(env, "x"), lpWinRect.left);
204+
bounds.Set(Napi::String::New(env, "y"), lpWinRect.top);
205+
bounds.Set(Napi::String::New(env, "width"), lpWinRect.right - lpWinRect.left);
206+
bounds.Set(Napi::String::New(env, "height"), lpWinRect.bottom - lpWinRect.top);
207+
208+
// bounds content
209+
POINT rectTopLeft = {lpClientRect.left, lpClientRect.top};
210+
ClientToScreen(hwnd, &rectTopLeft);
211+
POINT rectBottomRight = {lpClientRect.right, lpClientRect.bottom};
212+
ClientToScreen(hwnd, &rectBottomRight);
213+
214+
Napi::Object contentBounds = Napi::Object::New(env);
215+
216+
contentBounds.Set(Napi::String::New(env, "x"), rectTopLeft.x);
217+
contentBounds.Set(Napi::String::New(env, "y"), rectTopLeft.y);
218+
contentBounds.Set(Napi::String::New(env, "width"), rectBottomRight.x - rectTopLeft.x);
219+
contentBounds.Set(Napi::String::New(env, "height"), rectBottomRight.y - rectTopLeft.y);
203220

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

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

213231
return activeWinObj;

index.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,16 @@ export type LinuxResult = {
9595

9696
export type WindowsResult = {
9797
platform: 'windows';
98+
99+
/**
100+
Window content position and size, which excludes the title bar, menu bar, and frame.
101+
*/
102+
contentBounds: {
103+
x: number;
104+
y: number;
105+
width: number;
106+
height: number;
107+
};
98108
} & BaseResult;
99109

100110
export type Result = MacOSResult | LinuxResult | WindowsResult;

index.test-d.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,11 @@ if (result) {
3939
expectType<string | undefined>(result.url);
4040
} else if (result.platform === 'linux') {
4141
expectType<LinuxResult>(result);
42-
expectError(result.owner.bundleId);
4342
} else {
4443
expectType<WindowsResult>(result);
45-
expectError(result.owner.bundleId);
44+
expectType<number>(result.contentBounds.x);
45+
expectType<number>(result.contentBounds.y);
46+
expectType<number>(result.contentBounds.width);
47+
expectType<number>(result.contentBounds.height);
4648
}
4749
}

readme.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ Returns a `Promise<object>` with the result, or `Promise<undefined>` if there is
8080
- `y` *(number)*
8181
- `width` *(number)*
8282
- `height` *(number)*
83+
- `contentBounds` *(Object)* - Window content position and size, which excludes the title bar, menu bar, and frame *(Windows only)*
84+
- `x` *(number)*
85+
- `y` *(number)*
86+
- `width` *(number)*
87+
- `height` *(number)*
8388
- `owner` *(Object)* - App that owns the window
8489
- `name` *(string)* - Name of the app
8590
- `processId` *(number)* - Process identifier

0 commit comments

Comments
 (0)