Skip to content

Instantly share code, notes, and snippets.

@TBD
Created February 13, 2018 14:11
Show Gist options
  • Save TBD/87cb166eaa7e1705a2d16abf85278eae to your computer and use it in GitHub Desktop.
Save TBD/87cb166eaa7e1705a2d16abf85278eae to your computer and use it in GitHub Desktop.
dotgrid swift

dotgrid swift

what

WKWebview shell for dotgrid macOS app

needs

  • Swift 4 compiler
  • strip – to make the app even smaller

todo

[ ] remove that annoying beep when keypress

[ ] drag/drop on main window (maybe via JS injected)

[ ] touchbar support (exposed to JS)

[ ] clipboard support (exposed to JS)

[ ] load/save dialogs

// swiftc -o dotgrid dotgrid.swift -target x86_64-apple-macosx10.12
// strip -x dotgrid
import Cocoa
import WebKit
class WindowController: NSWindowController {
}
class AppDelegate: NSObject {
var mainWindow: NSWindow?
var webView: WKWebView!
}
extension AppDelegate: NSApplicationDelegate, WKUIDelegate {
@objc func quit(sender: NSMenuItem) {
NSApp.terminate(self)
}
func applicationDidFinishLaunching(_ aNotification: Notification) {
// --- create app menu items
let mainMenu = NSMenu()
let mainMenuFileItem = NSMenuItem(title: "File", action: nil, keyEquivalent: "")
let fileMenu = NSMenu(title: "File")
fileMenu.addItem(withTitle: "Quit", action: #selector(quit(sender:)), keyEquivalent: "q")
mainMenuFileItem.submenu = fileMenu
mainMenu.addItem(mainMenuFileItem)
NSApp.mainMenu = mainMenu
// --- create main app window
let window = NSWindow(contentRect: NSMakeRect(274, 145, 880, 660),
styleMask:[.titled, .closable, .resizable],
backing: .buffered,
defer: false)
window.backgroundColor = NSColor(calibratedRed: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
window.title = "dotgrid"
window.isMovableByWindowBackground = true
window.orderFrontRegardless()
window.center()
mainWindow = window
let url = NSURL(string : "http://chis.cloud/dotgrid")
let request = URLRequest(url: url! as URL)
// --- add JS and DevTools support
let webConfiguration = WKWebViewConfiguration()
webConfiguration.preferences.setValue(true, forKey: "developerExtrasEnabled")
webConfiguration.preferences.javaScriptEnabled = true
webView = WKWebView(frame: window.frame, configuration: webConfiguration)
webView.load(request)
// -- make WKWebView fill the entire window
window.contentView = webView
NSApp.activate(ignoringOtherApps: true)
}
func applicationWillTerminate(_ aNotification: Notification) {
}
func applicationShouldTerminateAfterLastWindowClosed(_ app: NSApplication) -> Bool{
return true
}
}
let app = NSApplication.shared
app.delegate = AppDelegate()
app.setActivationPolicy(.regular)
atexit_b { app.setActivationPolicy(.prohibited); return }
app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment