Various improvements

This commit is contained in:
Isaac
2023-12-20 22:17:06 +04:00
parent 46e203137a
commit 6730f7d2fb
25 changed files with 1047 additions and 189 deletions

View File

@@ -144,6 +144,58 @@ public func animationCacheFetchFile(postbox: Postbox, userLocation: MediaResourc
}
}
private func generatePeerNameColorImage(nameColor: PeerNameColors.Colors, isDark: Bool, bounds: CGSize = CGSize(width: 40.0, height: 40.0), size: CGSize = CGSize(width: 40.0, height: 40.0)) -> UIImage? {
return generateImage(bounds, rotatedContext: { contextSize, context in
let bounds = CGRect(origin: CGPoint(), size: contextSize)
context.clear(bounds)
let circleBounds = CGRect(origin: CGPoint(x: floorToScreenPixels((bounds.width - size.width) / 2.0), y: floorToScreenPixels((bounds.height - size.height) / 2.0)), size: size)
context.addEllipse(in: circleBounds)
context.clip()
if let secondColor = nameColor.secondary {
var firstColor = nameColor.main
var secondColor = secondColor
if isDark, nameColor.tertiary == nil {
firstColor = secondColor
secondColor = nameColor.main
}
context.setFillColor(secondColor.cgColor)
context.fill(circleBounds)
if let thirdColor = nameColor.tertiary {
context.move(to: CGPoint(x: contextSize.width, y: 0.0))
context.addLine(to: CGPoint(x: contextSize.width, y: contextSize.height))
context.addLine(to: CGPoint(x: 0.0, y: contextSize.height))
context.closePath()
context.setFillColor(firstColor.cgColor)
context.fillPath()
context.setFillColor(thirdColor.cgColor)
context.translateBy(x: contextSize.width / 2.0, y: contextSize.height / 2.0)
context.rotate(by: .pi / 4.0)
let rectSide = size.width / 40.0 * 18.0
let rectCornerRadius = round(size.width / 40.0 * 4.0)
let path = UIBezierPath(roundedRect: CGRect(origin: CGPoint(x: -rectSide / 2.0, y: -rectSide / 2.0), size: CGSize(width: rectSide, height: rectSide)), cornerRadius: rectCornerRadius)
context.addPath(path.cgPath)
context.fillPath()
} else {
context.move(to: .zero)
context.addLine(to: CGPoint(x: contextSize.width, y: 0.0))
context.addLine(to: CGPoint(x: 0.0, y: contextSize.height))
context.closePath()
context.setFillColor(firstColor.cgColor)
context.fillPath()
}
} else {
context.setFillColor(nameColor.main.cgColor)
context.fill(circleBounds)
}
})
}
public final class InlineStickerItemLayer: MultiAnimationRenderTarget {
public enum Context: Equatable {
public final class Custom: Equatable {
@@ -317,8 +369,13 @@ public final class InlineStickerItemLayer: MultiAnimationRenderTarget {
super.init()
if let topicInfo = emoji.topicInfo {
self.updateTopicInfo(topicInfo: topicInfo)
if let custom = emoji.custom {
switch custom {
case let .topic(id, info):
self.updateTopicInfo(topicInfo: (id, info))
case let .nameColors(colors):
self.updateNameColors(colors: colors)
}
} else if let file = file {
self.updateFile(file: file, attemptSynchronousLoad: attemptSynchronousLoad)
} else {
@@ -467,6 +524,30 @@ public final class InlineStickerItemLayer: MultiAnimationRenderTarget {
}
}
private func updateNameColors(colors: [UInt32]) {
if colors.isEmpty {
return
}
let main = UIColor(rgb: colors[0])
var secondary: UIColor?
if colors.count >= 2 {
secondary = UIColor(rgb: colors[1])
}
var tertiary: UIColor?
if colors.count >= 3 {
tertiary = UIColor(rgb: colors[2])
}
let mappedColor = PeerNameColors.Colors(main: main, secondary: secondary, tertiary: tertiary)
let image = generateImage(CGSize(width: 18.0, height: 18.0), contextGenerator: { size, context in
context.clear(CGRect(origin: .zero, size: size))
if let cgImage = generatePeerNameColorImage(nameColor: mappedColor, isDark: false, bounds: CGSize(width: 18.0, height: 18.0), size: CGSize(width: 16.0, height: 16.0))?.cgImage {
context.draw(cgImage, in: CGRect(origin: .zero, size: size))
}
})
self.contents = image?.cgImage
}
private func updateFile(file: TelegramMediaFile, attemptSynchronousLoad: Bool) {
if self.file?.fileId == file.fileId {
return