Skip to content

Instantly share code, notes, and snippets.

@damuellen
Last active July 3, 2022 17:43
Show Gist options
  • Save damuellen/f1a106109d225785d2845f706f2a4dea to your computer and use it in GitHub Desktop.
Save damuellen/f1a106109d225785d2845f706f2a4dea to your computer and use it in GitHub Desktop.
Minimal Swift App
#!/usr/bin/swift
import AppKit
let application = NSApplication.shared
let fileOpenPanel = NSOpenPanel(), fileSavePanel = NSSavePanel()
let window: NSWindow = { window in
window.setContentSize(NSSize(width:800, height:600))
window.styleMask = [.titled, .closable, .miniaturizable]
window.center()
window.title = "Minimal Swift App"
return window
}(NSWindow())
window.makeKeyAndOrderFront(window)
extension NSButton {
static func makeButton(title: String, target: AnyObject?, action: Selector) -> NSButton {
let button = NSButton()
button.title = title
button.target = target
button.action = action
button.bezelStyle = .rounded
return button
}
}
class Delegate: NSObject, NSApplicationDelegate, NSWindowDelegate, NSTextFieldDelegate {
var window: NSWindow
var textField = NSTextField()
var stack = NSStackView()
var imageView = NSImageView()
var mainStackView = NSStackView(frame: NSRect(x: 15, y: 15, width: 770, height: 570))
init(window: NSWindow) {
textField.isEditable = true
self.window = window
super.init()
textField.delegate = self
window.contentView!.addSubview(mainStackView)
imageView.imageFrameStyle = .grayBezel
let fileOpenButton = NSButton.makeButton(title: "File Open", target: delegate, action: #selector(Delegate.fileOpen(_:))),
fileSaveButton = NSButton.makeButton(title: "File Save", target: delegate, action: #selector(Delegate.fileSave(_:)))
if #available(iOS 9, OSX 10.11, *) {
mainStackView.addArrangedSubview(stack);mainStackView.addArrangedSubview(imageView);mainStackView.orientation = .vertical
stack.addArrangedSubview(fileOpenButton);stack.addArrangedSubview(fileSaveButton);stack.addArrangedSubview(textField)
}
}
func applicationDidFinishLaunching(_ notification: Notification) { }
func windowWillClose(_ notification: Notification) {
NSApplication.shared.stop(0)
}
override func controlTextDidEndEditing(_ obj: Notification) {
if let textField = obj.object as? NSTextField, let url = URL(string: textField.stringValue) {
imageView.image = NSImage(contentsOf: url)
}
}
@objc func fileOpen(_ sender: AnyObject) {
fileOpenPanel.runModal()
if let url = fileOpenPanel.urls.first {
textField.stringValue = url.absoluteString
imageView.image = NSImage(contentsOf: url)
}
}
@objc func fileSave(_ sender: AnyObject) {
fileSavePanel.allowedFileTypes = ["png"]
fileSavePanel.runModal()
guard let url = fileSavePanel.url, let rep = imageView.image?.representations.first as? NSBitmapImageRep else { return }
let _ = try? rep.representation(using: .png, properties: [:])?.write(to: url, options: [])
}
}
let delegate = Delegate(window: window)
application.delegate = delegate
window.delegate = delegate
application.activate(ignoringOtherApps: true)
application.setActivationPolicy(.regular)
application.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment