Skip to content

A fast file manager for Windows and macOS that never freezes: tabs, dual panes, archives as folders, Quick Look-style previews. A File Explorer and Finder alternative written in Rust.

Mac: drops from other apps arrive as files

gelenmedya351576a77de7
diff --git a/macos/Sources/RustyFiles/FileListController.swift b/macos/Sources/RustyFiles/FileListController.swift
index e8e84c6d..bae02fe7 100644
--- a/macos/Sources/RustyFiles/FileListController.swift
+++ b/macos/Sources/RustyFiles/FileListController.swift
@@ -192,7 +192,7 @@ final class FileListController: NSObject, NSTableViewDataSource, NSTableViewDele
table.action = #selector(singleClicked(_:))
table.setDraggingSourceOperationMask([.copy, .move], forLocal: false)
table.setDraggingSourceOperationMask([.copy, .move], forLocal: true)
- table.registerForDraggedTypes([.fileURL] + ArchiveDrag.draggedTypes)
+ table.registerForDraggedTypes([.fileURL] + ArchiveDrag.draggedTypes + ForeignDrop.types)
scroll.documentView = table
// under the rows (their own drawing is clear where the lens is)
@@ -225,7 +225,7 @@ final class FileListController: NSObject, NSTableViewDataSource, NSTableViewDele
tiles.list = self
tiles.setDraggingSourceOperationMask([.copy, .move], forLocal: false)
tiles.setDraggingSourceOperationMask([.copy, .move], forLocal: true)
- tiles.registerForDraggedTypes([.fileURL] + ArchiveDrag.draggedTypes)
+ tiles.registerForDraggedTypes([.fileURL] + ArchiveDrag.draggedTypes + ForeignDrop.types)
tileScroll.documentView = tiles
tileScroll.drawsBackground = false
tileScroll.hasVerticalScroller = true
@@ -2636,6 +2636,8 @@ extension FileListController {
// out of an archive: written out, then copied here
if ArchiveDrag.carries(info) { return .copy }
let urls = Self.droppedURLs(info)
+ // from another app with no file of its own (a browser's picture, a mail's attachment)
+ if urls.isEmpty, ForeignDrop.carries(info) { return .copy }
// nothing into itself, nothing back where it already is
let d = dest.standardizedFileURL.path
guard !urls.isEmpty, !urls.contains(where: { let s = $0.standardizedFileURL.path; return d == s || d.hasPrefix(s + "/") }) else { return [] }
@@ -2658,6 +2660,15 @@ extension FileListController {
return true
}
let urls = Self.droppedURLs(info)
+ if urls.isEmpty, ForeignDrop.carries(info) {
+ ForeignDrop.receive(info, into: dest) { [weak self] made, error in
+ if let error { self?.onError?(error.localizedDescription) }
+ guard !made.isEmpty else { return }
+ FileUndo.shared.record(.trash(made), title: String(localized: "Drop"))
+ if dest == self?.url { self?.reloadSelecting(made.map(\.lastPathComponent)) }
+ }
+ return true
+ }
if Self.dropAsks() {
askDrop(urls, into: dest, in: viewMode == .details ? table : tiles)
return true
diff --git a/macos/Sources/RustyFiles/ForeignDrop.swift b/macos/Sources/RustyFiles/ForeignDrop.swift
new file mode 100644
index 00000000..27bb0605
--- /dev/null
+++ b/macos/Sources/RustyFiles/ForeignDrop.swift
@@ -0,0 +1,72 @@
+import AppKit
+import UniformTypeIdentifiers
+
+/// What another app drops without a file of its own (Windows 0.9.3: pictures from a browser,
+/// Outlook's attachments): file promises (Mail, Photos, Safari's pictures) are written into the
+/// folder; a bare picture (Chrome) is saved as a PNG; a web link becomes a .webloc.
+@MainActor
+enum ForeignDrop {
+ static let types: [NSPasteboard.PasteboardType] = [.png, .tiff, .URL]
+
+ static func carries(_ info: NSDraggingInfo) -> Bool {
+ let pb = info.draggingPasteboard
+ if ArchiveDrag.current == nil, pb.canReadObject(forClasses: [NSFilePromiseReceiver.self], options: nil) { return true }
+ if pb.data(forType: .png) != nil || pb.data(forType: .tiff) != nil { return true }
+ return webLink(pb) != nil
+ }
+
+ private static func webLink(_ pb: NSPasteboard) -> URL? {
+ guard let u = (pb.readObjects(forClasses: [NSURL.self], options: nil) as? [URL])?.first, !u.isFileURL,
+ let scheme = u.scheme?.lowercased(), scheme == "http" || scheme == "https" else { return nil }
+ return u
+ }
+
+ /// Writes what was dropped into `dest`; `done` gets the new items (on the main thread).
+ static func receive(_ info: NSDraggingInfo, into dest: URL, done: @escaping ([URL], Error?) -> Void) {
+ let pb = info.draggingPasteboard
+ if let receivers = pb.readObjects(forClasses: [NSFilePromiseReceiver.self], options: nil) as? [NSFilePromiseReceiver], !receivers.isEmpty {
+ let queue = OperationQueue()
+ queue.qualityOfService = .userInitiated
+ var made: [URL] = []
+ var firstError: Error?
+ let group = DispatchGroup()
+ for r in receivers {
+ group.enter()
+ r.receivePromisedFiles(atDestination: dest, options: [:], operationQueue: queue) { url, error in
+ DispatchQueue.main.async {
+ if let error { firstError = firstError ?? error } else { made.append(url) }
+ group.leave()
+ }
+ }
+ }
+ group.notify(queue: .main) { done(made, firstError) }
+ return
+ }
+ let name = suggestedName(pb)
+ if let data = pb.data(forType: .png) ?? pb.data(forType: .tiff).flatMap({ NSBitmapImageRep(data: $0)?.representation(using: .png, properties: [:]) }) {
+ let to = FileOps.unique(name ?? String(localized: "Picture"), ext: "png", in: dest)
+ do {
+ try data.write(to: to, options: .withoutOverwriting)
+ done([to], nil)
+ } catch { done([], error) }
+ return
+ }
+ if let link = webLink(pb) {
+ let title = (pb.string(forType: NSPasteboard.PasteboardType("public.url-name")) ?? link.host ?? "Link")
+ .replacingOccurrences(of: "/", with: "-").replacingOccurrences(of: ":", with: "-")
+ let to = FileOps.unique(title, ext: "webloc", in: dest)
+ do {
+ let plist = try PropertyListSerialization.data(fromPropertyList: ["URL": link.absoluteString], format: .xml, options: 0)
+ try plist.write(to: to, options: .withoutOverwriting)
+ done([to], nil)
+ } catch { done([], error) }
+ }
+ }
+
+ /// A picture's name from the link it came from (its last part, without the extension).
+ private static func suggestedName(_ pb: NSPasteboard) -> String? {
+ guard let u = (pb.readObjects(forClasses: [NSURL.self], options: nil) as? [URL])?.first, !u.isFileURL else { return nil }
+ let base = (u.lastPathComponent as NSString).deletingPathExtension
+ return base.isEmpty || base == "/" ? nil : base
+ }
+}