mirror of
https://github.com/insidegui/VirtualBuddy.git
synced 2026-12-02 03:59:23 +08:00
43 lines
1.2 KiB
Swift
43 lines
1.2 KiB
Swift
//
|
|
// NSAlert+Confirmation.swift
|
|
// VirtualBuddy
|
|
//
|
|
// Created by Guilherme Rambo on 29/06/22.
|
|
//
|
|
|
|
import Cocoa
|
|
|
|
public extension NSAlert {
|
|
|
|
static func runConfirmationAlert(title: String,
|
|
message: String,
|
|
continueButtonTitle: String = "Continue",
|
|
cancelButtonTitle: String = "Cancel",
|
|
continueButtonIsDefault: Bool = false) async -> Bool
|
|
{
|
|
let alert = NSAlert()
|
|
|
|
alert.messageText = title
|
|
alert.informativeText = message
|
|
|
|
if continueButtonIsDefault {
|
|
alert.addButton(withTitle: continueButtonTitle)
|
|
alert.addButton(withTitle: cancelButtonTitle)
|
|
} else {
|
|
alert.addButton(withTitle: cancelButtonTitle)
|
|
alert.addButton(withTitle: continueButtonTitle)
|
|
}
|
|
|
|
let response: NSApplication.ModalResponse
|
|
|
|
if let window = NSApp?.keyWindow {
|
|
response = await alert.beginSheetModal(for: window)
|
|
} else {
|
|
response = alert.runModal()
|
|
}
|
|
|
|
return continueButtonIsDefault ? response == .alertFirstButtonReturn : response == .alertSecondButtonReturn
|
|
}
|
|
|
|
}
|