From 58aecf3a9dbcf654c126b2bbed6b6d2ada5cf3c8 Mon Sep 17 00:00:00 2001 From: Duraid Abdul Date: Tue, 22 Jun 2021 10:56:56 -0700 Subject: [PATCH] Update LCManager.swift --- Sources/LocalConsole/LCManager.swift | 45 ++++++++++++++++++---------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/Sources/LocalConsole/LCManager.swift b/Sources/LocalConsole/LCManager.swift index a736e7d..29f6021 100644 --- a/Sources/LocalConsole/LCManager.swift +++ b/Sources/LocalConsole/LCManager.swift @@ -284,25 +284,40 @@ public class LCManager: NSObject, UIGestureRecognizerDelegate { /// Print items to the console view. public func print(_ items: Any) { - if consoleTextView.contentOffset.y > consoleTextView.contentSize.height - 20 - consoleTextView.bounds.size.height || - _hasRelayedOffsetChange == false { - consoleTextView.pendingOffsetChange = true + func performActions() { + if consoleTextView.contentOffset.y > consoleTextView.contentSize.height - 20 - consoleTextView.bounds.size.height || + _hasRelayedOffsetChange == false { + consoleTextView.pendingOffsetChange = true + + _hasRelayedOffsetChange = true + } - _hasRelayedOffsetChange = true + let needsMenuUpdate = consoleTextView.text == "" + + let string: String = { + if consoleTextView.text == "" { + return "\(items)" + } else { + return consoleTextView.text + "\n\(items)" + } + }() + + setAttributedText(string) + + if needsMenuUpdate { + // Update the context menu to show the clipboard/clear actions. + menuButton.menu = makeMenu() + } } - let string: String = { - if consoleTextView.text == "" { - return "\(items)" - } else { - return consoleTextView.text + "\n\(items)" + // Ensure we are performing UI updates on the main thread. + DispatchQueue.main.async { + + // Ensure the console doesn't get caught into any external animation blocks. + UIView.performWithoutAnimation { + performActions() } - }() - - setAttributedText(string) - - // Update the context menu to show the clipboard/clear actions. - menuButton.menu = makeMenu() + } } /// Clear text in the console view.