From 2a1d8af7448234d1649fd5a0c2289d0009ea3f45 Mon Sep 17 00:00:00 2001 From: Ilya Laktyushin Date: Sat, 18 Dec 2021 21:21:24 +0400 Subject: [PATCH 1/3] Various Improvements --- .../Sources/InvisibleInkDustNode.swift | 2 +- .../Sources/ChatMessageTextBubbleContentNode.swift | 2 +- submodules/Translate/Sources/Translate.swift | 13 +++++++------ 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/submodules/InvisibleInkDustNode/Sources/InvisibleInkDustNode.swift b/submodules/InvisibleInkDustNode/Sources/InvisibleInkDustNode.swift index 6974a73a49..4f7d3316cb 100644 --- a/submodules/InvisibleInkDustNode/Sources/InvisibleInkDustNode.swift +++ b/submodules/InvisibleInkDustNode/Sources/InvisibleInkDustNode.swift @@ -217,7 +217,7 @@ public class InvisibleInkDustNode: ASDisplayNode { square += Float(rect.width * rect.height) } - self.emitter?.birthRate = square * 0.3 + self.emitter?.birthRate = square * 0.4 } public func update(size: CGSize, color: UIColor, rects: [CGRect]) { diff --git a/submodules/TelegramUI/Sources/ChatMessageTextBubbleContentNode.swift b/submodules/TelegramUI/Sources/ChatMessageTextBubbleContentNode.swift index 5cb4c3be32..5c181f4b3d 100644 --- a/submodules/TelegramUI/Sources/ChatMessageTextBubbleContentNode.swift +++ b/submodules/TelegramUI/Sources/ChatMessageTextBubbleContentNode.swift @@ -404,7 +404,7 @@ class ChatMessageTextBubbleContentNode: ChatMessageBubbleContentNode { strongSelf.dustNode = dustNode strongSelf.insertSubnode(dustNode, aboveSubnode: spoilerTextNode) } - dustNode.update(size: textFrame.size, color: messageTheme.secondaryTextColor, rects: textLayout.spoilers.map { $0.offsetBy(dx: 3.0, dy: 3.0).insetBy(dx: 0.0, dy: 1.0) }) + dustNode.update(size: textFrame.size, color: messageTheme.secondaryTextColor, rects: textLayout.spoilers.map { $0.offsetBy(dx: 3.0, dy: 3.0).insetBy(dx: 1.0, dy: 1.0) }) dustNode.frame = textFrame.insetBy(dx: -3.0, dy: -3.0).offsetBy(dx: 0.0, dy: 3.0) } else if let spoilerTextNode = strongSelf.spoilerTextNode { strongSelf.spoilerTextNode = nil diff --git a/submodules/Translate/Sources/Translate.swift b/submodules/Translate/Sources/Translate.swift index 539319d70b..42334de9a4 100644 --- a/submodules/Translate/Sources/Translate.swift +++ b/submodules/Translate/Sources/Translate.swift @@ -12,15 +12,16 @@ public func translateText(context: AccountContext, text: String) { return } if #available(iOS 15.0, *) { - let textField = UITextField() - textField.text = text + let textView = UITextView() + textView.text = text + textView.isEditable = false if let navigationController = context.sharedContext.mainWindow?.viewController as? NavigationController, let topController = navigationController.topViewController as? ViewController { - topController.view.addSubview(textField) - textField.selectAll(nil) - textField.perform(NSSelectorFromString(["_", "trans", "late:"].joined(separator: "")), with: nil) + topController.view.addSubview(textView) + textView.selectAll(nil) + textView.perform(NSSelectorFromString(["_", "trans", "late:"].joined(separator: "")), with: nil) DispatchQueue.main.async { - textField.removeFromSuperview() + textView.removeFromSuperview() } } } From 5ddfb0dd630a93d7da9a96596dd3079d070afc4a Mon Sep 17 00:00:00 2001 From: Ilya Laktyushin Date: Sat, 18 Dec 2021 23:44:55 +0400 Subject: [PATCH 2/3] Fix spoiler dust on truncated lines --- submodules/Display/Source/TextNode.swift | 7 ++++--- .../TelegramUI/Sources/ChatMessageReplyInfoNode.swift | 2 +- .../Sources/ChatMessageTextBubbleContentNode.swift | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/submodules/Display/Source/TextNode.swift b/submodules/Display/Source/TextNode.swift index ee16bed593..1fb4070ea1 100644 --- a/submodules/Display/Source/TextNode.swift +++ b/submodules/Display/Source/TextNode.swift @@ -973,7 +973,7 @@ public class TextNode: ASDisplayNode { let lineCharacterCount = CTTypesetterSuggestLineBreak(typesetter, lastLineCharacterIndex, Double(lineConstrainedWidth)) - func addSpoiler(line: CTLine, ascent: CGFloat, descent: CGFloat, startIndex: Int, endIndex: Int) { + func addSpoiler(line: CTLine, ascent: CGFloat, descent: CGFloat, startIndex: Int, endIndex: Int, rightInset: CGFloat = 0.0) { var secondaryLeftOffset: CGFloat = 0.0 let rawLeftOffset = CTLineGetOffsetForStringIndex(line, startIndex, &secondaryLeftOffset) var leftOffset = floor(rawLeftOffset) @@ -988,7 +988,7 @@ public class TextNode: ASDisplayNode { rightOffset = ceil(secondaryRightOffset) } - spoilers.append(TextNodeSpoiler(range: NSMakeRange(startIndex, endIndex - startIndex + 1), frame: CGRect(x: min(leftOffset, rightOffset), y: descent - (ascent + descent), width: abs(rightOffset - leftOffset), height: ascent + descent))) + spoilers.append(TextNodeSpoiler(range: NSMakeRange(startIndex, endIndex - startIndex + 1), frame: CGRect(x: min(leftOffset, rightOffset), y: descent - (ascent + descent), width: abs(rightOffset - leftOffset) + rightInset, height: ascent + descent))) } var isLastLine = false @@ -1033,6 +1033,7 @@ public class TextNode: ASDisplayNode { let truncationToken = CTLineCreateWithAttributedString(truncatedTokenString) coreTextLine = CTLineCreateTruncatedLine(originalLine, Double(lineConstrainedSize.width), truncationType, truncationToken) ?? truncationToken + brokenLineRange.length = CTLineGetGlyphCount(coreTextLine) - 1 truncated = true } @@ -1063,7 +1064,7 @@ public class TextNode: ASDisplayNode { if let currentStartIndex = startIndex, let currentIndex = currentIndex { startIndex = nil let endIndex = currentIndex - addSpoiler(line: coreTextLine, ascent: ascent, descent: descent, startIndex: currentStartIndex, endIndex: endIndex) + addSpoiler(line: coreTextLine, ascent: ascent, descent: descent, startIndex: currentStartIndex, endIndex: endIndex, rightInset: truncated ? 12.0 : 0.0) } } else if let _ = attributes[NSAttributedString.Key.strikethroughStyle] { let lowerX = floor(CTLineGetOffsetForStringIndex(coreTextLine, range.location, nil)) diff --git a/submodules/TelegramUI/Sources/ChatMessageReplyInfoNode.swift b/submodules/TelegramUI/Sources/ChatMessageReplyInfoNode.swift index 73eafa58ac..a300505b13 100644 --- a/submodules/TelegramUI/Sources/ChatMessageReplyInfoNode.swift +++ b/submodules/TelegramUI/Sources/ChatMessageReplyInfoNode.swift @@ -250,8 +250,8 @@ class ChatMessageReplyInfoNode: ASDisplayNode { node.dustNode = dustNode node.contentNode.insertSubnode(dustNode, aboveSubnode: textNode) } - dustNode.update(size: textFrame.size, color: titleColor, rects: textLayout.spoilers.map { $0.offsetBy(dx: 3.0, dy: 3.0).insetBy(dx: 1.0, dy: 1.0) }) dustNode.frame = textFrame.insetBy(dx: -3.0, dy: -3.0).offsetBy(dx: 0.0, dy: 3.0) + dustNode.update(size: dustNode.frame.size, color: titleColor, rects: textLayout.spoilers.map { $0.offsetBy(dx: 3.0, dy: 3.0).insetBy(dx: 1.0, dy: 1.0) }) } else if let dustNode = node.dustNode { dustNode.removeFromSupernode() node.dustNode = nil diff --git a/submodules/TelegramUI/Sources/ChatMessageTextBubbleContentNode.swift b/submodules/TelegramUI/Sources/ChatMessageTextBubbleContentNode.swift index 5c181f4b3d..4d4231b572 100644 --- a/submodules/TelegramUI/Sources/ChatMessageTextBubbleContentNode.swift +++ b/submodules/TelegramUI/Sources/ChatMessageTextBubbleContentNode.swift @@ -404,8 +404,8 @@ class ChatMessageTextBubbleContentNode: ChatMessageBubbleContentNode { strongSelf.dustNode = dustNode strongSelf.insertSubnode(dustNode, aboveSubnode: spoilerTextNode) } - dustNode.update(size: textFrame.size, color: messageTheme.secondaryTextColor, rects: textLayout.spoilers.map { $0.offsetBy(dx: 3.0, dy: 3.0).insetBy(dx: 1.0, dy: 1.0) }) dustNode.frame = textFrame.insetBy(dx: -3.0, dy: -3.0).offsetBy(dx: 0.0, dy: 3.0) + dustNode.update(size: dustNode.frame.size, color: messageTheme.secondaryTextColor, rects: textLayout.spoilers.map { $0.offsetBy(dx: 3.0, dy: 3.0).insetBy(dx: 1.0, dy: 1.0) }) } else if let spoilerTextNode = strongSelf.spoilerTextNode { strongSelf.spoilerTextNode = nil spoilerTextNode.removeFromSupernode() From 6cdf72427c7d9da2621bb4cf8375c13060ec4310 Mon Sep 17 00:00:00 2001 From: Ilya Laktyushin Date: Sun, 19 Dec 2021 18:34:04 +0400 Subject: [PATCH 3/3] Check if broken line range is out of string length --- submodules/Display/Source/TextNode.swift | 3 +++ 1 file changed, 3 insertions(+) diff --git a/submodules/Display/Source/TextNode.swift b/submodules/Display/Source/TextNode.swift index 1fb4070ea1..03be16238d 100644 --- a/submodules/Display/Source/TextNode.swift +++ b/submodules/Display/Source/TextNode.swift @@ -1034,6 +1034,9 @@ public class TextNode: ASDisplayNode { coreTextLine = CTLineCreateTruncatedLine(originalLine, Double(lineConstrainedSize.width), truncationType, truncationToken) ?? truncationToken brokenLineRange.length = CTLineGetGlyphCount(coreTextLine) - 1 + if brokenLineRange.location + brokenLineRange.length > attributedString.length { + brokenLineRange.length = attributedString.length - brokenLineRange.location + } truncated = true }