mirror of
https://github.com/leonboe1/QuickDrop.git
synced 2026-11-22 22:36:01 +00:00
Add ability to drop files into main window to upload
This commit is contained in:
1 parent
9f2af19adb
commit
dacb1f868a
1 file changed
+42
@@ -7,6 +7,7 @@
|
|||||||
|
|
||||||
import SwiftUI
|
import SwiftUI
|
||||||
import NearbyShare
|
import NearbyShare
|
||||||
|
import UniformTypeIdentifiers
|
||||||
|
|
||||||
struct WelcomeScreen: View {
|
struct WelcomeScreen: View {
|
||||||
|
|
||||||
@@ -83,6 +84,10 @@ struct WelcomeScreen: View {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
.frame(width: 1000, height: 600)
|
.frame(width: 1000, height: 600)
|
||||||
|
.onDrop(of: [.fileURL], isTargeted: nil) { providers in
|
||||||
|
handleDrop(providers: providers)
|
||||||
|
return true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func openPrivacyPolicy() {
|
func openPrivacyPolicy() {
|
||||||
@@ -104,6 +109,43 @@ struct WelcomeScreen: View {
|
|||||||
}
|
}
|
||||||
return "externaldrive.connected.to.line.below.fill"
|
return "externaldrive.connected.to.line.below.fill"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func handleDrop(providers: [NSItemProvider]) {
|
||||||
|
let dispatchGroup = DispatchGroup()
|
||||||
|
var urls: [URL] = []
|
||||||
|
let urlQueue = DispatchQueue(label: "DroppedURLsQueue")
|
||||||
|
|
||||||
|
for provider in providers {
|
||||||
|
if provider.hasItemConformingToTypeIdentifier(UTType.fileURL.identifier) {
|
||||||
|
dispatchGroup.enter()
|
||||||
|
provider.loadItem(forTypeIdentifier: UTType.fileURL.identifier, options: nil) { (item, error) in
|
||||||
|
defer { dispatchGroup.leave() }
|
||||||
|
if let data = item as? Data,
|
||||||
|
let url = NSURL(absoluteURLWithDataRepresentation: data, relativeTo: nil) as URL? {
|
||||||
|
urlQueue.sync {
|
||||||
|
urls.append(url)
|
||||||
|
}
|
||||||
|
} else if let url = item as? URL {
|
||||||
|
urlQueue.sync {
|
||||||
|
urls.append(url)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
dispatchGroup.notify(queue: .main) {
|
||||||
|
if !urls.isEmpty {
|
||||||
|
sendToSharingService(items: urls)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func sendToSharingService(items: [Any]) {
|
||||||
|
if let sharingService = NSSharingService(named: NSSharingService.Name("com.leonboettger.neardrop.ShareExtension")) {
|
||||||
|
sharingService.perform(withItems: items)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user