Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: App/Navigator.swift3// Description: Where the person is in one window: sidebar section, current folder and its back/forward history, selection, inline rename, search and filters, the command palette, and which sheet is open. Each window has its own navigator.4import Foundation5import Observation6import SPDriveCore7import SPDriveNext8import SPDriveSync9import SwiftUI1011enum SidebarItem: String, Hashable, CaseIterable, Identifiable {12 case drive, recent, shared, portals, favorites, offline, downloads, transfers, conflicts, trash, storage13 // SP Drive 2.0: shown only when the connected server offers the feature (`AppModel.isAvailable`)14 case collections, sharedWithMe, activity, admin15 var id: String { rawValue }16 var title: String {17 switch self {18 case .drive: "Mon Drive"; case .recent: "Récents"; case .shared: "Partagés"; case .portals: "Portails"; case .favorites: "Favoris"19 case .offline: "Hors ligne"; case .downloads: "Téléchargements"; case .transfers: "Transferts"; case .conflicts: "Conflits"; case .trash: "Corbeille"; case .storage: "Stockage"20 case .collections: "Collections"; case .sharedWithMe: "Partagés avec moi"; case .activity: "Activité"; case .admin: "Administration"21 }22 }23 var symbol: String {24 switch self {25 case .drive: "externaldrive"; case .recent: "clock"; case .shared: "link"; case .portals: "tray.and.arrow.down"; case .favorites: "star"26 case .offline: "arrow.down.circle"; case .downloads: "arrow.down.circle"; case .transfers: "arrow.up.arrow.down"; case .conflicts: "exclamationmark.triangle"; case .trash: "trash"; case .storage: "chart.pie"27 case .collections: "square.stack.3d.up"; case .sharedWithMe: "person.2"; case .activity: "clock.arrow.circlepath"; case .admin: "server.rack"28 }29 }30 /// Sections that show items of the tree in the browser.31 var isBrowser: Bool { [.drive, .recent, .favorites, .offline, .trash].contains(self) }32 /// Sections that exist on every server. The others depend on `GET /meta` (see `AppModel.isAvailable`).33 var isAvailable: Bool { ![.collections, .sharedWithMe, .activity, .admin].contains(self) }34}3536enum SheetKind: Identifiable, Equatable {37 case newFolder(parent: String), rename(DriveItem), move([DriveItem]), share(DriveItem), portal(folder: DriveItem), versions(DriveItem), conflict(SyncConflict)38 case members(DriveItem), collection(SmartCollection?), ask(DriveItem?), suggest([DriveItem])39 var id: String {40 switch self {41 case .newFolder(let p): "new-\(p)"; case .rename(let i): "rename-\(i.id)"; case .move(let i): "move-\(i.map(\.id).joined())"42 case .share(let i): "share-\(i.id)"; case .portal(let f): "portal-\(f.id)"; case .versions(let i): "versions-\(i.id)"; case .conflict(let c): "conflict-\(c.id)"43 case .members(let f): "members-\(f.id)"; case .collection(let c): "collection-\(c?.id ?? "new")"; case .ask(let i): "ask-\(i?.id ?? "drive")"; case .suggest(let i): "suggest-\(i.map(\.id).joined())"44 }45 }46}4748enum TransferFilter: String, CaseIterable, Identifiable { case all, uploads, downloads, issues49 var id: String { rawValue }50 var title: String { switch self { case .all: "Tous"; case .uploads: "Envois"; case .downloads: "Téléchargements"; case .issues: "À voir" } }51}5253@MainActor @Observable final class Navigator {54 enum Direction { case deeper, shallower, lateral }55 var section: SidebarItem = .drive {56 didSet {57 guard section != oldValue else { return }58 if section == .downloads { section = .transfers; transferFilter = .downloads } // one transfer centre; "Téléchargements" is a filter of it59 selection = []; searchText = ""; filter = LocalSearch(); renamingID = nil; direction = .lateral60 }61 }62 /// Folder shown in "Mon Drive"; nil = the root. Set through `go(to:)` to keep the history.63 var folderID: String? { didSet { if folderID != oldValue { selection = []; renamingID = nil } } }64 var selection: Set<String> = []65 /// Where a shift-extended selection started (grid).66 var anchorID: String?67 /// Item whose name is being edited in place.68 var renamingID: String?69 var searchText = ""70 /// Filters of the search (type, extension, dates, sizes, favourites, shared).71 var filter = LocalSearch()72 var searchEverywhere = true73 var sheet: SheetKind?74 var showInspector = false75 var showPalette = false76 var confirmPurge: [DriveItem] = []77 var confirmEmptyTrash = false78 var transferFilter = TransferFilter.all79 /// The smart collection shown by the Collections section.80 var collectionID: String?81 /// Search 2.0: where the server looks (names, content, both) and whether it searches by meaning.82 var searchScope = SearchScope.all83 var searchSemantic = false84 /// How the last folder change moved in the tree: drives the direction of the transition.85 var direction = Direction.lateral86 private(set) var backStack: [String?] = []87 private(set) var forwardStack: [String?] = []8889 /// Shows a folder of Mon Drive (nil = root), remembering where we came from.90 func go(to id: String?, direction d: Direction = .deeper) {91 if section != .drive { section = .drive }92 guard id != folderID else { return }93 backStack.append(folderID); if backStack.count > 100 { backStack.removeFirst() }94 forwardStack.removeAll()95 direction = d; folderID = id96 }97 func open(folder id: String?) { go(to: id, direction: .deeper) }98 var canGoBack: Bool { section == .drive && !backStack.isEmpty }99 var canGoForward: Bool { section == .drive && !forwardStack.isEmpty }100 func back() { guard canGoBack, let prev = backStack.popLast() else { return }; forwardStack.append(folderID); direction = .shallower; folderID = prev }101 func forward() { guard canGoForward, let next = forwardStack.popLast() else { return }; backStack.append(folderID); direction = .deeper; folderID = next }102 var isSearching: Bool { !searchText.trimmingCharacters(in: .whitespaces).isEmpty || !filter.isEmpty }103}104105/// What the menu commands need to know about the focused window to enable themselves. Published as a focused value:106/// that is what makes the menus re-validate (a Commands body does not follow an observable model by itself).107struct WindowFocus: Equatable {108 var section = SidebarItem.drive109 var folderID: String?110 var hasFolder = false111 var selectionCount = 0112 var selectionHasTrashed = false113 var canGoBack = false, canGoForward = false114 var locked = false, paused = false, finderReady = false115}116/// Carries `WindowFocus` to the menu commands as an ObservableObject, which a Commands body is documented to follow.117@MainActor final class MenuBridge: ObservableObject { @Published var focus: WindowFocus? }118private struct NavigatorKey: FocusedValueKey { typealias Value = Navigator }119extension FocusedValues {120 /// The navigator of the window that has the focus (menu commands act on it).121 var spdNavigator: Navigator? { get { self[NavigatorKey.self] } set { self[NavigatorKey.self] = newValue } }122}123