Skip to content

Commit 8a028a3

Browse files
committed
Fix Text Scrolling Under Panel, Fix Resizing Bug, Fix Test
1 parent 9ed65d6 commit 8a028a3

16 files changed

+292
-545
lines changed

Example/CodeEditSourceEditorExample/CodeEditSourceEditorExample/CodeEditSourceEditorExampleApp.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ struct CodeEditSourceEditorExampleApp: App {
1212
var body: some Scene {
1313
DocumentGroup(newDocument: CodeEditSourceEditorExampleDocument()) { file in
1414
ContentView(document: file.$document, fileURL: file.fileURL)
15+
.toolbar {
16+
Button("Toolbar Item") {
17+
print("Toolbar Item Pressed")
18+
}
19+
}
1520
}
21+
.windowToolbarStyle(.unifiedCompact)
1622
}
1723
}

Example/CodeEditSourceEditorExample/CodeEditSourceEditorExample/Views/ContentView.swift

Lines changed: 79 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -32,92 +32,96 @@ struct ContentView: View {
3232
}
3333

3434
var body: some View {
35-
CodeEditSourceEditor(
36-
$document.text,
37-
language: language,
38-
theme: theme,
39-
font: font,
40-
tabWidth: 4,
41-
lineHeight: 1.2,
42-
wrapLines: wrapLines,
43-
cursorPositions: $cursorPositions,
44-
useThemeBackground: true,
45-
highlightProviders: [treeSitterClient],
46-
useSystemCursor: useSystemCursor
47-
)
48-
.safeAreaInset(edge: .bottom, spacing: 0) {
49-
HStack {
50-
Toggle("Wrap Lines", isOn: $wrapLines)
51-
.toggleStyle(.button)
52-
.buttonStyle(.accessoryBar)
53-
if #available(macOS 14, *) {
54-
Toggle("Use System Cursor", isOn: $useSystemCursor)
55-
.toggleStyle(.button)
56-
.buttonStyle(.accessoryBar)
57-
} else {
58-
Toggle("Use System Cursor", isOn: $useSystemCursor)
59-
.disabled(true)
60-
.help("macOS 14 required")
35+
GeometryReader { proxy in
36+
CodeEditSourceEditor(
37+
$document.text,
38+
language: language,
39+
theme: theme,
40+
font: font,
41+
tabWidth: 4,
42+
lineHeight: 1.2,
43+
wrapLines: wrapLines,
44+
cursorPositions: $cursorPositions,
45+
useThemeBackground: true,
46+
highlightProviders: [treeSitterClient],
47+
contentInsets: NSEdgeInsets(top: proxy.safeAreaInsets.top, left: 0, bottom: 0, right: 0),
48+
useSystemCursor: useSystemCursor
49+
)
50+
.safeAreaInset(edge: .bottom, spacing: 0) {
51+
HStack {
52+
Toggle("Wrap Lines", isOn: $wrapLines)
6153
.toggleStyle(.button)
6254
.buttonStyle(.accessoryBar)
63-
}
55+
if #available(macOS 14, *) {
56+
Toggle("Use System Cursor", isOn: $useSystemCursor)
57+
.toggleStyle(.button)
58+
.buttonStyle(.accessoryBar)
59+
} else {
60+
Toggle("Use System Cursor", isOn: $useSystemCursor)
61+
.disabled(true)
62+
.help("macOS 14 required")
63+
.toggleStyle(.button)
64+
.buttonStyle(.accessoryBar)
65+
}
6466

65-
Spacer()
66-
Group {
67-
if isInLongParse {
68-
HStack(spacing: 5) {
69-
ProgressView()
70-
.controlSize(.small)
71-
Text("Parsing Document")
67+
Spacer()
68+
Group {
69+
if isInLongParse {
70+
HStack(spacing: 5) {
71+
ProgressView()
72+
.controlSize(.small)
73+
Text("Parsing Document")
74+
}
75+
} else {
76+
Text(getLabel(cursorPositions))
7277
}
73-
} else {
74-
Text(getLabel(cursorPositions))
7578
}
76-
}
77-
.foregroundStyle(.secondary)
78-
Divider()
79-
.frame(height: 12)
80-
LanguagePicker(language: $language)
81-
.buttonStyle(.borderless)
82-
}
83-
.font(.subheadline)
84-
.fontWeight(.medium)
85-
.controlSize(.small)
86-
.padding(.horizontal, 8)
87-
.frame(height: 28)
88-
.background(.bar)
89-
.overlay(alignment: .top) {
90-
VStack {
79+
.foregroundStyle(.secondary)
9180
Divider()
92-
.overlay {
93-
if colorScheme == .dark {
94-
Color.black
81+
.frame(height: 12)
82+
LanguagePicker(language: $language)
83+
.buttonStyle(.borderless)
84+
}
85+
.font(.subheadline)
86+
.fontWeight(.medium)
87+
.controlSize(.small)
88+
.padding(.horizontal, 8)
89+
.frame(height: 28)
90+
.background(.bar)
91+
.overlay(alignment: .top) {
92+
VStack {
93+
Divider()
94+
.overlay {
95+
if colorScheme == .dark {
96+
Color.black
97+
}
9598
}
96-
}
99+
}
100+
}
101+
.zIndex(2)
102+
.onAppear {
103+
self.language = detectLanguage(fileURL: fileURL) ?? .default
104+
self.theme = colorScheme == .dark ? .dark : .light
97105
}
98106
}
99-
.zIndex(2)
100-
.onAppear {
101-
self.language = detectLanguage(fileURL: fileURL) ?? .default
102-
self.theme = colorScheme == .dark ? .dark : .light
103-
}
104-
}
105-
.frame(maxWidth: .infinity, maxHeight: .infinity)
106-
.onReceive(NotificationCenter.default.publisher(for: TreeSitterClient.Constants.longParse)) { _ in
107-
withAnimation(.easeIn(duration: 0.1)) {
108-
isInLongParse = true
107+
.ignoresSafeArea()
108+
.frame(maxWidth: .infinity, maxHeight: .infinity)
109+
.onReceive(NotificationCenter.default.publisher(for: TreeSitterClient.Constants.longParse)) { _ in
110+
withAnimation(.easeIn(duration: 0.1)) {
111+
isInLongParse = true
112+
}
109113
}
110-
}
111-
.onReceive(NotificationCenter.default.publisher(for: TreeSitterClient.Constants.longParseFinished)) { _ in
112-
withAnimation(.easeIn(duration: 0.1)) {
113-
isInLongParse = false
114+
.onReceive(NotificationCenter.default.publisher(for: TreeSitterClient.Constants.longParseFinished)) { _ in
115+
withAnimation(.easeIn(duration: 0.1)) {
116+
isInLongParse = false
117+
}
114118
}
115-
}
116-
.onChange(of: colorScheme) { _, newValue in
117-
if newValue == .dark {
118-
theme = .dark
119-
} else {
120-
theme = .light
119+
.onChange(of: colorScheme) { _, newValue in
120+
if newValue == .dark {
121+
theme = .dark
122+
} else {
123+
theme = .light
124+
}
121125
}
122126
}
123127
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//
2+
// TextViewController+FindPanelTarget.swift
3+
// CodeEditSourceEditor
4+
//
5+
// Created by Khan Winter on 3/16/25.
6+
//
7+
8+
import Foundation
9+
import CodeEditTextView
10+
11+
extension TextViewController: FindPanelTarget {
12+
func findPanelWillShow(panelHeight: CGFloat) {
13+
scrollView.contentInsets.top += panelHeight
14+
gutterView.frame.origin.y = -scrollView.contentInsets.top
15+
}
16+
17+
func findPanelWillHide(panelHeight: CGFloat) {
18+
scrollView.contentInsets.top -= panelHeight
19+
gutterView.frame.origin.y = -scrollView.contentInsets.top
20+
}
21+
22+
var emphasizeAPI: EmphasizeAPI? {
23+
textView?.emphasizeAPI
24+
}
25+
}

Sources/CodeEditSourceEditor/Controller/TextViewController+LoadView.swift

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,7 @@ extension TextViewController {
1414
super.loadView()
1515

1616
scrollView = NSScrollView()
17-
textView.postsFrameChangedNotifications = true
18-
textView.translatesAutoresizingMaskIntoConstraints = false
19-
scrollView.translatesAutoresizingMaskIntoConstraints = false
20-
scrollView.contentView.postsFrameChangedNotifications = true
21-
scrollView.hasVerticalScroller = true
22-
scrollView.hasHorizontalScroller = !wrapLines
2317
scrollView.documentView = textView
24-
scrollView.contentView.postsBoundsChangedNotifications = true
2518

2619
gutterView = GutterView(
2720
font: font.rulerFont,

Sources/CodeEditSourceEditor/Controller/TextViewController+StyleViews.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ extension TextViewController {
1919

2020
/// Style the text view.
2121
package func styleTextView() {
22+
textView.postsFrameChangedNotifications = true
23+
textView.translatesAutoresizingMaskIntoConstraints = false
2224
textView.selectionManager.selectionBackgroundColor = theme.selection
2325
textView.selectionManager.selectedLineBackgroundColor = getThemeBackground()
2426
textView.selectionManager.highlightSelectedLine = isEditable
@@ -44,6 +46,7 @@ extension TextViewController {
4446

4547
/// Style the gutter view.
4648
package func styleGutterView() {
49+
// Note: If changing this value, also change in ``findPanelWillShow/Hide()``
4750
gutterView.frame.origin.y = -scrollView.contentInsets.top
4851
gutterView.selectedLineColor = useThemeBackground ? theme.lineHighlight : systemAppearance == .darkAqua
4952
? NSColor.quaternaryLabelColor
@@ -59,7 +62,12 @@ extension TextViewController {
5962

6063
/// Style the scroll view.
6164
package func styleScrollView() {
62-
guard let scrollView = view as? NSScrollView else { return }
65+
scrollView.translatesAutoresizingMaskIntoConstraints = false
66+
scrollView.contentView.postsFrameChangedNotifications = true
67+
scrollView.hasVerticalScroller = true
68+
scrollView.hasHorizontalScroller = !wrapLines
69+
70+
scrollView.contentView.postsBoundsChangedNotifications = true
6371
if let contentInsets {
6472
scrollView.automaticallyAdjustsContentInsets = false
6573
scrollView.contentInsets = contentInsets

Sources/CodeEditSourceEditor/Extensions/TextViewController+SearchTarget.swift

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
//
2-
// FindTarget.swift
2+
// FindPanelTarget.swift
33
// CodeEditSourceEditor
44
//
55
// Created by Khan Winter on 3/10/25.
66
//
77

8+
import Foundation
9+
810
// This dependency is not ideal, maybe we could make this another protocol that the emphasize API conforms to similar
911
// to this one?
1012
import CodeEditTextView
1113

12-
protocol FindTarget: AnyObject {
14+
protocol FindPanelTarget: AnyObject {
1315
var emphasizeAPI: EmphasizeAPI? { get }
1416
var text: String { get }
1517

1618
var cursorPositions: [CursorPosition] { get }
1719
func setCursorPositions(_ positions: [CursorPosition])
1820
func updateCursorPosition()
21+
22+
func findPanelWillShow(panelHeight: CGFloat)
23+
func findPanelWillHide(panelHeight: CGFloat)
1924
}

0 commit comments

Comments
 (0)