AVRSack/AVRsack/ASApplication.swift

220 lines
9.1 KiB
Swift
Raw Normal View History

2014-11-15 03:39:10 +00:00
//
// AppDelegate.swift
// AVRsack
//
// Created by Matthias Neeracher on 11/15/14.
2015-07-10 22:48:39 +00:00
// Copyright (c) 2014-2015 Aere Perennius. All rights reserved.
2014-11-15 03:39:10 +00:00
//
import Cocoa
2015-07-10 22:48:39 +00:00
import Carbon
2014-11-15 03:39:10 +00:00
@NSApplicationMain
2014-12-21 03:05:51 +00:00
class ASApplication: NSObject, NSApplicationDelegate, NSMenuDelegate {
2014-12-31 09:03:30 +00:00
@IBOutlet var themeMenu : NSMenu!
@IBOutlet var keyboardMenu : NSMenu!
@IBOutlet var preferences : ASPreferences!
2014-12-21 03:05:51 +00:00
var sketches = [String]()
var examples = [String]()
2014-12-31 09:03:30 +00:00
func hasDocument() -> Bool {
2015-11-16 01:56:33 +00:00
return NSDocumentController.sharedDocumentController().currentDocument != nil
2014-12-31 09:03:30 +00:00
}
func applicationWillFinishLaunching(notification: NSNotification) {
//
// Retrieve static app defaults
//
let fileManager = NSFileManager.defaultManager()
let workSpace = NSWorkspace.sharedWorkspace()
let userDefaults = NSUserDefaults.standardUserDefaults()
let appDefaultsURL = NSBundle.mainBundle().URLForResource("Defaults", withExtension: "plist")!
2015-02-14 16:43:20 +00:00
var appDefaults = NSDictionary(contentsOfURL: appDefaultsURL) as! [String: AnyObject]
//
// Add dynamic app defaults
//
if let arduinoPath = workSpace.URLForApplicationWithBundleIdentifier("cc.arduino.Arduino")?.path {
appDefaults["Arduino"] = arduinoPath
}
var sketchbooks = [NSString]()
for doc in fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask) {
sketchbooks.append(doc.URLByAppendingPathComponent("Arduino").path!)
sketchbooks.append(doc.URLByAppendingPathComponent("AVRSack").path!)
}
appDefaults["Sketchbooks"] = sketchbooks
if fileManager.fileExistsAtPath("/usr/local/CrossPack-AVR") {
appDefaults["Toolchain"] = "/usr/local/CrossPack-AVR"
} else {
appDefaults["Toolchain"] = ""
}
userDefaults.registerDefaults(appDefaults)
}
2014-11-15 03:39:10 +00:00
func applicationDidFinishLaunching(aNotification: NSNotification) {
themeMenu.removeAllItems()
2015-11-16 01:56:33 +00:00
for (index, theme) in (ACEThemeNames.humanThemeNames() as! [String]).enumerate() {
2015-03-17 23:17:55 +00:00
let menuItem = themeMenu.addItemWithTitle(theme, action: "changeTheme:", keyEquivalent: "")!
menuItem.tag = index
}
keyboardMenu.removeAllItems()
2015-11-16 01:56:33 +00:00
for (index, theme) in (ACEKeyboardHandlerNames.humanKeyboardHandlerNames() as! [String]).enumerate() {
2015-03-17 23:17:55 +00:00
let menuItem = keyboardMenu.addItemWithTitle(theme, action: "changeKeyboardHandler:", keyEquivalent: "")!
menuItem.tag = index
}
2014-11-15 03:39:10 +00:00
}
2014-12-27 01:21:25 +00:00
func applicationShouldOpenUntitledFile(sender: NSApplication) -> Bool {
return false
}
2014-11-15 03:39:10 +00:00
func applicationWillTerminate(aNotification: NSNotification) {
}
2014-12-21 03:05:51 +00:00
func menuNeedsUpdate(menu: NSMenu) {
switch menu.title {
case "Sketchbook":
menu.removeAllItems()
sketches = [String]()
2015-02-14 16:43:20 +00:00
for sketchBook in NSUserDefaults.standardUserDefaults().objectForKey("Sketchbooks") as! [String] {
2014-12-21 03:05:51 +00:00
if NSFileManager.defaultManager().fileExistsAtPath(sketchBook) {
ASSketchBook.addSketches(menu, target: self, action: "openSketch:", path: sketchBook, sketches: &sketches)
}
}
case "Examples":
menu.removeAllItems()
examples = [String]()
2015-11-16 01:56:33 +00:00
if let arduinoURL = NSWorkspace.sharedWorkspace().URLForApplicationWithBundleIdentifier("cc.arduino.Arduino") {
let examplePath = arduinoURL.URLByAppendingPathComponent("Contents/Resources/Java/examples", isDirectory:true).path!
2014-12-21 03:05:51 +00:00
ASSketchBook.addSketches(menu, target: self, action: "openExample:", path: examplePath, sketches: &examples)
}
2015-03-23 02:17:57 +00:00
case "Import Standard Library":
menu.removeAllItems()
ASLibraries.instance().addStandardLibrariesToMenu(menu)
case "Import Contributed Library":
menu.removeAllItems()
ASLibraries.instance().addContribLibrariesToMenu(menu)
2014-12-31 09:03:30 +00:00
case "Serial Monitor":
menu.itemAtIndex(0)?.hidden = !hasDocument()
while menu.numberOfItems > 2 {
menu.removeItemAtIndex(2)
}
2015-11-16 01:56:33 +00:00
for port in ASSerial.ports() {
2014-12-31 09:03:30 +00:00
menu.addItemWithTitle(port, action:"serialConnectMenu:", keyEquivalent:"")
}
2014-12-21 03:05:51 +00:00
default:
break
}
}
2014-12-31 09:03:30 +00:00
@IBAction func serialConnectMenu(port: NSMenuItem) {
ASSerialWin.showWindowWithPort(port.title)
}
2014-12-27 01:21:25 +00:00
func openTemplate(template: NSURL) {
ASApplication.newProjectLocation(nil,
message: "Save editable copy of project \(template.lastPathComponent)")
{ (saveTo) -> Void in
let oldName = template.lastPathComponent!
let newName = saveTo.lastPathComponent!
2014-12-27 01:21:25 +00:00
let fileManager = NSFileManager.defaultManager()
2015-11-16 01:56:33 +00:00
do {
try fileManager.copyItemAtURL(template, toURL: saveTo)
let contents = fileManager.enumeratorAtURL(saveTo,
includingPropertiesForKeys: [NSURLNameKey, NSURLPathKey],
options: .SkipsHiddenFiles, errorHandler: nil)
while let item = contents?.nextObject() as? NSURL {
let itemBase = item.URLByDeletingPathExtension?.lastPathComponent!
if itemBase == oldName {
let newItem = item.URLByDeletingLastPathComponent!.URLByAppendingPathComponent(
newName).URLByAppendingPathExtension(item.pathExtension!)
try fileManager.moveItemAtURL(item, toURL: newItem)
}
2014-12-27 01:21:25 +00:00
}
2015-11-16 01:56:33 +00:00
} catch (_) {
2014-12-27 01:21:25 +00:00
}
let sketch = ASSketchBook.findSketch(saveTo.path!)
switch sketch {
case .Sketch(_, let path):
2015-11-16 01:56:33 +00:00
let doc = NSDocumentController.sharedDocumentController()
doc.openDocumentWithContentsOfURL(NSURL(fileURLWithPath: path), display: true) { (doc, alreadyOpen, error) -> Void in
2014-12-27 01:21:25 +00:00
}
default:
break
}
}
}
2014-12-21 03:05:51 +00:00
@IBAction func openSketch(item: NSMenuItem) {
2015-11-16 01:56:33 +00:00
let url = NSURL(fileURLWithPath: sketches[item.tag])
let doc = NSDocumentController.sharedDocumentController()
doc.openDocumentWithContentsOfURL(url, display: true) { (doc, alreadyOpen, error) -> Void in
2014-12-21 03:05:51 +00:00
}
}
@IBAction func openExample(item: NSMenuItem) {
2015-11-16 01:56:33 +00:00
let url = NSURL(fileURLWithPath: examples[item.tag])
openTemplate(url.URLByDeletingLastPathComponent!)
2014-12-27 01:21:25 +00:00
}
2015-01-12 03:24:29 +00:00
2015-11-16 01:56:33 +00:00
@IBAction func createSketch(_: AnyObject) {
2015-01-12 03:24:29 +00:00
ASApplication.newProjectLocation(nil,
message: "Create Project")
{ (saveTo) -> Void in
let fileManager = NSFileManager.defaultManager()
2015-11-16 01:56:33 +00:00
do {
try fileManager.createDirectoryAtURL(saveTo, withIntermediateDirectories:false, attributes:nil)
let proj = saveTo.URLByAppendingPathComponent(saveTo.lastPathComponent!+".avrsackproj")
let docController = NSDocumentController.sharedDocumentController()
if let doc = try docController.openUntitledDocumentAndDisplay(true) as? ASProjDoc {
doc.fileURL = proj
doc.updateProjectURL()
doc.createFileAtURL(saveTo.URLByAppendingPathComponent(saveTo.lastPathComponent!+".ino"))
try doc.writeToURL(proj, ofType: "Project", forSaveOperation: .SaveAsOperation, originalContentsURL: nil)
}
} catch _ {
2015-01-12 03:24:29 +00:00
}
}
}
2014-12-27 01:21:25 +00:00
class func newProjectLocation(documentWindow: NSWindow?, message: String, completion: (NSURL) -> ()) {
let savePanel = NSSavePanel()
2015-11-16 01:56:33 +00:00
savePanel.allowedFileTypes = [kUTTypeFolder as String]
2014-12-27 01:21:25 +00:00
savePanel.message = message
if let window = documentWindow {
savePanel.beginSheetModalForWindow(window, completionHandler: { (returnCode) -> Void in
if returnCode == NSFileHandlingPanelOKButton {
completion(savePanel.URL!)
}
})
} else {
savePanel.beginWithCompletionHandler({ (returnCode) -> Void in
if returnCode == NSFileHandlingPanelOKButton {
completion(savePanel.URL!)
}
})
2014-12-21 03:05:51 +00:00
}
}
2015-07-10 22:48:39 +00:00
@IBAction func goToHelpPage(sender: AnyObject) {
let helpString: String
switch sender.tag() {
case 0:
helpString = "license.html"
default:
abort()
}
let locBookName = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleHelpBookName") as! String
AHGotoPage(locBookName, helpString, nil)
}
@IBAction func goToHelpURL(sender: AnyObject) {
let helpString: String
switch sender.tag() {
case 0:
helpString = "https://github.com/microtherion/AVRsack/issues"
default:
abort()
}
NSWorkspace.sharedWorkspace().openURL(NSURL(string: helpString)!)
}
2014-11-15 03:39:10 +00:00
}