-
Notifications
You must be signed in to change notification settings - Fork 895
/
Copy pathLinearProgressControl.swift
418 lines (335 loc) · 15.7 KB
/
LinearProgressControl.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
//
// LinearProgressControl.swift
// TGUIKit
//
// Created by keepcoder on 28/10/2016.
// Copyright © 2016 Telegram. All rights reserved.
//
import Cocoa
public enum LinearProgressAlignment {
case bottom
case center
case top
}
public class LinearProgressControl: Control {
public var hasMinumimVisibility: Bool = false
private var progressView:View!
private var fetchingView:View!
private var fetchingViewRanges:[View] = []
private var containerView:View!
private var _progress:CGFloat = 0
private var progress:CGFloat {
get {
return scrubblingTempState ?? _progress
}
set {
_progress = newValue
}
}
private var fetchingProgress: CGFloat = 0
private var fetchingProgressRanges: [Range<CGFloat>] = []
public var progressHeight:CGFloat
public var onUserChanged:((Float)->Void)?
public var onLiveScrobbling:((Float?)->Void)?
public var startScrobbling:(()->Void)?
public var endScrobbling:(()->Void)?
public var insets: NSEdgeInsets = NSEdgeInsets() {
didSet {
needsLayout = true
}
}
public var alignment: LinearProgressAlignment = .bottom
public var liveScrobbling: Bool = true
private var scrubber: ImageButton? = nil
private(set) public var scrubblingTempState: CGFloat? {
didSet {
needsLayout = true
self.progressView.layer?.removeAllAnimations()
self.scrubber?.layer?.removeAllAnimations()
}
}
public var cornerRadius: CGFloat = 0 {
didSet {
self.progressView.layer?.cornerRadius = cornerRadius
self.layer?.cornerRadius = cornerRadius
}
}
public var scrubberImage: CGImage? {
didSet {
if let scrubberImage = scrubberImage {
if scrubber == nil {
scrubber = ImageButton()
scrubber?.userInteractionEnabled = false
scrubber?.autohighlight = false
scrubber!.set(image: scrubberImage, for: .Normal)
_ = scrubber!.sizeToFit()
addSubview(scrubber!)
} else {
scrubber!.set(image: scrubberImage, for: .Normal)
_ = scrubber!.sizeToFit()
}
needsLayout = true
} else {
scrubber?.removeFromSuperview()
scrubber = nil
}
}
}
public var roundCorners: Bool = false {
didSet {
containerView.layer?.cornerRadius = roundCorners ? containerView.frame.height / 2 : 0
progressView.layer?.cornerRadius = roundCorners ? progressView.frame.height / 2 : 0
fetchingView.layer?.cornerRadius = roundCorners ? fetchingView.frame.height / 2 : 0
for view in fetchingViewRanges {
view.layer?.cornerRadius = roundCorners ? view.frame.height / 2 : 0
}
}
}
public var currentValue: CGFloat {
return progress
}
public override func mouseDragged(with event: NSEvent) {
super.mouseDragged(with: event)
if let onUserChanged = onUserChanged, isEnabled {
let location = containerView.convert(event.locationInWindow, from: nil)
// if location.x >= 0 && location.x <= frame.width {
let progress = min(max(Float(max(location.x, 0) / containerView.frame.width), 0), 1)
if liveScrobbling {
onUserChanged(progress)
} else {
self.scrubblingTempState = CGFloat(progress)
self.onLiveScrobbling?(progress)
}
// }
}
}
public override func mouseDown(with event: NSEvent) {
scrubblingTempState = nil
self.started = true
self.startScrobbling?()
if let _ = onUserChanged, !liveScrobbling, isEnabled {
let location = containerView.convert(event.locationInWindow, from: nil)
let progress = min(max(CGFloat(max(location.x, 0) / containerView.frame.width), 0), 1)
self.scrubblingTempState = progress
self.onLiveScrobbling?(nil)
// }
} else if !userInteractionEnabled {
//super.mouseDown(with: event)
}
}
private var started: Bool = false
public var hasTemporaryState: Bool {
return scrubblingTempState != nil || started
}
public override func mouseUp(with event: NSEvent) {
scrubblingTempState = nil
self.started = false
self.endScrobbling?()
if let onUserChanged = onUserChanged, isEnabled {
let location = containerView.convert(event.locationInWindow, from: nil)
let progress = min(max(Float(max(location.x, 0) / containerView.frame.width), 0), 1)
onUserChanged(progress)
self.onLiveScrobbling?(nil)
}
}
public var interactiveValue:Float {
if let window = window {
let location = containerView.convert(window.mouseLocationOutsideOfEventStream, from: nil)
return Float(location.x / containerView.frame.width)
}
return 0
}
open override func updateTrackingAreas() {
super.updateTrackingAreas();
if let trackingArea = trackingArea {
self.removeTrackingArea(trackingArea)
}
trackingArea = nil
if let _ = window {
let options:NSTrackingArea.Options = [NSTrackingArea.Options.cursorUpdate, NSTrackingArea.Options.mouseEnteredAndExited, NSTrackingArea.Options.mouseMoved, NSTrackingArea.Options.enabledDuringMouseDrag, NSTrackingArea.Options.activeInKeyWindow,NSTrackingArea.Options.inVisibleRect]
self.trackingArea = NSTrackingArea(rect: self.bounds, options: options, owner: self, userInfo: nil)
self.addTrackingArea(self.trackingArea!)
}
}
deinit {
if let trackingArea = self.trackingArea {
self.removeTrackingArea(trackingArea)
}
}
public override var style: ControlStyle {
set {
self.progressView.layer?.backgroundColor = newValue.foregroundColor.cgColor
super.style = newValue
}
get {
return super.style
}
}
public var containerBackground: NSColor = .clear {
didSet {
containerView.backgroundColor = containerBackground
}
}
public var fetchingColor: NSColor = presentation.colors.grayTransparent {
didSet {
self.fetchingView.layer?.backgroundColor = fetchingColor.cgColor
for fetchView in fetchingViewRanges {
fetchView.backgroundColor = fetchingColor
}
}
}
private func preparedAnimation(keyPath: String, from: NSValue, to: NSValue, duration: Double, beginTime: Double?, offset: Double, speed: Float, repeatForever: Bool = false) -> CAAnimation {
let animation = CABasicAnimation(keyPath: keyPath)
animation.fromValue = from
animation.toValue = to
animation.duration = duration
animation.fillMode = .both
animation.speed = speed
animation.timeOffset = offset
animation.isAdditive = false
if let beginTime = beginTime {
animation.beginTime = beginTime
}
return animation
}
public func set(progress:CGFloat, animated:Bool, duration: Double, beginTime: Double?, offset: Double, speed: Float, repeatForever: Bool = false) {
let progress:CGFloat = progress.isNaN ? 1 : progress
self.progress = progress
let size = NSMakeSize(floorToScreenPixels(backingScaleFactor, max(containerView.frame.width * self.progress, hasMinumimVisibility ? progressHeight : 0)), progressHeight)
progressView.centerY(x: 0)
let fromBounds = NSMakeRect(0, progressView.frame.minY, 0, size.height)
let toBounds = NSMakeRect(0, progressView.frame.minY, containerView.frame.width, size.height)
if animated, scrubblingTempState == nil {
progressView.layer?.add(preparedAnimation(keyPath: "bounds", from: NSValue(rect: fromBounds), to: NSValue(rect: toBounds), duration: duration, beginTime: beginTime, offset: offset, speed: speed), forKey: "bounds")
if let scrubber = scrubber {
scrubber.layer?.add(preparedAnimation(keyPath: "position", from: NSValue(point: NSMakePoint(containerView.frame.minX - scrubber.frame.width / 2, scrubber.frame.minY)), to: NSValue(point: NSMakePoint(containerView.frame.maxX - scrubber.frame.width / 2, scrubber.frame.minY)), duration: duration, beginTime: beginTime, offset: offset, speed: speed), forKey: "position")
}
} else {
progressView.layer?.removeAllAnimations()
set(progress: progress)
}
updateFetchingRanges(animated)
}
public override var frame: NSRect {
didSet {
layout()
}
}
public func set(progress:CGFloat, animated:Bool = false, duration: Double = 0.2, timingFunction: CAMediaTimingFunctionName = .linear, bounce: Bool = false) {
let progress:CGFloat = progress.isNaN ? 1 : progress
self.progress = progress
let size = NSMakeSize(floorToScreenPixels(backingScaleFactor, max(containerView.frame.width * self.progress, hasMinumimVisibility ? progressHeight : 0)), progressHeight)
if animated {
let size = NSMakeSize(size.width - progressView.frame.width, size.height - progressView.frame.height)
progressView.layer?.animateBounds(from: CGRect(origin: .zero, size: size), to: .zero, duration: duration, timingFunction: timingFunction, additive: true)
} else {
progressView.layer?.removeAnimation(forKey: "bounds")
}
progressView.change(size: size, animated: animated, duration: duration, timingFunction: timingFunction)
if let scrubber = scrubber {
scrubber.change(pos: NSMakePoint(max(0, min(containerView.frame.minX + size.width - scrubber.frame.width / 2, containerView.frame.maxX - scrubber.frame.width)), scrubber.frame.minY), animated: animated, timingFunction: timingFunction)
}
progressView.centerY(x: 0)
updateFetchingRanges(animated)
}
public func set(fetchingProgress: CGFloat, animated:Bool = false, duration: Double = 0.2) {
let fetchingProgress:CGFloat = fetchingProgress.isNaN ? 1 : fetchingProgress
self.fetchingProgress = fetchingProgress
let size = NSMakeSize(floorToScreenPixels(backingScaleFactor, containerView.frame.width * fetchingProgress), progressHeight)
fetchingView.change(size: size, animated: animated, duration: duration, timingFunction: .linear)
fetchingView.centerY(x: 0)
}
public func set(fetchingProgressRanges: [Range<CGFloat>], animated: Bool = true) {
self.fetchingProgressRanges = fetchingProgressRanges
updateFetchingRanges(animated)
}
private func updateFetchingRanges(_ animated: Bool) {
let fetchingProgressRanges = self.fetchingProgressRanges.filter({$0.contains(self.currentValue)})
if self.fetchingViewRanges.count == fetchingProgressRanges.count {
} else if self.fetchingViewRanges.count > fetchingProgressRanges.count {
while self.fetchingViewRanges.count != fetchingProgressRanges.count {
self.fetchingViewRanges.removeLast().removeFromSuperview()
}
} else if self.fetchingViewRanges.count < fetchingProgressRanges.count {
while self.fetchingViewRanges.count != fetchingProgressRanges.count {
let view = View(frame: NSMakeRect(0, 0, 0, progressHeight))
view.backgroundColor = fetchingColor
self.fetchingViewRanges.append(view)
containerView.addSubview(self.fetchingViewRanges.last!, positioned: .below, relativeTo: progressView)
}
}
for i in 0 ..< fetchingProgressRanges.count {
let range = fetchingProgressRanges[i]
let view = self.fetchingViewRanges[i]
let width = (range.upperBound - range.lowerBound) * containerView.frame.width
view.change(size: NSMakeSize(width, progressHeight), animated: animated, duration: 0.2, timingFunction: .linear)
view.setFrameOrigin(range.lowerBound * containerView.frame.width, floorToScreenPixels(backingScaleFactor, (containerView.frame.height - progressHeight) / 2))
view.layer?.cornerRadius = roundCorners ? view.frame.height / 2 : 0
}
}
public init(progressHeight:CGFloat = 4) {
self.progressHeight = progressHeight
super.init(frame: NSMakeRect(0, 0, 0, progressHeight))
initialize()
}
public override func layout() {
super.layout()
switch alignment {
case .bottom:
containerView.frame = NSMakeRect(insets.left, frame.height - progressHeight, frame.width - insets.left - insets.right, progressHeight)
case .top:
containerView.frame = NSMakeRect(insets.left, 0, frame.width - insets.left - insets.right, progressHeight)
case .center:
containerView.frame = NSMakeRect(insets.left, floorToScreenPixels(backingScaleFactor, (frame.height - progressHeight) / 2), frame.width - insets.left - insets.right, progressHeight)
}
let size = NSMakeSize(floorToScreenPixels(backingScaleFactor, max(containerView.frame.width * self.progress, hasMinumimVisibility ? progressHeight : 0)), progressHeight)
progressView.setFrameSize(size)
if let scrubber = scrubber {
scrubber.centerY(x: max(0, min(containerView.frame.minX + size.width - scrubber.frame.width / 2, containerView.frame.maxX - scrubber.frame.width)))
}
}
private func initialize() {
containerView = View(frame:NSMakeRect(0, 0, 0, progressHeight))
addSubview(containerView)
fetchingView = View(frame:NSMakeRect(0, 0, 0, progressHeight))
fetchingView.backgroundColor = style.foregroundColor
containerView.addSubview(fetchingView)
progressView = View(frame:NSMakeRect(0, 0, 0, progressHeight))
progressView.backgroundColor = style.foregroundColor
containerView.addSubview(progressView)
userInteractionEnabled = false
progressView.isEventLess = true
containerView.isEventLess = true
fetchingView.isEventLess = true
}
public var highlightOnHover: Bool = true
private func updateCursor() {
if mouseInside() && onUserChanged != nil, style.highlightColor != .clear, isEnabled {
if highlightOnHover {
set(background: style.highlightColor.withAlphaComponent(0.2), for: .Hover)
}
} else {
set(background: style.backgroundColor, for: .Hover)
}
}
public override func mouseEntered(with event: NSEvent) {
super.mouseEntered(with: event)
updateCursor()
}
public override func mouseExited(with event: NSEvent) {
super.mouseExited(with: event)
updateCursor()
}
public override func mouseMoved(with event: NSEvent) {
super.mouseMoved(with: event)
updateCursor()
}
required public init(frame frameRect: NSRect) {
self.progressHeight = frameRect.height
super.init(frame:frameRect)
initialize()
}
required public init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}