-
Notifications
You must be signed in to change notification settings - Fork 895
/
Copy pathTableRowView.swift
518 lines (394 loc) · 14.6 KB
/
TableRowView.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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
//
// TableRowView.swift
// TGUIKit
//
// Created by keepcoder on 07/09/16.
// Copyright © 2016 Telegram. All rights reserved.
//
import Cocoa
import SwiftSignalKit
import AVFoundation
import Foundation
open class TableRowView: NSTableRowView, CALayerDelegate {
private var animatedView: RowAnimateView?
private let longDisposable = MetaDisposable()
public internal(set) var isResorting: Bool = false {
didSet {
updateIsResorting()
}
}
var dynamicContentStateForRestore:Bool? = nil
var interactionStateForRestore:Bool? = nil
public var isDynamicContentLocked:Bool = false {
didSet {
if isDynamicContentLocked != oldValue {
viewDidUpdatedDynamicContent()
}
}
}
public var userInteractionEnabled:Bool = true {
didSet {
if userInteractionEnabled != oldValue {
viewDidUpdatedInteractivity()
}
}
}
open func viewDidUpdatedInteractivity() {
}
open func viewDidUpdatedDynamicContent() {
self._updateAnimatableContent()
}
open private(set) var item:TableRowItem?
private let menuDisposable = MetaDisposable()
// var selected:Bool?
open var border:BorderType?
public var animates:Bool = true
public private(set) var contextMenu:AppMenu?
required public override init(frame frameRect: NSRect) {
super.init(frame: frameRect)
// self.layer = (self.layerClass() as! CALayer.Type).init()
self.wantsLayer = true
backgroundColor = .clear
self.layerContentsRedrawPolicy = .never
layer?.masksToBounds = true
autoresizingMask = []
// self.layer?.delegate = self
autoresizesSubviews = false
pressureConfiguration = NSPressureConfiguration(pressureBehavior: .primaryDeepClick)
appearance = presentation.appearance
}
// open override var translatesAutoresizingMaskIntoConstraints: Bool {
// get {
// return false
// }
// set {
//
// }
// }
open func updateColors() {
self.layer?.backgroundColor = backdorColor.cgColor
}
open override func smartMagnify(with event: NSEvent) {
super.smartMagnify(with: event)
}
open var backdorColor: NSColor {
if let item = self.item {
return item.backdorColor
}
return presentation.colors.background
}
open var borderColor: NSColor {
if let item = self.item {
return item.borderColor
}
return presentation.colors.border
}
open var isSelect: Bool {
return item?.isSelected ?? false
}
open var isHighlighted: Bool {
return item?.isHighlighted ?? false
}
open override func draw(_ dirtyRect: NSRect) {
var bp:Int = 0
bp += 1
}
open func updateIsResorting() {
}
open override func isAccessibilityElement() -> Bool {
return false
}
open func draw(_ layer: CALayer, in ctx: CGContext) {
// ctx.setFillColor(backdorColor.cgColor)
// ctx.fill(layer.bounds)
// layer.draw(in: ctx)
if let border = border {
ctx.setFillColor(borderColor.cgColor)
if border.contains(.Top) {
ctx.fill(NSMakeRect(0, frame.height - .borderSize, frame.width, .borderSize))
}
if border.contains(.Bottom) {
ctx.fill(NSMakeRect(0, 0, frame.width, .borderSize))
}
if border.contains(.Left) {
ctx.fill(NSMakeRect(0, 0, .borderSize, frame.height))
}
if border.contains(.Right) {
ctx.fill(NSMakeRect(frame.width - .borderSize, 0, .borderSize, frame.height))
}
}
}
open func interactionContentView(for innerId: AnyHashable, animateIn: Bool ) -> NSView {
return self
}
open func interactionControllerDidFinishAnimation(interactive: Bool, innerId: AnyHashable) {
}
open func addAccesoryOnCopiedView(innerId: AnyHashable, view: NSView) {
}
open func videoTimebase(for innerId: AnyHashable) -> CMTimebase? {
return nil
}
open func applyTimebase(for innerId: AnyHashable, timebase: CMTimebase?) {
}
open func hasFirstResponder() -> Bool {
return false
}
open func nextResponder() -> NSResponder? {
return nil
}
open var firstResponder:NSResponder? {
return self
}
open override func mouseMoved(with event: NSEvent) {
super.mouseMoved(with: event)
updateMouse(animated: true)
}
open override func mouseEntered(with event: NSEvent) {
super.mouseEntered(with: event)
updateMouse(animated: true)
}
open override func mouseExited(with event: NSEvent) {
super.mouseMoved(with: event)
updateMouse(animated: true)
}
open override func mouseDown(with event: NSEvent) {
if event.modifierFlags.contains(.control) && event.clickCount == 1 {
showContextMenu(event)
} else {
if event.clickCount == 2 {
doubleClick(in: convert(event.locationInWindow, from: nil))
return
}
super.mouseDown(with: event)
}
}
private var lastPressureEventStage = 0
open override func pressureChange(with event: NSEvent) {
super.pressureChange(with: event)
if event.stage >= 1 && event.stage != lastPressureEventStage {
longDisposable.set(delaySignal(0.5).start(completed: { [weak self] in
if let strongSelf = self {
if NSEvent.pressedMouseButtons & (1 << 0) != 0 {
if strongSelf.window?.mouseLocationOutsideOfEventStream == event.locationInWindow {
strongSelf.forceClick(in: strongSelf.convert(event.locationInWindow, from: nil))
}
}
}
}))
}
if event.stage < 1 {
longDisposable.set(nil)
}
if event.stage == 2 && lastPressureEventStage < 2 {
longDisposable.set(nil)
forceClick(in: convert(event.locationInWindow, from: nil))
}
lastPressureEventStage = event.stage
}
open override func rightMouseDown(with event: NSEvent) {
super.rightMouseDown(with: event)
if mouseInside() {
showContextMenu(event)
}
}
open func doubleClick(in location:NSPoint) -> Void {
}
open func forceClick(in location: NSPoint) {
}
open func canMultiselectTextIn(_ location: NSPoint) -> Bool {
return true
}
open func convertWindowPointToContent(_ point: NSPoint) -> NSPoint {
return convert(point, from: nil)
}
open func canAnimateUpdate(_ item: TableRowItem) -> Bool {
return true
}
open func showContextMenu(_ event:NSEvent) -> Void {
menuDisposable.set(nil)
contextMenu = nil
if let item = item {
let items = item.menuItems(in: convertWindowPointToContent(event.locationInWindow))
|> deliverOnMainQueue
|> take(1)
menuDisposable.set(combineLatest(queue: .mainQueue(), items, item.menuAdditionView).start(next: { [weak self, weak item] items, topWindow in
if let strongSelf = self, let item = item {
let menu = ContextMenu(presentation: item.menuPresentation, isLegacy: item.isLegacyMenu)
menu.topWindow = topWindow
menu.onShow = { [weak strongSelf] menu in
strongSelf?.onShowContextMenu()
}
menu.onClose = { [weak strongSelf] in
strongSelf?.contextMenu = nil
strongSelf?.onCloseContextMenu()
}
for item in items {
menu.addItem(item)
}
strongSelf.contextMenu = AppMenu(menu: menu)
strongSelf.contextMenu?.show(event: event, view: strongSelf)
}
}))
}
}
open override func menu(for event: NSEvent) -> NSMenu? {
return NSMenu()
}
open func onShowContextMenu() ->Void {
self.layer?.setNeedsDisplay()
updateColors()
}
open func onCloseContextMenu() ->Void {
self.layer?.setNeedsDisplay()
updateColors()
}
required public init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
open func updateMouse(animated: Bool) {
}
public var isInsertionAnimated:Bool {
if let layer = layer?.presentation(), layer.animation(forKey: "position") != nil {
return true
}
return false
}
public var rect:NSRect {
if let layer = layer?.presentation(), layer.animation(forKey: "position") != nil {
let rect = NSMakeRect(layer.position.x, layer.position.y, frame.width, frame.height)
return rect
}
return frame
}
open override func setFrameSize(_ newSize: NSSize) {
super.setFrameSize(newSize)
}
override public static var isCompatibleWithResponsiveScrolling: Bool {
return true
}
open override func viewDidMoveToSuperview() {
super.viewDidMoveToSuperview()
_updateListeners()
_updateAnimatableContent()
}
open override func layout() {
super.layout()
self.updateLayout(size: self.frame.size, transition: .immediate)
}
public func notifySubviewsToLayout(_ subview:NSView) -> Void {
for sub in subview.subviews {
sub.needsLayout = true
}
}
open override var needsLayout: Bool {
set {
super.needsLayout = newValue
if newValue {
guard #available(OSX 10.12, *) else {
layout()
notifySubviewsToLayout(self)
return
}
}
}
get {
return super.needsLayout
}
}
deinit {
longDisposable.dispose()
menuDisposable.dispose()
removeNotificationListeners()
}
open var mouseInsideField: Bool {
return false
}
open override func copy() -> Any {
let view:View = View(frame:bounds)
view.backgroundColor = self.backdorColor
return view
}
open func onRemove(_ animation: NSTableView.AnimationOptions) {
}
open func onInsert(_ animation: NSTableView.AnimationOptions, appearAnimated: Bool) {
}
open func set(item:TableRowItem, animated:Bool = false) -> Void {
self.item = item;
updateColors()
self._updateAnimatableContent()
}
open func focusAnimation(_ innerId: AnyHashable?, text: String?) {
if animatedView == nil {
self.animatedView = RowAnimateView(frame: bounds)
self.animatedView?.isEventLess = true
self.addSubview(animatedView!)
animatedView?.backgroundColor = presentation.colors.focusAnimationColor
animatedView?.layer?.opacity = 0
}
animatedView?.stableId = item?.stableId
let animation: CABasicAnimation = makeSpringAnimation("opacity")
animation.fromValue = animatedView?.layer?.presentation()?.opacity ?? 0
animation.toValue = 0.5
animation.autoreverses = true
animation.isRemovedOnCompletion = true
animation.fillMode = CAMediaTimingFillMode.forwards
animation.delegate = CALayerAnimationDelegate(completion: { [weak self] completed in
if completed {
self?.animatedView?.removeFromSuperview()
self?.animatedView = nil
}
})
animation.isAdditive = false
animatedView?.layer?.add(animation, forKey: "opacity")
}
open var interactableView: NSView {
return self
}
open func shakeView() {
}
open func shakeViewWithData(_ data: Any) {
}
open func mouseInside() -> Bool {
return super._mouseInside()
}
open var rowAppearance: NSAppearance? {
return self.appearance
}
open func updateLayout(size: NSSize, transition: ContainedViewLayoutTransition) {
}
@objc private func _updateAnimatableContent() {
DispatchQueue.main.async { [weak self] in
self?.updateAnimatableContent()
}
}
open func updateAnimatableContent() {
}
open var isEmojiLite: Bool {
return false
}
open override func viewDidMoveToWindow() {
super.viewDidMoveToWindow()
_updateListeners()
_updateAnimatableContent()
}
private func _updateListeners() {
let center = NotificationCenter.default
if let window = window {
center.removeObserver(self)
center.addObserver(self, selector: #selector(_updateAnimatableContent), name: NSWindow.didBecomeKeyNotification, object: window)
center.addObserver(self, selector: #selector(_updateAnimatableContent), name: NSWindow.didResignKeyNotification, object: window)
center.addObserver(self, selector: #selector(_updateAnimatableContent), name: NSView.boundsDidChangeNotification, object: self.enclosingScrollView?.contentView)
center.addObserver(self, selector: #selector(_updateAnimatableContent), name: NSView.frameDidChangeNotification, object: self.enclosingScrollView?.documentView)
center.addObserver(self, selector: #selector(_updateAnimatableContent), name: NSView.frameDidChangeNotification, object: self)
} else {
center.removeObserver(self)
}
}
open override func cursorUpdate(with event: NSEvent) {
super.cursorUpdate(with: event)
NSCursor.arrow.set()
}
private func removeNotificationListeners() {
NotificationCenter.default.removeObserver(self)
}
}