@@ -232,8 +232,8 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {
232
232
menu. addItem ( NSMenuItem ( title: " Settings " , action: #selector( showSettings) , keyEquivalent: " , " ) )
233
233
menu. addItem ( NSMenuItem ( title: " About " , action: #selector( showAbout) , keyEquivalent: " i " ) )
234
234
// New Pause/Resume item:
235
- let hotkeyTitle = AppSettings . shared. hotkeysPaused ? " Resume " : " Pause "
236
- menu. addItem ( NSMenuItem ( title: hotkeyTitle, action: #selector( toggleHotkeys) , keyEquivalent: " p " ) )
235
+ let hotkeyTitle = AppSettings . shared. hotkeysPaused ? " Resume " : " Pause "
236
+ menu. addItem ( NSMenuItem ( title: hotkeyTitle, action: #selector( toggleHotkeys) , keyEquivalent: " p " ) )
237
237
menu. addItem ( NSMenuItem ( title: " Reset App " , action: #selector( resetApp) , keyEquivalent: " r " ) )
238
238
menu. addItem ( NSMenuItem . separator ( ) )
239
239
menu. addItem ( NSMenuItem ( title: " Quit " , action: #selector( NSApplication . terminate ( _: ) ) , keyEquivalent: " q " ) )
@@ -363,111 +363,111 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {
363
363
}
364
364
365
365
@MainActor private func showPopup( ) {
366
- appState. activeProvider. cancel ( )
366
+ appState. activeProvider. cancel ( )
367
+
368
+ DispatchQueue . main. async { [ weak self] in
369
+ guard let self = self else { return }
367
370
368
- DispatchQueue . main. async { [ weak self] in
371
+ if let currentFrontmostApp = NSWorkspace . shared. frontmostApplication {
372
+ self . appState. previousApplication = currentFrontmostApp
373
+ }
374
+
375
+ self . closePopupWindow ( )
376
+
377
+ let generalPasteboard = NSPasteboard . general
378
+ let oldContents = generalPasteboard. string ( forType: . string)
379
+
380
+ // Clear and perform copy command to get current selection
381
+ generalPasteboard. clearContents ( )
382
+ let source = CGEventSource ( stateID: . hidSystemState)
383
+ let keyDown = CGEvent ( keyboardEventSource: source, virtualKey: 0x08 , keyDown: true )
384
+ let keyUp = CGEvent ( keyboardEventSource: source, virtualKey: 0x08 , keyDown: false )
385
+ keyDown? . flags = . maskCommand
386
+ keyUp? . flags = . maskCommand
387
+ keyDown? . post ( tap: . cghidEventTap)
388
+ keyUp? . post ( tap: . cghidEventTap)
389
+
390
+ // Wait for the copy operation to complete, then process the pasteboard
391
+ DispatchQueue . main. asyncAfter ( deadline: . now( ) + 0.2 ) { [ weak self] in
369
392
guard let self = self else { return }
370
393
371
- if let currentFrontmostApp = NSWorkspace . shared. frontmostApplication {
372
- self . appState. previousApplication = currentFrontmostApp
373
- }
374
-
375
- self . closePopupWindow ( )
394
+ var foundImages : [ Data ] = [ ]
395
+ var selectedText = " "
376
396
377
- let generalPasteboard = NSPasteboard . general
378
- let oldContents = generalPasteboard. string ( forType: . string)
379
-
380
- // Clear and perform copy command to get current selection
381
- generalPasteboard. clearContents ( )
382
- let source = CGEventSource ( stateID: . hidSystemState)
383
- let keyDown = CGEvent ( keyboardEventSource: source, virtualKey: 0x08 , keyDown: true )
384
- let keyUp = CGEvent ( keyboardEventSource: source, virtualKey: 0x08 , keyDown: false )
385
- keyDown? . flags = . maskCommand
386
- keyUp? . flags = . maskCommand
387
- keyDown? . post ( tap: . cghidEventTap)
388
- keyUp? . post ( tap: . cghidEventTap)
389
-
390
- // Wait for the copy operation to complete, then process the pasteboard
391
- DispatchQueue . main. asyncAfter ( deadline: . now( ) + 0.2 ) { [ weak self] in
392
- guard let self = self else { return }
393
-
394
- var foundImages : [ Data ] = [ ]
395
- var selectedText = " "
396
-
397
- // First check for file URLs (for Finder selections)
398
- let classes = [ NSURL . self]
399
- let options : [ NSPasteboard . ReadingOptionKey : Any ] = [
400
- . urlReadingFileURLsOnly: true ,
401
- . urlReadingContentsConformToTypes: [
402
- " public.image " ,
403
- " public.png " ,
404
- " public.jpeg " ,
405
- " public.tiff " ,
406
- " com.compuserve.gif "
407
- ]
397
+ // First check for file URLs (for Finder selections)
398
+ let classes = [ NSURL . self]
399
+ let options : [ NSPasteboard . ReadingOptionKey : Any ] = [
400
+ . urlReadingFileURLsOnly: true ,
401
+ . urlReadingContentsConformToTypes: [
402
+ " public.image " ,
403
+ " public.png " ,
404
+ " public.jpeg " ,
405
+ " public.tiff " ,
406
+ " com.compuserve.gif "
408
407
]
409
-
410
- if let urls = generalPasteboard . readObjects ( forClasses : classes , options : options ) as? [ URL ] {
411
- for url in urls {
412
- if let imageData = try ? Data ( contentsOf : url ) {
413
- if NSImage ( data : imageData ) != nil {
414
- foundImages . append ( imageData )
415
- NSLog ( " Loaded image data from file: \( url . lastPathComponent ) " )
416
- }
408
+ ]
409
+
410
+ if let urls = generalPasteboard . readObjects ( forClasses : classes , options : options ) as? [ URL ] {
411
+ for url in urls {
412
+ if let imageData = try ? Data ( contentsOf : url ) {
413
+ if NSImage ( data : imageData ) != nil {
414
+ foundImages . append ( imageData )
415
+ NSLog ( " Loaded image data from file: \( url . lastPathComponent ) " )
417
416
}
418
417
}
419
418
}
419
+ }
420
+
421
+ // If no file URLs found, check for direct image data
422
+ if foundImages. isEmpty {
423
+ let supportedImageTypes = [
424
+ NSPasteboard . PasteboardType ( " public.png " ) ,
425
+ NSPasteboard . PasteboardType ( " public.jpeg " ) ,
426
+ NSPasteboard . PasteboardType ( " public.tiff " ) ,
427
+ NSPasteboard . PasteboardType ( " com.compuserve.gif " ) ,
428
+ NSPasteboard . PasteboardType ( " public.image " )
429
+ ]
420
430
421
- // If no file URLs found, check for direct image data
422
- if foundImages. isEmpty {
423
- let supportedImageTypes = [
424
- NSPasteboard . PasteboardType ( " public.png " ) ,
425
- NSPasteboard . PasteboardType ( " public.jpeg " ) ,
426
- NSPasteboard . PasteboardType ( " public.tiff " ) ,
427
- NSPasteboard . PasteboardType ( " com.compuserve.gif " ) ,
428
- NSPasteboard . PasteboardType ( " public.image " )
429
- ]
430
-
431
- for type in supportedImageTypes {
432
- if let data = generalPasteboard. data ( forType: type) {
433
- foundImages. append ( data)
434
- NSLog ( " Found direct image data of type: \( type) " )
435
- break
436
- }
431
+ for type in supportedImageTypes {
432
+ if let data = generalPasteboard. data ( forType: type) {
433
+ foundImages. append ( data)
434
+ NSLog ( " Found direct image data of type: \( type) " )
435
+ break
437
436
}
438
437
}
439
-
440
- // Get any text content
441
- selectedText = generalPasteboard. string ( forType: . string) ?? " "
442
-
443
- // Restore original pasteboard contents
444
- generalPasteboard. clearContents ( )
445
- if let oldContents = oldContents {
446
- generalPasteboard. setString ( oldContents, forType: . string)
447
- }
448
-
449
- // Update app state and show popup
450
- self . appState. selectedImages = foundImages
451
- self . appState. selectedText = selectedText
452
-
453
- let window = PopupWindow ( appState: self . appState)
454
- window. delegate = self
455
- self . popupWindow = window
456
-
457
- // Set window size based on content
458
- if !selectedText. isEmpty || !foundImages. isEmpty {
459
- window. setContentSize ( NSSize ( width: 400 , height: 400 ) )
460
- } else {
461
- window. setContentSize ( NSSize ( width: 400 , height: 100 ) )
462
- }
463
-
464
- window. positionNearMouse ( )
465
- NSApp . activate ( ignoringOtherApps: true )
466
- window. makeKeyAndOrderFront ( nil )
467
- window. orderFrontRegardless ( )
468
438
}
439
+
440
+ // Get any text content
441
+ selectedText = generalPasteboard. string ( forType: . string) ?? " "
442
+
443
+ // Restore original pasteboard contents
444
+ generalPasteboard. clearContents ( )
445
+ if let oldContents = oldContents {
446
+ generalPasteboard. setString ( oldContents, forType: . string)
447
+ }
448
+
449
+ // Update app state and show popup
450
+ self . appState. selectedImages = foundImages
451
+ self . appState. selectedText = selectedText
452
+
453
+ let window = PopupWindow ( appState: self . appState)
454
+ window. delegate = self
455
+ self . popupWindow = window
456
+
457
+ // Set window size based on content
458
+ if !selectedText. isEmpty || !foundImages. isEmpty {
459
+ window. setContentSize ( NSSize ( width: 400 , height: 400 ) )
460
+ } else {
461
+ window. setContentSize ( NSSize ( width: 400 , height: 100 ) )
462
+ }
463
+
464
+ window. positionNearMouse ( )
465
+ NSApp . activate ( ignoringOtherApps: true )
466
+ window. makeKeyAndOrderFront ( nil )
467
+ window. orderFrontRegardless ( )
469
468
}
470
469
}
470
+ }
471
471
472
472
// Closes and cleans up the popup window
473
473
private func closePopupWindow( ) {
@@ -512,7 +512,7 @@ extension AppDelegate {
512
512
513
513
// Register services provider
514
514
NSApp . servicesProvider = self
515
-
515
+
516
516
// Register the service
517
517
NSUpdateDynamicServices ( )
518
518
}
0 commit comments