Repository Public License
1import Foundation2import NavigatorCore3import WebRuntime45#if canImport(AppKit)6import AppKit7#elseif canImport(UIKit)8import UIKit9#endif1011/// Page actions that need the platform: clipboard, printing, PDF export, the Mac share picker.12@MainActor13enum PageActions {14 static func copy(_ text: String) {15 #if os(macOS)16 NSPasteboard.general.clearContents()17 NSPasteboard.general.setString(text, forType: .string)18 #else19 UIPasteboard.general.string = text20 #endif21 }2223 static func print(_ tab: TabModel, in window: WindowModel) async {24 #if os(macOS)25 do { try tab.engine.print(in: window.platformWindow) } catch {26 window.showToast(L("This page cannot be printed"), status: .warning)27 }28 #else29 if (try? await tab.engine.presentPrintController()) == nil {30 window.showToast(L("This page cannot be printed"), status: .warning)31 }32 #endif33 }3435 /// A file name for an export of the page: its title without characters a file system refuses.36 static func exportName(for tab: TabModel) -> String {37 let cleaned = tab.title.components(separatedBy: CharacterSet(charactersIn: "/\\:?%*|\"<>\n\r\t")).joined(separator: " ")38 .trimmingCharacters(in: .whitespacesAndNewlines)39 return String((cleaned.isEmpty ? "Page" : cleaned).prefix(80))40 }4142 static func savePDF(of tab: TabModel, in window: WindowModel) async {43 guard let data = try? await tab.engine.pdfData() else {44 window.showToast(L("This page cannot be saved as PDF"), status: .warning)45 return46 }47 let name = exportName(for: tab) + ".pdf"48 #if os(macOS)49 guard let nsWindow = window.platformWindow else { return }50 let panel = NSSavePanel()51 panel.nameFieldStringValue = name52 panel.allowedContentTypes = [.pdf]53 panel.canCreateDirectories = true54 guard await panel.beginSheetModal(for: nsWindow) == .OK, let url = panel.url else { return }55 let written = await Task.detached { (try? data.write(to: url, options: .atomic)) != nil }.value56 window.showToast(written ? L("PDF saved") : L("The PDF could not be saved"), status: written ? .success : .warning)57 #else58 // On iOS the file goes through the share sheet ("Save to Files", AirDrop, Print…).59 let url = FileManager.default.temporaryDirectory.appending(path: name)60 let written = await Task.detached { (try? data.write(to: url, options: .atomic)) != nil }.value61 if written { window.shareRequest = ShareRequest(url: url, title: tab.title) }62 else { window.showToast(L("The PDF could not be saved"), status: .warning) }63 #endif64 }6566 static func share(_ tab: TabModel, in window: WindowModel) {67 guard let url = tab.engine.url, !InternalAddress.isInternal(url) else { return }68 #if os(macOS)69 guard let view = window.platformWindow?.contentView else { return }70 // Anchored under the toolbar, at the top centre of the window.71 let anchor = NSRect(x: view.bounds.midX - 1, y: view.isFlipped ? 60 : view.bounds.maxY - 60, width: 2, height: 2)72 NSSharingServicePicker(items: [url]).show(relativeTo: anchor, of: view, preferredEdge: .minY)73 #else74 window.shareRequest = ShareRequest(url: url, title: tab.title)75 #endif76 }77}78