Skip to content

Commit 97522e9

Browse files
committed
Use emphasized/unemphasized selection colors and react to window key changes
- Add static emphasized/unemphasized colors and set background based on window key status in viewDidMoveToWindow - Observe NSWindow didBecomeKey/didResignKey to update selection highlight - Preserve appearance-change refresh - Update hierarchy comments to STSelectionView and STSelectionHighlightView
1 parent b80efb3 commit 97522e9

2 files changed

Lines changed: 20 additions & 3 deletions

File tree

Sources/STTextViewAppKit/Overlays/STSelectionHighlightView.swift

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ import AppKit
55

66
final class STSelectionHighlightView: NSView {
77

8-
var backgroundColor: NSColor? = .selectedTextBackgroundColor {
8+
private static let emphasizedColor = NSColor.selectedTextBackgroundColor
9+
private static let unemphasizedColor = NSColor.unemphasizedSelectedTextBackgroundColor
10+
11+
var backgroundColor: NSColor? {
912
didSet {
1013
layer?.backgroundColor = backgroundColor?.cgColor
1114
}
@@ -38,4 +41,18 @@ final class STSelectionHighlightView: NSView {
3841
self.backgroundColor = self.backgroundColor
3942
}
4043
}
44+
45+
override func viewDidMoveToWindow() {
46+
super.viewDidMoveToWindow()
47+
48+
self.backgroundColor = window?.isKeyWindow == true ? Self.emphasizedColor : Self.unemphasizedColor
49+
50+
NotificationCenter.default.addObserver(forName: NSWindow.didBecomeKeyNotification, object: window, queue: .main) { [weak self] notification in
51+
self?.backgroundColor = Self.emphasizedColor
52+
}
53+
54+
NotificationCenter.default.addObserver(forName: NSWindow.didResignKeyNotification, object: window, queue: .main) { [weak self] notification in
55+
self?.backgroundColor = Self.unemphasizedColor
56+
}
57+
}
4158
}

Sources/STTextViewAppKit/STTextView.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
// |---gutterView
88
// |---contentView
99
// |---STInsertionPointView
10-
// |---selectionView
11-
// |---(STLineHighlightView | SelectionHighlightView)
10+
// |---selectionView (STSelectionView)
11+
// |---(STLineHighlightView | STSelectionHighlightView)
1212
// |---contentViewportView
1313
// |---STTextLayoutFragmentView
1414
//

0 commit comments

Comments
 (0)