diff --git a/ElementX/Sources/Other/Pills/MessageText.swift b/ElementX/Sources/Other/Pills/MessageText.swift index 39ce0c87f..1931408d3 100644 --- a/ElementX/Sources/Other/Pills/MessageText.swift +++ b/ElementX/Sources/Other/Pills/MessageText.swift @@ -12,7 +12,6 @@ import SwiftUI final class MessageTextView: UITextView, PillAttachmentViewProviderDelegate, UIGestureRecognizerDelegate, NSLayoutManagerDelegate { var timelineContext: TimelineViewModel.Context? var updateClosure: (() -> Void)? - private var pillViews = NSHashTable.weakObjects() override func addGestureRecognizer(_ gestureRecognizer: UIGestureRecognizer) { // We don't need to change the behaviour on MacOS @@ -42,6 +41,9 @@ final class MessageTextView: UITextView, PillAttachmentViewProviderDelegate, UIG } } + /// UIKit removes old pill views from this text view when its text changes, so there's nothing to track. + func registerPillView(_ pillView: UIView) { } + /// Stops TextKit from hyphenating code when a word doesn't fit on a line, wrapping it by /// character instead. Code is meant to be verbatim, so an inserted hyphen would corrupt it. nonisolated func layoutManager(_ layoutManager: NSLayoutManager, shouldBreakLineByHyphenatingBeforeCharacterAt charIndex: Int) -> Bool { @@ -53,18 +55,6 @@ final class MessageTextView: UITextView, PillAttachmentViewProviderDelegate, UIG || textStorage.attribute(.CodeBlock, at: charIndex, effectiveRange: nil) != nil return !isCode } - - func registerPillView(_ pillView: UIView) { - pillViews.add(pillView) - } - - func flushPills() { - for view in pillViews.allObjects { - view.alpha = 0.0 - view.removeFromSuperview() - } - pillViews.removeAllObjects() - } } /// An `NSTextAttachment` that takes up its declared `bounds` but draws nothing — used @@ -96,13 +86,17 @@ struct MessageText: UIViewRepresentable { let reservedSize: CGSize } + /// The inputs the text view's text was built from. Compared instead of the text itself, which UIKit + /// normalises and which gets a new trailing spacer attachment on every build, so it never matches. + fileprivate struct TextInputs: Equatable { + let attributedString: AttributedString + let trailingReservedSize: CGSize + let layoutDirection: LayoutDirection + } + @State private var computedSizes = [SizeCacheKey: CGSize]() - @State var attributedString: AttributedString { - didSet { - computedSizes.removeAll() - } - } + let attributedString: AttributedString /// Reserves an invisible inline area of this size at the very end of the rendered /// text. Used so that the message bubble's natural size accommodates the timestamp @@ -111,6 +105,12 @@ struct MessageText: UIViewRepresentable { /// line (timestamp drops below the text). var trailingReservedSize: CGSize = .zero + private var textInputs: TextInputs { + TextInputs(attributedString: attributedString, + trailingReservedSize: trailingReservedSize, + layoutDirection: layoutDirection) + } + private func makeAttributedText() -> NSAttributedString? { guard let baseText = try? NSAttributedString(attributedString, including: \.elementX) else { return nil @@ -175,13 +175,9 @@ struct MessageText: UIViewRepresentable { // Need to use TextKit 1 for mentions let textView = MessageTextView(usingTextLayoutManager: false) textView.timelineContext = viewModel - textView.updateClosure = { [weak textView] in - guard let textView else { return } - do { - attributedString = try AttributedString(textView.attributedText, including: \.elementX) - } catch { - MXLog.error("Failed to update attributedString: \(error)]") - return + textView.updateClosure = { + DispatchQueue.main.async { + computedSizes.removeAll() } } textView.isEditable = false @@ -210,16 +206,17 @@ struct MessageText: UIViewRepresentable { textView.backgroundColor = .clear if let attributedText = makeAttributedText() { textView.attributedText = attributedText + context.coordinator.textInputs = textInputs } textView.delegate = context.coordinator return textView } func updateUIView(_ uiView: MessageTextView, context: Context) { - if let newAttributedText = makeAttributedText(), - uiView.attributedText != newAttributedText { - uiView.flushPills() + if context.coordinator.textInputs != textInputs, + let newAttributedText = makeAttributedText() { uiView.attributedText = newAttributedText + context.coordinator.textInputs = textInputs computedSizes.removeAll() } context.coordinator.openURLAction = openURLAction @@ -246,6 +243,7 @@ struct MessageText: UIViewRepresentable { final class Coordinator: NSObject, UITextViewDelegate { var openURLAction: OpenURLAction + fileprivate var textInputs: TextInputs? init(openURLAction: OpenURLAction) { self.openURLAction = openURLAction diff --git a/ElementX/Sources/Screens/RoomScreen/ComposerToolbar/View/MessageComposerTextField.swift b/ElementX/Sources/Screens/RoomScreen/ComposerToolbar/View/MessageComposerTextField.swift index 765867bc1..65d47d597 100644 --- a/ElementX/Sources/Screens/RoomScreen/ComposerToolbar/View/MessageComposerTextField.swift +++ b/ElementX/Sources/Screens/RoomScreen/ComposerToolbar/View/MessageComposerTextField.swift @@ -206,7 +206,6 @@ private protocol ElementTextViewDelegate: AnyObject { private class ElementTextView: UITextView, PillAttachmentViewProviderDelegate { private(set) var timelineContext: TimelineViewModel.Context? - private var pillViews = NSHashTable.weakObjects() weak var elementDelegate: ElementTextViewDelegate? @@ -309,9 +308,8 @@ private class ElementTextView: UITextView, PillAttachmentViewProviderDelegate { } } - func registerPillView(_ pillView: UIView) { - pillViews.add(pillView) - } + /// UIKit removes old pill views from this text view when its text changes, so there's nothing to track. + func registerPillView(_ pillView: UIView) { } // MARK: - Private