Add Sketchbook & Examples menus

This commit is contained in:
Matthias Neeracher 2014-12-21 04:05:51 +01:00 committed by Matthias Neeracher
parent 55b5ef96d3
commit bce412d7a6
4 changed files with 164 additions and 1 deletions

View File

@ -22,6 +22,7 @@
95A7C6401A38914300EF1963 /* ASPreferences.xib in Resources */ = {isa = PBXBuildFile; fileRef = 95A7C63E1A38914300EF1963 /* ASPreferences.xib */; };
95A7C6461A3894C900EF1963 /* ASPreferences.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95A7C6451A3894C900EF1963 /* ASPreferences.swift */; };
95BF80EB1A185C9E0004A693 /* ASFileTree.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95BF80EA1A185C9E0004A693 /* ASFileTree.swift */; };
95DF20671A45A6090013D1B5 /* ASSketchBook.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95DF20661A45A6090013D1B5 /* ASSketchBook.swift */; };
95EA32621A17B90600F66EB0 /* ACEView.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 95EA325B1A17B8DA00F66EB0 /* ACEView.framework */; };
95EA32651A17BA8C00F66EB0 /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 95EA32631A17BA8C00F66EB0 /* Cocoa.framework */; };
/* End PBXBuildFile section */
@ -86,6 +87,7 @@
95A7C63F1A38914300EF1963 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/ASPreferences.xib; sourceTree = "<group>"; };
95A7C6451A3894C900EF1963 /* ASPreferences.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ASPreferences.swift; sourceTree = "<group>"; };
95BF80EA1A185C9E0004A693 /* ASFileTree.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ASFileTree.swift; sourceTree = "<group>"; };
95DF20661A45A6090013D1B5 /* ASSketchBook.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ASSketchBook.swift; sourceTree = "<group>"; };
95EA32541A17B8DA00F66EB0 /* ACEView.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = ACEView.xcodeproj; path = ../ACEView/ACEView.xcodeproj; sourceTree = "<group>"; };
95EA32631A17BA8C00F66EB0 /* Cocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cocoa.framework; path = System/Library/Frameworks/Cocoa.framework; sourceTree = SDKROOT; };
95EA32641A17BA8C00F66EB0 /* WebKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = WebKit.framework; path = System/Library/Frameworks/WebKit.framework; sourceTree = SDKROOT; };
@ -179,6 +181,7 @@
951CD1761A2615000066C1A1 /* BuildProject */,
95539B661A3E7EAF00D8595C /* ASSerial.h */,
95539B671A3E7EAF00D8595C /* ASSerial.mm */,
95DF20661A45A6090013D1B5 /* ASSketchBook.swift */,
);
name = Source;
sourceTree = "<group>";
@ -356,6 +359,7 @@
951CD1741A23C9FC0066C1A1 /* ASBuilder.swift in Sources */,
9501D8021A17025C0034C530 /* ASApplication.swift in Sources */,
95BF80EB1A185C9E0004A693 /* ASFileTree.swift in Sources */,
95DF20671A45A6090013D1B5 /* ASSketchBook.swift in Sources */,
95A7C6461A3894C900EF1963 /* ASPreferences.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;

View File

@ -9,10 +9,12 @@
import Cocoa
@NSApplicationMain
class ASApplication: NSObject, NSApplicationDelegate {
class ASApplication: NSObject, NSApplicationDelegate, NSMenuDelegate {
@IBOutlet weak var themeMenu : NSMenu!
@IBOutlet weak var keyboardMenu : NSMenu!
@IBOutlet weak var preferences : ASPreferences!
var sketches = [String]()
var examples = [String]()
func applicationWillFinishLaunching(notification: NSNotification) {
//
@ -59,5 +61,42 @@ class ASApplication: NSObject, NSApplicationDelegate {
func applicationWillTerminate(aNotification: NSNotification) {
// Insert code here to tear down your application
}
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
}
}
@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])!
let doc = NSDocumentController.sharedDocumentController() as NSDocumentController
doc.openDocumentWithContentsOfURL(url, display: true) { (doc, alreadyOpen, error) -> Void in
}
}
}

View File

@ -0,0 +1,93 @@
//
// ASSketchBook.swift
// AVRsack
//
// Created by Matthias Neeracher on 12/20/14.
// Copyright © 2014 Aere Perennius. All rights reserved.
//
import Foundation
class ASSketchBook {
enum SketchBookItem {
case Nothing
case Sketch(String, String)
case SketchDir(String, [SketchBookItem])
}
private class func enumerateSketches(path: NSString) -> SketchBookItem {
let fileManager = NSFileManager.defaultManager()
let contents = fileManager.contentsOfDirectoryAtPath(path, error: nil) as [String]
for item in contents {
switch item.pathExtension {
case "ino", "avrsackproj":
return .Sketch(path.lastPathComponent, path.stringByAppendingPathComponent(item))
default:
break
}
}
var sketches = [SketchBookItem]()
for item in contents {
let subpath = path.stringByAppendingPathComponent(item)
var isDir : ObjCBool = false
if fileManager.fileExistsAtPath(subpath, isDirectory: &isDir) && isDir {
let subEnum = enumerateSketches(subpath)
switch subEnum {
case .Nothing:
break
default:
sketches.append(subEnum)
}
}
}
sketches.sort({ (a: SketchBookItem, b: SketchBookItem) -> Bool in
var itemA : String = ""
switch a {
case .Sketch(let item, _):
itemA = item
case .SketchDir(let item, _):
itemA = item
default:
break
}
switch b {
case .Sketch(let item, _):
return itemA < item
case .SketchDir(let item, _):
return itemA < item
default:
return itemA < ""
}
})
return sketches.count > 0 ? .SketchDir(path.lastPathComponent, sketches) : .Nothing
}
class func appendSketchesToMenu(menu: NSMenu, target: AnyObject, action: Selector, sketchList: [SketchBookItem], inout sketches: [String]) {
for sketch in sketchList {
switch (sketch) {
case .Sketch(let item, let path):
let menuItem = menu.addItemWithTitle(item, action: action, keyEquivalent: "")
menuItem?.target = target
menuItem?.tag = sketches.count
sketches.append(path)
case .SketchDir(let item, let subSketches):
let menuItem = menu.addItemWithTitle(item, action: nil, keyEquivalent: "")
let submenu = NSMenu()
submenu.autoenablesItems = false
appendSketchesToMenu(submenu, target: target, action: action, sketchList: subSketches, sketches: &sketches)
menu.setSubmenu(submenu, forItem: menuItem!)
default:
break
}
}
}
class func addSketches(menu: NSMenu, target: AnyObject, action: Selector, path: NSString, inout sketches: [String]) {
switch enumerateSketches(path) {
case .SketchDir(let item, let sketchList):
appendSketchesToMenu(menu, target: target, action: action, sketchList: sketchList, sketches: &sketches)
default:
break
}
}
}

View File

@ -98,6 +98,33 @@
</menu>
</menuItem>
<menuItem isSeparatorItem="YES" id="m54-Is-iLE"/>
<menuItem title="Sketchbook" id="4xr-TB-O2o">
<modifierMask key="keyEquivalentModifierMask"/>
<menu key="submenu" title="Sketchbook" autoenablesItems="NO" id="HXa-Bi-EMh">
<items>
<menuItem title="Item" id="9ed-eu-m7e">
<modifierMask key="keyEquivalentModifierMask"/>
</menuItem>
</items>
<connections>
<outlet property="delegate" destination="Voe-Tx-rLC" id="DkU-Mu-gQh"/>
</connections>
</menu>
</menuItem>
<menuItem title="Examples" id="BzW-Yi-WXB">
<modifierMask key="keyEquivalentModifierMask"/>
<menu key="submenu" title="Examples" autoenablesItems="NO" id="v0D-oY-a4X">
<items>
<menuItem title="Item" id="Ry3-aa-Q0v">
<modifierMask key="keyEquivalentModifierMask"/>
</menuItem>
</items>
<connections>
<outlet property="delegate" destination="Voe-Tx-rLC" id="isJ-WG-rC6"/>
</connections>
</menu>
</menuItem>
<menuItem isSeparatorItem="YES" id="zCK-BU-1ZK"/>
<menuItem title="Close" keyEquivalent="w" id="DVo-aG-piG">
<connections>
<action selector="performClose:" target="-1" id="HmO-Ls-i7Q"/>