1
+ #include " ../window_manager.h"
2
+ #import < AppKit/AppKit.h>
3
+ #import < ApplicationServices/ApplicationServices.h>
1
4
#include < CoreGraphics/CGWindow.h>
2
5
#import < Foundation/Foundation.h>
3
- #import < AppKit/AppKit.h>
4
- #include " ../window_manager.h"
5
6
6
- NSDictionary * getWindowInfo (int64_t windowHandle) {
7
- CGWindowListOption listOptions = kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements ;
8
- CFArrayRef windowList = CGWindowListCopyWindowInfo (listOptions, kCGNullWindowID );
7
+ NSDictionary *getWindowInfo (int64_t windowHandle) {
8
+ CGWindowListOption listOptions =
9
+ kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements ;
10
+ CFArrayRef windowList =
11
+ CGWindowListCopyWindowInfo (listOptions, kCGNullWindowID );
9
12
10
13
for (NSDictionary *info in (NSArray *)windowList) {
11
14
NSNumber *windowNumber = info[(id )kCGWindowNumber ];
25
28
}
26
29
27
30
WindowHandle getActiveWindow () {
28
- CGWindowListOption listOptions = kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements ;
29
- CFArrayRef windowList = CGWindowListCopyWindowInfo (listOptions, kCGNullWindowID );
31
+ CGWindowListOption listOptions =
32
+ kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements ;
33
+ CFArrayRef windowList =
34
+ CGWindowListCopyWindowInfo (listOptions, kCGNullWindowID );
30
35
31
36
for (NSDictionary *info in (NSArray *)windowList) {
32
37
NSNumber *ownerPid = info[(id )kCGWindowOwnerPID ];
33
38
NSNumber *windowNumber = info[(id )kCGWindowNumber ];
34
39
35
- auto app = [NSRunningApplication runningApplicationWithProcessIdentifier: [ownerPid intValue ]];
40
+ auto app = [NSRunningApplication
41
+ runningApplicationWithProcessIdentifier: [ownerPid intValue ]];
36
42
37
43
if (![app isActive ]) {
38
44
continue ;
@@ -49,16 +55,19 @@ WindowHandle getActiveWindow() {
49
55
}
50
56
51
57
std::vector<WindowHandle> getWindows () {
52
- CGWindowListOption listOptions = kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements ;
53
- CFArrayRef windowList = CGWindowListCopyWindowInfo (listOptions, kCGNullWindowID );
58
+ CGWindowListOption listOptions =
59
+ kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements ;
60
+ CFArrayRef windowList =
61
+ CGWindowListCopyWindowInfo (listOptions, kCGNullWindowID );
54
62
55
63
std::vector<WindowHandle> windowHandles;
56
64
57
65
for (NSDictionary *info in (NSArray *)windowList) {
58
66
NSNumber *ownerPid = info[(id )kCGWindowOwnerPID ];
59
67
NSNumber *windowNumber = info[(id )kCGWindowNumber ];
60
68
61
- auto app = [NSRunningApplication runningApplicationWithProcessIdentifier: [ownerPid intValue ]];
69
+ auto app = [NSRunningApplication
70
+ runningApplicationWithProcessIdentifier: [ownerPid intValue ]];
62
71
auto path = app ? [app.bundleURL.path UTF8String ] : " " ;
63
72
64
73
if (app && path != " " ) {
@@ -77,8 +86,10 @@ MMRect getWindowRect(const WindowHandle windowHandle) {
77
86
auto windowInfo = getWindowInfo (windowHandle);
78
87
if (windowInfo != nullptr && windowHandle >= 0 ) {
79
88
CGRect windowRect;
80
- if (CGRectMakeWithDictionaryRepresentation ((CFDictionaryRef )windowInfo[(id )kCGWindowBounds ], &windowRect)) {
81
- return MMRectMake (windowRect.origin .x , windowRect.origin .y , windowRect.size .width , windowRect.size .height );
89
+ if (CGRectMakeWithDictionaryRepresentation (
90
+ (CFDictionaryRef )windowInfo[(id )kCGWindowBounds ], &windowRect)) {
91
+ return MMRectMake (windowRect.origin .x , windowRect.origin .y ,
92
+ windowRect.size .width , windowRect.size .height );
82
93
}
83
94
}
84
95
return MMRectMake (0 , 0 , 0 , 0 );
@@ -88,38 +99,42 @@ MMRect getWindowRect(const WindowHandle windowHandle) {
88
99
auto windowInfo = getWindowInfo (windowHandle);
89
100
if (windowInfo != nullptr && windowHandle >= 0 ) {
90
101
NSString *windowName = windowInfo[(id )kCGWindowName ];
91
- return std::string ([windowName UTF8String ], [windowName lengthOfBytesUsingEncoding: NSUTF8StringEncoding]);
102
+ return std::string (
103
+ [windowName UTF8String ],
104
+ [windowName lengthOfBytesUsingEncoding: NSUTF8StringEncoding]);
92
105
}
93
106
return " " ;
94
107
}
95
108
96
109
bool focusWindow (const WindowHandle windowHandle) {
97
- CGWindowListOption listOptions = kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements ;
98
- CFArrayRef windowList = CGWindowListCopyWindowInfo (listOptions, kCGNullWindowID );
99
-
100
- for (NSDictionary *info in (NSArray *)windowList) {
101
- NSNumber *ownerPid = info[(id )kCGWindowOwnerPID ];
102
- NSNumber *windowNumber = info[(id )kCGWindowNumber ];
103
-
104
- if ([windowNumber intValue ] == windowHandle) {
105
- NSRunningApplication *app = [NSRunningApplication runningApplicationWithProcessIdentifier: [ownerPid intValue ]];
106
- [app activateWithOptions: NSApplicationActivateIgnoringOtherApps];
107
- CFRelease (windowList);
108
- return true ;
109
- }
110
- }
110
+ CGWindowListOption listOptions =
111
+ kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements ;
112
+ CFArrayRef windowList =
113
+ CGWindowListCopyWindowInfo (listOptions, kCGNullWindowID );
111
114
112
- if (windowList) {
113
- CFRelease (windowList);
114
- }
115
- return false ;
116
- }
115
+ bool activated = false ;
116
+
117
+ for ( NSDictionary *info in ( NSArray *)windowList) {
118
+ NSNumber *ownerPid = info[( id ) kCGWindowOwnerPID ] ;
119
+ NSNumber *windowNumber = info[( id ) kCGWindowNumber ];
117
120
121
+ if ([windowNumber intValue ] == windowHandle) {
122
+ NSRunningApplication *app = [NSRunningApplication
123
+ runningApplicationWithProcessIdentifier: [ownerPid intValue ]];
124
+ [app activateWithOptions: NSApplicationActivateIgnoringOtherApps];
125
+ activated = true ;
126
+ }
127
+ }
118
128
119
- bool resizeWindow (const WindowHandle windowHandle, MMRect rect) {
120
- if (windowHandle < 0 ) {
121
- return false ;
129
+ if (windowList) {
130
+ CFRelease (windowList);
122
131
}
123
132
133
+ return activated;
134
+ }
135
+
136
+ bool resizeWindow (int64_t windowHandle, MMRect rect) {
137
+ // this method is complicated due to the accessibility requirements on macos
138
+
124
139
return true ;
125
140
}
0 commit comments