[fix] fix macOS clipboard monitoring compilation error using changeCount polling

This commit is contained in:
dijunkun
2025-12-29 01:01:08 +08:00
parent 17b7ba6b72
commit 5f320af6e6

View File

@@ -70,20 +70,29 @@ void StartMacOSClipboardMonitoring() {
CFRetain(g_monitor_runloop);
}
// Register for clipboard change notifications
id observer =
[[NSNotificationCenter defaultCenter] addObserverForName:NSPasteboardDidChangeNotification
object:pasteboard
queue:nil
usingBlock:^(NSNotification* notification) {
if (!g_monitoring.load()) {
return;
}
HandleClipboardChange();
}];
// Track changeCount to detect clipboard changes
// Use __block to allow modification inside the block
__block NSInteger lastChangeCount = [pasteboard changeCount];
LOG_INFO("Clipboard event monitoring started (macOS)");
// Use a timer to periodically check changeCount
// This is more reliable than NSPasteboardDidChangeNotification which may not be available
NSTimer* timer =
[NSTimer scheduledTimerWithTimeInterval:0.1
repeats:YES
block:^(NSTimer* timer) {
if (!g_monitoring.load()) {
[timer invalidate];
return;
}
NSInteger currentChangeCount = [pasteboard changeCount];
if (currentChangeCount != lastChangeCount) {
lastChangeCount = currentChangeCount;
HandleClipboardChange();
}
}];
while (g_monitoring.load()) {
@autoreleasepool {
NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0.1];
@@ -92,9 +101,7 @@ void StartMacOSClipboardMonitoring() {
}
// Cleanup
[[NSNotificationCenter defaultCenter] removeObserver:observer
name:NSPasteboardDidChangeNotification
object:pasteboard];
[timer invalidate];
if (g_monitor_runloop) {
CFRelease(g_monitor_runloop);
g_monitor_runloop = nullptr;