AVRSack/AVRsack/ASApplication.swift

157 lines
6.7 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.
// Copyright (c) 2014 Aere Perennius. All rights reserved.
//
import Cocoa
@NSApplicationMain
2014-12-21 03:05:51 +00:00
class ASApplication: NSObject, NSApplicationDelegate, NSMenuDelegate {
@IBOutlet weak var themeMenu : NSMenu!
@IBOutlet weak var keyboardMenu : NSMenu!
2014-12-15 01:49:33 +00:00
@IBOutlet weak var preferences : ASPreferences!
2014-12-21 03:05:51 +00:00
var sketches = [String]()
var examples = [String]()
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")!
let appDefaults = NSMutableDictionary(contentsOfURL: appDefaultsURL)!
//
// 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()
for (index, theme) in enumerate(ACEThemeNames.humanThemeNames() as [NSString]) {
2014-11-17 04:53:33 +00:00
let menuItem = themeMenu.addItemWithTitle(theme, action: "changeTheme:", keyEquivalent: "")
menuItem!.tag = index
}
keyboardMenu.removeAllItems()
for (index, theme) in enumerate(ACEKeyboardHandlerNames.humanKeyboardHandlerNames() as [NSString]) {
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]()
for sketchBook in NSUserDefaults.standardUserDefaults().objectForKey("Sketchbooks") as [NSString] {
if NSFileManager.defaultManager().fileExistsAtPath(sketchBook) {
ASSketchBook.addSketches(menu, target: self, action: "openSketch:", path: sketchBook, sketches: &sketches)
}
}
case "Examples":
menu.removeAllItems()
examples = [String]()
if let arduinoPath = NSWorkspace.sharedWorkspace().URLForApplicationWithBundleIdentifier("cc.arduino.Arduino")?.path {
let examplePath = arduinoPath.stringByAppendingPathComponent("Contents/Resources/Java/examples")
ASSketchBook.addSketches(menu, target: self, action: "openExample:", path: examplePath, sketches: &examples)
}
default:
break
}
}
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
let fileManager = NSFileManager.defaultManager()
fileManager.copyItemAtURL(template, toURL: saveTo, error: nil)
let contents = fileManager.enumeratorAtURL(saveTo,
includingPropertiesForKeys: [NSURLNameKey, NSURLPathKey],
options: .SkipsHiddenFiles, errorHandler: nil)
while let item = contents?.nextObject() as? NSURL {
var renameItem = false
var itemName = item.lastPathComponent
if itemName.stringByDeletingPathExtension == oldName {
renameItem = true
itemName = newName.stringByAppendingPathExtension(itemName.pathExtension)!
}
if renameItem {
fileManager.moveItemAtURL(item,
toURL: item.URLByDeletingLastPathComponent!.URLByAppendingPathComponent(itemName),
error: nil)
}
}
let sketch = ASSketchBook.findSketch(saveTo.path!)
switch sketch {
case .Sketch(_, let path):
let doc = NSDocumentController.sharedDocumentController() as NSDocumentController
doc.openDocumentWithContentsOfURL(NSURL(fileURLWithPath: path)!, display: true) { (doc, alreadyOpen, error) -> Void in
}
default:
break
}
}
}
2014-12-21 03:05:51 +00:00
@IBAction func openSketch(item: NSMenuItem) {
let url = NSURL(fileURLWithPath: sketches[item.tag])!
let doc = NSDocumentController.sharedDocumentController() as NSDocumentController
doc.openDocumentWithContentsOfURL(url, display: true) { (doc, alreadyOpen, error) -> Void in
}
}
@IBAction func openExample(item: NSMenuItem) {
let url = NSURL(fileURLWithPath: examples[item.tag])!
2014-12-27 01:21:25 +00:00
openTemplate(url.URLByDeletingLastPathComponent!)
}
class func newProjectLocation(documentWindow: NSWindow?, message: String, completion: (NSURL) -> ()) {
let savePanel = NSSavePanel()
savePanel.allowedFileTypes = [kUTTypeFolder]
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
}
}
2014-11-15 03:39:10 +00:00
}