Skip to content

armgit / peel

Public Imported from GitHub
0

Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.

2.9 KB
Raw Blame
import AppKit
import SwiftUI

extension View {
    /// Fades the column in again when `value` changes while the column is on screen. The first value never
    /// fades, and a change while a fade is running does not start another.
    func fadesInColumn(on value: some Hashable) -> some View {
        background(ColumnFade(value: AnyHashable(value)))
    }

    /// Fades the column in again when its rows change, such as after a Move to Trash or a rescan that finds
    /// something else. On macOS 26.0 a `List` cannot animate rows as they are removed, so the whole list fades in
    /// instead. Pass the rows as found, not as filtered, so typing in a search field fades nothing. A detail page
    /// is rebuilt for each selection, so choosing another row does not count as a change.
    func fadesInColumn<ID: Hashable>(whenRowsChange rows: [ID]?) -> some View {
        fadesInColumn(on: rows)
    }
}

private struct ColumnFade: NSViewRepresentable {
    let value: AnyHashable

    func makeNSView(context: Context) -> Anchor {
        Anchor()
    }

    func updateNSView(_ anchor: Anchor, context: Context) {
        guard anchor.value != value else { return }
        if anchor.value != nil, anchor.window != nil, !anchor.isFading {
            anchor.fadeIn()
        }
        anchor.value = value
    }

    final class Anchor: NSView {
        var value: AnyHashable?

        var isFading: Bool {
            column?.layer?.animation(forKey: "fade") != nil
        }

        func fadeIn() {
            guard let layer = column?.layer else { return }
            let fade = CABasicAnimation(keyPath: "opacity")
            fade.fromValue = 0
            fade.duration = Motion.step.duration
            fade.timingFunction = CAMediaTimingFunction(name: .easeOut)
            layer.add(fade, forKey: "fade")
        }

        /// The ancestor two levels below the `NSSplitView`, found by walking up the view tree, because a split
        /// view that was just built does not report `enclosingSplitViewItem` yet. This structure is not API (see
        /// `PrivateStructure`). Outside a split view, such as in a sheet, it is nil and nothing fades.
        private var column: NSView? {
            var view = superview
            while let current = view, !(current.superview?.superview is NSSplitView) {
                view = current.superview
            }
            return view
        }

        /// A column in a split view should find its view; in a sheet there is no split view, and nothing to find.
        override func layout() {
            super.layout()
            if column != nil {
                PrivateStructure.found(.columnFade)
            } else if sequence(first: superview, next: { $0?.superview }).contains(where: { $0 is NSSplitView }) {
                PrivateStructure.check(.columnFade)
            }
        }

        override func hitTest(_ point: NSPoint) -> NSView? {
            nil
        }
    }
}