SPB Git

Repository Public

SPDriveApp.swift 6.6 KB 114 lines swift

1// SP Drive iOS2// File: App/SPDriveApp.swift3// Description: Entry point: one app model for every window, background transfer and background task plumbing, scene lifecycle.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import SwiftUI8import UIKit9import UserNotifications1011@main12struct SPDriveApp: App {13    @UIApplicationDelegateAdaptor(AppDelegate.self) private var delegate14    @Environment(\.scenePhase) private var scenePhase1516    var body: some Scene {17        WindowGroup {18            RootView()19                .environment(delegate.model)20                .task { await delegate.model.start() }21                .onOpenURL { delegate.model.open(url: $0) }22        }23        .onChange(of: scenePhase) { _, phase in24            switch phase {25            case .active: Task { await delegate.model.sceneBecameActive() }26            case .background: delegate.model.sceneEnteredBackground()27            default: break28            }29        }30        .commands { DriveCommands() }3132        // iPad: a folder in a window of its own (« Ouvrir dans une nouvelle fenêtre », ⌥⌘N)33        WindowGroup("SP Drive", id: "browser", for: String.self) { $folderID in34            BrowserWindow(folderID: folderID ?? "")35                .environment(delegate.model)36                .task { await delegate.model.start() }37        }38        .commands { DriveCommands() }39    }40}4142final class AppDelegate: NSObject, UIApplicationDelegate, UNUserNotificationCenterDelegate {43    let model = AppModel()44    /// For the app-hosted tests, which need the running app's model.45    nonisolated(unsafe) static weak var current: AppDelegate?4647    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {48        AppDelegate.current = self49        BackgroundWork.register(model: model)50        UNUserNotificationCenter.current().delegate = self51        return true52    }5354    /// The system relaunched the app because background transfers finished: reconnect the session so their results are processed.55    func application(_ application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: @escaping () -> Void) {56        nonisolated(unsafe) let done = completionHandler57        model.background.setSystemCompletion { done() }58        Task { await model.start() }59    }6061    nonisolated func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification) async -> UNNotificationPresentationOptions { [.banner, .list] }62}6364/// Menu-bar commands for iPad with a hardware keyboard (and the ⌘-hold overlay). They are delivered to the front window through notifications.65struct DriveCommands: Commands {66    private func send(_ c: DriveCommand) { NotificationCenter.default.post(name: .driveCommand, object: c) }67    var body: some Commands {68        CommandGroup(replacing: .newItem) {69            Button("Nouveau dossier") { send(.newFolder) }.keyboardShortcut("n", modifiers: [.command, .shift])70            Button("Nouvelle fenêtre") { send(.newWindow) }.keyboardShortcut("n", modifiers: [.command, .option])71            Divider()72            Button("Importer des fichiers…") { send(.importFiles) }.keyboardShortcut("o", modifiers: .command)73            Button("Numériser un document…") { send(.scan) }.keyboardShortcut("d", modifiers: [.command, .shift])74        }75        CommandMenu("Drive") {76            Button("Synchroniser maintenant") { send(.sync) }.keyboardShortcut("r", modifiers: .command)77            Button("Rechercher") { send(.search) }.keyboardShortcut("f", modifiers: .command)78            Button("Transferts") { send(.transfers) }.keyboardShortcut("t", modifiers: [.command, .shift])79            Divider()80            Button("Sélectionner") { send(.select) }.keyboardShortcut("e", modifiers: .command)81            Button("Tout sélectionner") { send(.selectAll) }.keyboardShortcut("a", modifiers: .command)82            Button("Informations") { send(.inspector) }.keyboardShortcut("i", modifiers: .command)83            Button("Supprimer la sélection") { send(.trashSelection) }.keyboardShortcut(.delete, modifiers: .command)84        }85        CommandMenu("Affichage") {86            Button("Afficher en liste") { send(.list) }.keyboardShortcut("1", modifiers: .command)87            Button("Afficher en grille") { send(.grid) }.keyboardShortcut("2", modifiers: .command)88            Button("Afficher ou masquer la barre latérale") { send(.toggleSidebar) }.keyboardShortcut("s", modifiers: [.command, .control])89            Divider()90            Button("Raccourcis clavier") { send(.shortcuts) }.keyboardShortcut("/", modifiers: .command)91        }92    }93}9495enum DriveCommand { case newFolder, importFiles, sync, search, list, grid, scan, transfers, select, selectAll, inspector, trashSelection, newWindow, toggleSidebar, shortcuts }96extension Notification.Name { static let driveCommand = Notification.Name("ai.spboucher.spdrive.command") }9798/// The shortcuts, as data for the in-app list (⌘/).99struct DriveShortcut: Identifiable, Sendable {100    let keys: String, title: String, symbol: String101    var id: String { keys }102    static let groups: [(title: String, items: [DriveShortcut])] = [103        ("Fichiers", [.init(keys: "⇧⌘N", title: "Nouveau dossier", symbol: "folder.badge.plus"), .init(keys: "⌘O", title: "Importer des fichiers…", symbol: "doc.badge.plus"),104                      .init(keys: "⇧⌘D", title: "Numériser un document…", symbol: "doc.viewfinder"), .init(keys: "⌥⌘N", title: "Nouvelle fenêtre", symbol: "macwindow.badge.plus")]),105        ("Drive", [.init(keys: "⌘R", title: "Synchroniser maintenant", symbol: "arrow.triangle.2.circlepath"), .init(keys: "⌘F", title: "Rechercher", symbol: "magnifyingglass"),106                   .init(keys: "⇧⌘T", title: "Transferts", symbol: "arrow.up.arrow.down.circle"), .init(keys: "⌘E", title: "Sélectionner", symbol: "checkmark.circle"),107                   .init(keys: "⌘A", title: "Tout sélectionner", symbol: "checklist"), .init(keys: "⌘I", title: "Informations", symbol: "info.circle"),108                   .init(keys: "⌘⌫", title: "Supprimer la sélection", symbol: "trash")]),109        ("Affichage", [.init(keys: "⌘1", title: "Afficher en liste", symbol: "list.bullet"), .init(keys: "⌘2", title: "Afficher en grille", symbol: "square.grid.2x2"),110                       .init(keys: "⌃⌘S", title: "Afficher ou masquer la barre latérale", symbol: "sidebar.left"), .init(keys: "⌘/", title: "Raccourcis clavier", symbol: "keyboard"),111                       .init(keys: "⌘S", title: "Enregistrer (éditeur de texte, annotation)", symbol: "square.and.arrow.down")]),112    ]113}114
Files main
Menu

Theme