Skip to content

Commit 71baba8

Browse files
committed
add documentation and clean code
1 parent 5c6fbbb commit 71baba8

File tree

1 file changed

+63
-21
lines changed

1 file changed

+63
-21
lines changed

Diff for: src/macos/window_manager.mm

+63-21
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,30 @@ MMRect getWindowRect(const WindowHandle windowHandle) {
107107
return "";
108108
}
109109

110+
/**
111+
* Focuses on the window provided via its handle.
112+
*
113+
* This function collects a list of on-screen windows and matches the
114+
* windowHandle with their window numbers. If found, the corresponding
115+
* application is brought to foreground. The function then uses accessibility
116+
* APIs to specifically focus the target window using its title.
117+
*
118+
* @param windowHandle Handle to the window that needs to be focused.
119+
*
120+
* @return bool If the function executes without any errors, it returns true.
121+
* If it can't retrieve window information or windowHandle is
122+
* invalid, it returns false.
123+
*/
110124
bool focusWindow(const WindowHandle windowHandle) {
111125

126+
// Collect list of on-screen windows
112127
CGWindowListOption listOptions =
113128
kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements;
114129
CFArrayRef windowList =
115130
CGWindowListCopyWindowInfo(listOptions, kCGNullWindowID);
116131
bool activated = false;
132+
133+
// Look for matching window and bring application to foreground
117134
for (NSDictionary *info in (NSArray *)windowList) {
118135
NSNumber *ownerPid = info[(id)kCGWindowOwnerPID];
119136
NSNumber *windowNumber = info[(id)kCGWindowNumber];
@@ -124,24 +141,31 @@ bool focusWindow(const WindowHandle windowHandle) {
124141
activated = true;
125142
}
126143
}
144+
145+
// Clean up window list
127146
if (windowList) {
128147
CFRelease(windowList);
129148
}
130-
149+
150+
// Retrieve window info
131151
NSDictionary *windowInfo = getWindowInfo(windowHandle);
132152
if (windowInfo == nullptr || windowHandle < 0) {
133-
NSLog(@"Could not find window info for window handle %lld", windowHandle);
153+
// NSLog(@"Could not find window info for window handle %lld", windowHandle);
134154
return false;
135155
}
136156

157+
// Create application object for accessibility
137158
pid_t pid = [[windowInfo objectForKey:(id)kCGWindowOwnerPID] intValue];
138159
AXUIElementRef app = AXUIElementCreateApplication(pid);
139160

161+
// Get target window title
140162
NSString *targetWindowTitle = [windowInfo objectForKey:(id)kCGWindowName];
141163

142164
CFArrayRef windowArray;
143165
AXError error = AXUIElementCopyAttributeValue(app, kAXWindowsAttribute,
144166
(CFTypeRef *)&windowArray);
167+
168+
// Iterate through windows to find target and bring it to front
145169
if (error == kAXErrorSuccess) {
146170
CFIndex count = CFArrayGetCount(windowArray);
147171
for (CFIndex i = 0; i < count; i++) {
@@ -155,75 +179,93 @@ bool focusWindow(const WindowHandle windowHandle) {
155179
if ([title isEqualToString:targetWindowTitle]) {
156180
AXError error = AXUIElementPerformAction(window, kAXRaiseAction);
157181
if (error == kAXErrorSuccess) {
158-
NSLog(@"Successfully brought the window to front.");
182+
// NSLog(@"Successfully brought the window to front.");
159183
} else {
160-
NSLog(@"Failed to bring the window to front.");
161-
NSLog(@"AXUIElementSetAttributeValue error: %d", error);
184+
// NSLog(@"Failed to bring the window to front.");
185+
// NSLog(@"AXUIElementSetAttributeValue error: %d", error);
162186
}
163187
break;
164188
}
165189
}
190+
191+
// Clean up window title
166192
if (windowTitle) {
167193
CFRelease(windowTitle);
168194
}
169195
}
196+
197+
// Clean up window array
170198
CFRelease(windowArray);
171199
} else {
172-
NSLog(@"Failed to retrieve the window array.");
200+
// NSLog(@"Failed to retrieve the window array.");
173201
}
174202

203+
// Clean up application object
175204
CFRelease(app);
176205

177-
// log the window title
178-
NSString *windowName = windowInfo[(id)kCGWindowName];
179-
NSLog(@"attempted to focus window: %@", windowName);
206+
// Successfully executed
180207
return true;
181208
}
182209

183-
/*
184-
This function takes an input windowhandle (a kCGWindowNumber) and a rect (size
185-
& origin) and resizes the window to the given rect.
186-
*/
210+
/**
211+
* Resizes and repositions the window provided via its handle to the specified rectangle.
212+
*
213+
* This function retrieves window information using the provided window handle, then uses
214+
* macOS Accessibility APIs to resize and reposition the window to fit within the provided
215+
* rectangle dimensions and location.
216+
*
217+
* @param windowHandle Handle to the window that needs to be resized.
218+
* @param rect The rectangle area to which the window should be resized and repositioned.
219+
*
220+
* @return bool If the function executes without any errors and successfully resizes the
221+
* window, it returns true. If it can't retrieve window information or
222+
* windowHandle is invalid, or the window resizing operation fails, it returns false.
223+
*/
187224
bool resizeWindow(const WindowHandle windowHandle, const MMRect rect) {
188225

226+
// Retrieve window info
189227
NSDictionary *windowInfo = getWindowInfo(windowHandle);
190228
if (windowInfo == nullptr || windowHandle < 0) {
191-
NSLog(@"Could not find window info for window handle %lld", windowHandle);
229+
// NSLog(@"Could not find window info for window handle %lld", windowHandle);
192230
return false;
193231
}
194232

233+
// Create application object for accessibility
195234
pid_t pid = [[windowInfo objectForKey:(id)kCGWindowOwnerPID] intValue];
196235
AXUIElementRef app = AXUIElementCreateApplication(pid);
197236
AXUIElementRef window;
237+
198238
AXError error = AXUIElementCopyAttributeValue(app, kAXFocusedWindowAttribute,
199239
(CFTypeRef *)&window);
200-
240+
241+
// If no error occurred, proceed with the resize and reposition operations
201242
if (error == kAXErrorSuccess) {
243+
244+
// Create AXValue objects for position and size
202245
AXValueRef positionValue = AXValueCreate((AXValueType)kAXValueCGPointType,
203246
(const void *)&rect.origin);
204-
205-
// extract the size from the rect
206-
207247
CGSize size = CGSizeMake(rect.size.width, rect.size.height);
208248
AXValueRef sizeValue =
209249
AXValueCreate((AXValueType)kAXValueCGSizeType, (const void *)&size);
210250

251+
// Set new position and size
211252
AXUIElementSetAttributeValue(window, kAXPositionAttribute, positionValue);
212253
AXUIElementSetAttributeValue(window, kAXSizeAttribute, sizeValue);
213254

214-
// log the position and size of the window
215-
255+
// Clean up AXValue and AXUIElement objects
216256
CFRelease(positionValue);
217257
CFRelease(sizeValue);
218258
CFRelease(window);
219259
CFRelease(app);
220260

261+
// Return true to indicate successful resize
221262
return true;
222263
} else {
223-
NSLog(@"Could not resize window with window handle %lld", windowHandle);
264+
// NSLog(@"Could not resize window with window handle %lld", windowHandle);
224265
CFRelease(app);
225266
return false;
226267
}
227268

228269
return YES;
229270
}
271+

0 commit comments

Comments
 (0)