mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-03 16:10:20 +00:00
Pill View rendering, performance improvement part 2 (#1894)
* improvdement * code improvement
This commit is contained in:
@@ -26,7 +26,7 @@ final class MessageTextView: UITextView, PillAttachmentViewProviderDelegate {
|
|||||||
gestureRecognizer as? UILongPressGestureRecognizer == nil
|
gestureRecognizer as? UILongPressGestureRecognizer == nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func invalidateTextAttachmentsDisplay(update: Bool) {
|
func invalidateTextAttachmentsDisplay() {
|
||||||
attributedText.enumerateAttribute(.attachment,
|
attributedText.enumerateAttribute(.attachment,
|
||||||
in: NSRange(location: 0, length: attributedText.length),
|
in: NSRange(location: 0, length: attributedText.length),
|
||||||
options: []) { value, range, _ in
|
options: []) { value, range, _ in
|
||||||
@@ -34,9 +34,7 @@ final class MessageTextView: UITextView, PillAttachmentViewProviderDelegate {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
self.layoutManager.invalidateDisplay(forCharacterRange: range)
|
self.layoutManager.invalidateDisplay(forCharacterRange: range)
|
||||||
if update {
|
updateClosure?()
|
||||||
updateClosure?()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ protocol PillAttachmentViewProviderDelegate: AnyObject {
|
|||||||
var roomContext: RoomScreenViewModel.Context? { get }
|
var roomContext: RoomScreenViewModel.Context? { get }
|
||||||
|
|
||||||
func registerPillView(_ pillView: UIView)
|
func registerPillView(_ pillView: UIView)
|
||||||
func invalidateTextAttachmentsDisplay(update: Bool)
|
func invalidateTextAttachmentsDisplay()
|
||||||
}
|
}
|
||||||
|
|
||||||
final class PillAttachmentViewProvider: NSTextAttachmentViewProvider {
|
final class PillAttachmentViewProvider: NSTextAttachmentViewProvider {
|
||||||
@@ -44,7 +44,8 @@ final class PillAttachmentViewProvider: NSTextAttachmentViewProvider {
|
|||||||
override func loadView() {
|
override func loadView() {
|
||||||
super.loadView()
|
super.loadView()
|
||||||
|
|
||||||
guard let textAttachmentData = (textAttachment as? PillTextAttachment)?.pillData else {
|
guard let textAttachment = textAttachment as? PillTextAttachment,
|
||||||
|
let pillData = textAttachment.pillData else {
|
||||||
MXLog.failure("[PillAttachmentViewProvider]: attachment is missing data or not of expected class")
|
MXLog.failure("[PillAttachmentViewProvider]: attachment is missing data or not of expected class")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -56,15 +57,16 @@ final class PillAttachmentViewProvider: NSTextAttachmentViewProvider {
|
|||||||
context = PillContext.mock(type: .loadUser(isOwn: false))
|
context = PillContext.mock(type: .loadUser(isOwn: false))
|
||||||
imageProvider = MockMediaProvider()
|
imageProvider = MockMediaProvider()
|
||||||
} else if let roomContext = delegate?.roomContext {
|
} else if let roomContext = delegate?.roomContext {
|
||||||
context = PillContext(roomContext: roomContext, data: textAttachmentData)
|
context = PillContext(roomContext: roomContext, data: pillData)
|
||||||
imageProvider = roomContext.imageProvider
|
imageProvider = roomContext.imageProvider
|
||||||
} else {
|
} else {
|
||||||
MXLog.failure("[PillAttachmentViewProvider]: missing room context")
|
MXLog.failure("[PillAttachmentViewProvider]: missing room context")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
let view = PillView(imageProvider: imageProvider, context: context) { [weak self] in
|
let view = PillView(imageProvider: imageProvider, context: context) { [weak self, weak textAttachment] in
|
||||||
self?.delegate?.invalidateTextAttachmentsDisplay(update: true)
|
textAttachment?.invalidateLastBounds()
|
||||||
|
self?.delegate?.invalidateTextAttachmentsDisplay()
|
||||||
}
|
}
|
||||||
let controller = UIHostingController(rootView: view)
|
let controller = UIHostingController(rootView: view)
|
||||||
controller.view.backgroundColor = .clear
|
controller.view.backgroundColor = .clear
|
||||||
@@ -93,5 +95,5 @@ extension WysiwygTextView: PillAttachmentViewProviderDelegate {
|
|||||||
(mentionDisplayHelper as? ComposerMentionDisplayHelper)?.roomContext
|
(mentionDisplayHelper as? ComposerMentionDisplayHelper)?.roomContext
|
||||||
}
|
}
|
||||||
|
|
||||||
func invalidateTextAttachmentsDisplay(update: Bool) { }
|
func invalidateTextAttachmentsDisplay() { }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,13 +26,22 @@ final class PillTextAttachment: NSTextAttachment {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private(set) var pillData: PillTextAttachmentData!
|
private(set) var pillData: PillTextAttachmentData!
|
||||||
|
private var lastBounds: CGRect?
|
||||||
|
|
||||||
|
func invalidateLastBounds() {
|
||||||
|
lastBounds = nil
|
||||||
|
}
|
||||||
|
|
||||||
override func attachmentBounds(for textContainer: NSTextContainer?, proposedLineFragment lineFrag: CGRect, glyphPosition position: CGPoint, characterIndex charIndex: Int) -> CGRect {
|
override func attachmentBounds(for textContainer: NSTextContainer?, proposedLineFragment lineFrag: CGRect, glyphPosition position: CGPoint, characterIndex charIndex: Int) -> CGRect {
|
||||||
|
if let lastBounds {
|
||||||
|
return lastBounds
|
||||||
|
}
|
||||||
var 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
|
let fontData = pillData.fontData
|
||||||
// Align the pill text vertically with the surrounding text.
|
// Align the pill text vertically with the surrounding text.
|
||||||
rect.origin.y = fontData.descender + (fontData.lineHeight - rect.height) / 2.0
|
rect.origin.y = fontData.descender + (fontData.lineHeight - rect.height) / 2.0
|
||||||
|
lastBounds = rect
|
||||||
return rect
|
return rect
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user