From ab79c1200f9d89f688d4162079aaa0599a8f28f2 Mon Sep 17 00:00:00 2001 From: Duraid Abdul Date: Mon, 16 Aug 2021 11:40:35 -0700 Subject: [PATCH] Update LCManager.swift Fix for some potential issues that could be caused by initializing LCManager off the main queue, and improved the console text view's ability to stick to the bottom if it is scrolled to the bottom. --- Sources/LocalConsole/LCManager.swift | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/Sources/LocalConsole/LCManager.swift b/Sources/LocalConsole/LCManager.swift index 1c6dfb6..50b692f 100644 --- a/Sources/LocalConsole/LCManager.swift +++ b/Sources/LocalConsole/LCManager.swift @@ -86,11 +86,11 @@ public class LCManager: NSObject, UIGestureRecognizerDelegate { var consoleWindow: ConsoleWindow? // The console needs a parent view controller in order to display context menus. - let viewController = UIViewController() + lazy var viewController = UIViewController() lazy var consoleView = viewController.view! /// Text view that displays printed items. - let consoleTextView = InvertedTextView() + lazy var consoleTextView = InvertedTextView() /// Button that reveals menu. lazy var menuButton = UIButton() @@ -99,7 +99,7 @@ public class LCManager: NSObject, UIGestureRecognizerDelegate { var scrollLocked = true /// Feedback generator for the long press action. - let feedbackGenerator = UISelectionFeedbackGenerator() + lazy var feedbackGenerator = UISelectionFeedbackGenerator() lazy var panRecognizer = UIPanGestureRecognizer(target: self, action: #selector(consolePiPPanner(recognizer:))) lazy var longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(longPressAction(recognizer:))) @@ -303,8 +303,6 @@ public class LCManager: NSObject, UIGestureRecognizerDelegate { } } - private var _hasRelayedOffsetChange = false - /// Print items to the console view. public func print(_ items: Any) { if currentText == "" { @@ -462,15 +460,15 @@ public class LCManager: NSObject, UIGestureRecognizerDelegate { func commitTextChanges(requestMenuUpdate menuUpdateRequested: Bool) { - if consoleTextView.contentOffset.y > consoleTextView.contentSize.height - consoleTextView.bounds.size.height - 20 - || _hasRelayedOffsetChange == false { + if consoleTextView.contentOffset.y > consoleTextView.contentSize.height - consoleTextView.bounds.size.height - 20 { + + // Weird, weird fix that makes the scroll view bottom pinning system work. + consoleTextView.isScrollEnabled.toggle() + consoleTextView.isScrollEnabled.toggle() consoleTextView.pendingOffsetChange = true - _hasRelayedOffsetChange = true } - consoleTextView.text = currentText - setAttributedText(currentText) if menuUpdateRequested {