Various improvements

This commit is contained in:
Ilya Laktyushin
2023-12-20 14:20:11 +04:00
parent e6ba681c03
commit 7c435bf03e
6 changed files with 62 additions and 50 deletions

View File

@@ -86,7 +86,6 @@ public class ChatMessageTextBubbleContentNode: ChatMessageBubbleContentNode {
private let textNode: TextNodeWithEntities
private var spoilerTextNode: TextNodeWithEntities?
private var dustNode: InvisibleInkDustNode?
private var moreNode: TextNode?
private let textAccessibilityOverlayNode: TextAccessibilityOverlayNode
public var statusNode: ChatMessageDateAndStatusNode?
@@ -167,7 +166,6 @@ public class ChatMessageTextBubbleContentNode: ChatMessageBubbleContentNode {
let textLayout = TextNodeWithEntities.asyncLayout(self.textNode)
let spoilerTextLayout = TextNodeWithEntities.asyncLayout(self.spoilerTextNode)
let statusLayout = ChatMessageDateAndStatusNode.asyncLayout(self.statusNode)
let moreLayout = TextNode.asyncLayout(self.moreNode)
let currentCachedChatMessageText = self.cachedChatMessageText
@@ -505,28 +503,40 @@ public class ChatMessageTextBubbleContentNode: ChatMessageBubbleContentNode {
}
attributedText = updatedString
}
let hideAllAdditionalInfo = item.presentationData.isPreview
var moreLayoutAndApply: (TextNodeLayout, () -> TextNode)?
var cutout: TextNodeCutout? = nil
var customTruncationToken: NSAttributedString?
var maximumNumberOfLines: Int = 0
if item.presentationData.isPreview {
moreLayoutAndApply = moreLayout(TextNodeLayoutArguments(attributedString: NSAttributedString(string: item.presentationData.strings.Conversation_ReadMore, font: textFont, textColor: messageTheme.accentTextColor), maximumNumberOfLines: 1, truncationType: .end, constrainedSize: textConstrainedSize))
if let moreSize = moreLayoutAndApply?.0.size {
cutout = TextNodeCutout(bottomRight: CGSize(width: moreSize.width + 8.0, height: moreSize.height))
if item.message.groupingKey != nil {
maximumNumberOfLines = 6
} else if let image = item.message.media.first(where: { $0 is TelegramMediaImage }) as? TelegramMediaImage, let dimensions = image.representations.first?.dimensions {
if dimensions.width > dimensions.height {
maximumNumberOfLines = 9
} else {
maximumNumberOfLines = 6
}
} else if let file = item.message.media.first(where: { $0 is TelegramMediaFile }) as? TelegramMediaFile, file.isVideo || file.isAnimated, let dimensions = file.dimensions {
if dimensions.width > dimensions.height {
maximumNumberOfLines = 9
} else {
maximumNumberOfLines = 6
}
} else {
maximumNumberOfLines = 12
}
let truncationToken = NSMutableAttributedString()
truncationToken.append(NSAttributedString(string: "\u{2026} ", font: textFont, textColor: messageTheme.primaryTextColor))
truncationToken.append(NSAttributedString(string: item.presentationData.strings.Conversation_ReadMore, font: textFont, textColor: messageTheme.accentTextColor))
customTruncationToken = truncationToken
}
let textInsets = UIEdgeInsets(top: 2.0, left: 2.0, bottom: 5.0, right: 2.0)
let (textLayout, textApply) = textLayout(TextNodeLayoutArguments(attributedString: attributedText, backgroundColor: nil, maximumNumberOfLines: hideAllAdditionalInfo ? 12 : 0, truncationType: .end, constrainedSize: textConstrainedSize, alignment: .natural, cutout: cutout, insets: textInsets, lineColor: messageTheme.accentControlColor))
if !textLayout.truncated {
moreLayoutAndApply = nil
}
let (textLayout, textApply) = textLayout(TextNodeLayoutArguments(attributedString: attributedText, backgroundColor: nil, maximumNumberOfLines: maximumNumberOfLines, truncationType: .end, constrainedSize: textConstrainedSize, alignment: .natural, cutout: nil, insets: textInsets, lineColor: messageTheme.accentControlColor, customTruncationToken: customTruncationToken))
let spoilerTextLayoutAndApply: (TextNodeLayout, (TextNodeWithEntities.Arguments?) -> TextNodeWithEntities)?
if !textLayout.spoilers.isEmpty {
spoilerTextLayoutAndApply = spoilerTextLayout(TextNodeLayoutArguments(attributedString: attributedText, backgroundColor: nil, maximumNumberOfLines: hideAllAdditionalInfo ? 12 : 0, truncationType: .end, constrainedSize: textConstrainedSize, alignment: .natural, cutout: cutout, insets: textInsets, lineColor: messageTheme.accentControlColor, displaySpoilers: true, displayEmbeddedItemsUnderSpoilers: true))
spoilerTextLayoutAndApply = spoilerTextLayout(TextNodeLayoutArguments(attributedString: attributedText, backgroundColor: nil, maximumNumberOfLines: maximumNumberOfLines, truncationType: .end, constrainedSize: textConstrainedSize, alignment: .natural, cutout: nil, insets: textInsets, lineColor: messageTheme.accentControlColor, displaySpoilers: true, displayEmbeddedItemsUnderSpoilers: true))
} else {
spoilerTextLayoutAndApply = nil
}
@@ -546,12 +556,12 @@ public class ChatMessageTextBubbleContentNode: ChatMessageBubbleContentNode {
}
let dateLayoutInput: ChatMessageDateAndStatusNode.LayoutInput
dateLayoutInput = .trailingContent(contentWidth: trailingWidthToMeasure, reactionSettings: hideAllAdditionalInfo ? nil : ChatMessageDateAndStatusNode.TrailingReactionSettings(displayInline: shouldDisplayInlineDateReactions(message: item.message, isPremium: item.associatedData.isPremium, forceInline: item.associatedData.forceInlineReactions), preferAdditionalInset: false))
dateLayoutInput = .trailingContent(contentWidth: trailingWidthToMeasure, reactionSettings: item.presentationData.isPreview ? nil : ChatMessageDateAndStatusNode.TrailingReactionSettings(displayInline: shouldDisplayInlineDateReactions(message: item.message, isPremium: item.associatedData.isPremium, forceInline: item.associatedData.forceInlineReactions), preferAdditionalInset: false))
statusSuggestedWidthAndContinue = statusLayout(ChatMessageDateAndStatusNode.Arguments(
context: item.context,
presentationData: item.presentationData,
edited: edited && !hideAllAdditionalInfo,
edited: edited && !item.presentationData.isPreview,
impressionCount: viewCount,
dateText: dateText,
type: statusType,
@@ -575,14 +585,6 @@ public class ChatMessageTextBubbleContentNode: ChatMessageBubbleContentNode {
textFrame = textFrame.offsetBy(dx: layoutConstants.text.bubbleInsets.left, dy: topInset)
textFrameWithoutInsets = textFrameWithoutInsets.offsetBy(dx: layoutConstants.text.bubbleInsets.left, dy: topInset)
var readMoreFrame: CGRect = .zero
if let (readMoreLayout, _ ) = moreLayoutAndApply {
let remainingLineWidth = textLayout.size.width - textLayout.trailingLineWidth
if readMoreLayout.size.width < remainingLineWidth {
readMoreFrame = CGRect(origin: CGPoint(x: textFrame.maxX - readMoreLayout.size.width - textInsets.right, y: textFrame.maxY - readMoreLayout.size.height - textInsets.bottom), size: readMoreLayout.size)
}
}
var suggestedBoundingWidth: CGFloat = textFrameWithoutInsets.width
if let statusSuggestedWidthAndContinue = statusSuggestedWidthAndContinue {
@@ -674,19 +676,6 @@ public class ChatMessageTextBubbleContentNode: ChatMessageBubbleContentNode {
}
}
if let (_, moreApply) = moreLayoutAndApply {
let moreNode = moreApply()
if strongSelf.moreNode == nil {
moreNode.displaysAsynchronously = false
strongSelf.moreNode = moreNode
strongSelf.containerNode.insertSubnode(moreNode, aboveSubnode: strongSelf.textNode.textNode)
}
moreNode.frame = readMoreFrame
} else if let moreNode = strongSelf.moreNode {
strongSelf.moreNode = nil
moreNode.removeFromSupernode()
}
switch strongSelf.visibility {
case .none:
strongSelf.textNode.visibilityRect = nil