Avoid registering to notification center for transient document twice (#945)

This commit is contained in:
1024jp 2019-05-14 15:09:33 +09:00
parent 79f19ded04
commit 1e24714044
2 changed files with 19 additions and 4 deletions

View File

@ -16,6 +16,7 @@ Change Log
- Fix an issue where the application did crash when the selected text contains some specific characters.
- Fix an issue where needless live document analysis performed even when the status bar and inspector are invisible.
- Fix missing localization.
- Improve stability.

View File

@ -83,6 +83,11 @@ final class DocumentViewController: NSSplitViewController, SyntaxParserDelegate,
name: didUpdateSettingNotification,
object: ThemeManager.shared)
// observe cursor
NotificationCenter.default.addObserver(self, selector: #selector(textViewDidChangeSelection),
name: NSTextView.didChangeSelectionNotification,
object: self.editorViewControllers.first!.textView!)
// observe defaults change
self.defaultsObservers.forEach { $0.invalidate() }
self.defaultsObservers = [
@ -168,9 +173,17 @@ final class DocumentViewController: NSSplitViewController, SyntaxParserDelegate,
/// deliver document to child view controllers
override var representedObject: Any? {
willSet {
guard let document = representedObject as? Document else { return }
NotificationCenter.default.removeObserver(self, name: Document.didChangeSyntaxStyleNotification, object: document)
}
didSet {
guard let document = representedObject as? Document else { return }
// This setter can be invoked twice if the view was initially made for a transient document.
(self.statusBarItem?.viewController as? StatusBarController)?.documentAnalyzer = document.analyzer
document.textStorage.delegate = self
@ -785,6 +798,11 @@ final class DocumentViewController: NSSplitViewController, SyntaxParserDelegate,
currentEditorViewController.textView?.centerSelectionInVisibleArea(self)
newEditorViewController.textView?.centerSelectionInVisibleArea(self)
// observe cursor
NotificationCenter.default.addObserver(self, selector: #selector(textViewDidChangeSelection),
name: NSTextView.didChangeSelectionNotification,
object: newEditorViewController.textView)
// move focus to the new editor
self.view.window?.makeFirstResponder(newEditorViewController.textView)
}
@ -919,10 +937,6 @@ final class DocumentViewController: NSSplitViewController, SyntaxParserDelegate,
textView.baseWritingDirection = baseTextView.baseWritingDirection
textView.isAutomaticTabExpansionEnabled = baseTextView.isAutomaticTabExpansionEnabled
}
NotificationCenter.default.addObserver(self, selector: #selector(textViewDidChangeSelection),
name: NSTextView.didChangeSelectionNotification,
object: editorViewController.textView)
}