Skip to content

Instantly share code, notes, and snippets.

@brandonasuncion
Last active August 30, 2024 20:56
Show Gist options
  • Select an option

  • Save brandonasuncion/d763a1da73ebe19c28436da66cca01df to your computer and use it in GitHub Desktop.

Select an option

Save brandonasuncion/d763a1da73ebe19c28436da66cca01df to your computer and use it in GitHub Desktop.
minimal macOS SwiftUI app from complete scratch
import SwiftUI
class AppDelegate: NSObject, NSApplicationDelegate { }
@main
struct CustomApp: App {
@NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
import AppKit
import SwiftUI
private func makeWindow<V: View>(view: V) -> NSWindow {
let window = NSWindow(
contentRect: NSRect(x: 0, y: 0, width: 480, height: 300),
styleMask: [.titled, .closable, .miniaturizable,
.resizable,
.fullSizeContentView],
backing: .buffered,
defer: false
)
window.contentView = NSHostingView(rootView: view)
window.maxSize = NSSize(width: CGFloat.greatestFiniteMagnitude, height: .greatestFiniteMagnitude)
window.appearance = NSAppearance(named: .darkAqua)
window.titlebarAppearsTransparent = true
window.isMovableByWindowBackground = true
return window
}
class AppDelegate<V: View>: NSObject, NSApplicationDelegate {
let view: V
var window: NSWindow!
init(view: V) {
self.view = view
super.init()
}
func applicationWillFinishLaunching(_ notification: Notification) {
}
func applicationDidFinishLaunching(_ aNotification: Notification) {
window = makeWindow(view: view)
window.center()
window.makeKeyAndOrderFront(nil)
}
func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
return true
}
}
let view = VStack {
Text("wow")
Button("hello") {
print("world")
}
}
.frame(width: 500, height: 500)
let app = NSApplication.shared
let delegate = AppDelegate(view: view)
app.delegate = delegate
app.setActivationPolicy(.regular)
app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment