Fix leak in EditorTextView

This commit is contained in:
1024jp 2024-03-31 00:54:40 +09:00
parent 0e0d3f41fb
commit e1a026cdcc
2 changed files with 15 additions and 7 deletions

View File

@ -14,6 +14,7 @@
### Fixes
- [beta] Fix a memory leak.
- [beta] Fix an issue on macOS 14 that multiple cursors in editors were not drawn when the editor becomes active again.
- [beta] Add missing localization.

View File

@ -134,7 +134,7 @@ class EditorTextView: NSTextView, Themable, CurrentLineHighlighting, MultiCursor
private var defaultsObservers: Set<AnyCancellable> = []
private var fontObservers: Set<AnyCancellable> = []
private var windowOpacityObserver: AnyCancellable?
private var keyStateObserver: AnyCancellable?
private var keyStateObservers: [any NSObjectProtocol] = []
@ -347,14 +347,21 @@ class EditorTextView: NSTextView, Themable, CurrentLineHighlighting, MultiCursor
self?.lineHighlightColor = self?.theme?.lineHighlightColor(forOpaqueBackground: $0)
}
// observe key window state for insertion points drawing
if #available(macOS 14, *), let window {
self.keyStateObserver = Publishers.Merge(
NotificationCenter.default.publisher(for: NSWindow.didBecomeKeyNotification, object: window),
NotificationCenter.default.publisher(for: NSWindow.didResignKeyNotification, object: window)
)
.sink { [unowned self] _ in self.invalidateInsertionIndicatorDisplayMode() }
self.keyStateObservers = [
NotificationCenter.default.addObserver(forName: NSWindow.didBecomeKeyNotification, object: window, queue: .main) { [weak self] _ in
self?.invalidateInsertionIndicatorDisplayMode()
},
NotificationCenter.default.addObserver(forName: NSWindow.didResignKeyNotification, object: window, queue: .main) { [weak self] _ in
self?.invalidateInsertionIndicatorDisplayMode()
},
]
} else {
for observer in self.keyStateObservers {
NotificationCenter.default.removeObserver(observer)
}
self.keyStateObservers.removeAll()
}
}