Created
November 7, 2015 01:16
-
-
Save erica/b6d330615aa241b36f90 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
// 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