Skip to content

Commit

Permalink
Enable popup windows during login
Browse files Browse the repository at this point in the history
Signed-off-by: Laurin Brandner <[email protected]>
  • Loading branch information
lbrndnr committed Feb 25, 2021
1 parent 723745a commit 18c63bc
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
5 changes: 5 additions & 0 deletions Nuage/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,10 @@
<string>1</string>
<key>LSMinimumSystemVersion</key>
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
</dict>
</plist>
7 changes: 3 additions & 4 deletions Nuage/Views/LoginView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@ struct LoginView: View {
private var onLogin: (String, Date?) -> ()

var body: some View {
WebView(url: URL(string: "https://soundcloud.com")!)
WebView(url: URL(string: "https://soundcloud.com/signin")!)
.cookie(name: "oauth_token") { cookie in
onLogin(cookie.value, cookie.expiresDate)
}
.frame(minWidth: 300, minHeight: 300)
// .fixedSize()
.padding()
.frame(minWidth: 1000, minHeight: 700)
.navigationTitle("Login")
}

init(onLogin: @escaping (String, Date?) -> ()) {
Expand Down
22 changes: 21 additions & 1 deletion Nuage/Views/WebView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ struct WebView: NSViewRepresentable {
let view = WKWebView(frame: .zero, configuration: conf)
view.load(URLRequest(url: url))
view.navigationDelegate = context.coordinator
view.uiDelegate = context.coordinator

return view
}
Expand All @@ -44,7 +45,7 @@ struct WebView: NSViewRepresentable {

}

class WebViewCoordinator: NSObject, WKNavigationDelegate {
class WebViewCoordinator: NSObject, WKNavigationDelegate, WKUIDelegate {

var handlers = [(String, CookieHandler)]()

Expand All @@ -63,4 +64,23 @@ class WebViewCoordinator: NSObject, WKNavigationDelegate {
decisionHandler(.allow)
}

func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
let view = WKWebView(frame: .zero, configuration: configuration)
view.navigationDelegate = self
view.uiDelegate = self

let size = NSSize(width: windowFeatures.width?.doubleValue ?? 500, height: windowFeatures.height?.doubleValue ?? 500)
let window = NSWindow(contentRect: NSRect(origin: .zero, size: size), styleMask: [.closable, .titled], backing: .buffered, defer: false)
window.isReleasedWhenClosed = false
window.contentView = view
window.makeKeyAndOrderFront(nil)
window.center()

return view
}

func webViewDidClose(_ webView: WKWebView) {
webView.window?.close()
}

}

0 comments on commit 18c63bc

Please sign in to comment.