Make progress on LogWindowController.

This commit is contained in:
Brent Simmons 2017-11-14 22:04:14 -08:00
parent 887e562716
commit 48ca636796
1 changed files with 59 additions and 13 deletions

View File

@ -12,48 +12,94 @@ class LogWindowController: NSWindowController {
@IBOutlet private var textView: NSTextView!
private var title: String!
private weak var log: Log?
// private let attributes = [NSFont.Att
public convenience init(title: String) {
public convenience init(title: String, log: Log) {
self.init(windowNibName: NSNib.Name(rawValue: "LogWindow"))
self.log = log
self.title = title
NotificationCenter.default.addObserver(self, selector: #selector(logDidAddItem(_:)), name: .LogDidAddItem, object: log)
}
override func windowDidLoad() {
window!.title = title
addExistingLogItems()
}
public func appendLine(_ s: String) {
// Adds two line feeds before the text.
// MARK: - Notifications
@objc func logDidAddItem(_ note: Notification) {
guard let logItem = note.userInfo?[Log.logItemKey] as? LogItem else {
return
}
public func setTextViewText(_ s: String) {
let attributedString = NSAttributedString(string: s)
textView.textStorage?.setAttributedString(attributedString)
validateButtons()
appendLogItem(logItem)
}
// MARK: - Actions
@IBAction func clearContents(_ sender: Any?) {
setTextViewText("")
setTextViewAttributedString(NSAttributedString(string: ""))
}
@IBAction func saveToFile(_ sender: Any?) {
}
}
private extension LogWindowController {
func addExistingLogItems() {
guard let logItems = log?.logItems else {
return
}
let attString = NSMutableAttributedString()
for logItem in logItems {
let oneAttString = attributedString(for: logItem)
attString.append(oneAttString)
}
textView.textStorage?.setAttributedString(attString)
validateButtons()
}
func setTextViewAttributedString(_ attString: NSAttributedString) {
textView.textStorage?.setAttributedString(attString)
validateButtons()
}
func appendAttributedString(_ attString: NSAttributedString) {
if !Thread.isMainThread {
DispatchQueue.main.async {
self.appendAttributedString(attString)
}
return
}
validateButtons()
}
func appendLogItem(_ logItem: LogItem) {
}
func attributedString(for logItem: LogItem) -> NSAttributedString {
return NSAttributedString() //TODO
}
func validateButtons() {
}