Skip to content

Commit 7360f00

Browse files
Update GutterView Insets Correctly (#240)
### Description - Refactors a few style functions to correctly update the gutter view's position when the content insets change. - Adds the gutter view position to the content insets test case. ### Related Issues * Required to finish [CodeEdit#1603](CodeEditApp/CodeEdit#1603) ### 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 ### Screenshots N/A
1 parent a72e6c9 commit 7360f00

File tree

5 files changed

+22
-17
lines changed

5 files changed

+22
-17
lines changed

Package.resolved

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
"kind" : "remoteSourceControl",
1515
"location" : "https://github.com/CodeEditApp/CodeEditTextView.git",
1616
"state" : {
17-
"revision" : "6653c21a603babf365a12d4d331fadc8f8b52d99",
18-
"version" : "0.7.2"
17+
"revision" : "86b980464bcb67693e2053283c7a99bdc6f358bc",
18+
"version" : "0.7.3"
1919
}
2020
},
2121
{
@@ -68,8 +68,8 @@
6868
"kind" : "remoteSourceControl",
6969
"location" : "https://github.com/ChimeHQ/TextFormation",
7070
"state" : {
71-
"revision" : "b4987856bc860643ac2c9cdbc7d5f3e9ade68377",
72-
"version" : "0.8.1"
71+
"revision" : "f6faed6abd768ae95b70d10113d4008a7cac57a7",
72+
"version" : "0.8.2"
7373
}
7474
},
7575
{

Package.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ let package = Package(
3232
// Rules for indentation, pair completion, whitespace
3333
.package(
3434
url: "https://github.com/ChimeHQ/TextFormation",
35-
from: "0.8.1"
35+
from: "0.8.2"
3636
)
3737
],
3838
targets: [

Sources/CodeEditSourceEditor/Controller/TextViewController+LoadView.swift

+1-6
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,13 @@ extension TextViewController {
2121
scrollView.hasHorizontalScroller = true
2222
scrollView.documentView = textView
2323
scrollView.contentView.postsBoundsChangedNotifications = true
24-
if let contentInsets {
25-
scrollView.automaticallyAdjustsContentInsets = false
26-
scrollView.contentInsets = contentInsets
27-
}
2824

2925
gutterView = GutterView(
3026
font: font.rulerFont,
3127
textColor: .secondaryLabelColor,
3228
textView: textView,
3329
delegate: self
3430
)
35-
gutterView.frame.origin.y = -scrollView.contentInsets.top
3631
gutterView.updateWidthIfNeeded()
3732
scrollView.addFloatingSubview(
3833
gutterView,
@@ -45,8 +40,8 @@ extension TextViewController {
4540
}
4641

4742
styleTextView()
48-
styleGutterView()
4943
styleScrollView()
44+
styleGutterView()
5045
setUpHighlighter()
5146
setUpTextFormation()
5247

Sources/CodeEditSourceEditor/Controller/TextViewController.swift

+6-2
Original file line numberDiff line numberDiff line change
@@ -264,9 +264,9 @@ public class TextViewController: NSViewController {
264264
textView.isEditable = isEditable
265265
textView.isSelectable = isSelectable
266266

267+
styleScrollView()
267268
styleTextView()
268269
styleGutterView()
269-
styleScrollView()
270270

271271
highlighter?.invalidate()
272272
}
@@ -297,6 +297,7 @@ public class TextViewController: NSViewController {
297297

298298
/// Style the gutter view.
299299
package func styleGutterView() {
300+
gutterView.frame.origin.y = -scrollView.contentInsets.top
300301
gutterView.selectedLineColor = useThemeBackground ? theme.lineHighlight : systemAppearance == .darkAqua
301302
? NSColor.quaternaryLabelColor
302303
: NSColor.selectedTextBackgroundColor.withSystemEffect(.disabled)
@@ -314,8 +315,11 @@ public class TextViewController: NSViewController {
314315
guard let scrollView = view as? NSScrollView else { return }
315316
scrollView.drawsBackground = useThemeBackground
316317
scrollView.backgroundColor = useThemeBackground ? theme.background : .clear
317-
if let contentInsets = contentInsets {
318+
if let contentInsets {
319+
scrollView.automaticallyAdjustsContentInsets = false
318320
scrollView.contentInsets = contentInsets
321+
} else {
322+
scrollView.automaticallyAdjustsContentInsets = true
319323
}
320324
scrollView.contentInsets.bottom = (contentInsets?.bottom ?? 0) + bottomContentInsets
321325
}

Tests/CodeEditSourceEditorTests/TextViewControllerTests.swift

+10-4
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,12 @@ final class TextViewControllerTests: XCTestCase {
112112

113113
func test_editorInsets() throws {
114114
let scrollView = try XCTUnwrap(controller.view as? NSScrollView)
115-
scrollView.frame = .init(x: .zero,
116-
y: .zero,
117-
width: 100,
118-
height: 100)
115+
scrollView.frame = .init(
116+
x: .zero,
117+
y: .zero,
118+
width: 100,
119+
height: 100
120+
)
119121

120122
func assertInsetsEqual(_ lhs: NSEdgeInsets, _ rhs: NSEdgeInsets) throws {
121123
XCTAssertEqual(lhs.top, rhs.top)
@@ -130,18 +132,21 @@ final class TextViewControllerTests: XCTestCase {
130132

131133
// contentInsets: 0
132134
try assertInsetsEqual(scrollView.contentInsets, NSEdgeInsets(top: 0, left: 0, bottom: 0, right: 0))
135+
XCTAssertEqual(controller.gutterView.frame.origin.y, 0)
133136

134137
// contentInsets: 16
135138
controller.contentInsets = NSEdgeInsets(top: 16, left: 16, bottom: 16, right: 16)
136139
controller.reloadUI()
137140

138141
try assertInsetsEqual(scrollView.contentInsets, NSEdgeInsets(top: 16, left: 16, bottom: 16, right: 16))
142+
XCTAssertEqual(controller.gutterView.frame.origin.y, -16)
139143

140144
// contentInsets: different
141145
controller.contentInsets = NSEdgeInsets(top: 32.5, left: 12.3, bottom: 20, right: 1)
142146
controller.reloadUI()
143147

144148
try assertInsetsEqual(scrollView.contentInsets, NSEdgeInsets(top: 32.5, left: 12.3, bottom: 20, right: 1))
149+
XCTAssertEqual(controller.gutterView.frame.origin.y, -32.5)
145150

146151
// contentInsets: 16
147152
// editorOverscroll: 0.5
@@ -150,6 +155,7 @@ final class TextViewControllerTests: XCTestCase {
150155
controller.reloadUI()
151156

152157
try assertInsetsEqual(scrollView.contentInsets, NSEdgeInsets(top: 16, left: 16, bottom: 16 + 50, right: 16))
158+
XCTAssertEqual(controller.gutterView.frame.origin.y, -16)
153159
}
154160

155161
func test_editorOverScroll_ZeroCondition() throws {

0 commit comments

Comments
 (0)