Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update GutterView Insets Correctly #240

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
},
{
Expand Down Expand Up @@ -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"
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,13 @@ 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,
textColor: .secondaryLabelColor,
textView: textView,
delegate: self
)
gutterView.frame.origin.y = -scrollView.contentInsets.top
gutterView.updateWidthIfNeeded()
scrollView.addFloatingSubview(
gutterView,
Expand All @@ -45,8 +40,8 @@ extension TextViewController {
}

styleTextView()
styleGutterView()
styleScrollView()
styleGutterView()
setUpHighlighter()
setUpTextFormation()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,9 @@ public class TextViewController: NSViewController {
textView.isEditable = isEditable
textView.isSelectable = isSelectable

styleScrollView()
styleTextView()
styleGutterView()
styleScrollView()

highlighter?.invalidate()
}
Expand Down Expand Up @@ -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)
Expand All @@ -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
}
Expand Down
14 changes: 10 additions & 4 deletions Tests/CodeEditSourceEditorTests/TextViewControllerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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 {
Expand Down
Loading