Last active
April 21, 2022 18:21
-
-
Save alladinian/0f6bd22fb7591a9b1782c1fa3ef4f24c to your computer and use it in GitHub Desktop.
A super simple Cocoa App boilerplate for SwiftUI prototyping _without_ Xcode
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
import Cocoa | |
import SwiftUI | |
func startApp(content: NSView) { | |
let app = NSApplication.shared | |
let menubar = NSMenu() | |
let appMenuItem = NSMenuItem() | |
let appMenu = NSMenu() | |
let appName = ProcessInfo.processInfo.processName | |
let quitTitle = "Quit \(appName)" | |
let quitMenuItem = NSMenuItem(title: quitTitle, action: #selector(NSApplication.terminate), keyEquivalent: "q") | |
let windowRect = NSRect(x: 0, y: 0, width: 480, height: 300) | |
let styleMask: NSWindow.StyleMask = [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView] | |
let window = NSWindow(contentRect: windowRect, styleMask: styleMask, backing: .buffered, defer: false) | |
app.setActivationPolicy(.regular) | |
menubar.addItem(appMenuItem) | |
app.mainMenu = menubar | |
appMenu.addItem(quitMenuItem) | |
appMenuItem.submenu = appMenu | |
window.center() | |
window.title = appName | |
window.contentView = content | |
window.makeKeyAndOrderFront(nil) | |
app.run() | |
} | |
struct ContentView: View { | |
var body: some View { | |
Text("Hello") | |
.font(.largeTitle) | |
.frame(maxWidth: .infinity, maxHeight: .infinity) | |
} | |
} | |
let content = NSHostingView(rootView: ContentView()) | |
startApp(content: content) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to run this?