Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import Cocoa
// Interactive Playground Alert Utilities
public struct WindowManager {
internal static var screen: NSScreen! = NSScreen.mainScreen()
internal static var activationToken: dispatch_once_t = 0
internal static func buildWindow() -> NSWindow {
dispatch_once(&WindowManager.activationToken) {
NSApplication.sharedApplication().setActivationPolicy(.Regular) // show icon in dock
}
let frame = WindowManager.screen.frame
let center = CGPointMake(frame.midX, frame.midY)
let window = NSWindow(); window.setFrameOrigin(center); window.level = 7;
window.styleMask = NSBorderlessWindowMask;
window.makeKeyAndOrderFront(nil); window.alphaValue = 0.0
return window
}
public static func showInfo(info: String) {
let alert = NSAlert(); alert.messageText = info
alert.beginSheetModalForWindow(WindowManager.buildWindow(), completionHandler: nil)
}
// NSModalResponseStop, NSModalResponseAbort, NSModalResponseContinue
public static func queryBool(question: String, completion: (Bool) -> Void) {
let alert = NSAlert(); alert.messageText = question
alert.addButtonWithTitle("Yes")
alert.addButtonWithTitle("No")
alert.beginSheetModalForWindow(WindowManager.buildWindow()) {
(response) -> Void in
completion(response == abs(NSModalResponseStop))
}
}
public static func queryText(question: String, completion: (String) -> Void) {
let alert = NSAlert(); alert.messageText = question
alert.addButtonWithTitle("Okay")
alert.addButtonWithTitle("Cancel")
let input = NSTextField(frame: CGRectMake(0, 0, 200, 24))
alert.accessoryView = input
alert.beginSheetModalForWindow(WindowManager.buildWindow()) {
(response) -> Void in
if response == abs(NSModalResponseStop) {
completion(input.stringValue)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.