reverting some of the performance improvements

This commit is contained in:
Mauro Romito 2023-10-18 11:51:03 +02:00 committed by Mauro
parent 6e8dff2ef6
commit 2b89721aa0
3 changed files with 3 additions and 22 deletions

View File

@ -64,8 +64,7 @@ final class PillAttachmentViewProvider: NSTextAttachmentViewProvider {
return
}
let view = PillView(imageProvider: imageProvider, context: context) { [weak self, weak textAttachment] in
textAttachment?.invalidateLastBounds()
let view = PillView(imageProvider: imageProvider, context: context) { [weak self] in
self?.delegate?.invalidateTextAttachmentsDisplay()
}
let controller = UIHostingController(rootView: view)
@ -73,7 +72,6 @@ final class PillAttachmentViewProvider: NSTextAttachmentViewProvider {
// This allows the text view to handle it as a link
controller.view.isUserInteractionEnabled = false
self.view = controller.view
textAttachment.view = controller.view
delegate?.registerPillView(controller.view)
}
}

View File

@ -26,29 +26,13 @@ final class PillTextAttachment: NSTextAttachment {
}
private(set) var pillData: PillTextAttachmentData!
weak var view: UIView?
private var lastBounds: CGRect?
func invalidateLastBounds() {
lastBounds = nil
}
override func attachmentBounds(for textContainer: NSTextContainer?, proposedLineFragment lineFrag: CGRect, glyphPosition position: CGPoint, characterIndex charIndex: Int) -> CGRect {
var rect: CGRect
if let lastBounds {
return lastBounds
} else if let view {
rect = CGRect(origin: .zero, size: view.sizeThatFits(UIView.layoutFittingExpandedSize))
} else {
rect = super.attachmentBounds(for: textContainer, proposedLineFragment: lineFrag, glyphPosition: position, characterIndex: charIndex)
}
var rect = super.attachmentBounds(for: textContainer, proposedLineFragment: lineFrag, glyphPosition: position, characterIndex: charIndex)
let fontData = pillData.fontData
// Align the pill text vertically with the surrounding text.
rect.origin.y = fontData.descender + (fontData.lineHeight - rect.height) / 2.0
// Set the max width of the pill
rect.size.width = min(rect.size.width, PillConstants.maxWidth)
lastBounds = rect
return rect
}
}

View File

@ -34,6 +34,7 @@ struct PillView: View {
.padding(.vertical, 4)
// for now design has defined no color so we will just use gray
.background(Capsule().foregroundColor(context.viewState.isOwnMention ? .compound.bgCriticalPrimary : .gray))
.frame(maxWidth: PillConstants.maxWidth)
.onChange(of: context.viewState.displayText) { _ in
didChangeText()
}
@ -53,13 +54,11 @@ struct PillView_Previews: PreviewProvider, TestablePreview {
PillView(imageProvider: mockMediaProvider,
context: PillContext.mock(type: .loadedUser(isOwn: false))) { }
// To simulate the trimming that happens at UIKit level
.frame(maxWidth: PillConstants.maxWidth)
.previewDisplayName("Loaded Long")
PillView(imageProvider: mockMediaProvider,
context: PillContext.mock(type: .loadedUser(isOwn: true))) { }
// To simulate the trimming that happens at UIKit level
.frame(maxWidth: PillConstants.maxWidth)
.previewDisplayName("Loaded Long Own")
PillView(imageProvider: mockMediaProvider,
context: PillContext.mock(type: .allUsers)) { }
.previewDisplayName("All Users")