Skip to content

Commit 01d47e9

Browse files
committed
update focus window method
1 parent f8a5dd7 commit 01d47e9

File tree

1 file changed

+51
-36
lines changed

1 file changed

+51
-36
lines changed

Diff for: src/macos/window_manager.mm

+51-36
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1+
#include "../window_manager.h"
2+
#import <AppKit/AppKit.h>
3+
#import <ApplicationServices/ApplicationServices.h>
14
#include <CoreGraphics/CGWindow.h>
25
#import <Foundation/Foundation.h>
3-
#import <AppKit/AppKit.h>
4-
#include "../window_manager.h"
56

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);
912

1013
for (NSDictionary *info in (NSArray *)windowList) {
1114
NSNumber *windowNumber = info[(id)kCGWindowNumber];
@@ -25,14 +28,17 @@
2528
}
2629

2730
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);
3035

3136
for (NSDictionary *info in (NSArray *)windowList) {
3237
NSNumber *ownerPid = info[(id)kCGWindowOwnerPID];
3338
NSNumber *windowNumber = info[(id)kCGWindowNumber];
3439

35-
auto app = [NSRunningApplication runningApplicationWithProcessIdentifier: [ownerPid intValue]];
40+
auto app = [NSRunningApplication
41+
runningApplicationWithProcessIdentifier:[ownerPid intValue]];
3642

3743
if (![app isActive]) {
3844
continue;
@@ -49,16 +55,19 @@ WindowHandle getActiveWindow() {
4955
}
5056

5157
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);
5462

5563
std::vector<WindowHandle> windowHandles;
5664

5765
for (NSDictionary *info in (NSArray *)windowList) {
5866
NSNumber *ownerPid = info[(id)kCGWindowOwnerPID];
5967
NSNumber *windowNumber = info[(id)kCGWindowNumber];
6068

61-
auto app = [NSRunningApplication runningApplicationWithProcessIdentifier: [ownerPid intValue]];
69+
auto app = [NSRunningApplication
70+
runningApplicationWithProcessIdentifier:[ownerPid intValue]];
6271
auto path = app ? [app.bundleURL.path UTF8String] : "";
6372

6473
if (app && path != "") {
@@ -77,8 +86,10 @@ MMRect getWindowRect(const WindowHandle windowHandle) {
7786
auto windowInfo = getWindowInfo(windowHandle);
7887
if (windowInfo != nullptr && windowHandle >= 0) {
7988
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);
8293
}
8394
}
8495
return MMRectMake(0, 0, 0, 0);
@@ -88,38 +99,42 @@ MMRect getWindowRect(const WindowHandle windowHandle) {
8899
auto windowInfo = getWindowInfo(windowHandle);
89100
if (windowInfo != nullptr && windowHandle >= 0) {
90101
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]);
92105
}
93106
return "";
94107
}
95108

96109
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);
111114

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];
117120

121+
if ([windowNumber intValue] == windowHandle) {
122+
NSRunningApplication *app = [NSRunningApplication
123+
runningApplicationWithProcessIdentifier:[ownerPid intValue]];
124+
[app activateWithOptions:NSApplicationActivateIgnoringOtherApps];
125+
activated = true;
126+
}
127+
}
118128

119-
bool resizeWindow(const WindowHandle windowHandle, MMRect rect) {
120-
if (windowHandle < 0) {
121-
return false;
129+
if (windowList) {
130+
CFRelease(windowList);
122131
}
123132

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+
124139
return true;
125140
}

0 commit comments

Comments
 (0)