44extern void finderActivatedCallbackCGO (int pid);
55
66static id gAppActivationObserver = nil ;
7+ static AXObserverRef gFinderWindowObserver = nil ;
8+ static pid_t gFinderPid = 0 ;
9+
10+ // AXObserver callback - called when Finder's focused window changes
11+ static void finderWindowFocusCallback (AXObserverRef observer, AXUIElementRef element, CFStringRef notification, void *refcon) {
12+ if (gFinderPid > 0 ) {
13+ finderActivatedCallbackCGO (gFinderPid );
14+ }
15+ }
16+
17+ static void startFinderWindowObserver (pid_t pid) {
18+ // Stop existing observer if any
19+ if (gFinderWindowObserver ) {
20+ CFRunLoopRemoveSource (CFRunLoopGetMain (),
21+ AXObserverGetRunLoopSource (gFinderWindowObserver ),
22+ kCFRunLoopDefaultMode );
23+ CFRelease (gFinderWindowObserver );
24+ gFinderWindowObserver = nil ;
25+ }
26+
27+ gFinderPid = pid;
28+
29+ // Create AXObserver for window focus changes
30+ AXObserverRef observer = NULL ;
31+ AXError err = AXObserverCreate (pid, finderWindowFocusCallback, &observer);
32+ if (err != kAXErrorSuccess || !observer) {
33+ return ;
34+ }
35+
36+ gFinderWindowObserver = observer;
37+
38+ // Get the application element and add notification
39+ AXUIElementRef app = AXUIElementCreateApplication (pid);
40+ if (app) {
41+ AXObserverAddNotification (observer, app, kAXFocusedWindowChangedNotification , NULL );
42+ CFRelease (app);
43+ }
44+
45+ // Add observer to run loop
46+ CFRunLoopAddSource (CFRunLoopGetMain (), AXObserverGetRunLoopSource (observer), kCFRunLoopDefaultMode );
47+ }
48+
49+ static void stopFinderWindowObserver () {
50+ if (gFinderWindowObserver ) {
51+ CFRunLoopRemoveSource (CFRunLoopGetMain (),
52+ AXObserverGetRunLoopSource (gFinderWindowObserver ),
53+ kCFRunLoopDefaultMode );
54+ CFRelease (gFinderWindowObserver );
55+ gFinderWindowObserver = nil ;
56+ }
57+ gFinderPid = 0 ;
58+ }
759
860void startFinderMonitor () {
961 @autoreleasepool {
@@ -16,14 +68,29 @@ void startFinderMonitor() {
1668 usingBlock: ^(NSNotification *notification) {
1769 NSRunningApplication *app = [[notification userInfo ] objectForKey: NSWorkspaceApplicationKey ];
1870 if (app && [[app bundleIdentifier ] isEqualToString: @" com.apple.finder" ]) {
19- finderActivatedCallbackCGO ([app processIdentifier ]);
71+ pid_t pid = [app processIdentifier ];
72+ finderActivatedCallbackCGO (pid);
73+ // Start observing window focus changes within Finder
74+ startFinderWindowObserver (pid);
75+ } else {
76+ // Stop observing when switching away from Finder
77+ stopFinderWindowObserver ();
2078 }
2179 }];
80+
81+ // Check if Finder is already active
82+ NSRunningApplication *activeApp = [[NSWorkspace sharedWorkspace ] frontmostApplication ];
83+ if (activeApp && [[activeApp bundleIdentifier ] isEqualToString: @" com.apple.finder" ]) {
84+ pid_t pid = [activeApp processIdentifier ];
85+ finderActivatedCallbackCGO (pid);
86+ startFinderWindowObserver (pid);
87+ }
2288 }
2389}
2490
2591void stopFinderMonitor () {
2692 @autoreleasepool {
93+ stopFinderWindowObserver ();
2794 if (gAppActivationObserver ) {
2895 [[NSWorkspace sharedWorkspace ].notificationCenter removeObserver: gAppActivationObserver ];
2996 gAppActivationObserver = nil ;
0 commit comments