Skip to content

Commit 0ebf841

Browse files
yusufozgullukepistrol
authored andcommitted
Change overscroll strategy
1 parent 4936418 commit 0ebf841

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

Sources/CodeEditTextView/CodeEditTextView.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public struct CodeEditTextView: NSViewControllerRepresentable {
2727
font: Binding<NSFont>,
2828
tabWidth: Binding<Int>,
2929
lineHeight: Binding<Double>,
30-
overScrollLineCount: Binding<Int> = .constant(0),
30+
overScrollRatio: Binding<Double> = .constant(0.0),
3131
cursorPosition: Published<(Int, Int)>.Publisher? = nil
3232
) {
3333
self._text = text
@@ -36,7 +36,7 @@ public struct CodeEditTextView: NSViewControllerRepresentable {
3636
self._font = font
3737
self._tabWidth = tabWidth
3838
self._lineHeight = lineHeight
39-
self._overScrollLineCount = overScrollLineCount
39+
self._overScrollRatio = overScrollRatio
4040
self.cursorPosition = cursorPosition
4141
}
4242

@@ -46,7 +46,7 @@ public struct CodeEditTextView: NSViewControllerRepresentable {
4646
@Binding private var font: NSFont
4747
@Binding private var tabWidth: Int
4848
@Binding private var lineHeight: Double
49-
@Binding private var overScrollLineCount: Int
49+
@Binding private var overScrollRatio: Double
5050
private var cursorPosition: Published<(Int, Int)>.Publisher?
5151

5252
public typealias NSViewControllerType = STTextViewController
@@ -59,7 +59,7 @@ public struct CodeEditTextView: NSViewControllerRepresentable {
5959
theme: theme,
6060
tabWidth: tabWidth,
6161
cursorPosition: cursorPosition,
62-
overScrollLineCount: overScrollLineCount
62+
overScrollRatio: overScrollRatio
6363
)
6464
controller.lineHeightMultiple = lineHeight
6565
return controller
@@ -71,7 +71,7 @@ public struct CodeEditTextView: NSViewControllerRepresentable {
7171
controller.theme = theme
7272
controller.tabWidth = tabWidth
7373
controller.lineHeightMultiple = lineHeight
74-
controller.overScrollLineCount = overScrollLineCount
74+
controller.overScrollRatio = overScrollRatio
7575
controller.reloadUI()
7676
return
7777
}

Sources/CodeEditTextView/STTextViewController.swift

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt
4343
public var font: NSFont
4444

4545
/// The overScrollLineCount to use for the textView over scroll
46-
public var overScrollLineCount: Int
46+
public var overScrollRatio: Double
4747

4848
// MARK: - Highlighting
4949

@@ -59,15 +59,15 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt
5959
theme: EditorTheme,
6060
tabWidth: Int,
6161
cursorPosition: Published<(Int, Int)>.Publisher? = nil,
62-
overScrollLineCount: Int
62+
overScrollRatio: Double
6363
) {
6464
self.text = text
6565
self.language = language
6666
self.font = font
6767
self.theme = theme
6868
self.tabWidth = tabWidth
6969
self.cursorPosition = cursorPosition
70-
self.overScrollLineCount = overScrollLineCount
70+
self.overScrollRatio = overScrollRatio
7171
super.init(nibName: nil, bundle: nil)
7272
}
7373

@@ -117,8 +117,6 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt
117117

118118
scrollView.translatesAutoresizingMaskIntoConstraints = false
119119

120-
scrollView.contentInsets.bottom = Double(overScrollLineCount) * lineHeight
121-
122120
self.view = scrollView
123121

124122
NSLayoutConstraint.activate([
@@ -143,6 +141,11 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt
143141
self.cursorPositionCancellable = self.cursorPosition?.sink(receiveValue: { value in
144142
self.setCursorPosition(value)
145143
})
144+
145+
NotificationCenter.default.addObserver(forName: NSWindow.didResizeNotification, object: nil, queue: .main) { [weak self] _ in
146+
guard let self = self else { return }
147+
(self.view as? NSScrollView)?.contentView.contentInsets.bottom = self.bottomContentInsets
148+
}
146149
}
147150

148151
internal func setUpHighlighting() {
@@ -176,6 +179,18 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt
176179
return paragraph
177180
}
178181

182+
/// ScrollView's bottom inset using as editor overscroll
183+
private var bottomContentInsets: CGFloat {
184+
let height = view.frame.height
185+
var inset = overScrollRatio * height
186+
187+
if height - inset < lineHeight {
188+
inset = height - lineHeight
189+
}
190+
191+
return max(inset, .zero)
192+
}
193+
179194
/// Reloads the UI to apply changes to ``STTextViewController/font``, ``STTextViewController/theme``, ...
180195
internal func reloadUI() {
181196
textView?.font = font
@@ -189,7 +204,7 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt
189204
rulerView?.separatorColor = theme.invisibles
190205
rulerView?.baselineOffset = baselineOffset
191206

192-
(view as? NSScrollView)?.contentInsets.bottom = Double(overScrollLineCount) * lineHeight
207+
(view as? NSScrollView)?.contentView.contentInsets.bottom = bottomContentInsets
193208

194209
setStandardAttributes()
195210
}

0 commit comments

Comments
 (0)