SPB Git

Repository Public

SystemShare.swift 3.2 KB 64 lines swift

1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Sources/Shared/System/SystemShare.swift3// Description: Native sharing of the open message (.eml) and of its attachments through the system share sheet (NSSharingServicePicker on4// macOS, the activity sheet on iOS / iPadOS), offered from the File menu / hardware keyboard. The files shared are the decrypted temporary5// copies the reader already uses; they are removed with the other temporary files.6import SPMailCore7import SwiftUI8#if os(macOS)9import AppKit10#else11import UIKit12#endif1314@MainActor15enum SystemShare {16    static func present(_ urls: [URL]) {17        guard !urls.isEmpty else { return }18        #if os(macOS)19        guard let view = NSApp?.keyWindow?.contentView else { return }20        NSSharingServicePicker(items: urls).show(relativeTo: NSRect(x: view.bounds.midX, y: view.bounds.maxY - 40, width: 1, height: 1), of: view, preferredEdge: .minY)21        #else22        guard let scene = UIApplication.shared.connectedScenes.compactMap({ $0 as? UIWindowScene }).first(where: { $0.activationState == .foregroundActive }), var top = scene.keyWindow?.rootViewController else { return }23        while let next = top.presentedViewController { top = next }24        let sheet = UIActivityViewController(activityItems: urls, applicationActivities: nil)25        sheet.popoverPresentationController?.sourceView = top.view26        sheet.popoverPresentationController?.sourceRect = CGRect(x: top.view.bounds.midX, y: 60, width: 1, height: 1)27        top.present(sheet, animated: true)28        #endif29    }3031    /// The message being read, as an .eml file.32    static func shareMessage(_ scene: MailScene) async {33        guard let email = scene.reader.focusedEmail, let url = try? await scene.emlFile(for: email) else { return }34        present([url])35    }36    /// The attachments of the message being read.37    static func shareAttachments(_ scene: MailScene) async {38        guard let email = scene.reader.focusedEmail, let parts = scene.reader.bodies[email.id]?.attachments, !parts.isEmpty else { return }39        var urls: [URL] = []40        for part in parts { if let url = try? await scene.file(for: part) { urls.append(url) } }41        present(urls)42    }43}4445/// « Partager » in the File menu: the message being read in the frontmost window, and its attachments.46struct SystemCommands: Commands {47    let app: AppModel48    @FocusedValue(MailScene.self) private var focused4950    private var scene: MailScene? { focused ?? app.system.router.activeScene ?? app.main }51    private var email: EmailSummary? { app.phase == .ready ? scene?.reader.focusedEmail : nil }52    private var hasAttachments: Bool { email.flatMap { scene?.reader.bodies[$0.id]?.attachments.isEmpty == false } ?? false }5354    var body: some Commands {55        CommandGroup(after: .importExport) {56            Button("Partager le message…") { if let scene { Task { await SystemShare.shareMessage(scene) } } }57                .keyboardShortcut("s", modifiers: [.command, .option]) // ⌃⌘S is the system's « Show / Hide Sidebar »58                .disabled(email == nil)59            Button("Partager les pièces jointes…") { if let scene { Task { await SystemShare.shareAttachments(scene) } } }60                .disabled(!hasAttachments)61        }62    }63}64
Files main
Menu

Theme