Files
Pulse/Sources/PulseUI/Extensions/NSTableView+Extensions.swift
2022-08-13 18:30:36 -04:00

58 lines
1.6 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// The MIT License (MIT)
//
// Copyright (c) 20202022 Alexander Grebenyuk (github.com/kean).
#if os(macOS)
import AppKit
extension NSTableView {
func apply<T: Hashable>(diff: CollectionDifference<T>) {
var deletes = IndexSet()
var inserts = IndexSet()
var moves: [(from: Int, to: Int)] = []
for update in diff.inferringMoves() {
switch update {
case .remove(let offset, _, let move):
if let move = move {
moves.append((offset, move))
} else {
deletes.insert(offset)
}
case .insert(let offset, _, let move):
if move == nil {
inserts.insert(offset)
}
}
}
NSAnimationContext.runAnimationGroup { context in
if visibleRect.origin.y > 0 {
context.duration = 0
}
let oldScrollOffset = visibleRect.origin
let previousHeight = bounds.size.height
beginUpdates()
if !deletes.isEmpty {
removeRows(at: deletes, withAnimation: .slideLeft)
}
if !inserts.isEmpty {
insertRows(at: inserts, withAnimation: .effectGap)
}
endUpdates()
var newScrollOffset = oldScrollOffset
newScrollOffset.y += (bounds.size.height - previousHeight)
if oldScrollOffset.y == 0 {
scroll(oldScrollOffset)
} else if newScrollOffset.y > oldScrollOffset.y {
scroll(newScrollOffset)
}
}
}
}
#endif