Handle themes in serial window

This commit is contained in:
Matthias Neeracher 2014-12-31 04:16:43 +01:00 committed by Matthias Neeracher
parent f8315ff7ba
commit a0dfc98401
7 changed files with 159 additions and 22 deletions

View File

@ -24,6 +24,7 @@
95A7C6401A38914300EF1963 /* ASPreferences.xib in Resources */ = {isa = PBXBuildFile; fileRef = 95A7C63E1A38914300EF1963 /* ASPreferences.xib */; }; 95A7C6401A38914300EF1963 /* ASPreferences.xib in Resources */ = {isa = PBXBuildFile; fileRef = 95A7C63E1A38914300EF1963 /* ASPreferences.xib */; };
95A7C6461A3894C900EF1963 /* ASPreferences.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95A7C6451A3894C900EF1963 /* ASPreferences.swift */; }; 95A7C6461A3894C900EF1963 /* ASPreferences.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95A7C6451A3894C900EF1963 /* ASPreferences.swift */; };
95BF80EB1A185C9E0004A693 /* ASFileTree.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95BF80EA1A185C9E0004A693 /* ASFileTree.swift */; }; 95BF80EB1A185C9E0004A693 /* ASFileTree.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95BF80EA1A185C9E0004A693 /* ASFileTree.swift */; };
95DA9B161A515AF700DE018D /* ACEViewExt.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95DA9B151A515AF700DE018D /* ACEViewExt.swift */; };
95DF20671A45A6090013D1B5 /* ASSketchBook.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95DF20661A45A6090013D1B5 /* ASSketchBook.swift */; }; 95DF20671A45A6090013D1B5 /* ASSketchBook.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95DF20661A45A6090013D1B5 /* ASSketchBook.swift */; };
95EA32621A17B90600F66EB0 /* ACEView.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 95EA325B1A17B8DA00F66EB0 /* ACEView.framework */; }; 95EA32621A17B90600F66EB0 /* ACEView.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 95EA325B1A17B8DA00F66EB0 /* ACEView.framework */; };
95EA32651A17BA8C00F66EB0 /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 95EA32631A17BA8C00F66EB0 /* Cocoa.framework */; }; 95EA32651A17BA8C00F66EB0 /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 95EA32631A17BA8C00F66EB0 /* Cocoa.framework */; };
@ -91,6 +92,7 @@
95A7C63F1A38914300EF1963 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/ASPreferences.xib; sourceTree = "<group>"; }; 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>"; }; 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>"; }; 95BF80EA1A185C9E0004A693 /* ASFileTree.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ASFileTree.swift; sourceTree = "<group>"; };
95DA9B151A515AF700DE018D /* ACEViewExt.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ACEViewExt.swift; sourceTree = "<group>"; };
95DF20661A45A6090013D1B5 /* ASSketchBook.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ASSketchBook.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>"; }; 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; }; 95EA32631A17BA8C00F66EB0 /* Cocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cocoa.framework; path = System/Library/Frameworks/Cocoa.framework; sourceTree = SDKROOT; };
@ -187,6 +189,7 @@
95539B671A3E7EAF00D8595C /* ASSerial.mm */, 95539B671A3E7EAF00D8595C /* ASSerial.mm */,
95DF20661A45A6090013D1B5 /* ASSketchBook.swift */, 95DF20661A45A6090013D1B5 /* ASSketchBook.swift */,
956005F21A4EF79D00327007 /* ASSerialWin.swift */, 956005F21A4EF79D00327007 /* ASSerialWin.swift */,
95DA9B151A515AF700DE018D /* ACEViewExt.swift */,
); );
name = Source; name = Source;
sourceTree = "<group>"; sourceTree = "<group>";
@ -364,6 +367,7 @@
9501D8041A17025C0034C530 /* ASProjDoc.swift in Sources */, 9501D8041A17025C0034C530 /* ASProjDoc.swift in Sources */,
95539B681A3E7EAF00D8595C /* ASSerial.mm in Sources */, 95539B681A3E7EAF00D8595C /* ASSerial.mm in Sources */,
95468DDF1A228BE600668EE2 /* ASHardware.swift in Sources */, 95468DDF1A228BE600668EE2 /* ASHardware.swift in Sources */,
95DA9B161A515AF700DE018D /* ACEViewExt.swift in Sources */,
951CD1741A23C9FC0066C1A1 /* ASBuilder.swift in Sources */, 951CD1741A23C9FC0066C1A1 /* ASBuilder.swift in Sources */,
9501D8021A17025C0034C530 /* ASApplication.swift in Sources */, 9501D8021A17025C0034C530 /* ASApplication.swift in Sources */,
95BF80EB1A185C9E0004A693 /* ASFileTree.swift in Sources */, 95BF80EB1A185C9E0004A693 /* ASFileTree.swift in Sources */,

