-
Notifications
You must be signed in to change notification settings - Fork 873
/
Copy pathAppMenuAnimatedImage.swift
212 lines (168 loc) · 6.49 KB
/
AppMenuAnimatedImage.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
//
// AppMenuAnimatedImage.swift
// Telegram
//
// Created by Mikhail Filimonov on 08.12.2021.
// Copyright © 2021 Telegram. All rights reserved.
//
import Foundation
import TGUIKit
import SwiftSignalKit
import AppKit
import TelegramCore
import Postbox
import TelegramMedia
typealias MenuAnimation = LocalAnimatedSticker
extension MenuAnimation {
var value: (NSColor, ContextMenuItem)-> AppMenuItemImageDrawable {
{ color, item in
return AppMenuAnimatedImage(self, color, item)
}
}
}
struct MenuRemoteAnimation {
fileprivate let context: AccountContext
fileprivate let file: TelegramMediaFile
fileprivate let thumb: LocalAnimatedSticker
fileprivate let bot: Peer
init(_ context: AccountContext, file: TelegramMediaFile, bot: Peer, thumb: LocalAnimatedSticker) {
self.context = context
self.file = file
self.bot = bot
self.thumb = thumb
}
var value: (NSColor, ContextMenuItem)-> AppMenuItemImageDrawable {
{ color, item in
return AppMenuAnimatedRemoteImage(self, color, item)
}
}
}
final class AppMenuAnimatedImage : LottiePlayerView, AppMenuItemImageDrawable {
private let sticker: LocalAnimatedSticker
private let item: ContextMenuItem
private let imageView = ImageView(frame: NSMakeRect(0, 0, 18, 18))
private let disposable = MetaDisposable()
init(_ sticker: LocalAnimatedSticker, _ color: NSColor?, _ item: ContextMenuItem) {
self.sticker = sticker
self.item = item
super.init(frame: NSMakeRect(0, 0, 18, 18))
addSubview(imageView)
self.imageView.image = sticker.menuIcon(color ?? theme.colors.text)
if let data = sticker.data {
var colors:[LottieColor] = []
if let color = color {
colors = [.init(keyPath: "", color: color)]
} else {
colors = []
}
let animation = LottieAnimation(compressed: data, key: LottieAnimationEntryKey(key: .bundle(self.sticker.rawValue), size: frame.size), type: .lottie, cachePurpose: .none, playPolicy: .framesCount(1), maximumFps: 60, colors: colors, metalSupport: false)
self.animation = animation
}
disposable.set(state.start(next: { [weak self] state in
self?.imageView.isHidden = state == .playing
if state == .finished {
let animation = self?.animation
self?.set(nil, animated: false)
self?.animation = animation
}
}))
}
deinit {
disposable.dispose()
}
required init?(coder decoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
required init() {
fatalError("init() has not been implemented")
}
required init(frame frameRect: NSRect) {
fatalError("init(frame:) has not been implemented")
}
func isEqual(to item: ContextMenuItem) -> Bool {
return self.item.id == item.id
}
func setColor(_ color: NSColor) {
self.setColors([.init(keyPath: "", color: color)])
self.imageView.image = sticker.menuIcon(color)
}
func updateState(_ controlState: ControlState) {
switch controlState {
case .Hover:
if !isLite(.menu_animations) {
if self.currentState != .playing {
self.set(self.animation?.withUpdatedPolicy(.once), reset: false)
}
}
default:
break
}
}
}
final class AppMenuAnimatedRemoteImage : LottiePlayerView, AppMenuItemImageDrawable {
private let sticker: MenuRemoteAnimation
private let item: ContextMenuItem
private let disposable = MetaDisposable()
private let color: NSColor?
init(_ sticker: MenuRemoteAnimation, _ color: NSColor?, _ item: ContextMenuItem) {
self.sticker = sticker
self.item = item
self.color = color
super.init(frame: NSMakeRect(0, 0, 18, 18))
if let reference = PeerReference(sticker.bot) {
_ = fetchedMediaResource(mediaBox: sticker.context.account.postbox.mediaBox, userLocation: .peer(sticker.bot.id), userContentType: .other, reference: .media(media: .attachBot(peer: reference, media: sticker.file), resource: sticker.file.resource)).start()
}
let signal = sticker.context.account.postbox.mediaBox.resourceData(sticker.file.resource, attemptSynchronously: true) |> deliverOnMainQueue
disposable.set(signal.start(next: { [weak self] data in
if data.complete, let data = try? Data(contentsOf: URL(fileURLWithPath: data.path)) {
self?.apply(data)
} else {
if let data = self?.sticker.thumb.data {
self?.apply(data)
}
}
}))
}
private func apply(_ data: Data) {
var colors:[LottieColor] = []
if let color = color {
colors = [.init(keyPath: "", color: color)]
} else {
colors = []
}
let animation = LottieAnimation(compressed: data, key: LottieAnimationEntryKey(key: .bundle("file_id_\(self.sticker.file.fileId)_\(data.hashValue)"), size: frame.size), type: .lottie, cachePurpose: .none, playPolicy: .framesCount(1), maximumFps: 60, colors: colors, metalSupport: false)
self.set(animation, reset: true, saveContext: false, animated: false)
}
deinit {
disposable.dispose()
}
required init?(coder decoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
required init() {
fatalError("init() has not been implemented")
}
required init(frame frameRect: NSRect) {
fatalError("init(frame:) has not been implemented")
}
func isEqual(to item: ContextMenuItem) -> Bool {
return self.item.id == item.id
}
func setColor(_ color: NSColor) {
self.setColors([.init(keyPath: "", color: color)])
}
func updateState(_ controlState: ControlState) {
switch controlState {
case .Hover:
if !isLite(.menu_animations) {
if self.animation?.playPolicy == .framesCount(1) {
self.set(self.animation?.withUpdatedPolicy(.once), reset: false)
} else {
self.playAgain()
}
}
default:
break
}
}
}