From cccc33c8478249036c5f3e593e6f60809da7ff63 Mon Sep 17 00:00:00 2001 From: Matthias Neeracher Date: Mon, 17 Nov 2014 05:39:39 +0100 Subject: [PATCH] Theme, Font, and Keyboard Binding Support --- AVRsack/ASApplication.swift | 14 +- AVRsack/ASProjDoc.swift | 110 +++++++- AVRsack/AVRsack-Bridging-Header.h | 2 + AVRsack/Base.lproj/ASProjDoc.xib | 2 +- AVRsack/Base.lproj/MainMenu.xib | 447 ++++++------------------------ 5 files changed, 204 insertions(+), 371 deletions(-) diff --git a/AVRsack/ASApplication.swift b/AVRsack/ASApplication.swift index 3a6cb13..80ebeb5 100644 --- a/AVRsack/ASApplication.swift +++ b/AVRsack/ASApplication.swift @@ -10,8 +10,20 @@ import Cocoa @NSApplicationMain class ASApplication: NSObject, NSApplicationDelegate { + @IBOutlet weak var themeMenu : NSMenu! + @IBOutlet weak var keyboardMenu : NSMenu! + func applicationDidFinishLaunching(aNotification: NSNotification) { - // Insert code here to initialize your application + themeMenu.removeAllItems() + for (index, theme) in enumerate(ACEThemeNames.humanThemeNames() as [NSString]) { + let menuItem = themeMenu.addItemWithTitle(theme, action: "setTheme:", 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 + } } func applicationWillTerminate(aNotification: NSNotification) { diff --git a/AVRsack/ASProjDoc.swift b/AVRsack/ASProjDoc.swift index 3ade4c5..7fb9b05 100644 --- a/AVRsack/ASProjDoc.swift +++ b/AVRsack/ASProjDoc.swift @@ -8,24 +8,62 @@ import Cocoa +private var keyboardHandler : ACEKeyboardHandler = .Ace + class ASProjDoc: NSDocument, NSOutlineViewDelegate { @IBOutlet weak var editor : ACEView! @IBOutlet weak var outline : NSOutlineView! let files : ASFileTree = ASFileTree() var mainEditor : ASFileNode? + var currentTheme : UInt = 0 + var fontSize : UInt = 12 + var themeObserver : AnyObject? + let kVersionKey = "Version" + let kCurVersion = 1.0 + let kFilesKey = "Files" + let kThemeKey = "Theme" + let kFontSizeKey = "FontSize" + let kBindingsKey = "Bindings" + // MARK: Initialization / Finalization override init() { super.init() - // Add your subclass-specific initialization here. + let userDefaults = NSUserDefaults.standardUserDefaults() + userDefaults.registerDefaults([kThemeKey: "xcode", kFontSizeKey: 12, kBindingsKey: "Ace"]) + if let themeName = userDefaults.stringForKey(kThemeKey) { + for (themeIdx, theme) in enumerate(ACEThemeNames.themeNames() as [NSString]) { + if themeName == theme { + currentTheme = UInt(themeIdx) + break + } + } + } + if let handlerName = userDefaults.stringForKey(kBindingsKey) { + for (handlerIdx, handler) in enumerate(ACEKeyboardHandlerNames.humanKeyboardHandlerNames() as [NSString]) { + if handlerName == handler { + keyboardHandler = ACEKeyboardHandler(rawValue: UInt(handlerIdx))! + break + } + } + } + fontSize = UInt(userDefaults.integerForKey(kFontSizeKey)) + themeObserver = NSNotificationCenter.defaultCenter().addObserverForName(kBindingsKey, object: nil, queue: nil, usingBlock: { (NSNotification) in + self.editor.setKeyboardHandler(keyboardHandler) + }) } override func finalize() { saveCurEditor() + NSNotificationCenter.defaultCenter().removeObserver(themeObserver!) } override func windowControllerDidLoadNib(aController: NSWindowController) { super.windowControllerDidLoadNib(aController) + editor.setShowPrintMargin(false) + editor.setTheme(currentTheme) + editor.setKeyboardHandler(keyboardHandler) + editor.setFontSize(fontSize) outline.setDataSource(files) files.apply() { node in if let group = node as? ASFileGroup { @@ -34,6 +72,8 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate { } } } + updateChangeCount(.ChangeCleared) + outlineViewSelectionDidChange(NSNotification(name: "", object: nil)) } override class func autosavesInPlace() -> Bool { @@ -54,12 +94,11 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate { } } - let kVersionKey = "Version" - let kCurVersion = 1.0 - let kFilesKey = "Files" - override func dataOfType(typeName: String, error outError: NSErrorPointer) -> NSData? { - let data = [kVersionKey: kCurVersion, kFilesKey: files.propertyList()] + let data = [kVersionKey: kCurVersion, + kThemeKey: ACEThemeNames.nameForTheme(currentTheme), + kFontSizeKey: fontSize, + kFilesKey: files.propertyList()] return NSPropertyListSerialization.dataFromPropertyList(data, format: .XMLFormat_v1_0, errorDescription: nil) } @@ -101,7 +140,19 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate { let projectData : NSDictionary = NSPropertyListSerialization.propertyListFromData(data, mutabilityOption: .Immutable, format: nil, errorDescription: nil) as NSDictionary let projectVersion = projectData[kVersionKey] as Double assert(projectVersion <= floor(kCurVersion+1.0), "Project version too new for this app") + if let themeName = projectData[kThemeKey] as? NSString { + for (themeIdx, theme) in enumerate(ACEThemeNames.themeNames() as [NSString]) { + if themeName == theme { + currentTheme = UInt(themeIdx) + break + } + } + } + if let fontSz = projectData[kFontSizeKey] as? Int { + fontSize = UInt(fontSz) + } files.readPropertyList(projectData[kFilesKey] as NSDictionary) + updateChangeCount(.ChangeCleared) return true } @@ -117,15 +168,62 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate { var enc : UInt = 0 editor.setString(NSString(contentsOfURL:file.url, usedEncoding:&enc, error:nil)) editor.setMode(UInt(file.type.aceMode)) + editor.alphaValue = 1.0 + } else { + editor.alphaValue = 0.0 } } func outlineViewItemDidExpand(notification: NSNotification) { let group = notification.userInfo!["NSObject"] as ASFileGroup group.expanded = true + updateChangeCount(.ChangeDone) } func outlineViewItemDidCollapse(notification: NSNotification) { let group = notification.userInfo!["NSObject"] as ASFileGroup group.expanded = false + updateChangeCount(.ChangeDone) + } + + // MARK: Editor configuration + + @IBAction func setTheme(item: NSMenuItem) { + currentTheme = UInt(item.tag) + editor.setTheme(currentTheme) + NSUserDefaults.standardUserDefaults().setObject( + ACEThemeNames.humanNameForTheme(currentTheme), forKey: kThemeKey) + updateChangeCount(.ChangeDone) + } + @IBAction func changeKeyboardHandler(item: NSMenuItem) { + keyboardHandler = ACEKeyboardHandler(rawValue: UInt(item.tag))! + NSUserDefaults.standardUserDefaults().setObject( + ACEKeyboardHandlerNames.humanNameForKeyboardHandler(keyboardHandler), forKey: kBindingsKey) + NSNotificationCenter.defaultCenter().postNotificationName(kBindingsKey, object: item) + } + + override func validateUserInterfaceItem(anItem: NSValidatedUserInterfaceItem) -> Bool { + if let menuItem = anItem as? NSMenuItem { + if menuItem.action == "setTheme:" { + menuItem.state = (menuItem.tag == Int(currentTheme) ? NSOnState : NSOffState) + return true + } else if menuItem.action == "changeKeyboardHandler:" { + menuItem.state = (menuItem.tag == Int(keyboardHandler.rawValue) ? NSOnState : NSOffState) + return true + } + } + return super.validateUserInterfaceItem(anItem) + } + + @IBAction func makeTextLarger(AnyObject) { + fontSize += 1 + editor.setFontSize(fontSize) + updateChangeCount(.ChangeDone) + } + @IBAction func makeTextSmaller(AnyObject) { + if fontSize > 6 { + fontSize -= 1 + editor.setFontSize(fontSize) + updateChangeCount(.ChangeDone) + } } } diff --git a/AVRsack/AVRsack-Bridging-Header.h b/AVRsack/AVRsack-Bridging-Header.h index 6fefd4a..7b757d3 100644 --- a/AVRsack/AVRsack-Bridging-Header.h +++ b/AVRsack/AVRsack-Bridging-Header.h @@ -2,3 +2,5 @@ // Use this file to import your target's public headers that you would like to expose to Swift. // #import +#import +#import diff --git a/AVRsack/Base.lproj/ASProjDoc.xib b/AVRsack/Base.lproj/ASProjDoc.xib index c051384..ac181f3 100644 --- a/AVRsack/Base.lproj/ASProjDoc.xib +++ b/AVRsack/Base.lproj/ASProjDoc.xib @@ -13,7 +13,7 @@ - + diff --git a/AVRsack/Base.lproj/MainMenu.xib b/AVRsack/Base.lproj/MainMenu.xib index 103f46b..ae40988 100644 --- a/AVRsack/Base.lproj/MainMenu.xib +++ b/AVRsack/Base.lproj/MainMenu.xib @@ -11,7 +11,12 @@ - + + + + + + @@ -123,217 +128,161 @@ - + - + - + - + - + - + - - + + - + - + - + - + - + - - - - - - - + - + - + - + - - + + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - + + - + - + - + - + - + - + - + - + - + - - + - + - + - - - - - - - - - - - - - - - - - - - - - - - - - + - + - + - + - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - + @@ -342,265 +291,41 @@ - + - + - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - + - - - - - - - - - - - + - - - - - - - - - - - - - - - - - - - Default - - - - - - - Left to Right - - - - - - - Right to Left - - - - - - - - - - - Default - - - - - - - Left to Right - - - - - - - Right to Left - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + @@ -662,9 +387,5 @@ - - - -