Skip to content

Commit 603e89c

Browse files
Move Overscroll To TextView (#296)
### Description The current overscroll behavior that was being handled in the SourceEditor relied on a content inset, which was a little buggy and did not allow the user to click in the over scrolled area to set the cursor. This behavior was moved to TextView instead in order to fix this. ### Related Issues ### Checklist - [x] I read and understood the [contributing guide](https://github.com/CodeEditApp/CodeEdit/blob/main/CONTRIBUTING.md) as well as the [code of conduct](https://github.com/CodeEditApp/CodeEdit/blob/main/CODE_OF_CONDUCT.md) - [x] The issues this PR addresses are related to each other - [x] My changes generate no new warnings - [x] My code builds and runs on my machine - [x] My changes are all related to the related issue above - [x] I documented my code --------- Co-authored-by: Khan Winter <[email protected]>
1 parent 2d3b462 commit 603e89c

File tree

4 files changed

+14
-33
lines changed

4 files changed

+14
-33
lines changed

Sources/CodeEditSourceEditor/Controller/TextViewController+IndentLines.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
//
22
// TextViewController+IndentLines.swift
3-
//
3+
// CodeEditTextView
44
//
55
// Created by Ludwig, Tom on 11.09.24.
66
//
77

8-
import CodeEditTextView
98
import AppKit
9+
import CodeEditTextView
1010

1111
extension TextViewController {
1212
/// Handles indentation and unindentation
@@ -23,7 +23,7 @@ extension TextViewController {
2323
guard !cursorPositions.isEmpty else { return }
2424

2525
textView.undoManager?.beginUndoGrouping()
26-
for cursorPosition in self.cursorPositions.reversed() {
26+
for cursorPosition in self.cursorPositions.reversed() {
2727
// get lineindex, i.e line-numbers+1
2828
guard let lineIndexes = getHighlightedLines(for: cursorPosition.range) else { continue }
2929

Sources/CodeEditSourceEditor/Controller/TextViewController+StyleViews.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,6 @@ extension TextViewController {
6767
} else {
6868
scrollView.automaticallyAdjustsContentInsets = true
6969
}
70-
scrollView.contentInsets.bottom = (contentInsets?.bottom ?? 0) + bottomContentInsets
70+
scrollView.contentInsets.bottom = contentInsets?.bottom ?? 0
7171
}
7272
}

Sources/CodeEditSourceEditor/Controller/TextViewController.swift

+5-13
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,11 @@ public class TextViewController: NSViewController {
104104
///
105105
/// Measured in a percentage of the view's total height, meaning a `0.3` value will result in overscroll
106106
/// of 1/3 of the view.
107-
public var editorOverscroll: CGFloat
107+
public var editorOverscroll: CGFloat {
108+
didSet {
109+
textView.overscrollAmount = editorOverscroll
110+
}
111+
}
108112

109113
/// Whether the code editor should use the theme background color or be transparent
110114
public var useThemeBackground: Bool
@@ -183,18 +187,6 @@ public class TextViewController: NSViewController {
183187

184188
internal var cancellables = Set<AnyCancellable>()
185189

186-
/// ScrollView's bottom inset using as editor overscroll
187-
package var bottomContentInsets: CGFloat {
188-
let height = view.frame.height
189-
var inset = editorOverscroll * height
190-
191-
if height - inset < font.lineHeight * lineHeightMultiple {
192-
inset = height - font.lineHeight * lineHeightMultiple
193-
}
194-
195-
return max(inset, .zero)
196-
}
197-
198190
/// The trailing inset for the editor. Grows when line wrapping is disabled.
199191
package var textViewTrailingInset: CGFloat {
200192
// See https://github.com/CodeEditApp/CodeEditTextView/issues/66

Tests/CodeEditSourceEditorTests/TextViewControllerTests.swift

+5-16
Original file line numberDiff line numberDiff line change
@@ -66,30 +66,19 @@ final class TextViewControllerTests: XCTestCase {
6666
// MARK: Overscroll
6767

6868
func test_editorOverScroll() throws {
69-
let scrollView = try XCTUnwrap(controller.view as? NSScrollView)
70-
scrollView.frame = .init(x: .zero,
71-
y: .zero,
72-
width: 100,
73-
height: 100)
74-
7569
controller.editorOverscroll = 0
76-
controller.contentInsets = nil
77-
controller.reloadUI()
7870

7971
// editorOverscroll: 0
80-
XCTAssertEqual(scrollView.contentView.contentInsets.bottom, 0)
72+
XCTAssertEqual(controller.textView.overscrollAmount, 0)
8173

8274
controller.editorOverscroll = 0.5
83-
controller.reloadUI()
8475

8576
// editorOverscroll: 0.5
86-
XCTAssertEqual(scrollView.contentView.contentInsets.bottom, 50.0)
77+
XCTAssertEqual(controller.textView.overscrollAmount, 0.5)
8778

8879
controller.editorOverscroll = 1.0
89-
controller.reloadUI()
9080

91-
// editorOverscroll: 1.0
92-
XCTAssertEqual(scrollView.contentView.contentInsets.bottom, 87.0)
81+
XCTAssertEqual(controller.textView.overscrollAmount, 1.0)
9382
}
9483

9584
// MARK: Insets
@@ -135,10 +124,10 @@ final class TextViewControllerTests: XCTestCase {
135124
// contentInsets: 16
136125
// editorOverscroll: 0.5
137126
controller.contentInsets = NSEdgeInsets(top: 16, left: 16, bottom: 16, right: 16)
138-
controller.editorOverscroll = 0.5
127+
controller.editorOverscroll = 0.5 // Should be ignored
139128
controller.reloadUI()
140129

141-
try assertInsetsEqual(scrollView.contentInsets, NSEdgeInsets(top: 16, left: 16, bottom: 16 + 50, right: 16))
130+
try assertInsetsEqual(scrollView.contentInsets, NSEdgeInsets(top: 16, left: 16, bottom: 16, right: 16))
142131
XCTAssertEqual(controller.gutterView.frame.origin.y, -16)
143132
}
144133

0 commit comments

Comments
 (0)