Skip to content

Commit aa4cc6f

Browse files
author
Simon Hofmann
committed
(#17) Refactored window manager macOS implementation
1 parent 2f1cf9b commit aa4cc6f

File tree

1 file changed

+27
-38
lines changed

1 file changed

+27
-38
lines changed

src/macos/window_manager.mm

+27-38
Original file line numberDiff line numberDiff line change
@@ -3,80 +3,69 @@
33
#import <ApplicationServices/ApplicationServices.h>
44
#include "../window_manager.h"
55

6-
std::vector<int64_t> getWindows() {
6+
NSDictionary* getWindowInfo(int64_t windowHandle) {
77
CGWindowListOption listOptions = kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements;
88
CFArrayRef windowList = CGWindowListCopyWindowInfo(listOptions, kCGNullWindowID);
99

10-
std::vector<int64_t> windowHandles;
11-
1210
for (NSDictionary *info in (NSArray *)windowList) {
13-
NSNumber *ownerPid = info[(id)kCGWindowOwnerPID];
1411
NSNumber *windowNumber = info[(id)kCGWindowNumber];
1512

16-
auto app = [NSRunningApplication runningApplicationWithProcessIdentifier: [ownerPid intValue]];
17-
auto path = app ? [app.bundleURL.path UTF8String] : "";
18-
19-
if (app && path != "") {
20-
windowHandles.push_back([windowNumber intValue]);
13+
if (windowHandle == [windowNumber intValue]) {
14+
CFRetain(info);
15+
CFRelease(windowList);
16+
return info;
2117
}
2218
}
2319

2420
if (windowList) {
2521
CFRelease(windowList);
2622
}
2723

28-
return windowHandles;
24+
return nullptr;
2925
}
3026

31-
MMRect getWindowRect(const int64_t windowHandle) {
27+
std::vector<WindowHandle> getWindows() {
3228
CGWindowListOption listOptions = kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements;
3329
CFArrayRef windowList = CGWindowListCopyWindowInfo(listOptions, kCGNullWindowID);
3430

35-
std::vector<std::string> windowNames;
31+
std::vector<WindowHandle> windowHandles;
3632

3733
for (NSDictionary *info in (NSArray *)windowList) {
3834
NSNumber *ownerPid = info[(id)kCGWindowOwnerPID];
3935
NSNumber *windowNumber = info[(id)kCGWindowNumber];
4036

4137
auto app = [NSRunningApplication runningApplicationWithProcessIdentifier: [ownerPid intValue]];
38+
auto path = app ? [app.bundleURL.path UTF8String] : "";
4239

43-
if (app && [windowNumber intValue] == windowHandle) {
44-
CGRect windowRect;
45-
if (CGRectMakeWithDictionaryRepresentation((CFDictionaryRef)info[(id)kCGWindowBounds], &windowRect)) {
46-
return MMRectMake(windowRect.origin.x, windowRect.origin.y, windowRect.size.height, windowRect.size.width);
47-
}
48-
return MMRectMake(0, 0, 0, 0);
40+
if (app && path != "") {
41+
windowHandles.push_back([windowNumber intValue]);
4942
}
5043
}
5144

5245
if (windowList) {
5346
CFRelease(windowList);
5447
}
5548

56-
return MMRectMake(0, 0, 0, 0);
49+
return windowHandles;
5750
}
5851

59-
std::string getWindowTitle(const int64_t windowHandle) {
60-
CGWindowListOption listOptions = kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements;
61-
CFArrayRef windowList = CGWindowListCopyWindowInfo(listOptions, kCGNullWindowID);
62-
63-
std::vector<std::string> windowNames;
64-
65-
for (NSDictionary *info in (NSArray *)windowList) {
66-
NSNumber *ownerPid = info[(id)kCGWindowOwnerPID];
67-
NSNumber *windowNumber = info[(id)kCGWindowNumber];
68-
69-
auto app = [NSRunningApplication runningApplicationWithProcessIdentifier: [ownerPid intValue]];
70-
71-
if (app && [windowNumber intValue] == windowHandle) {
72-
NSString *windowName = info[(id)kCGWindowName];
73-
return [windowName UTF8String];
52+
MMRect getWindowRect(const WindowHandle windowHandle) {
53+
auto windowInfo = getWindowInfo(windowHandle);
54+
if (windowInfo != nullptr) {
55+
CGRect windowRect;
56+
if (CGRectMakeWithDictionaryRepresentation((CFDictionaryRef)windowInfo[(id)kCGWindowBounds], &windowRect)) {
57+
return MMRectMake(windowRect.origin.x, windowRect.origin.y, windowRect.size.height, windowRect.size.width);
7458
}
59+
return MMRectMake(0, 0, 0, 0);
7560
}
61+
return MMRectMake(0, 0, 0, 0);
62+
}
7663

77-
if (windowList) {
78-
CFRelease(windowList);
64+
std::string getWindowTitle(const WindowHandle windowHandle) {
65+
auto windowInfo = getWindowInfo(windowHandle);
66+
if (windowInfo != nullptr) {
67+
NSString *windowName = windowInfo[(id)kCGWindowName];
68+
return [windowName UTF8String];
7969
}
80-
8170
return "";
82-
}
71+
}

0 commit comments

Comments
 (0)