I found a classic NSAlert implementation in Objective-C (written by TroikaTronix).
I managed to rewrite the entire implementation in Swift:
https://github.com/TroikaTronix/NSCustomAlert/pull/3
(See NSClassicAlert.swift
)
It looks good except that all buttons in NSClassicAlert does not execute its designated actions. I don't know why.
@discardableResult public func addButton(withTitle title: String) -> NSButton {
let frame = NSRect(x: 0, y: 0, width: 40, height: 30)
let btn = NSButton(frame: frame)
btn.bezelStyle = .rounded
btn.font = .systemFont(ofSize: NSFont.systemFontSize)
btn.target = self // TARGET ACTION
btn.action = #selector(buttonPressed(_:))
btn.title = title
btn.sizeToFit()
btn.tag = buttons.count
expandButtonSize(button: btn, amount: 10, minWidth: 75)
buttons.append(btn)
panel?.contentView?.addSubview(btn)
return btn
}
@objc internal func buttonPressed(_ sender: NSButton) {
guard let panel = panel else { return }
var response = NSApplication.ModalResponse.alertFirstButtonReturn
switch sender.tag {
case 1: response = NSApplication.ModalResponse.alertSecondButtonReturn
case 2: response = NSApplication.ModalResponse.alertThirdButtonReturn
default: response = NSApplication.ModalResponse.alertFirstButtonReturn
}
if let docWindow = docWindow {
docWindow.endSheet(panel, returnCode: response)
} else {
NSApp.stopModal(withCode: response)
}
panel.orderOut(nil)
}