Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 48 additions & 13 deletions Sources/airdrop/AirDropCLI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class AirDropCLI: NSObject, NSApplicationDelegate, NSSharingServiceDelegate {
private var individualSharingSuccessful = 0
private var individualSharingFailed = 0
private var sharingStartTime: Date?
private var sharingSourceWindow: NSWindow?
private var sharingCompleted = false
private var windowCloseObserver: NSObjectProtocol?

func applicationDidFinishLaunching(_ aNotification: Notification) {
let argCount = Int(CommandLine.argc)
Expand Down Expand Up @@ -135,6 +138,8 @@ class AirDropCLI: NSObject, NSApplicationDelegate, NSSharingServiceDelegate {


func sharingService(_ sharingService: NSSharingService, didShareItems items: [Any]) {
sharingCompleted = true
closeSharingSourceWindow()
if isIndividualSharing {
individualSharingSuccessful += 1
guard let service: NSSharingService = NSSharingService(named: .sendViaAirDrop) else {
Expand All @@ -148,10 +153,12 @@ class AirDropCLI: NSObject, NSApplicationDelegate, NSSharingServiceDelegate {
}

func sharingService(_ sharingService: NSSharingService, didFailToShareItems items: [Any], error: Error) {
sharingCompleted = true
closeSharingSourceWindow()
if isIndividualSharing {
individualSharingFailed += 1
consoleIO.writeMessage("Failed to share item: \(error.localizedDescription)", to: .error)

guard let service: NSSharingService = NSSharingService(named: .sendViaAirDrop) else {
exit(2)
}
Expand All @@ -167,18 +174,46 @@ class AirDropCLI: NSObject, NSApplicationDelegate, NSSharingServiceDelegate {
}

func sharingService(_ sharingService: NSSharingService, sourceWindowForShareItems items: [Any], sharingContentScope: UnsafeMutablePointer<NSSharingService.SharingContentScope>) -> NSWindow? {
let airDropMenuWindow = NSWindow(contentRect: .init(origin: .zero,
size: .init(width: 1,
height: 1)),
styleMask: [.closable],
backing: .buffered,
defer: false)

airDropMenuWindow.center()
airDropMenuWindow.level = .popUpMenu
airDropMenuWindow.makeKeyAndOrderFront(nil)

return airDropMenuWindow
let window = NSWindow(
contentRect: .init(origin: .zero, size: .init(width: 1, height: 1)),
styleMask: [.borderless],
backing: .buffered,
defer: false
)
window.center()
window.level = .popUpMenu
window.alphaValue = 0
window.isOpaque = false
window.backgroundColor = .clear
window.orderFront(nil)
sharingSourceWindow = window

// When the AirDrop picker is dismissed without selecting a recipient,
// no delegate method fires. Observe for the picker window closing so we can exit cleanly.
windowCloseObserver = NotificationCenter.default.addObserver(
forName: NSWindow.willCloseNotification,
object: nil,
queue: .main
) { [weak self] notification in
guard let self = self,
let closingWindow = notification.object as? NSWindow,
closingWindow !== self.sharingSourceWindow,
!self.sharingCompleted else { return }
// The AirDrop picker window closed without a share completing — user cancelled.
self.closeSharingSourceWindow()
exit(0)
}

return window
}

private func closeSharingSourceWindow() {
if let observer = windowCloseObserver {
NotificationCenter.default.removeObserver(observer)
windowCloseObserver = nil
}
sharingSourceWindow?.close()
sharingSourceWindow = nil
}

private func shareItemsIndividually(service: NSSharingService, _ items: [URL]) {
Expand Down