-
Notifications
You must be signed in to change notification settings - Fork 895
/
Copy pathPollTimerView.swift
271 lines (218 loc) · 10.8 KB
/
PollTimerView.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
//
// PollTimerView.swift
// Telegram
//
// Created by Mikhail Filimonov on 09/04/2020.
// Copyright © 2020 Telegram. All rights reserved.
//
import Cocoa
private func textForTimeout(value: Int) -> String {
//TODO: localize
if value > 60 * 60 {
let hours = value / (60 * 60)
return "\(hours)h"
} else {
let minutes = value / 60
let seconds = value % 60
let minutesPadding = minutes < 10 ? "0" : ""
let secondsPadding = seconds < 10 ? "0" : ""
return "\(minutesPadding)\(minutes):\(secondsPadding)\(seconds)"
}
}
private enum ContentState: Equatable {
case clock(NSColor)
case timeout(NSColor, CGFloat)
}
private struct ContentParticle {
var position: CGPoint
var direction: CGPoint
var velocity: CGFloat
var alpha: CGFloat
var lifetime: Double
var beginTime: Double
init(position: CGPoint, direction: CGPoint, velocity: CGFloat, alpha: CGFloat, lifetime: Double, beginTime: Double) {
self.position = position
self.direction = direction
self.velocity = velocity
self.alpha = alpha
self.lifetime = lifetime
self.beginTime = beginTime
}
}
public final class PollBubbleTimerView: View {
private struct Params: Equatable {
var regularColor: NSColor
var proximityColor: NSColor
var timeout: Int32
var deadlineTimestamp: Int32?
}
private var animator: ConstantDisplayLinkAnimator?
private let textView: TextView = TextView()
private let contentView: ImageView = ImageView()
private var currentContentState: ContentState?
private var particles: [ContentParticle] = []
private var currentParams: Params?
public var reachedTimeout: (() -> Void)?
public required init(frame frameRect: NSRect) {
super.init(frame: frameRect)
self.addSubview(self.textView)
self.addSubview(self.contentView)
textView.userInteractionEnabled = false
textView.isSelectable = false
textView.isEventLess = true
}
public required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
deinit {
self.animator?.invalidate()
}
public func update(regularColor: NSColor, proximityColor: NSColor, timeout: Int32, deadlineTimestamp: Int32?) {
let params = Params(
regularColor: regularColor,
proximityColor: proximityColor,
timeout: timeout,
deadlineTimestamp: deadlineTimestamp
)
self.currentParams = params
self.updateValues()
}
public override func viewWillMove(toWindow newWindow: NSWindow?) {
super.viewWillMove(toWindow: newWindow)
self.animator?.isPaused = newWindow == nil
}
private func updateValues() {
guard let params = self.currentParams else {
return
}
let fractionalTimeout: Double
if let deadlineTimestamp = params.deadlineTimestamp {
let fractionalTimestamp = CFAbsoluteTimeGetCurrent() + NSTimeIntervalSince1970
fractionalTimeout = min(Double(params.timeout), max(0.0, Double(deadlineTimestamp) + 1.0 - fractionalTimestamp))
} else {
fractionalTimeout = Double(params.timeout)
}
let timeout = Int(round(fractionalTimeout))
let proximityInterval: Double = 5.0
let timerInterval: Double = 60.0
let isProximity = timeout <= Int(proximityInterval)
let isTimer = timeout <= Int(timerInterval)
let color = isProximity ? params.proximityColor : params.regularColor
let attributed = NSAttributedString.initialize(string: textForTimeout(value: timeout), color: color, font: .normal(.short))
let textLayout = TextViewLayout(attributed)
textLayout.measure(width: 100)
// self.textView.attributedText = NSAttributedString(string: textForTimeout(value: timeout), font: Font.regular(14.0), textColor: color)
let textSize = textLayout.layoutSize
self.textView.update(textLayout)
let contentState: ContentState
if isTimer {
var fraction: CGFloat = 1.0
if fractionalTimeout <= timerInterval {
fraction = CGFloat(fractionalTimeout) / min(CGFloat(timerInterval), CGFloat(params.timeout))
}
fraction = max(0.0, min(0.99, fraction))
contentState = .timeout(color, 1.0 - fraction)
} else {
contentState = .clock(color)
}
if self.currentContentState != contentState {
self.currentContentState = contentState
let image: CGImage?
let diameter: CGFloat = 14.0
let inset: CGFloat = 8.0
let lineWidth: CGFloat = 1.2
switch contentState {
case let .clock(color):
image = generateImage(CGSize(width: diameter + inset, height: diameter + inset), rotatedContext: { size, context in
context.clear(CGRect(origin: CGPoint(), size: size))
context.setStrokeColor(color.cgColor)
context.setLineWidth(lineWidth)
context.setLineCap(.round)
let clockFrame = CGRect(origin: CGPoint(x: (size.width - diameter) / 2.0, y: (size.height - diameter) / 2.0), size: CGSize(width: diameter, height: diameter))
context.strokeEllipse(in: clockFrame.insetBy(dx: lineWidth / 2.0, dy: lineWidth / 2.0))
context.move(to: CGPoint(x: size.width / 2.0, y: size.height / 2.0))
context.addLine(to: CGPoint(x: size.width / 2.0, y: clockFrame.minY + 4.0))
context.strokePath()
let topWidth: CGFloat = 4.0
context.move(to: CGPoint(x: size.width / 2.0 - topWidth / 2.0, y: clockFrame.minY - 2.0))
context.addLine(to: CGPoint(x: size.width / 2.0 + topWidth / 2.0, y: clockFrame.minY - 2.0))
context.strokePath()
})
case let .timeout(color, fraction):
let timestamp = CACurrentMediaTime()
let center = CGPoint(x: (diameter + inset) / 2.0, y: (diameter + inset) / 2.0)
let radius: CGFloat = (diameter - lineWidth / 2.0) / 2.0
let startAngle: CGFloat = -CGFloat.pi / 2.0
let endAngle: CGFloat = -CGFloat.pi / 2.0 + 2.0 * CGFloat.pi * fraction
let v = CGPoint(x: sin(endAngle), y: -cos(endAngle))
let c = CGPoint(x: -v.y * radius + center.x, y: v.x * radius + center.y)
let dt: CGFloat = 1.0 / 60.0
var removeIndices: [Int] = []
for i in 0 ..< self.particles.count {
let currentTime = timestamp - self.particles[i].beginTime
if currentTime > self.particles[i].lifetime {
removeIndices.append(i)
} else {
let input: CGFloat = CGFloat(currentTime / self.particles[i].lifetime)
let decelerated: CGFloat = (1.0 - (1.0 - input) * (1.0 - input))
self.particles[i].alpha = 1.0 - decelerated
var p = self.particles[i].position
let d = self.particles[i].direction
let v = self.particles[i].velocity
p = CGPoint(x: p.x + d.x * v * dt, y: p.y + d.y * v * dt)
self.particles[i].position = p
}
}
for i in removeIndices.reversed() {
self.particles.remove(at: i)
}
let newParticleCount = 1
for _ in 0 ..< newParticleCount {
let degrees: CGFloat = CGFloat(arc4random_uniform(140)) - 40.0
let angle: CGFloat = degrees * CGFloat.pi / 180.0
let direction = CGPoint(x: v.x * cos(angle) - v.y * sin(angle), y: v.x * sin(angle) + v.y * cos(angle))
let velocity = (20.0 + (CGFloat(arc4random()) / CGFloat(UINT32_MAX)) * 4.0) * 0.3
let lifetime = Double(0.4 + CGFloat(arc4random_uniform(100)) * 0.01)
let particle = ContentParticle(position: c, direction: direction, velocity: velocity, alpha: 1.0, lifetime: lifetime, beginTime: timestamp)
self.particles.append(particle)
}
image = generateImage(CGSize(width: diameter + inset, height: diameter + inset), rotatedContext: { size, context in
context.clear(CGRect(origin: CGPoint(), size: size))
context.setStrokeColor(color.cgColor)
context.setFillColor(color.cgColor)
context.setLineWidth(lineWidth)
context.setLineCap(.round)
let path = CGMutablePath()
path.addArc(center: center, radius: radius, startAngle: startAngle, endAngle: endAngle, clockwise: true)
context.addPath(path)
context.strokePath()
for particle in self.particles {
let size: CGFloat = 1.15
context.setAlpha(particle.alpha)
context.fillEllipse(in: CGRect(origin: CGPoint(x: particle.position.x - size / 2.0, y: particle.position.y - size / 2.0), size: CGSize(width: size, height: size)))
}
})
}
self.contentView.image = image
self.contentView.sizeToFit()
self.contentView.centerY(x: frame.width - contentView.frame.width)
self.textView.centerY(x: frame.width - contentView.frame.width - textSize.width - 4)
}
if let reachedTimeout = self.reachedTimeout, fractionalTimeout <= .ulpOfOne {
reachedTimeout()
}
if fractionalTimeout <= .ulpOfOne {
self.animator?.invalidate()
self.animator = nil
} else {
if self.animator == nil {
let animator = ConstantDisplayLinkAnimator(update: { [weak self] in
self?.updateValues()
})
animator.isPaused = self.window == nil
self.animator = animator
// animator.isPaused = self.inHierarchyValue
}
}
}
}