3
3
#import < ApplicationServices/ApplicationServices.h>
4
4
#include " ../window_manager.h"
5
5
6
- std::vector< int64_t > getWindows ( ) {
6
+ NSDictionary * getWindowInfo ( int64_t windowHandle ) {
7
7
CGWindowListOption listOptions = kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements ;
8
8
CFArrayRef windowList = CGWindowListCopyWindowInfo (listOptions, kCGNullWindowID );
9
9
10
- std::vector<int64_t > windowHandles;
11
-
12
10
for (NSDictionary *info in (NSArray *)windowList) {
13
- NSNumber *ownerPid = info[(id )kCGWindowOwnerPID ];
14
11
NSNumber *windowNumber = info[(id )kCGWindowNumber ];
15
12
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;
21
17
}
22
18
}
23
19
24
20
if (windowList) {
25
21
CFRelease (windowList);
26
22
}
27
23
28
- return windowHandles ;
24
+ return nullptr ;
29
25
}
30
26
31
- MMRect getWindowRect ( const int64_t windowHandle ) {
27
+ std::vector<WindowHandle> getWindows ( ) {
32
28
CGWindowListOption listOptions = kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements ;
33
29
CFArrayRef windowList = CGWindowListCopyWindowInfo (listOptions, kCGNullWindowID );
34
30
35
- std::vector<std::string> windowNames ;
31
+ std::vector<WindowHandle> windowHandles ;
36
32
37
33
for (NSDictionary *info in (NSArray *)windowList) {
38
34
NSNumber *ownerPid = info[(id )kCGWindowOwnerPID ];
39
35
NSNumber *windowNumber = info[(id )kCGWindowNumber ];
40
36
41
37
auto app = [NSRunningApplication runningApplicationWithProcessIdentifier: [ownerPid intValue ]];
38
+ auto path = app ? [app.bundleURL.path UTF8String ] : " " ;
42
39
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 ]);
49
42
}
50
43
}
51
44
52
45
if (windowList) {
53
46
CFRelease (windowList);
54
47
}
55
48
56
- return MMRectMake ( 0 , 0 , 0 , 0 ) ;
49
+ return windowHandles ;
57
50
}
58
51
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 );
74
58
}
59
+ return MMRectMake (0 , 0 , 0 , 0 );
75
60
}
61
+ return MMRectMake (0 , 0 , 0 , 0 );
62
+ }
76
63
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 ];
79
69
}
80
-
81
70
return " " ;
82
- }
71
+ }
0 commit comments