Skip to content

Move Overscroll To TextView #296

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

Merged
merged 3 commits into from
Apr 7, 2025
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
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
//
// TextViewController+IndentLines.swift
//
// CodeEditTextView
//
// Created by Ludwig, Tom on 11.09.24.
//

import CodeEditTextView
import AppKit
import CodeEditTextView

extension TextViewController {
/// Handles indentation and unindentation
Expand All @@ -23,7 +23,7 @@ extension TextViewController {
guard !cursorPositions.isEmpty else { return }

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,6 @@ extension TextViewController {
} else {
scrollView.automaticallyAdjustsContentInsets = true
}
scrollView.contentInsets.bottom = (contentInsets?.bottom ?? 0) + bottomContentInsets
scrollView.contentInsets.bottom = contentInsets?.bottom ?? 0
}
}
18 changes: 5 additions & 13 deletions Sources/CodeEditSourceEditor/Controller/TextViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,11 @@ public class TextViewController: NSViewController {
///
/// Measured in a percentage of the view's total height, meaning a `0.3` value will result in overscroll
/// of 1/3 of the view.
public var editorOverscroll: CGFloat
public var editorOverscroll: CGFloat {
didSet {
textView.overscrollAmount = editorOverscroll
}
}

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

internal var cancellables = Set<AnyCancellable>()

/// ScrollView's bottom inset using as editor overscroll
package var bottomContentInsets: CGFloat {
let height = view.frame.height
var inset = editorOverscroll * height

if height - inset < font.lineHeight * lineHeightMultiple {
inset = height - font.lineHeight * lineHeightMultiple
}

return max(inset, .zero)
}

/// The trailing inset for the editor. Grows when line wrapping is disabled.
package var textViewTrailingInset: CGFloat {
// See https://github.com/CodeEditApp/CodeEditTextView/issues/66
Expand Down
21 changes: 5 additions & 16 deletions Tests/CodeEditSourceEditorTests/TextViewControllerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,30 +66,19 @@ final class TextViewControllerTests: XCTestCase {
// MARK: Overscroll

func test_editorOverScroll() throws {
let scrollView = try XCTUnwrap(controller.view as? NSScrollView)
scrollView.frame = .init(x: .zero,
y: .zero,
width: 100,
height: 100)

controller.editorOverscroll = 0
controller.contentInsets = nil
controller.reloadUI()

// editorOverscroll: 0
XCTAssertEqual(scrollView.contentView.contentInsets.bottom, 0)
XCTAssertEqual(controller.textView.overscrollAmount, 0)

controller.editorOverscroll = 0.5
controller.reloadUI()

// editorOverscroll: 0.5
XCTAssertEqual(scrollView.contentView.contentInsets.bottom, 50.0)
XCTAssertEqual(controller.textView.overscrollAmount, 0.5)

controller.editorOverscroll = 1.0
controller.reloadUI()

// editorOverscroll: 1.0
XCTAssertEqual(scrollView.contentView.contentInsets.bottom, 87.0)
XCTAssertEqual(controller.textView.overscrollAmount, 1.0)
}

// MARK: Insets
Expand Down Expand Up @@ -135,10 +124,10 @@ final class TextViewControllerTests: XCTestCase {
// contentInsets: 16
// editorOverscroll: 0.5
controller.contentInsets = NSEdgeInsets(top: 16, left: 16, bottom: 16, right: 16)
controller.editorOverscroll = 0.5
controller.editorOverscroll = 0.5 // Should be ignored
controller.reloadUI()

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

Expand Down