Ariver
2026-06-19 cb67d8b2fa9b9c9d30cd81d72b8737ee2baa44b6
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
import CoreGraphics
import Foundation
 
public enum FinderTabSpaceAttributionPolicy {
    private static let finderBundleIdentifier = "com.apple.finder"
    private static let minimumOverlapRatio: CGFloat = 0.85
    private static let minimumSizeSimilarityRatio: CGFloat = 0.80
    private static let minimumBestScoreGap: CGFloat = 0.05
 
    public static func attributedRecords(_ records: [WindowEnumerationRecord]) -> [WindowEnumerationRecord] {
        let hosts = records.filter(isFinderFullscreenHost)
        guard !hosts.isEmpty else { return records }
 
        return records.map { record in
            guard isFinderTabCandidate(record),
                  let host = bestHost(for: record, in: hosts)
            else {
                return record
            }
 
            return copy(
                record,
                isFullscreen: true,
                spaceIDs: host.spaceIDs
            )
        }
    }
 
    private static func isFinderTabCandidate(_ record: WindowEnumerationRecord) -> Bool {
        WindowEnumerationPolicy.shouldInclude(record)
            && record.identifierSource == .cgWindow
            && record.app.bundleIdentifier == finderBundleIdentifier
            && record.spaceIDs.isEmpty
            && !record.isMinimized
            && !record.title.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
            && record.frame?.isEmpty == false
    }
 
    private static func isFinderFullscreenHost(_ record: WindowEnumerationRecord) -> Bool {
        WindowEnumerationPolicy.shouldInclude(record)
            && record.app.bundleIdentifier == finderBundleIdentifier
            && record.isFullscreen
            && !record.spaceIDs.isEmpty
            && record.subrole == .standard
            && record.frame?.isEmpty == false
    }
 
    private static func bestHost(
        for candidate: WindowEnumerationRecord,
        in hosts: [WindowEnumerationRecord]
    ) -> WindowEnumerationRecord? {
        let compatibleHosts = hosts.filter { host in
            sameProcess(candidate, host)
        }
        let scoredHosts = compatibleHosts.compactMap { host -> (record: WindowEnumerationRecord, score: CGFloat)? in
            guard let score = frameCompatibilityScore(candidate.frame, host.frame)
            else {
                return nil
            }
 
            return (host, score)
        }
        .sorted { lhs, rhs in
            if lhs.score != rhs.score {
                return lhs.score > rhs.score
            }
            return lhs.record.id < rhs.record.id
        }
 
        guard let best = scoredHosts.first else { return nil }
        if scoredHosts.count == 1 {
            return best.record
        }
 
        let sameSpaceCompetitors = scoredHosts.filter { $0.record.spaceIDs == best.record.spaceIDs }
        if sameSpaceCompetitors.count == scoredHosts.count {
            return best.record
        }
 
        let secondBest = scoredHosts[1]
        return best.score - secondBest.score >= minimumBestScoreGap ? best.record : nil
    }
 
    private static func frameCompatibilityScore(_ lhs: CGRect?, _ rhs: CGRect?) -> CGFloat? {
        guard let overlap = overlapScore(lhs, rhs),
              overlap >= minimumOverlapRatio,
              let sizeSimilarity = sizeSimilarityScore(lhs, rhs),
              sizeSimilarity >= minimumSizeSimilarityRatio
        else {
            return nil
        }
 
        return min(overlap, sizeSimilarity)
    }
 
    private static func sameProcess(
        _ lhs: WindowEnumerationRecord,
        _ rhs: WindowEnumerationRecord
    ) -> Bool {
        guard let lhsPID = lhs.app.processIdentifier,
              let rhsPID = rhs.app.processIdentifier
        else {
            return false
        }
 
        return lhsPID == rhsPID
    }
 
    private static func overlapScore(_ lhs: CGRect?, _ rhs: CGRect?) -> CGFloat? {
        guard let lhs,
              let rhs,
              !lhs.isEmpty,
              !rhs.isEmpty
        else {
            return nil
        }
 
        let intersection = lhs.intersection(rhs)
        guard !intersection.isNull,
              !intersection.isEmpty
        else {
            return nil
        }
 
        let denominator = min(lhs.width * lhs.height, rhs.width * rhs.height)
        guard denominator > 0 else { return nil }
        return (intersection.width * intersection.height) / denominator
    }
 
    private static func sizeSimilarityScore(_ lhs: CGRect?, _ rhs: CGRect?) -> CGFloat? {
        guard let lhs,
              let rhs,
              lhs.width > 0,
              lhs.height > 0,
              rhs.width > 0,
              rhs.height > 0
        else {
            return nil
        }
 
        let widthSimilarity = min(lhs.width, rhs.width) / max(lhs.width, rhs.width)
        let heightSimilarity = min(lhs.height, rhs.height) / max(lhs.height, rhs.height)
        return min(widthSimilarity, heightSimilarity)
    }
 
    private static func copy(
        _ record: WindowEnumerationRecord,
        isFullscreen: Bool,
        spaceIDs: [UInt64]
    ) -> WindowEnumerationRecord {
        WindowEnumerationRecord(
            id: record.id,
            app: record.app,
            activationPolicy: record.activationPolicy,
            title: record.title,
            identifierSource: record.identifierSource,
            size: record.size,
            frame: record.frame,
            subrole: record.subrole,
            isMinimized: record.isMinimized,
            isFullscreen: isFullscreen,
            isGhost: record.isGhost,
            isDesktopElement: record.isDesktopElement,
            isSystemCritical: record.isSystemCritical,
            isInteractable: record.isInteractable,
            hasAXBacking: record.hasAXBacking,
            isOnscreen: record.isOnscreen,
            spaceIDs: spaceIDs
        )
    }
}