Skip to content

Commit c068dac

Browse files
author
Simon Hofmann
committed
(#15) getActiveWindow on macOS, fixed wrong order of width and height in getWindowRect
1 parent 760e05b commit c068dac

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

Diff for: src/macos/window_manager.mm

+25-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,30 @@
2424
return nullptr;
2525
}
2626

27+
WindowHandle getActiveWindow() {
28+
CGWindowListOption listOptions = kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements;
29+
CFArrayRef windowList = CGWindowListCopyWindowInfo(listOptions, kCGNullWindowID);
30+
31+
for (NSDictionary *info in (NSArray *)windowList) {
32+
NSNumber *ownerPid = info[(id)kCGWindowOwnerPID];
33+
NSNumber *windowNumber = info[(id)kCGWindowNumber];
34+
35+
auto app = [NSRunningApplication runningApplicationWithProcessIdentifier: [ownerPid intValue]];
36+
37+
if (![app isActive]) {
38+
continue;
39+
}
40+
41+
CFRelease(windowList);
42+
return [windowNumber intValue];
43+
}
44+
45+
if (windowList) {
46+
CFRelease(windowList);
47+
}
48+
return -1;
49+
}
50+
2751
std::vector<WindowHandle> getWindows() {
2852
CGWindowListOption listOptions = kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements;
2953
CFArrayRef windowList = CGWindowListCopyWindowInfo(listOptions, kCGNullWindowID);
@@ -54,7 +78,7 @@ MMRect getWindowRect(const WindowHandle windowHandle) {
5478
if (windowInfo != nullptr) {
5579
CGRect windowRect;
5680
if (CGRectMakeWithDictionaryRepresentation((CFDictionaryRef)windowInfo[(id)kCGWindowBounds], &windowRect)) {
57-
return MMRectMake(windowRect.origin.x, windowRect.origin.y, windowRect.size.height, windowRect.size.width);
81+
return MMRectMake(windowRect.origin.x, windowRect.origin.y, windowRect.size.width, windowRect.size.height);
5882
}
5983
return MMRectMake(0, 0, 0, 0);
6084
}

0 commit comments

Comments
 (0)