|
1 | | -import Cocoa |
| 1 | +import SwiftUI |
2 | 2 | import WebKit |
3 | 3 |
|
4 | | -class AppDelegate: NSObject, NSApplicationDelegate, WKNavigationDelegate, WKDownloadDelegate, WKUIDelegate { |
5 | | - var window: NSWindow! |
6 | | - var webView: WKWebView! |
7 | | - var port: String = "7777" |
| 4 | +@main |
| 5 | +struct PeergosApp: App { |
| 6 | + static let name = "Peergos" |
| 7 | + static let port = ProcessInfo.processInfo.environment["PEERGOS_PORT"] ?? "7777"; |
8 | 8 |
|
9 | | - func applicationDidFinishLaunching(_ notification: Notification) { |
10 | | - let args = CommandLine.arguments |
11 | | - if args.count > 1 { |
12 | | - port = args[1] |
13 | | - } |
14 | | - |
15 | | - window = NSWindow( |
16 | | - contentRect: NSRect(x: 0, y: 0, width: 1280, height: 900), |
17 | | - styleMask: [.titled, .closable, .miniaturizable, .resizable], |
18 | | - backing: .buffered, |
19 | | - defer: false |
20 | | - ) |
21 | | - window.title = "Peergos" |
22 | | - window.center() |
| 9 | + @NSApplicationDelegateAdaptor private var appDelegate: AppDelegate |
23 | 10 |
|
24 | | - webView = WKWebView(frame: window.contentView!.bounds) |
25 | | - webView.autoresizingMask = [.width, .height] |
26 | | - webView.navigationDelegate = self |
27 | | - webView.uiDelegate = self |
| 11 | + var body: some Scene { |
| 12 | + WindowGroup(Self.name) { |
| 13 | + PeergosWebView() |
| 14 | + }.defaultSize(width: 1280, height: 900) |
| 15 | + } |
| 16 | +} |
28 | 17 |
|
29 | | - window.contentView?.addSubview(webView) |
| 18 | +class AppDelegate: NSObject, NSApplicationDelegate { |
| 19 | + func applicationDidFinishLaunching(_ notification: Notification) { |
| 20 | + let app = NSApplication.shared; |
| 21 | + app.setActivationPolicy(.regular) |
| 22 | + app.activate() |
| 23 | + } |
| 24 | +} |
30 | 25 |
|
31 | | - webView.load(URLRequest(url: URL(string: "http://localhost:\(port)")!)) |
| 26 | +struct PeergosWebView: NSViewRepresentable { |
| 27 | + func makeCoordinator() -> Coordinator { |
| 28 | + Coordinator() |
| 29 | + } |
32 | 30 |
|
33 | | - window.makeKeyAndOrderFront(nil) |
| 31 | + func makeNSView(context: Context) -> WKWebView { |
| 32 | + let webView = WKWebView() |
| 33 | + webView.navigationDelegate = context.coordinator |
| 34 | + webView.uiDelegate = context.coordinator |
| 35 | + webView.load(URLRequest(url: URL(string: "http://localhost:\(PeergosApp.port)")!)) |
| 36 | + return webView |
34 | 37 | } |
35 | 38 |
|
36 | | - func applicationWillTerminate(_ notification: Notification) { |
37 | | - NSApplication.shared.terminate(self) |
| 39 | + func updateNSView(_ uiView: WKWebView, context: Context) { |
38 | 40 | } |
39 | 41 |
|
40 | | - // MARK: - WKNavigationDelegate |
| 42 | + class Coordinator: NSObject, WKNavigationDelegate, WKDownloadDelegate, WKUIDelegate { |
| 43 | + // MARK: - WKNavigationDelegate |
41 | 44 |
|
42 | | - // Handle <a download> links and Content-Disposition: attachment navigation actions |
43 | | - func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) { |
44 | | - if #available(macOS 11.3, *) { |
45 | | - if navigationAction.shouldPerformDownload { |
46 | | - decisionHandler(.download) |
47 | | - return |
| 45 | + // Handle <a download> links and Content-Disposition: attachment navigation actions |
| 46 | + func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) { |
| 47 | + if #available(macOS 11.3, *) { |
| 48 | + if navigationAction.shouldPerformDownload { |
| 49 | + decisionHandler(.download) |
| 50 | + return |
| 51 | + } |
48 | 52 | } |
| 53 | + decisionHandler(.allow) |
49 | 54 | } |
50 | | - decisionHandler(.allow) |
51 | | - } |
52 | 55 |
|
53 | | - // Handle responses that should be downloaded rather than rendered |
54 | | - func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) { |
55 | | - if #available(macOS 11.3, *) { |
56 | | - let isAttachment = (navigationResponse.response as? HTTPURLResponse) |
57 | | - .flatMap { $0.value(forHTTPHeaderField: "Content-Disposition") } |
58 | | - .map { $0.lowercased().hasPrefix("attachment") } ?? false |
59 | | - if isAttachment || !navigationResponse.canShowMIMEType { |
60 | | - decisionHandler(.download) |
61 | | - return |
| 56 | + // Handle responses that should be downloaded rather than rendered |
| 57 | + func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) { |
| 58 | + if #available(macOS 11.3, *) { |
| 59 | + let isAttachment = (navigationResponse.response as? HTTPURLResponse) |
| 60 | + .flatMap { $0.value(forHTTPHeaderField: "Content-Disposition") } |
| 61 | + .map { $0.lowercased().hasPrefix("attachment") } ?? false |
| 62 | + if isAttachment || !navigationResponse.canShowMIMEType { |
| 63 | + decisionHandler(.download) |
| 64 | + return |
| 65 | + } |
62 | 66 | } |
| 67 | + decisionHandler(.allow) |
63 | 68 | } |
64 | | - decisionHandler(.allow) |
65 | | - } |
66 | 69 |
|
67 | | - // Called when a navigation action becomes a download (macOS 11.3+) |
68 | | - @available(macOS 11.3, *) |
69 | | - func webView(_ webView: WKWebView, navigationAction: WKNavigationAction, didBecome download: WKDownload) { |
70 | | - download.delegate = self |
71 | | - } |
| 70 | + // Called when a navigation action becomes a download (macOS 11.3+) |
| 71 | + @available(macOS 11.3, *) |
| 72 | + func webView(_ webView: WKWebView, navigationAction: WKNavigationAction, didBecome download: WKDownload) { |
| 73 | + download.delegate = self |
| 74 | + } |
72 | 75 |
|
73 | | - // Called when a navigation response becomes a download (macOS 11.3+) |
74 | | - @available(macOS 11.3, *) |
75 | | - func webView(_ webView: WKWebView, navigationResponse: WKNavigationResponse, didBecome download: WKDownload) { |
76 | | - download.delegate = self |
77 | | - } |
| 76 | + // Called when a navigation response becomes a download (macOS 11.3+) |
| 77 | + @available(macOS 11.3, *) |
| 78 | + func webView(_ webView: WKWebView, navigationResponse: WKNavigationResponse, didBecome download: WKDownload) { |
| 79 | + download.delegate = self |
| 80 | + } |
78 | 81 |
|
79 | | - // MARK: - WKUIDelegate |
| 82 | + // MARK: - WKUIDelegate |
80 | 83 |
|
81 | | - // Present the native file picker for <input type="file"> |
82 | | - func webView(_ webView: WKWebView, runOpenPanelWith parameters: WKOpenPanelParameters, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping ([URL]?) -> Void) { |
83 | | - let openPanel = NSOpenPanel() |
84 | | - openPanel.allowsMultipleSelection = parameters.allowsMultipleSelection |
85 | | - openPanel.canChooseDirectories = parameters.allowsDirectories |
86 | | - openPanel.canChooseFiles = true |
87 | | - openPanel.begin { result in |
88 | | - completionHandler(result == .OK ? openPanel.urls : nil) |
| 84 | + // Present the native file picker for <input type="file"> |
| 85 | + func webView(_ webView: WKWebView, runOpenPanelWith parameters: WKOpenPanelParameters, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping ([URL]?) -> Void) { |
| 86 | + let openPanel = NSOpenPanel() |
| 87 | + openPanel.allowsMultipleSelection = parameters.allowsMultipleSelection |
| 88 | + openPanel.canChooseDirectories = parameters.allowsDirectories |
| 89 | + openPanel.canChooseFiles = true |
| 90 | + openPanel.begin { result in |
| 91 | + completionHandler(result == .OK ? openPanel.urls : nil) |
| 92 | + } |
89 | 93 | } |
90 | | - } |
91 | 94 |
|
92 | | - // MARK: - WKDownloadDelegate |
| 95 | + // MARK: - WKDownloadDelegate |
93 | 96 |
|
94 | | - @available(macOS 11.3, *) |
95 | | - func download(_ download: WKDownload, decideDestinationUsing response: URLResponse, suggestedFilename: String, completionHandler: @escaping (URL?) -> Void) { |
96 | | - let savePanel = NSSavePanel() |
97 | | - savePanel.nameFieldStringValue = suggestedFilename |
98 | | - savePanel.begin { result in |
99 | | - completionHandler(result == .OK ? savePanel.url : nil) |
| 97 | + @available(macOS 11.3, *) |
| 98 | + func download(_ download: WKDownload, decideDestinationUsing response: URLResponse, suggestedFilename: String, completionHandler: @escaping (URL?) -> Void) { |
| 99 | + let savePanel = NSSavePanel() |
| 100 | + savePanel.nameFieldStringValue = suggestedFilename |
| 101 | + savePanel.begin { result in |
| 102 | + completionHandler(result == .OK ? savePanel.url : nil) |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + @available(macOS 11.3, *) |
| 107 | + func download(_ download: WKDownload, didFailWithError error: Error, resumeData: Data?) { |
| 108 | + let alert = NSAlert() |
| 109 | + alert.messageText = "Download Failed" |
| 110 | + alert.informativeText = error.localizedDescription |
| 111 | + alert.runModal() |
100 | 112 | } |
101 | | - } |
102 | 113 |
|
103 | | - @available(macOS 11.3, *) |
104 | | - func download(_ download: WKDownload, didFailWithError error: Error, resumeData: Data?) { |
105 | | - let alert = NSAlert() |
106 | | - alert.messageText = "Download Failed" |
107 | | - alert.informativeText = error.localizedDescription |
108 | | - alert.runModal() |
109 | 114 | } |
110 | 115 | } |
111 | | - |
112 | | -// Bootstrap the app |
113 | | -let app = NSApplication.shared |
114 | | -let delegate = AppDelegate() |
115 | | -app.delegate = delegate |
116 | | -app.run() |
0 commit comments