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.

Return to Home when a volume is unmounted on macOS

sunzhaozheng58e00bd8acf0
diff --git a/macos/Sources/RustyFiles/BrowserWindowController.swift b/macos/Sources/RustyFiles/BrowserWindowController.swift
index 19e1ae81..273bb70a 100644
--- a/macos/Sources/RustyFiles/BrowserWindowController.swift
+++ b/macos/Sources/RustyFiles/BrowserWindowController.swift
@@ -1011,17 +1011,53 @@ final class BrowserWindowController: NSWindowController, NSWindowDelegate, NSMen
refreshPane(t)
}
- /// Ejects a disk; a tab showing it goes Home.
- func eject(_ volume: URL) {
- let path = volume.standardizedFileURL.path
- for t in tabs where t.url.path == path || t.url.path.hasPrefix(path + "/") {
- if t === tab { goHome(nil) } else { t.url = FileManager.default.homeDirectoryForCurrentUser }
+ /// Move views of an unmounted volume to Home, including inactive tabs and the other pane.
+ func volumeDidUnmount(_ volume: URL) {
+ guard volume.isFileURL else { return }
+ let mount = volume.standardizedFileURL.path
+ guard mount != "/" else { return }
+ func isOnVolume(_ url: URL) -> Bool {
+ guard url.isFileURL else { return false }
+ let path = url.standardizedFileURL.path
+ return path == mount || path.hasPrefix(mount + "/")
+ }
+ func isAffected(_ t: BrowserTab) -> Bool {
+ isOnVolume(t.diskRoot ?? t.searchSpec?.root ?? ArchivePlace.locate(t.url)?.archive ?? t.url)
+ }
+ var changed = false
+ for t in tabs.flatMap({ [$0] + ($0.right.map { [$0] } ?? []) }) where isAffected(t) {
+ t.search?.cancel()
+ t.search = nil
+ t.url = BrowserTab.homeURL
+ t.folder = nil
+ t.archive = nil
+ t.error = nil
+ t.selection = []
+ t.scrollY = nil
+ t.forward.removeAll()
+ t.back.removeAll(where: isOnVolume)
+ if t === tab { navBar.omnibar.end() }
+ load(t)
+ changed = true
}
+ if changed { refreshTabs() }
+ }
+
+ /// Failed ejects don't change the current location; successful unmounts notify all windows.
+ func eject(_ volume: URL) {
+ // The directory watcher holds an open descriptor that can prevent ejecting.
watcher = nil
Task.detached(priority: .userInitiated) {
let result = Result { try NSWorkspace.shared.unmountAndEjectDevice(at: volume) }
- if case .failure(let e) = result {
- await MainActor.run { [weak self] in self?.showError(e.localizedDescription) }
+ await MainActor.run { [weak self] in
+ switch result {
+ case .success:
+ for controller in AppDelegate.shared.controllers { controller.volumeDidUnmount(volume) }
+ self?.watch()
+ case .failure(let error):
+ self?.watch()
+ self?.showError(error.localizedDescription)
+ }
}
}
}
diff --git a/macos/Sources/RustyFiles/Finderlike.swift b/macos/Sources/RustyFiles/Finderlike.swift
index cb5c5021..86e849d6 100644
--- a/macos/Sources/RustyFiles/Finderlike.swift
+++ b/macos/Sources/RustyFiles/Finderlike.swift
@@ -115,6 +115,12 @@ enum MountWatcher {
guard let url = note.userInfo?[NSWorkspace.volumeURLUserInfoKey] as? URL else { return }
MainActor.assumeIsolated { mounted(url) }
}
+ NSWorkspace.shared.notificationCenter.addObserver(forName: NSWorkspace.didUnmountNotification, object: nil, queue: .main) { note in
+ guard let url = note.userInfo?[NSWorkspace.volumeURLUserInfoKey] as? URL else { return }
+ MainActor.assumeIsolated {
+ for controller in AppDelegate.shared.controllers { controller.volumeDidUnmount(url) }
+ }
+ }
}
private static func mounted(_ url: URL) {