-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathTextViewController.swift
More file actions
101 lines (90 loc) · 3.1 KB
/
TextViewController.swift
File metadata and controls
101 lines (90 loc) · 3.1 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
//
// TextViewController.swift
// CodeEditTextViewExample
//
// Created by Khan Winter on 1/9/25.
//
import AppKit
import CodeEditTextView
class TextViewController: NSViewController {
var scrollView: NSScrollView!
var textView: TextView!
var enableEdgeInsets: Bool = false {
didSet {
if enableEdgeInsets {
textView.edgeInsets = .init(left: 20, right: 30)
textView.textInsets = .init(left: 10, right: 30)
} else {
textView.edgeInsets = .zero
textView.textInsets = .zero
}
}
}
var wrapLines: Bool = true {
didSet {
textView.wrapLines = wrapLines
}
}
var useSystemCursor: Bool = false {
didSet {
textView.useSystemCursor = useSystemCursor
// Force cursor update by temporarily removing and re-adding the selection
if let range = textView.selectionManager.textSelections.first?.range {
textView.selectionManager.setSelectedRange(NSRange(location: range.location, length: 0))
}
}
}
var isSelectable: Bool = true {
didSet {
textView.isSelectable = isSelectable
}
}
var isEditable: Bool = true {
didSet {
textView.isEditable = isEditable
}
}
init(string: String) {
textView = TextView(string: string)
super.init(nibName: nil, bundle: nil)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func loadView() {
scrollView = NSScrollView()
textView.translatesAutoresizingMaskIntoConstraints = false
textView.wrapLines = wrapLines
if enableEdgeInsets {
textView.edgeInsets = .init(left: 30, right: 30)
textView.textInsets = .init(left: 0, right: 30)
} else {
textView.edgeInsets = .zero
textView.textInsets = .zero
}
scrollView.translatesAutoresizingMaskIntoConstraints = false
scrollView.documentView = textView
scrollView.contentView.postsFrameChangedNotifications = true
scrollView.contentView.postsBoundsChangedNotifications = true
scrollView.hasVerticalScroller = true
self.view = scrollView
NSLayoutConstraint.activate([
scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
scrollView.topAnchor.constraint(equalTo: view.topAnchor),
scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
])
// Layout on scroll change
NotificationCenter.default.addObserver(
forName: NSView.frameDidChangeNotification,
object: scrollView.contentView,
queue: .main
) { [weak self] _ in
self?.textView.updatedViewport(self?.scrollView.documentVisibleRect ?? .zero)
}
textView.updateFrameIfNeeded()
}
deinit {
NotificationCenter.default.removeObserver(self)
}
}