From c7016f6c630dfc4f4ad564b43b488307c6047459 Mon Sep 17 00:00:00 2001 From: Abe M Date: Sun, 30 Mar 2025 16:57:16 -0700 Subject: [PATCH 1/5] Added scroll past end behavior --- .../TextSelectionManager.swift | 1 - .../TextView/TextView+Insert.swift | 1 + .../TextView/TextView+Layout.swift | 4 +++- Sources/CodeEditTextView/TextView/TextView.swift | 16 ++++++++++++++++ 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/Sources/CodeEditTextView/TextSelectionManager/TextSelectionManager.swift b/Sources/CodeEditTextView/TextSelectionManager/TextSelectionManager.swift index b6311a9ab..cce5e6853 100644 --- a/Sources/CodeEditTextView/TextSelectionManager/TextSelectionManager.swift +++ b/Sources/CodeEditTextView/TextSelectionManager/TextSelectionManager.swift @@ -221,7 +221,6 @@ public class TextSelectionManager: NSObject { .getLine(atOffset: range.location - (selectedLine.range.location))? .height ?? layoutManager?.estimateLineHeight() - } /// Removes all cursor views and stops the cursor blink timer. diff --git a/Sources/CodeEditTextView/TextView/TextView+Insert.swift b/Sources/CodeEditTextView/TextView/TextView+Insert.swift index 8c4fc408e..a31b42fe2 100644 --- a/Sources/CodeEditTextView/TextView/TextView+Insert.swift +++ b/Sources/CodeEditTextView/TextView/TextView+Insert.swift @@ -6,6 +6,7 @@ // import AppKit +import TextStory extension TextView { override public func insertNewline(_ sender: Any?) { diff --git a/Sources/CodeEditTextView/TextView/TextView+Layout.swift b/Sources/CodeEditTextView/TextView/TextView+Layout.swift index 038dd349a..8626d298c 100644 --- a/Sources/CodeEditTextView/TextView/TextView+Layout.swift +++ b/Sources/CodeEditTextView/TextView/TextView+Layout.swift @@ -64,7 +64,9 @@ extension TextView { public func updateFrameIfNeeded() -> Bool { var availableSize = scrollView?.contentSize ?? .zero availableSize.height -= (scrollView?.contentInsets.top ?? 0) + (scrollView?.contentInsets.bottom ?? 0) - let newHeight = max(layoutManager.estimatedHeight(), availableSize.height) + + let extraHeight = scrollPastEnd ? availableSize.height * scrollPastEndAmount : 0 + let newHeight = max(layoutManager.estimatedHeight() + extraHeight, availableSize.height) let newWidth = layoutManager.estimatedWidth() var didUpdate = false diff --git a/Sources/CodeEditTextView/TextView/TextView.swift b/Sources/CodeEditTextView/TextView/TextView.swift index 84a0a46d6..7f0f74df5 100644 --- a/Sources/CodeEditTextView/TextView/TextView.swift +++ b/Sources/CodeEditTextView/TextView/TextView.swift @@ -107,6 +107,22 @@ public class TextView: NSView, NSTextContent { } } + /// Determines if the text view should allow scrolling past the end of the document + public var scrollPastEnd: Bool = true { + didSet { + updateFrameIfNeeded() + } + } + + /// The amount of extra space to add when scrolling past end is enabled, as a percentage of the viewport height + public var scrollPastEndAmount: CGFloat = 0.5 { + didSet { + if scrollPastEnd { + updateFrameIfNeeded() + } + } + } + /// Whether or not the editor should wrap lines public var wrapLines: Bool { get { From 9dcc01e5cb7e49525b5dd8768ee7845bfeee5f1e Mon Sep 17 00:00:00 2001 From: Abe M Date: Sun, 30 Mar 2025 17:02:03 -0700 Subject: [PATCH 2/5] Remove import --- Sources/CodeEditTextView/TextView/TextView+Insert.swift | 1 - 1 file changed, 1 deletion(-) diff --git a/Sources/CodeEditTextView/TextView/TextView+Insert.swift b/Sources/CodeEditTextView/TextView/TextView+Insert.swift index a31b42fe2..8c4fc408e 100644 --- a/Sources/CodeEditTextView/TextView/TextView+Insert.swift +++ b/Sources/CodeEditTextView/TextView/TextView+Insert.swift @@ -6,7 +6,6 @@ // import AppKit -import TextStory extension TextView { override public func insertNewline(_ sender: Any?) { From 32dc672a7acc192b4380930cebabcaf8cb9f5b95 Mon Sep 17 00:00:00 2001 From: Abe M Date: Sun, 30 Mar 2025 17:24:14 -0700 Subject: [PATCH 3/5] Refactor variable names to be like Xcode --- Sources/CodeEditTextView/TextView/TextView+Layout.swift | 2 +- Sources/CodeEditTextView/TextView/TextView.swift | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Sources/CodeEditTextView/TextView/TextView+Layout.swift b/Sources/CodeEditTextView/TextView/TextView+Layout.swift index 8626d298c..ba2369bcb 100644 --- a/Sources/CodeEditTextView/TextView/TextView+Layout.swift +++ b/Sources/CodeEditTextView/TextView/TextView+Layout.swift @@ -65,7 +65,7 @@ extension TextView { var availableSize = scrollView?.contentSize ?? .zero availableSize.height -= (scrollView?.contentInsets.top ?? 0) + (scrollView?.contentInsets.bottom ?? 0) - let extraHeight = scrollPastEnd ? availableSize.height * scrollPastEndAmount : 0 + let extraHeight = overscroll ? availableSize.height * overscrollAmount : 0 let newHeight = max(layoutManager.estimatedHeight() + extraHeight, availableSize.height) let newWidth = layoutManager.estimatedWidth() diff --git a/Sources/CodeEditTextView/TextView/TextView.swift b/Sources/CodeEditTextView/TextView/TextView.swift index 7f0f74df5..9b7cc7402 100644 --- a/Sources/CodeEditTextView/TextView/TextView.swift +++ b/Sources/CodeEditTextView/TextView/TextView.swift @@ -108,16 +108,16 @@ public class TextView: NSView, NSTextContent { } /// Determines if the text view should allow scrolling past the end of the document - public var scrollPastEnd: Bool = true { + public var overscroll: Bool = true { didSet { updateFrameIfNeeded() } } /// The amount of extra space to add when scrolling past end is enabled, as a percentage of the viewport height - public var scrollPastEndAmount: CGFloat = 0.5 { + public var overscrollAmount: CGFloat = 0.5 { didSet { - if scrollPastEnd { + if overscroll { updateFrameIfNeeded() } } From 437fadbcf11aae24102710afe2ba72ae6b6ca055 Mon Sep 17 00:00:00 2001 From: Abe M Date: Sun, 30 Mar 2025 17:25:13 -0700 Subject: [PATCH 4/5] Update comment --- Sources/CodeEditTextView/TextView/TextView.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/CodeEditTextView/TextView/TextView.swift b/Sources/CodeEditTextView/TextView/TextView.swift index 9b7cc7402..f1973e80c 100644 --- a/Sources/CodeEditTextView/TextView/TextView.swift +++ b/Sources/CodeEditTextView/TextView/TextView.swift @@ -114,7 +114,7 @@ public class TextView: NSView, NSTextContent { } } - /// The amount of extra space to add when scrolling past end is enabled, as a percentage of the viewport height + /// The amount of extra space to add when overscroll is enabled, as a percentage of the viewport height public var overscrollAmount: CGFloat = 0.5 { didSet { if overscroll { From 2fc1076e657976616772931f4e0a42963e28548d Mon Sep 17 00:00:00 2001 From: Abe M Date: Mon, 31 Mar 2025 02:46:31 -0700 Subject: [PATCH 5/5] Remove overscroll boolean --- .../CodeEditTextView/TextView/TextView+Layout.swift | 2 +- Sources/CodeEditTextView/TextView/TextView.swift | 12 +++--------- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/Sources/CodeEditTextView/TextView/TextView+Layout.swift b/Sources/CodeEditTextView/TextView/TextView+Layout.swift index ba2369bcb..6f237daab 100644 --- a/Sources/CodeEditTextView/TextView/TextView+Layout.swift +++ b/Sources/CodeEditTextView/TextView/TextView+Layout.swift @@ -65,7 +65,7 @@ extension TextView { var availableSize = scrollView?.contentSize ?? .zero availableSize.height -= (scrollView?.contentInsets.top ?? 0) + (scrollView?.contentInsets.bottom ?? 0) - let extraHeight = overscroll ? availableSize.height * overscrollAmount : 0 + let extraHeight = availableSize.height * overscrollAmount let newHeight = max(layoutManager.estimatedHeight() + extraHeight, availableSize.height) let newWidth = layoutManager.estimatedWidth() diff --git a/Sources/CodeEditTextView/TextView/TextView.swift b/Sources/CodeEditTextView/TextView/TextView.swift index f1973e80c..12db02c77 100644 --- a/Sources/CodeEditTextView/TextView/TextView.swift +++ b/Sources/CodeEditTextView/TextView/TextView.swift @@ -107,19 +107,13 @@ public class TextView: NSView, NSTextContent { } } - /// Determines if the text view should allow scrolling past the end of the document - public var overscroll: Bool = true { - didSet { - updateFrameIfNeeded() - } - } - /// The amount of extra space to add when overscroll is enabled, as a percentage of the viewport height public var overscrollAmount: CGFloat = 0.5 { didSet { - if overscroll { - updateFrameIfNeeded() + if overscrollAmount < 0 { + overscrollAmount = 0 } + updateFrameIfNeeded() } }