示例工程 41-system-integration

41-system-integration/Sources/Ch41App/App.swift

swift · 1.5 KB

import Ch41Kit
import SwiftUI
import UniformTypeIdentifiers
 
@main
struct Ch41App: App {
    @State private var document = TextDocument(text: "示例内容")
    @State private var showingImporter = false
    @State private var lastURL: URL?
 
    var body: some Scene {
        WindowGroup("系统集成") {
            VStack(spacing: 14) {
                IntegrationBoard()
                HStack {
                    Button("导入") { showingImporter = true }
                    Button("复制") {
                        NSPasteboard.general.clearContents()
                        NSPasteboard.general.setString(document.text, forType: .string)
                    }
                }
                .buttonStyle(.bordered)
                if let lastURL {
                    Text(lastURL.lastPathComponent)
                        .font(.system(size: 11, design: .monospaced))
                }
            }
            .padding(20)
            .frame(minWidth: 420, minHeight: 460)
            .fileImporter(
                isPresented: $showingImporter,
                allowedContentTypes: TextDocument.readableContentTypes
            ) { result in
                if case .success(let url) = result {
                    lastURL = url
                }
            }
            .onOpenURL { url in
                if let route = DeepLink.parse(url) {
                    lastURL = URL(string: "myapp://\(route)") ?? url
                }
            }
        }
    }
}