Skip to content

Commit

Permalink
bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Renoir committed Apr 16, 2022
1 parent dbc989f commit 04c0559
Show file tree
Hide file tree
Showing 17 changed files with 780 additions and 90 deletions.
21 changes: 21 additions & 0 deletions Telegram-Mac/CachedResourceRepresentations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -253,3 +253,24 @@ public enum EmojiFitzModifier: Int32, Equatable {
}
}
}



final class CachedPreparedSvgRepresentation: CachedMediaResourceRepresentation {
public let keepDuration: CachedMediaRepresentationKeepDuration = .general

public var uniqueId: String {
return "prepared-svg"
}

public init() {
}

public func isEqual(to: CachedMediaResourceRepresentation) -> Bool {
if to is CachedPreparedSvgRepresentation {
return true
} else {
return false
}
}
}
4 changes: 3 additions & 1 deletion Telegram-Mac/ChatAnimatedStickerItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ final class ChatAnimatedStickerMediaLayoutParameters : ChatMediaLayoutParameters
let playOnHover: Bool?
let thumbAtFrame: Int
let shimmer: Bool
init(playPolicy: LottiePlayPolicy?, alwaysAccept: Bool? = nil, cache: ASCachePurpose? = nil, hidePlayer: Bool = false, media: TelegramMediaFile, colors: [LottieColor] = [], playOnHover: Bool? = nil, shimmer: Bool = true, thumbAtFrame: Int = 0) {
let noThumb: Bool
init(playPolicy: LottiePlayPolicy?, alwaysAccept: Bool? = nil, cache: ASCachePurpose? = nil, hidePlayer: Bool = false, media: TelegramMediaFile, colors: [LottieColor] = [], playOnHover: Bool? = nil, shimmer: Bool = true, thumbAtFrame: Int = 0, noThumb: Bool = false) {
self.playPolicy = playPolicy
self.alwaysAccept = alwaysAccept
self.cache = cache
Expand All @@ -30,6 +31,7 @@ final class ChatAnimatedStickerMediaLayoutParameters : ChatMediaLayoutParameters
self.playOnHover = playOnHover
self.shimmer = shimmer
self.thumbAtFrame = thumbAtFrame
self.noThumb = noThumb
super.init(presentation: .empty, media: media, automaticDownload: true, autoplayMedia: AutoplayMediaPreferences.defaultSettings)
}
}
Expand Down
2 changes: 1 addition & 1 deletion Telegram-Mac/ChatInputAttachView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class ChatInputAttachView: ImageButton, Notifable {
if canAddAttach {
items.append(ContextMenuItem(attach.shortName, handler: { [weak self] in
let invoke:()->Void = { [weak self] in
showModal(with: WebpageModalController(context: context, url: "", title: attach.peer.displayTitle, requestData: .normal(url: nil, peerId: peerId, bot: attach.peer, replyTo: replyTo, buttonText: "", payload: nil, fromMenu: false, complete: chatInteraction.afterSentTransition), chatInteraction: self?.chatInteraction, thumbFile: MenuAnimation.menu_folder_bot.file), for: context.window)
showModal(with: WebpageModalController(context: context, url: "", title: attach.peer.displayTitle, requestData: .normal(url: nil, peerId: peerId, bot: attach.peer, replyTo: replyTo, buttonText: "", payload: nil, fromMenu: false, complete: chatInteraction.afterSentTransition), chatInteraction: self?.chatInteraction, thumbFile: thumbFile), for: context.window)
}
if FastSettings.shouldConfirmWebApp(peer.id) {
confirm(for: context.window, header: strings().webAppFirstOpenTitle, information: strings().webAppFirstOpenInfo(attach.peer.displayTitle), successHandler: { _ in
Expand Down
42 changes: 39 additions & 3 deletions Telegram-Mac/ChatInterfaceInteraction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,18 @@ final class ChatInteraction : InterfaceObserver {
let openAttach:(Peer)->Void = { [weak self] peer in

let invoke:()->Void = { [weak self] in
showModal(with: WebpageModalController(context: context, url: "", title: peer.displayTitle, requestData: .normal(url: nil, peerId: peerId, bot: peer, replyTo: replyId, buttonText: "", payload: payload, fromMenu: false, complete: self?.afterSentTransition), chatInteraction: self, thumbFile: MenuAnimation.menu_folder_bot.file), for: context.window)
_ = showModalProgress(signal: context.engine.messages.getAttachMenuBot(botId: peer.id, cached: true), for: context.window).start(next: { attach in

let thumbFile: TelegramMediaFile
if let file = attach.icons[.macOSAnimated] {
thumbFile = file
} else {
thumbFile = MenuAnimation.menu_folder_bot.file
}

showModal(with: WebpageModalController(context: context, url: "", title: peer.displayTitle, requestData: .normal(url: nil, peerId: peerId, bot: peer, replyTo: replyId, buttonText: "", payload: payload, fromMenu: false, complete: self?.afterSentTransition), chatInteraction: self, thumbFile: thumbFile), for: context.window)

})
}
if peer.isVerified {
invoke()
Expand Down Expand Up @@ -584,7 +595,21 @@ final class ChatInteraction : InterfaceObserver {
guard let strongSelf = self else {
return
}
showModal(with: WebpageModalController(context: context, url: url, title: bot.displayTitle, requestData: .normal(url: url, peerId: peerId, bot: bot, replyTo: replyTo, buttonText: buttonText, payload: nil, fromMenu: true, complete: strongSelf.afterSentTransition), chatInteraction: strongSelf, thumbFile: MenuAnimation.menu_folder_bot.file), for: context.window)

let invoke:()->Void = { [weak self] in
_ = showModalProgress(signal: context.engine.messages.getAttachMenuBot(botId: bot.id, cached: true), for: context.window).start(next: { attach in

let thumbFile: TelegramMediaFile
if let file = attach.icons[.macOSAnimated] {
thumbFile = file
} else {
thumbFile = MenuAnimation.menu_folder_bot.file
}
showModal(with: WebpageModalController(context: context, url: url, title: bot.displayTitle, requestData: .normal(url: url, peerId: peerId, bot: bot, replyTo: replyTo, buttonText: buttonText, payload: nil, fromMenu: true, complete: strongSelf.afterSentTransition), chatInteraction: strongSelf, thumbFile: thumbFile), for: context.window)

})
}

}
if FastSettings.shouldConfirmWebApp(bot.id) {
confirm(for: context.window, header: strings().webAppFirstOpenTitle, information: strings().webAppFirstOpenInfo(bot.displayTitle), successHandler: { _ in
Expand Down Expand Up @@ -684,7 +709,18 @@ final class ChatInteraction : InterfaceObserver {
showModal(with: WebpageModalController(context: context, url: url, title: bot.displayTitle, requestData: .simple(url: hashUrl, bot: bot), chatInteraction: strongSelf, thumbFile: MenuAnimation.menu_folder_bot.file), for: context.window)
})
} else {
showModal(with: WebpageModalController(context: context, url: hashUrl, title: bot.displayTitle, requestData: .normal(url: hashUrl, peerId: peerId, bot: bot, replyTo: replyTo, buttonText: button.title, payload: nil, fromMenu: false, complete: strongSelf.afterSentTransition), chatInteraction: strongSelf, thumbFile: MenuAnimation.menu_folder_bot.file), for: context.window)

_ = showModalProgress(signal: context.engine.messages.getAttachMenuBot(botId: bot.id, cached: true), for: context.window).start(next: { attach in

let thumbFile: TelegramMediaFile
if let file = attach.icons[.macOSAnimated] {
thumbFile = file
} else {
thumbFile = MenuAnimation.menu_folder_bot.file
}
showModal(with: WebpageModalController(context: context, url: hashUrl, title: bot.displayTitle, requestData: .normal(url: hashUrl, peerId: peerId, bot: bot, replyTo: replyTo, buttonText: button.title, payload: nil, fromMenu: false, complete: strongSelf.afterSentTransition), chatInteraction: strongSelf, thumbFile: thumbFile), for: context.window)

})
}

}
Expand Down
2 changes: 1 addition & 1 deletion Telegram-Mac/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
</dict>
</array>
<key>CFBundleVersion</key>
<string>229731</string>
<string>229758</string>
<key>LSApplicationCategoryType</key>
<string>public.app-category.social-networking</string>
<key>LSFileQuarantineEnabled</key>
Expand Down
107 changes: 54 additions & 53 deletions Telegram-Mac/MediaAnimatedStickerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -298,67 +298,68 @@ class MediaAnimatedStickerView: ChatMediaContentView {

let arguments = TransformImageArguments(corners: ImageCorners(), imageSize: size, boundingSize: size, intrinsicInsets: NSEdgeInsets())





self.thumbView.setSignal(signal: cachedMedia(media: file, arguments: arguments, scale: backingScaleFactor), clearInstantly: updated)

let hasPlaceholder = (parent == nil || file.immediateThumbnailData != nil) && self.thumbView.image == nil && size.height >= 30 && (parameters == nil || parameters!.shimmer)
if updated {
if hasPlaceholder {
let current: StickerShimmerEffectView
if let local = self.placeholderView {
current = local
} else {
current = StickerShimmerEffectView()
current.frame = bounds
self.placeholderView = current
addSubview(current, positioned: .below, relativeTo: playerView)
if animated {
current.layer?.animateAlpha(from: 0, to: 1, duration: 0.2)

if parameters?.noThumb == false || parameters == nil {
self.thumbView.setSignal(signal: cachedMedia(media: file, arguments: arguments, scale: backingScaleFactor), clearInstantly: updated)

let hasPlaceholder = (parent == nil || file.immediateThumbnailData != nil) && self.thumbView.image == nil && size.height >= 30 && (parameters == nil || parameters!.shimmer)
if updated {
if hasPlaceholder {
let current: StickerShimmerEffectView
if let local = self.placeholderView {
current = local
} else {
current = StickerShimmerEffectView()
current.frame = bounds
self.placeholderView = current
addSubview(current, positioned: .below, relativeTo: playerView)
if animated {
current.layer?.animateAlpha(from: 0, to: 1, duration: 0.2)
}
}
current.update(backgroundColor: nil, foregroundColor: NSColor(rgb: 0x748391, alpha: 0.2), shimmeringColor: NSColor(rgb: 0x748391, alpha: 0.35), data: file.immediateThumbnailData, size: size)
current.updateAbsoluteRect(bounds, within: size)
} else {
self.removePlaceholder(animated: animated)
}
current.update(backgroundColor: nil, foregroundColor: NSColor(rgb: 0x748391, alpha: 0.2), shimmeringColor: NSColor(rgb: 0x748391, alpha: 0.35), data: file.immediateThumbnailData, size: size)
current.updateAbsoluteRect(bounds, within: size)
} else {
self.removePlaceholder(animated: animated)
}
}

self.thumbView.imageUpdated = { [weak self] value in
if value != nil {
self?.removePlaceholder(animated: animated)
self.thumbView.imageUpdated = { [weak self] value in
if value != nil {
self?.removePlaceholder(animated: animated)
}
}
}



if !self.thumbView.isFullyLoaded {

let signal: Signal<ImageDataTransformation, NoError>




switch file.mimeType {
case "image/webp":
signal = chatMessageSticker(postbox: context.account.postbox, file: reference, small: size.width <= 40, scale: backingScaleFactor, fetched: true)
default:
signal = chatMessageAnimatedSticker(postbox: context.account.postbox, file: reference, small: size.width <= 40, scale: backingScaleFactor, size: size, fetched: true, thumbAtFrame: parameters?.thumbAtFrame ?? 0, isVideo: file.fileName == "webm-preview")
}
self.thumbView.setSignal(signal, cacheImage: { [weak file, weak self] result in
if let file = file {
cacheMedia(result, media: file, arguments: arguments, scale: System.backingScale)
if !self.thumbView.isFullyLoaded {

let signal: Signal<ImageDataTransformation, NoError>



switch file.mimeType {
case "image/webp":
signal = chatMessageSticker(postbox: context.account.postbox, file: reference, small: size.width <= 40, scale: backingScaleFactor, fetched: true)
default:
signal = chatMessageAnimatedSticker(postbox: context.account.postbox, file: reference, small: size.width <= 40, scale: backingScaleFactor, size: size, fetched: true, thumbAtFrame: parameters?.thumbAtFrame ?? 0, isVideo: file.fileName == "webm-preview")
}
self?.removePlaceholder(animated: false)
})
}
self.thumbView.set(arguments: arguments)
if updated {
self.playerView.removeFromSuperview()
addSubview(self.thumbView)
}
self.thumbView.setSignal(signal, cacheImage: { [weak file, weak self] result in
if let file = file {
cacheMedia(result, media: file, arguments: arguments, scale: System.backingScale)
}
self?.removePlaceholder(animated: false)
})
}
self.thumbView.set(arguments: arguments)
if updated {
self.playerView.removeFromSuperview()
addSubview(self.thumbView)
}

}



fetchDisposable.set(fetchedMediaResource(mediaBox: context.account.postbox.mediaBox, reference: mediaResource).start())
if updated {
Expand Down
60 changes: 60 additions & 0 deletions Telegram-Mac/MediaUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,66 @@ func chatMessagePhotoDatas(postbox: Postbox, imageReference: ImageMediaReference
}


func svgIconImageFile(account: Account, fileReference: FileMediaReference?, scale:CGFloat = 2, stickToTop: Bool = false) -> Signal<(TransformImageArguments) -> DrawingContext?, NoError> {
let data: Signal<MediaResourceData, NoError>
if let fileReference = fileReference {
data = account.postbox.mediaBox.cachedResourceRepresentation(fileReference.media.resource, representation: CachedPreparedSvgRepresentation(), complete: false, fetch: true)
} else {
data = Signal { subscriber in
if let url = Bundle.main.url(forResource: "durgerking", withExtension: "placeholder"), let data = try? Data(contentsOf: url, options: .mappedRead) {
subscriber.putNext(MediaResourceData(path: url.path, offset: 0, size: data.count, complete: true))
subscriber.putCompletion()
}
return EmptyDisposable
}
}

return data
|> map { value in
let fullSizePath = value.path
let fullSizeComplete = value.complete
return { arguments in
let context = DrawingContext(size: arguments.drawingSize, scale: scale, clear: true)

let drawingRect = arguments.drawingRect
var fittedSize = arguments.imageSize.aspectFilled(arguments.boundingSize).fitted(arguments.imageSize)

var fullSizeImage: CGImage?
let imageOrientation: ImageOrientation = .up

if fullSizeComplete, let data = try? Data(contentsOf: URL(fileURLWithPath: fullSizePath)) {
let renderSize: CGSize
if stickToTop {
renderSize = .zero
} else {
renderSize = CGSize(width: 90.0, height: 90.0)
}
fullSizeImage = renderPreparedImage(data, renderSize, .clear, scale)?._cgImage
if let image = fullSizeImage {
fittedSize = image.size.aspectFitted(arguments.boundingSize)
}
}

var fittedRect = CGRect(origin: CGPoint(x: drawingRect.origin.x + (drawingRect.size.width - fittedSize.width) / 2.0, y: drawingRect.origin.y + (drawingRect.size.height - fittedSize.height) / 2.0), size: fittedSize)
if stickToTop {
fittedRect.origin.y = drawingRect.size.height - fittedSize.height
}

context.withFlippedContext { c in
if let fullSizeImage = fullSizeImage {
c.setBlendMode(.normal)
c.interpolationQuality = .medium
drawImage(context: c, image: fullSizeImage, orientation: imageOrientation, in: fittedRect)
}
}

addCorners(context, arguments: arguments, scale: scale)

return context
}
}
}


private func chatMessageWebFilePhotoDatas(account: Account, photo: TelegramMediaWebFile, synchronousLoad: Bool = false) -> Signal<ImageRenderData, NoError> {
let maybeFullSize = account.postbox.mediaBox.resourceData(photo.resource, attemptSynchronously: synchronousLoad)
Expand Down
5 changes: 3 additions & 2 deletions Telegram-Mac/StickerPackPanelRowItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@ class StickerPackPanelRowItem: TableRowItem {

private let preloadFeaturedDisposable = MetaDisposable()
let canSend: Bool
init(_ initialSize: NSSize, context: AccountContext, arguments: StickerPanelArguments, files:[TelegramMediaFile], packInfo: StickerPackInfo, collectionId: StickerPackCollectionId, canSend: Bool) {
let playOnHover: Bool
init(_ initialSize: NSSize, context: AccountContext, arguments: StickerPanelArguments, files:[TelegramMediaFile], packInfo: StickerPackInfo, collectionId: StickerPackCollectionId, canSend: Bool, playOnHover: Bool = false) {
self.context = context
self.arguments = arguments
self.canSend = canSend
self._files = files

self.playOnHover = playOnHover

let title: String?
var count: Int32 = 0
Expand Down
Loading

0 comments on commit 04c0559

Please sign in to comment.