@@ -107,13 +107,30 @@ MMRect getWindowRect(const WindowHandle windowHandle) {
107
107
return " " ;
108
108
}
109
109
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
+ */
110
124
bool focusWindow (const WindowHandle windowHandle) {
111
125
126
+ // Collect list of on-screen windows
112
127
CGWindowListOption listOptions =
113
128
kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements ;
114
129
CFArrayRef windowList =
115
130
CGWindowListCopyWindowInfo (listOptions, kCGNullWindowID );
116
131
bool activated = false ;
132
+
133
+ // Look for matching window and bring application to foreground
117
134
for (NSDictionary *info in (NSArray *)windowList) {
118
135
NSNumber *ownerPid = info[(id )kCGWindowOwnerPID ];
119
136
NSNumber *windowNumber = info[(id )kCGWindowNumber ];
@@ -124,24 +141,31 @@ bool focusWindow(const WindowHandle windowHandle) {
124
141
activated = true ;
125
142
}
126
143
}
144
+
145
+ // Clean up window list
127
146
if (windowList) {
128
147
CFRelease (windowList);
129
148
}
130
-
149
+
150
+ // Retrieve window info
131
151
NSDictionary *windowInfo = getWindowInfo (windowHandle);
132
152
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);
134
154
return false ;
135
155
}
136
156
157
+ // Create application object for accessibility
137
158
pid_t pid = [[windowInfo objectForKey: (id )kCGWindowOwnerPID ] intValue ];
138
159
AXUIElementRef app = AXUIElementCreateApplication (pid);
139
160
161
+ // Get target window title
140
162
NSString *targetWindowTitle = [windowInfo objectForKey: (id )kCGWindowName ];
141
163
142
164
CFArrayRef windowArray;
143
165
AXError error = AXUIElementCopyAttributeValue (app, kAXWindowsAttribute ,
144
166
(CFTypeRef *)&windowArray);
167
+
168
+ // Iterate through windows to find target and bring it to front
145
169
if (error == kAXErrorSuccess ) {
146
170
CFIndex count = CFArrayGetCount (windowArray);
147
171
for (CFIndex i = 0 ; i < count; i++) {
@@ -155,75 +179,93 @@ bool focusWindow(const WindowHandle windowHandle) {
155
179
if ([title isEqualToString: targetWindowTitle]) {
156
180
AXError error = AXUIElementPerformAction (window, kAXRaiseAction );
157
181
if (error == kAXErrorSuccess ) {
158
- NSLog (@" Successfully brought the window to front." );
182
+ // NSLog(@"Successfully brought the window to front.");
159
183
} 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);
162
186
}
163
187
break ;
164
188
}
165
189
}
190
+
191
+ // Clean up window title
166
192
if (windowTitle) {
167
193
CFRelease (windowTitle);
168
194
}
169
195
}
196
+
197
+ // Clean up window array
170
198
CFRelease (windowArray);
171
199
} else {
172
- NSLog (@" Failed to retrieve the window array." );
200
+ // NSLog(@"Failed to retrieve the window array.");
173
201
}
174
202
203
+ // Clean up application object
175
204
CFRelease (app);
176
205
177
- // log the window title
178
- NSString *windowName = windowInfo[(id )kCGWindowName ];
179
- NSLog (@" attempted to focus window: %@ " , windowName);
206
+ // Successfully executed
180
207
return true ;
181
208
}
182
209
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
+ */
187
224
bool resizeWindow (const WindowHandle windowHandle, const MMRect rect) {
188
225
226
+ // Retrieve window info
189
227
NSDictionary *windowInfo = getWindowInfo (windowHandle);
190
228
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);
192
230
return false ;
193
231
}
194
232
233
+ // Create application object for accessibility
195
234
pid_t pid = [[windowInfo objectForKey: (id )kCGWindowOwnerPID ] intValue ];
196
235
AXUIElementRef app = AXUIElementCreateApplication (pid);
197
236
AXUIElementRef window;
237
+
198
238
AXError error = AXUIElementCopyAttributeValue (app, kAXFocusedWindowAttribute ,
199
239
(CFTypeRef *)&window);
200
-
240
+
241
+ // If no error occurred, proceed with the resize and reposition operations
201
242
if (error == kAXErrorSuccess ) {
243
+
244
+ // Create AXValue objects for position and size
202
245
AXValueRef positionValue = AXValueCreate ((AXValueType)kAXValueCGPointType ,
203
246
(const void *)&rect.origin );
204
-
205
- // extract the size from the rect
206
-
207
247
CGSize size = CGSizeMake (rect.size .width , rect.size .height );
208
248
AXValueRef sizeValue =
209
249
AXValueCreate ((AXValueType)kAXValueCGSizeType , (const void *)&size);
210
250
251
+ // Set new position and size
211
252
AXUIElementSetAttributeValue (window, kAXPositionAttribute , positionValue);
212
253
AXUIElementSetAttributeValue (window, kAXSizeAttribute , sizeValue);
213
254
214
- // log the position and size of the window
215
-
255
+ // Clean up AXValue and AXUIElement objects
216
256
CFRelease (positionValue);
217
257
CFRelease (sizeValue);
218
258
CFRelease (window);
219
259
CFRelease (app);
220
260
261
+ // Return true to indicate successful resize
221
262
return true ;
222
263
} else {
223
- NSLog (@" Could not resize window with window handle %lld " , windowHandle);
264
+ // NSLog(@"Could not resize window with window handle %lld", windowHandle);
224
265
CFRelease (app);
225
266
return false ;
226
267
}
227
268
228
269
return YES ;
229
270
}
271
+
0 commit comments