Skip to content

Instantly share code, notes, and snippets.

@vi4m
Created December 31, 2016 20:09
Show Gist options
  • Save vi4m/7cac8d00fb08e37313126ee6dc114912 to your computer and use it in GitHub Desktop.
Save vi4m/7cac8d00fb08e37313126ee6dc114912 to your computer and use it in GitHub Desktop.
single page macos swift app webview wrap of http://asciiwwdc.com
#!/usr/bin/swift -target x86_64-apple-macosx10.12 -v
import Cocoa
import WebKit
func halfWindow(title: String = "Half Window (^c to quit)") -> NSWindow {
let window = NSWindow()
if let screen = window.screen {
var frame = screen.visibleFrame
let size = CGSize(width: frame.size.width / 2, height: frame.size.height)
frame = CGRect(origin: .zero, size: size)
window.setFrame(frame, display: true)
window.title = title
}
window.orderFrontRegardless()
return window
}
func webView(parent: NSView) -> WKWebView {
var frame = parent.frame
let size = CGSize(width: frame.size.width, height: frame.size.height - 20)
frame = CGRect(origin: .zero, size: size)
let webConfiguration = WKWebViewConfiguration()
let webView = WKWebView(frame: frame, configuration: webConfiguration)
parent.addSubview(webView)
return webView
}
let src = "http://asciiwwdc.com"
if let v = halfWindow(title: src).contentView {
let w = webView(parent: v)
if let url = URL(string: src) {
let request = URLRequest(url: url)
dump(request)
w.load(request)
}
}
NSApp.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment