Last active
April 21, 2022 17:37
-
-
Save mattyoung/55dbe9415e7f2e1efabc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env xcrun swift | |
import WebKit | |
let application = NSApplication.sharedApplication() | |
application.setActivationPolicy(.Regular) | |
let window = NSWindow(contentRect: NSMakeRect(0, 0, 800, 600), | |
styleMask: NSTitledWindowMask | | |
NSClosableWindowMask | | |
NSMiniaturizableWindowMask | | |
NSResizableWindowMask, | |
backing: .Buffered, | |
`defer`: false) | |
window.center() | |
window.title = "Minimal Swift WebKit Browser" | |
window.makeKeyAndOrderFront(window) | |
class WindowDelegate: NSObject, NSWindowDelegate { | |
func windowWillClose(notification: NSNotification) { | |
NSApplication.sharedApplication().terminate(0) | |
} | |
func windowDidResize(notification: NSNotification) { | |
print("windowDidResize called") | |
} | |
} | |
let windowDelegate = WindowDelegate() | |
window.delegate = windowDelegate | |
class ApplicationDelegate: NSObject, NSApplicationDelegate { | |
let _window: NSWindow | |
init(window: NSWindow) { | |
self._window = window | |
} | |
func applicationDidFinishLaunching(notification: NSNotification) { | |
let webView = WebView(frame: self._window.contentView!.frame) | |
self._window.contentView!.addSubview(webView) | |
var urlString = Process.arguments.count > 1 ? Process.arguments[1] : "https://www.google.com/ncr" | |
if false == urlString.hasPrefix("http://") && false == urlString.hasPrefix("https://") { | |
urlString = "http://" + urlString | |
} | |
guard let url = NSURL(string: urlString) else { | |
fatalError("Bad URL: \(urlString)") | |
} | |
webView.mainFrame.loadRequest(NSURLRequest(URL: url)) | |
} | |
func applicationShouldTerminateAfterLastWindowClosed(sender: NSApplication) -> Bool { | |
return true | |
} | |
} | |
let applicationDelegate = ApplicationDelegate(window: window) | |
application.delegate = applicationDelegate | |
application.activateIgnoringOtherApps(true) | |
application.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment