Fix extra space in message bubbles after a mention's name loads (#6393)

* Remove the timeline's pill view flushing

MessageTextView is created with UIKit's factory initialiser, which skips
Swift's stored-property initialisers, so its pill view table was always nil
and flushPills never did anything. UIKit already removes old attachment
views when the text is replaced (checked on iOS 18.5, 26.5 and 27).

The rich text composer does leave old pill views behind, so it keeps
registering them. The timeline and the plain composer now implement
registration as a no-op and drop their unused pill view tables.

* Stop adding a timestamp spacer whenever a mention's name loads

When a mention pill's display text changed, MessageText copied the rendered
text, spacer included, back into its source, so another invisible timestamp
spacer was appended and the bubble could grow a line. The text comparison in
updateUIView also never matched while a spacer was reserved (a new spacer is
built every time), so every update rebuilt the text and its pills.

Rebuild the text only when its inputs change, and only invalidate the cached
size when a pill resizes.
This commit is contained in:
Mauro
2026-10-27 17:21:52 +01:00
committed by GitHub
parent 693c690e47
commit a01f546fb2
2 changed files with 28 additions and 32 deletions
+26 -28
View File
@@ -12,7 +12,6 @@ import SwiftUI
final class MessageTextView: UITextView, PillAttachmentViewProviderDelegate, UIGestureRecognizerDelegate, NSLayoutManagerDelegate {
var timelineContext: TimelineViewModel.Context?
var updateClosure: (() -> Void)?
private var pillViews = NSHashTable<UIView>.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
@@ -206,7 +206,6 @@ private protocol ElementTextViewDelegate: AnyObject {
private class ElementTextView: UITextView, PillAttachmentViewProviderDelegate {
private(set) var timelineContext: TimelineViewModel.Context?
private var pillViews = NSHashTable<UIView>.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