-
Notifications
You must be signed in to change notification settings - Fork 136
Expand file tree
/
Copy pathTextViewController+StyleViews.swift
More file actions
124 lines (106 loc) · 4.95 KB
/
TextViewController+StyleViews.swift
File metadata and controls
124 lines (106 loc) · 4.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//
// TextViewController+StyleViews.swift
// CodeEditSourceEditor
//
// Created by Khan Winter on 7/3/24.
//
import AppKit
import CodeEditTextView
extension TextViewController {
package func generateParagraphStyle() -> NSMutableParagraphStyle {
// swiftlint:disable:next force_cast
let paragraph = NSParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle
paragraph.tabStops.removeAll()
paragraph.defaultTabInterval = CGFloat(tabWidth) * fontCharWidth
return paragraph
}
/// Style the text view.
package func styleTextView() {
textView.postsFrameChangedNotifications = true
textView.translatesAutoresizingMaskIntoConstraints = false
textView.selectionManager.selectionBackgroundColor = theme.selection
textView.selectionManager.selectedLineBackgroundColor = getThemeBackground()
textView.selectionManager.highlightSelectedLine = isEditable
textView.selectionManager.insertionPointColor = theme.insertionPoint
textView.enclosingScrollView?.backgroundColor = useThemeBackground ? theme.background : .clear
paragraphStyle = generateParagraphStyle()
textView.typingAttributes = attributesFor(nil)
}
/// Finds the preferred use theme background.
/// - Returns: The background color to use.
private func getThemeBackground() -> NSColor {
if useThemeBackground {
return theme.lineHighlight
}
if systemAppearance == .darkAqua {
return NSColor.quaternaryLabelColor
}
return NSColor.selectedTextBackgroundColor.withSystemEffect(.disabled)
}
/// Style the gutter view.
package func styleGutterView() {
gutterView.selectedLineColor = useThemeBackground ? theme.lineHighlight : systemAppearance == .darkAqua
? NSColor.quaternaryLabelColor
: NSColor.selectedTextBackgroundColor.withSystemEffect(.disabled)
gutterView.highlightSelectedLines = isEditable
gutterView.font = font.rulerFont
gutterView.backgroundColor = useThemeBackground ? theme.background : .windowBackgroundColor
if self.isEditable == false {
gutterView.selectedLineTextColor = nil
gutterView.selectedLineColor = .clear
}
}
/// Style the scroll view.
package func styleScrollView() {
scrollView.translatesAutoresizingMaskIntoConstraints = false
scrollView.contentView.postsFrameChangedNotifications = true
scrollView.hasVerticalScroller = true
scrollView.hasHorizontalScroller = !wrapLines
scrollView.scrollerStyle = .overlay
}
package func styleMinimapView() {
minimapView.postsFrameChangedNotifications = true
minimapView.isHidden = !showMinimap
}
/// Updates all relevant content insets including the find panel, scroll view, minimap and gutter position.
package func updateContentInsets() {
updateTextInsets()
scrollView.contentView.postsBoundsChangedNotifications = true
if let contentInsets {
scrollView.automaticallyAdjustsContentInsets = false
scrollView.contentInsets = contentInsets
minimapView.scrollView.automaticallyAdjustsContentInsets = false
minimapView.scrollView.contentInsets.top = contentInsets.top
minimapView.scrollView.contentInsets.bottom = contentInsets.bottom
} else {
scrollView.automaticallyAdjustsContentInsets = true
minimapView.scrollView.automaticallyAdjustsContentInsets = true
}
// `additionalTextInsets` only effects text content.
scrollView.contentInsets.top += additionalTextInsets?.top ?? 0
scrollView.contentInsets.bottom += additionalTextInsets?.bottom ?? 0
minimapView.scrollView.contentInsets.top += additionalTextInsets?.top ?? 0
minimapView.scrollView.contentInsets.bottom += additionalTextInsets?.bottom ?? 0
// Inset the top by the find panel height
let findInset: CGFloat = if findViewController?.viewModel.isShowingFindPanel ?? false {
findViewController?.viewModel.panelHeight ?? 0
} else {
0
}
scrollView.contentInsets.top += findInset
minimapView.scrollView.contentInsets.top += findInset
findViewController?.topPadding = contentInsets?.top
gutterView.frame.origin.y = textView.frame.origin.y - scrollView.contentInsets.top
// Update scrollview tiling
scrollView.reflectScrolledClipView(scrollView.contentView)
minimapView.scrollView.reflectScrolledClipView(minimapView.scrollView.contentView)
}
/// Updates the text view's text insets. See ``textViewInsets`` for calculation.
func updateTextInsets() {
// Allow this method to be called before ``loadView()``
guard textView != nil, minimapView != nil else { return }
if textView.textInsets != textViewInsets {
textView.textInsets = textViewInsets
}
}
}