diff --git a/Frameworks/RSCore/RSCore/AppKit/LogWindowController.swift b/Frameworks/RSCore/RSCore/AppKit/LogWindowController.swift index 617721fec..654e5e3b6 100644 --- a/Frameworks/RSCore/RSCore/AppKit/LogWindowController.swift +++ b/Frameworks/RSCore/RSCore/AppKit/LogWindowController.swift @@ -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) { + // MARK: - Notifications - // Adds two line feeds before the text. + @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() { }