Skip to content

Commit afba1e5

Browse files
committed
fix mac Os webview close behaviour
1 parent 32fe312 commit afba1e5

File tree

3 files changed

+89
-90
lines changed

3 files changed

+89
-90
lines changed

packager/MacWebview.swift

Lines changed: 87 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,116 +1,115 @@
1-
import Cocoa
1+
import SwiftUI
22
import WebKit
33

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";
88

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
2310

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+
}
2817

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+
}
3025

31-
webView.load(URLRequest(url: URL(string: "http://localhost:\(port)")!))
26+
struct PeergosWebView: NSViewRepresentable {
27+
func makeCoordinator() -> Coordinator {
28+
Coordinator()
29+
}
3230

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
3437
}
3538

36-
func applicationWillTerminate(_ notification: Notification) {
37-
NSApplication.shared.terminate(self)
39+
func updateNSView(_ uiView: WKWebView, context: Context) {
3840
}
3941

40-
// MARK: - WKNavigationDelegate
42+
class Coordinator: NSObject, WKNavigationDelegate, WKDownloadDelegate, WKUIDelegate {
43+
// MARK: - WKNavigationDelegate
4144

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+
}
4852
}
53+
decisionHandler(.allow)
4954
}
50-
decisionHandler(.allow)
51-
}
5255

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+
}
6266
}
67+
decisionHandler(.allow)
6368
}
64-
decisionHandler(.allow)
65-
}
6669

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+
}
7275

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+
}
7881

79-
// MARK: - WKUIDelegate
82+
// MARK: - WKUIDelegate
8083

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+
}
8993
}
90-
}
9194

92-
// MARK: - WKDownloadDelegate
95+
// MARK: - WKDownloadDelegate
9396

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()
100112
}
101-
}
102113

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()
109114
}
110115
}
111-
112-
// Bootstrap the app
113-
let app = NSApplication.shared
114-
let delegate = AppDelegate()
115-
app.delegate = delegate
116-
app.run()

packager/PackagePeergos.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public static void main(String[] a) throws Exception {
102102
"--add-launcher", "peergos=windows-cli.properties"
103103
);
104104
else if (isMac) {
105-
runCommand("swiftc", "-o", "PeergosWebView", "MacWebview.swift", "-framework", "Cocoa", "-framework", "WebKit");
105+
runCommand("swiftc", "-parse-as-library", "-o", "PeergosWebView", "MacWebview.swift", "-framework", "Cocoa", "-framework", "WebKit");
106106
runCommand("security", "find-identity", "-v", "-p", "codesigning", System.getenv("RUNNER_TEMP") + "/app-signing.keychain-db");
107107
runCommand("codesign", "--force", "--options", "runtime", "--timestamp",
108108
"--keychain", System.getenv("RUNNER_TEMP") + "/app-signing.keychain-db",

peergos

0 commit comments

Comments
 (0)