29
AVRsack/ACEViewExt.swift Normal file
View File

@ -0,0 +1,29 @@
//
// ACEViewExt.swift
// AVRsack
//
// Created by Matthias Neeracher on 29/12/14.
// Copyright (c) 2014 Aere Perennius. All rights reserved.
//
import Foundation
extension ACEView {
class func themeIdByName(themeName: String) -> UInt? {
for (themeIdx, theme) in enumerate(ACEThemeNames.themeNames() as [NSString]) {
if themeName == theme {
return UInt(themeIdx)
}
}
return nil
}
class func handlerIdByName(handlerName: String) -> ACEKeyboardHandler? {
for (handlerIdx, handler) in enumerate(ACEKeyboardHandlerNames.humanKeyboardHandlerNames() as [NSString]) {
if handlerName == handler {
return ACEKeyboardHandler(rawValue: UInt(handlerIdx))!
}
}
return nil
}
}

View File

@ -43,7 +43,7 @@ enum ASFileType : String {
case .Markdown: case .Markdown:
return ACEModeMarkdown return ACEModeMarkdown
default: default:
return ACEModeASCIIDoc return ACEModeText
} }
} }
} }

View File

@ -67,19 +67,13 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate, NSMenuDelegate {
super.init() super.init()
let userDefaults = NSUserDefaults.standardUserDefaults() let userDefaults = NSUserDefaults.standardUserDefaults()
if let themeName = userDefaults.stringForKey(kThemeKey) { if let themeName = userDefaults.stringForKey(kThemeKey) {
for (themeIdx, theme) in enumerate(ACEThemeNames.themeNames() as [NSString]) { if let themeId = ACEView.themeIdByName(themeName) {
if themeName == theme { currentTheme = themeId
currentTheme = UInt(themeIdx)
break
}
} }
} }
if let handlerName = userDefaults.stringForKey(kBindingsKey) { if let handlerName = userDefaults.stringForKey(kBindingsKey) {
for (handlerIdx, handler) in enumerate(ACEKeyboardHandlerNames.humanKeyboardHandlerNames() as [NSString]) { if let handlerId = ACEView.handlerIdByName(handlerName) {
if handlerName == handler { keyboardHandler = handlerId
keyboardHandler = ACEKeyboardHandler(rawValue: UInt(handlerIdx))!
break
}
} }
} }
@ -199,11 +193,8 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate, NSMenuDelegate {
let projectVersion = projectData[kVersionKey] as Double let projectVersion = projectData[kVersionKey] as Double
assert(projectVersion <= floor(kCurVersion+1.0), "Project version too new for this app") assert(projectVersion <= floor(kCurVersion+1.0), "Project version too new for this app")
if let themeName = projectData[kThemeKey] as? NSString { if let themeName = projectData[kThemeKey] as? NSString {
for (themeIdx, theme) in enumerate(ACEThemeNames.themeNames() as [NSString]) { if let themeId = ACEView.themeIdByName(themeName) {
if themeName == theme { currentTheme = themeId
currentTheme = UInt(themeIdx)
break
}
} }
} }
if let fontSz = projectData[kFontSizeKey] as? Int { if let fontSz = projectData[kFontSizeKey] as? Int {
@ -257,7 +248,7 @@ class ASProjDoc: NSDocument, NSOutlineViewDelegate, NSMenuDelegate {
mainEditor = selection mainEditor = selection
} else if let log = (selection as? ASLogNode) { } else if let log = (selection as? ASLogNode) {
editor.setString("") editor.setString("")
editor.setMode(UInt(ACEModeASCIIDoc)) editor.setMode(UInt(ACEModeText))
editor.alphaValue = 0.8 editor.alphaValue = 0.8
logModified = NSDate.distantPast() as NSDate logModified = NSDate.distantPast() as NSDate
logSize = -1 logSize = -1

View File

@ -8,7 +8,8 @@
import Cocoa import Cocoa
private var serialInstances = [String : ASSerialWin]() private var serialInstances = [String : ASSerialWin]()
private var keyboardHandler : ACEKeyboardHandler = .Ace
class ASSerialWin: NSWindowController { class ASSerialWin: NSWindowController {
@IBOutlet weak var portPopUp : NSPopUpButton! @IBOutlet weak var portPopUp : NSPopUpButton!
@ -25,10 +26,20 @@ class ASSerialWin: NSWindowController {
} }
var sendCR = false var sendCR = false
var sendLF = true var sendLF = true
var scrollToBottom : Bool = true {
didSet(oldScroll) {
if scrollToBottom {
logView.gotoLine(1000000000, column: 0, animated: true)
}
}
}
var port = "" var port = ""
var serialData = "" var serialData = ""
var serialObserver : AnyObject! var serialObserver : AnyObject!
dynamic var portHandle : NSFileHandle? dynamic var portHandle : NSFileHandle?
var currentTheme : UInt = 0
var fontSize : UInt = 12
var portDefaults = [String: AnyObject]()
class func showWindowWithPort(port: String) { class func showWindowWithPort(port: String) {
if let existing = serialInstances[port] { if let existing = serialInstances[port] {
@ -40,15 +51,49 @@ class ASSerialWin: NSWindowController {
} }
} }
convenience init(port: String) { init(port: String) {
self.init(windowNibName:"ASSerialWin") super.init()
self.port = port self.port = port
let userDefaults = NSUserDefaults.standardUserDefaults()
if let portDef = (userDefaults.objectForKey("SerialDefaults") as NSDictionary).objectForKey(port) as? [String: AnyObject] {
portDefaults = portDef
} else {
portDefaults["Theme"] = userDefaults.stringForKey("SerialTheme")
portDefaults["FontSize"] = userDefaults.objectForKey("FontSize")
portDefaults["SendCR"] = sendCR
portDefaults["SendLF"] = sendLF
}
if let themeId = ACEView.themeIdByName(portDefaults["Theme"] as String) {
currentTheme = themeId
}
fontSize = portDefaults["FontSize"] as UInt
sendCR = portDefaults["SendCR"] as Bool
sendLF = portDefaults["SendLF"] as Bool
if let handlerName = userDefaults.stringForKey("Bindings") {
if let handlerId = ACEView.handlerIdByName(handlerName) {
keyboardHandler = handlerId
}
}
var nc = NSNotificationCenter.defaultCenter() var nc = NSNotificationCenter.defaultCenter()
serialObserver = nc.addObserverForName(kASSerialPortsChanged, object: nil, queue: nil, usingBlock: { (NSNotification) in serialObserver = nc.addObserverForName(kASSerialPortsChanged, object: nil, queue: nil, usingBlock: { (NSNotification) in
self.rebuildPortMenu() self.rebuildPortMenu()
}) })
} }
func windowNibName() -> String {
return "ASSerialWin"
}
required override init(window: NSWindow!) {
super.init(window:window)
}
required init?(coder: NSCoder) {
super.init(coder:coder)
}
override func finalize() { override func finalize() {
NSNotificationCenter.defaultCenter().removeObserver(serialObserver) NSNotificationCenter.defaultCenter().removeObserver(serialObserver)
serialInstances.removeValueForKey(port) serialInstances.removeValueForKey(port)
@ -57,6 +102,11 @@ class ASSerialWin: NSWindowController {
override func windowDidLoad() { override func windowDidLoad() {
logView.setReadOnly(true) logView.setReadOnly(true)
logView.setShowPrintMargin(false) logView.setShowPrintMargin(false)
logView.setTheme(currentTheme)
logView.setKeyboardHandler(keyboardHandler)
logView.setFontSize(fontSize)
logView.setMode(UInt(ACEModeText))
logView.alphaValue = 0.8
rebuildPortMenu() rebuildPortMenu()
window?.title = port window?.title = port
connect(self) connect(self)
@ -96,6 +146,9 @@ class ASSerialWin: NSWindowController {
self.serialData += newString self.serialData += newString
dispatch_async(dispatch_get_main_queue(), { () -> Void in dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.logView.setString(self.serialData) self.logView.setString(self.serialData)
if self.scrollToBottom {
self.logView.gotoLine(1000000000, column: 0, animated: true)
}
}) })
} }
} }
@ -110,4 +163,57 @@ class ASSerialWin: NSWindowController {
class func keyPathsForValuesAffectingConnectButtonTitle() -> NSSet { class func keyPathsForValuesAffectingConnectButtonTitle() -> NSSet {
return NSSet(object: "portHandle") return NSSet(object: "portHandle")
} }
// MARK: Editor configuration
@IBAction func changeTheme(item: NSMenuItem) {
let userDefaults = NSUserDefaults.standardUserDefaults()
currentTheme = UInt(item.tag)
logView.setTheme(currentTheme)
let themeName = ACEThemeNames.humanNameForTheme(currentTheme)
userDefaults.setObject(themeName, forKey: "SerialTheme")
portDefaults["Theme"] = themeName
updatePortDefaults()
}
@IBAction func changeKeyboardHandler(item: NSMenuItem) {
keyboardHandler = ACEKeyboardHandler(rawValue: UInt(item.tag))!
NSUserDefaults.standardUserDefaults().setObject(
ACEKeyboardHandlerNames.humanNameForKeyboardHandler(keyboardHandler), forKey: "Bindings")
NSNotificationCenter.defaultCenter().postNotificationName("Bindings", object: item)
}
func validateUserInterfaceItem(anItem: NSValidatedUserInterfaceItem) -> Bool {
if let menuItem = anItem as? NSMenuItem {
if menuItem.action == "changeTheme:" {
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 true
}
@IBAction func makeTextLarger(AnyObject) {
fontSize += 1
logView.setFontSize(fontSize)
portDefaults["FontSize"] = fontSize
updatePortDefaults()
}
@IBAction func makeTextSmaller(AnyObject) {
if fontSize > 6 {
fontSize -= 1
logView.setFontSize(fontSize)
portDefaults["FontSize"] = fontSize
updatePortDefaults()
}
}
func updatePortDefaults() {
let userDefaults = NSUserDefaults.standardUserDefaults()
let serialDefaults = userDefaults.objectForKey("SerialDefaults") as NSDictionary
serialDefaults.setValue(portDefaults, forKey:port)
userDefaults.setObject(serialDefaults, forKey:"SerialDefaults")
}
} }

View File

@ -68,9 +68,12 @@
<constraint firstAttribute="height" constant="20" id="s7i-ZT-S7W"/> <constraint firstAttribute="height" constant="20" id="s7i-ZT-S7W"/>
</constraints> </constraints>
<buttonCell key="cell" type="smallSquare" title="Now" bezelStyle="smallSquare" imagePosition="overlaps" alignment="center" state="on" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="vZ4-fd-aE8"> <buttonCell key="cell" type="smallSquare" title="Now" bezelStyle="smallSquare" imagePosition="overlaps" alignment="center" state="on" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="vZ4-fd-aE8">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/> <behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES" changeBackground="YES" changeGray="YES"/>
<font key="font" metaFont="system"/> <font key="font" metaFont="system"/>
</buttonCell> </buttonCell>
<connections>
<binding destination="-2" name="value" keyPath="scrollToBottom" id="xlw-TM-qoV"/>
</connections>
</button> </button>
<button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="YB5-mV-vY7"> <button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="YB5-mV-vY7">
<rect key="frame" x="336" y="372" width="35" height="19"/> <rect key="frame" x="336" y="372" width="35" height="19"/>

View File

@ -2,8 +2,12 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0"> <plist version="1.0">
<dict> <dict>
<key>SerialDefaults</key>
<dict/>
<key>SerialTheme</key>
<string>tomorrow_night_blue</string>
<key>ShowSourceInDisassembly</key> <key>ShowSourceInDisassembly</key>
<string></string> <false/>
<key>UploadVerbosity</key> <key>UploadVerbosity</key>
<integer>2</integer> <integer>2</integer>
<key>RecentProgrammers</key> <key>RecentProgrammers</key>