Last active
January 30, 2024 11:00
-
-
Save lucamarrocco/2b06c92e4e6df01de04b to your computer and use it in GitHub Desktop.
swift osx application without nib
This file contains 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 | |
class WindowController: NSWindowController { | |
} | |
class AppDelegate: NSObject { | |
var mainWindow: NSWindow? | |
var mainController: NSWindowController? | |
} | |
extension AppDelegate { | |
@objc func quit(sender: NSMenuItem) { | |
NSApp.terminate(self) | |
} | |
} | |
extension AppDelegate: NSApplicationDelegate { | |
func applicationDidFinishLaunching(_ aNotification: Notification) { | |
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 | |
let window = NSWindow(contentRect: NSMakeRect(1280, 960, 1280, 960), styleMask:[.titled, .closable, .resizable], backing: .buffered, defer: false) | |
window.backgroundColor = NSColor(calibratedRed: 0.1, green: 0.1, blue: 0.1, alpha: 1.0) | |
window.title = "App" | |
window.orderFrontRegardless() | |
mainWindow = window | |
let controller = WindowController(window: window) | |
controller.showWindow(window) | |
mainController = controller | |
NSApp.activate(ignoringOtherApps: true) | |
} | |
func applicationWillTerminate(_ aNotification: Notification) { | |
} | |
func applicationShouldTerminateAfterLastWindowClosed(_ app: NSApplication) -> Bool{ | |
return true | |
} | |
} | |
let app = NSApplication.shared | |
let delegate = AppDelegate() | |
app.delegate = delegate | |
app.setActivationPolicy(.regular) | |
atexit_b { app.setActivationPolicy(.prohibited); return } | |
app.run() |
This file contains 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
.PHONY: build | |
build: | |
@DEVELOPER_DIR="/Applications/Xcode.app" SDKROOT="/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk" xcrun swiftc -o app app.swift && ./app |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
WindowController
what is this?