mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-24 18:59:36 +00:00
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:
@@ -12,7 +12,6 @@ import SwiftUI
|
|||||||
final class MessageTextView: UITextView, PillAttachmentViewProviderDelegate, UIGestureRecognizerDelegate, NSLayoutManagerDelegate {
|
final class MessageTextView: UITextView, PillAttachmentViewProviderDelegate, UIGestureRecognizerDelegate, NSLayoutManagerDelegate {
|
||||||
var timelineContext: TimelineViewModel.Context?
|
var timelineContext: TimelineViewModel.Context?
|
||||||
var updateClosure: (() -> Void)?
|
var updateClosure: (() -> Void)?
|
||||||
private var pillViews = NSHashTable<UIView>.weakObjects()
|
|
||||||
|
|
||||||
override func addGestureRecognizer(_ gestureRecognizer: UIGestureRecognizer) {
|
override func addGestureRecognizer(_ gestureRecognizer: UIGestureRecognizer) {
|
||||||
// We don't need to change the behaviour on MacOS
|
// 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
|
/// 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.
|
/// character instead. Code is meant to be verbatim, so an inserted hyphen would corrupt it.
|
||||||
nonisolated func layoutManager(_ layoutManager: NSLayoutManager, shouldBreakLineByHyphenatingBeforeCharacterAt charIndex: Int) -> Bool {
|
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
|
|| textStorage.attribute(.CodeBlock, at: charIndex, effectiveRange: nil) != nil
|
||||||
return !isCode
|
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
|
/// An `NSTextAttachment` that takes up its declared `bounds` but draws nothing — used
|
||||||
@@ -96,13 +86,17 @@ struct MessageText: UIViewRepresentable {
|
|||||||
let reservedSize: CGSize
|
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 private var computedSizes = [SizeCacheKey: CGSize]()
|
||||||
|
|
||||||
@State var attributedString: AttributedString {
|
let attributedString: AttributedString
|
||||||
didSet {
|
|
||||||
computedSizes.removeAll()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Reserves an invisible inline area of this size at the very end of the rendered
|
/// 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
|
/// 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).
|
/// line (timestamp drops below the text).
|
||||||
var trailingReservedSize: CGSize = .zero
|
var trailingReservedSize: CGSize = .zero
|
||||||
|
|
||||||
|
private var textInputs: TextInputs {
|
||||||
|
TextInputs(attributedString: attributedString,
|
||||||
|
trailingReservedSize: trailingReservedSize,
|
||||||
|
layoutDirection: layoutDirection)
|
||||||
|
}
|
||||||
|
|
||||||
private func makeAttributedText() -> NSAttributedString? {
|
private func makeAttributedText() -> NSAttributedString? {
|
||||||
guard let baseText = try? NSAttributedString(attributedString, including: \.elementX) else {
|
guard let baseText = try? NSAttributedString(attributedString, including: \.elementX) else {
|
||||||
return nil
|
return nil
|
||||||
@@ -175,13 +175,9 @@ struct MessageText: UIViewRepresentable {
|
|||||||
// Need to use TextKit 1 for mentions
|
// Need to use TextKit 1 for mentions
|
||||||
let textView = MessageTextView(usingTextLayoutManager: false)
|
let textView = MessageTextView(usingTextLayoutManager: false)
|
||||||
textView.timelineContext = viewModel
|
textView.timelineContext = viewModel
|
||||||
textView.updateClosure = { [weak textView] in
|
textView.updateClosure = {
|
||||||
guard let textView else { return }
|
DispatchQueue.main.async {
|
||||||
do {
|
computedSizes.removeAll()
|
||||||
attributedString = try AttributedString(textView.attributedText, including: \.elementX)
|
|
||||||
} catch {
|
|
||||||
MXLog.error("Failed to update attributedString: \(error)]")
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
textView.isEditable = false
|
textView.isEditable = false
|
||||||
@@ -210,16 +206,17 @@ struct MessageText: UIViewRepresentable {
|
|||||||
textView.backgroundColor = .clear
|
textView.backgroundColor = .clear
|
||||||
if let attributedText = makeAttributedText() {
|
if let attributedText = makeAttributedText() {
|
||||||
textView.attributedText = attributedText
|
textView.attributedText = attributedText
|
||||||
|
context.coordinator.textInputs = textInputs
|
||||||
}
|
}
|
||||||
textView.delegate = context.coordinator
|
textView.delegate = context.coordinator
|
||||||
return textView
|
return textView
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateUIView(_ uiView: MessageTextView, context: Context) {
|
func updateUIView(_ uiView: MessageTextView, context: Context) {
|
||||||
if let newAttributedText = makeAttributedText(),
|
if context.coordinator.textInputs != textInputs,
|
||||||
uiView.attributedText != newAttributedText {
|
let newAttributedText = makeAttributedText() {
|
||||||
uiView.flushPills()
|
|
||||||
uiView.attributedText = newAttributedText
|
uiView.attributedText = newAttributedText
|
||||||
|
context.coordinator.textInputs = textInputs
|
||||||
computedSizes.removeAll()
|
computedSizes.removeAll()
|
||||||
}
|
}
|
||||||
context.coordinator.openURLAction = openURLAction
|
context.coordinator.openURLAction = openURLAction
|
||||||
@@ -246,6 +243,7 @@ struct MessageText: UIViewRepresentable {
|
|||||||
|
|
||||||
final class Coordinator: NSObject, UITextViewDelegate {
|
final class Coordinator: NSObject, UITextViewDelegate {
|
||||||
var openURLAction: OpenURLAction
|
var openURLAction: OpenURLAction
|
||||||
|
fileprivate var textInputs: TextInputs?
|
||||||
|
|
||||||
init(openURLAction: OpenURLAction) {
|
init(openURLAction: OpenURLAction) {
|
||||||
self.openURLAction = openURLAction
|
self.openURLAction = openURLAction
|
||||||
|
|||||||
+2
-4
@@ -206,7 +206,6 @@ private protocol ElementTextViewDelegate: AnyObject {
|
|||||||
|
|
||||||
private class ElementTextView: UITextView, PillAttachmentViewProviderDelegate {
|
private class ElementTextView: UITextView, PillAttachmentViewProviderDelegate {
|
||||||
private(set) var timelineContext: TimelineViewModel.Context?
|
private(set) var timelineContext: TimelineViewModel.Context?
|
||||||
private var pillViews = NSHashTable<UIView>.weakObjects()
|
|
||||||
|
|
||||||
weak var elementDelegate: ElementTextViewDelegate?
|
weak var elementDelegate: ElementTextViewDelegate?
|
||||||
|
|
||||||
@@ -309,9 +308,8 @@ private class ElementTextView: UITextView, PillAttachmentViewProviderDelegate {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func registerPillView(_ pillView: UIView) {
|
/// UIKit removes old pill views from this text view when its text changes, so there's nothing to track.
|
||||||
pillViews.add(pillView)
|
func registerPillView(_ pillView: UIView) { }
|
||||||
}
|
|
||||||
|
|
||||||
// MARK: - Private
|
// MARK: - Private
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user