Skip to content

Commit 26f226c

Browse files
committed
fix focus window implementation on macos
1 parent 53729cc commit 26f226c

File tree

3 files changed

+56
-58
lines changed

3 files changed

+56
-58
lines changed

src/macos/window_manager.mm

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -93,35 +93,33 @@ MMRect getWindowRect(const WindowHandle windowHandle) {
9393
return "";
9494
}
9595

96-
BOOL focusWindow(NSWindow *window) {
97-
// if (window) {
98-
// // Restore the window if it's minimized
99-
// if ([window isMiniaturized]) {
100-
// [window deminiaturize:nil];
101-
// }
102-
103-
// // Try to set the window to the foreground
104-
// [window makeKeyAndOrderFront:nil];
105-
106-
// return YES;
107-
// }
108-
return NO;
96+
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+
}
111+
112+
if (windowList) {
113+
CFRelease(windowList);
114+
}
115+
return false;
109116
}
110117

111-
BOOL resizeWindow(NSWindow *window, MMRect rect) {
112-
if (window) {
113-
NSRect frame;
114-
115-
//size
116-
frame.size.width = rect.size.width;
117-
frame.size.height = rect.size.height;
118118

119-
//origin
120-
frame.origin.x = rect.origin.x;
121-
frame.origin.y = rect.origin.y;
119+
bool resizeWindow(const WindowHandle windowHandle, MMRect rect) {
120+
if (windowHandle < 0) {
121+
return false;
122+
}
122123

123-
[window setFrame:frame display:YES animate:NO];
124-
return YES;
125-
}
126-
return NO;
124+
return true;
127125
}

src/main.cc

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -654,35 +654,35 @@ Napi::Boolean _focusWindow(const Napi::CallbackInfo &info) {
654654
return Napi::Boolean::New(env, result);
655655
}
656656

657-
Napi::Value _resizeWindow(const Napi::CallbackInfo& info) {
658-
Napi::Env env = info.Env();
659-
660-
if (info.Length() < 2 || !info[0].IsNumber() || !info[1].IsObject()) {
661-
Napi::TypeError::New(env, "Invalid arguments. Expected handle (number) and rect (object).").ThrowAsJavaScriptException();
662-
return Napi::Boolean::New(env, false);
663-
}
664-
665-
int64_t handle = info[0].As<Napi::Number>().Int64Value();
666-
Napi::Object rectObj = info[1].As<Napi::Object>();
667-
668-
int64_t x = 0;
669-
int64_t y = 0;
670-
int64_t width = 0;
671-
int64_t height = 0;
672-
673-
if (rectObj.Has("x"))
674-
x = rectObj.Get("x").As<Napi::Number>().Int64Value();
675-
if (rectObj.Has("y"))
676-
y = rectObj.Get("y").As<Napi::Number>().Int64Value();
677-
if (rectObj.Has("width"))
678-
width = rectObj.Get("width").As<Napi::Number>().Int64Value();
679-
if (rectObj.Has("height"))
680-
height = rectObj.Get("height").As<Napi::Number>().Int64Value();
681-
682-
MMRect rect = MMRectMake(x, y, width, height);
683-
resizeWindow(handle, rect);
684-
685-
return Napi::Boolean::New(env, true);
657+
Napi::Boolean _resizeWindow(const Napi::CallbackInfo& info) {
658+
// Napi::Env env = info.Env();
659+
660+
// if (info.Length() < 2 || !info[0].IsNumber() || !info[1].IsObject()) {
661+
// Napi::TypeError::New(env, "Invalid arguments. Expected handle (number) and rect (object).").ThrowAsJavaScriptException();
662+
// return Napi::Boolean::New(env, false);
663+
// }
664+
665+
// int64_t handle = info[0].As<Napi::Number>().Int64Value();
666+
// Napi::Object rectObj = info[1].As<Napi::Object>();
667+
668+
// int64_t x = 0;
669+
// int64_t y = 0;
670+
// int64_t width = 0;
671+
// int64_t height = 0;
672+
673+
// if (rectObj.Has("x"))
674+
// x = rectObj.Get("x").As<Napi::Number>().Int64Value();
675+
// if (rectObj.Has("y"))
676+
// y = rectObj.Get("y").As<Napi::Number>().Int64Value();
677+
// if (rectObj.Has("width"))
678+
// width = rectObj.Get("width").As<Napi::Number>().Int64Value();
679+
// if (rectObj.Has("height"))
680+
// height = rectObj.Get("height").As<Napi::Number>().Int64Value();
681+
682+
// MMRect rect = MMRectMake(x, y, width, height);
683+
// resizeWindow(handle, rect);
684+
685+
// return Napi::Boolean::New(env, true);
686686
}
687687

688688

src/win32/window_manager.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ std::string getWindowTitle(const WindowHandle windowHandle) {
5252
return "";
5353
}
5454

55-
bool focusWindow(const WindowHandle windowHandle) {
55+
std::bool focusWindow(const WindowHandle windowHandle) {
5656
HWND hWnd = reinterpret_cast<HWND>(windowHandle);
5757
if (IsWindow(hWnd)) {
5858
// Restore the window if it's minimized
@@ -66,7 +66,7 @@ bool focusWindow(const WindowHandle windowHandle) {
6666
return false;
6767
}
6868

69-
bool resizeWindow(const WindowHandle windowHandle, const MMRect& rect) {
69+
std::bool resizeWindow(const WindowHandle windowHandle, const MMRect& rect) {
7070
HWND hWnd = reinterpret_cast<HWND>(windowHandle);
7171
if (IsWindow(hWnd)) {
7272
//size

0 commit comments

Comments
 (0)