Last active
August 30, 2024 20:56
-
-
Save brandonasuncion/d763a1da73ebe19c28436da66cca01df to your computer and use it in GitHub Desktop.
minimal macOS SwiftUI app from complete scratch
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 SwiftUI | |
| class AppDelegate: NSObject, NSApplicationDelegate { } | |
| @main | |
| struct CustomApp: App { | |
| @NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate | |
| var body: some Scene { | |
| WindowGroup { | |
| ContentView() | |
| } | |
| } | |
| } |
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 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