From 584a552be48926e8ed35294fd1d8b6731cc45af9 Mon Sep 17 00:00:00 2001 From: Khan Winter <35942988+thecoolwinter@users.noreply.github.com> Date: Fri, 8 Mar 2024 21:09:13 -0600 Subject: [PATCH] Update GutterView Insets Correctly --- Package.resolved | 8 ++++---- Package.swift | 2 +- .../Controller/TextViewController+LoadView.swift | 7 +------ .../Controller/TextViewController.swift | 8 ++++++-- .../TextViewControllerTests.swift | 14 ++++++++++---- 5 files changed, 22 insertions(+), 17 deletions(-) diff --git a/Package.resolved b/Package.resolved index 0f28e643e..08f1665e7 100644 --- a/Package.resolved +++ b/Package.resolved @@ -14,8 +14,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/CodeEditApp/CodeEditTextView.git", "state" : { - "revision" : "6653c21a603babf365a12d4d331fadc8f8b52d99", - "version" : "0.7.2" + "revision" : "86b980464bcb67693e2053283c7a99bdc6f358bc", + "version" : "0.7.3" } }, { @@ -68,8 +68,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/ChimeHQ/TextFormation", "state" : { - "revision" : "b4987856bc860643ac2c9cdbc7d5f3e9ade68377", - "version" : "0.8.1" + "revision" : "f6faed6abd768ae95b70d10113d4008a7cac57a7", + "version" : "0.8.2" } }, { diff --git a/Package.swift b/Package.swift index 8362b19be..02e835b4f 100644 --- a/Package.swift +++ b/Package.swift @@ -32,7 +32,7 @@ let package = Package( // Rules for indentation, pair completion, whitespace .package( url: "https://github.com/ChimeHQ/TextFormation", - from: "0.8.1" + from: "0.8.2" ) ], targets: [ diff --git a/Sources/CodeEditSourceEditor/Controller/TextViewController+LoadView.swift b/Sources/CodeEditSourceEditor/Controller/TextViewController+LoadView.swift index 8bc5b2f35..598359647 100644 --- a/Sources/CodeEditSourceEditor/Controller/TextViewController+LoadView.swift +++ b/Sources/CodeEditSourceEditor/Controller/TextViewController+LoadView.swift @@ -21,10 +21,6 @@ extension TextViewController { scrollView.hasHorizontalScroller = true scrollView.documentView = textView scrollView.contentView.postsBoundsChangedNotifications = true - if let contentInsets { - scrollView.automaticallyAdjustsContentInsets = false - scrollView.contentInsets = contentInsets - } gutterView = GutterView( font: font.rulerFont, @@ -32,7 +28,6 @@ extension TextViewController { textView: textView, delegate: self ) - gutterView.frame.origin.y = -scrollView.contentInsets.top gutterView.updateWidthIfNeeded() scrollView.addFloatingSubview( gutterView, @@ -45,8 +40,8 @@ extension TextViewController { } styleTextView() - styleGutterView() styleScrollView() + styleGutterView() setUpHighlighter() setUpTextFormation() diff --git a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift index 605f5d840..d2ae830f1 100644 --- a/Sources/CodeEditSourceEditor/Controller/TextViewController.swift +++ b/Sources/CodeEditSourceEditor/Controller/TextViewController.swift @@ -264,9 +264,9 @@ public class TextViewController: NSViewController { textView.isEditable = isEditable textView.isSelectable = isSelectable + styleScrollView() styleTextView() styleGutterView() - styleScrollView() highlighter?.invalidate() } @@ -297,6 +297,7 @@ public class TextViewController: NSViewController { /// Style the gutter view. package func styleGutterView() { + gutterView.frame.origin.y = -scrollView.contentInsets.top gutterView.selectedLineColor = useThemeBackground ? theme.lineHighlight : systemAppearance == .darkAqua ? NSColor.quaternaryLabelColor : NSColor.selectedTextBackgroundColor.withSystemEffect(.disabled) @@ -314,8 +315,11 @@ public class TextViewController: NSViewController { guard let scrollView = view as? NSScrollView else { return } scrollView.drawsBackground = useThemeBackground scrollView.backgroundColor = useThemeBackground ? theme.background : .clear - if let contentInsets = contentInsets { + if let contentInsets { + scrollView.automaticallyAdjustsContentInsets = false scrollView.contentInsets = contentInsets + } else { + scrollView.automaticallyAdjustsContentInsets = true } scrollView.contentInsets.bottom = (contentInsets?.bottom ?? 0) + bottomContentInsets } diff --git a/Tests/CodeEditSourceEditorTests/TextViewControllerTests.swift b/Tests/CodeEditSourceEditorTests/TextViewControllerTests.swift index 92dbd6c8b..cbea40799 100644 --- a/Tests/CodeEditSourceEditorTests/TextViewControllerTests.swift +++ b/Tests/CodeEditSourceEditorTests/TextViewControllerTests.swift @@ -112,10 +112,12 @@ final class TextViewControllerTests: XCTestCase { func test_editorInsets() throws { let scrollView = try XCTUnwrap(controller.view as? NSScrollView) - scrollView.frame = .init(x: .zero, - y: .zero, - width: 100, - height: 100) + scrollView.frame = .init( + x: .zero, + y: .zero, + width: 100, + height: 100 + ) func assertInsetsEqual(_ lhs: NSEdgeInsets, _ rhs: NSEdgeInsets) throws { XCTAssertEqual(lhs.top, rhs.top) @@ -130,18 +132,21 @@ final class TextViewControllerTests: XCTestCase { // contentInsets: 0 try assertInsetsEqual(scrollView.contentInsets, NSEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)) + XCTAssertEqual(controller.gutterView.frame.origin.y, 0) // contentInsets: 16 controller.contentInsets = NSEdgeInsets(top: 16, left: 16, bottom: 16, right: 16) controller.reloadUI() try assertInsetsEqual(scrollView.contentInsets, NSEdgeInsets(top: 16, left: 16, bottom: 16, right: 16)) + XCTAssertEqual(controller.gutterView.frame.origin.y, -16) // contentInsets: different controller.contentInsets = NSEdgeInsets(top: 32.5, left: 12.3, bottom: 20, right: 1) controller.reloadUI() try assertInsetsEqual(scrollView.contentInsets, NSEdgeInsets(top: 32.5, left: 12.3, bottom: 20, right: 1)) + XCTAssertEqual(controller.gutterView.frame.origin.y, -32.5) // contentInsets: 16 // editorOverscroll: 0.5 @@ -150,6 +155,7 @@ final class TextViewControllerTests: XCTestCase { controller.reloadUI() try assertInsetsEqual(scrollView.contentInsets, NSEdgeInsets(top: 16, left: 16, bottom: 16 + 50, right: 16)) + XCTAssertEqual(controller.gutterView.frame.origin.y, -16) } func test_editorOverScroll_ZeroCondition() throws {