Ariver
2026-06-29 5816f189ed5fb0bb8bedef8200cee198164fcce2
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
import CoreGraphics
import Foundation
 
public enum AlignerActivationPolicy: Equatable, Sendable {
    case regular
    case accessory
    case prohibited
    case unknown
}
 
public enum AlignerWindowSubrole: Equatable, Sendable {
    case standard
    case dialog
    case sheet
    case unknown(String)
}
 
public struct WindowEnumerationRecord: Equatable, Sendable {
    public let id: UInt32
    public let identifierSource: AlignerWindow.IdentifierSource
    public let app: AlignerApp
    public let activationPolicy: AlignerActivationPolicy
    public let title: String
    public let size: CGSize
    public let frame: CGRect?
    public let subrole: AlignerWindowSubrole
    public let isMinimized: Bool
    public let isFullscreen: Bool
    public let isGhost: Bool
    public let isDesktopElement: Bool
    public let isSystemCritical: Bool
    public let isInteractable: Bool
    public let hasAXBacking: Bool
    public let isOnscreen: Bool
    public let spaceIDs: [UInt64]
 
    public init(
        id: UInt32,
        app: AlignerApp,
        activationPolicy: AlignerActivationPolicy = .regular,
        title: String,
        identifierSource: AlignerWindow.IdentifierSource = .cgWindow,
        size: CGSize,
        frame: CGRect? = nil,
        subrole: AlignerWindowSubrole,
        isMinimized: Bool = false,
        isFullscreen: Bool = false,
        isGhost: Bool = false,
        isDesktopElement: Bool = false,
        isSystemCritical: Bool = false,
        isInteractable: Bool = true,
        hasAXBacking: Bool = false,
        isOnscreen: Bool = false,
        spaceIDs: [UInt64] = []
    ) {
        self.id = id
        self.identifierSource = identifierSource
        self.app = app
        self.activationPolicy = activationPolicy
        self.title = title
        self.size = size
        self.frame = frame
        self.subrole = subrole
        self.isMinimized = isMinimized
        self.isFullscreen = isFullscreen
        self.isGhost = isGhost
        self.isDesktopElement = isDesktopElement
        self.isSystemCritical = isSystemCritical
        self.isInteractable = isInteractable
        self.hasAXBacking = hasAXBacking
        self.isOnscreen = isOnscreen
        self.spaceIDs = spaceIDs
    }
}
 
public enum WindowEnumerationPolicy {
    public static let minimumVisibleWidth: CGFloat = 100
    public static let minimumVisibleHeight: CGFloat = 50
 
    public static func shouldInclude(_ record: WindowEnumerationRecord) -> Bool {
        guard record.id != 0 else { return false }
        guard hasSupportedActivationPolicy(record) else { return false }
        guard isSupportedSubrole(record.subrole) else { return false }
        guard !record.app.isHidden || record.isFullscreen else { return false }
        guard !record.isGhost else { return false }
        guard !record.isDesktopElement else { return false }
        guard !record.isSystemCritical else { return false }
        guard record.isInteractable else { return false }
        guard !isKnownClosedShottrCGPlaceholder(record) else { return false }
        guard !isCGOnlyOffscreenGhost(record) else { return false }
        guard !isKnownClosedDocumentPlaceholder(record) else { return false }
 
        return record.isMinimized || record.isFullscreen || isLargeEnough(record.size)
    }
 
    public static func shouldIncludeAXOnlyRecord(
        identifierSource: AlignerWindow.IdentifierSource,
        isMinimized: Bool,
        isFullscreen: Bool,
        spaceIDs: [UInt64]
    ) -> Bool {
        if identifierSource == .syntheticAX {
            return isMinimized
        }
        if isMinimized {
            return true
        }
        if isFullscreen {
            return !spaceIDs.isEmpty
        }
        return false
    }
 
    private static func hasSupportedActivationPolicy(_ record: WindowEnumerationRecord) -> Bool {
        record.activationPolicy == .regular
            || (record.activationPolicy == .accessory && isKnownAccessoryMainWindow(record))
    }
 
    public static func window(from record: WindowEnumerationRecord) -> AlignerWindow? {
        guard shouldInclude(record) else { return nil }
 
        return AlignerWindow(
            id: record.id,
            app: record.app,
            title: displayTitle(for: record),
            identifierSource: record.identifierSource,
            isMinimized: record.isMinimized,
            isFullscreen: record.isFullscreen,
            isGhost: record.isGhost,
            frame: record.frame,
            spaceIDs: record.spaceIDs
        )
    }
 
    private static func isSupportedSubrole(_ subrole: AlignerWindowSubrole) -> Bool {
        switch subrole {
        case .standard, .dialog, .sheet:
            return true
        case .unknown:
            return false
        }
    }
 
    private static func isLargeEnough(_ size: CGSize) -> Bool {
        size.width > minimumVisibleWidth && size.height > minimumVisibleHeight
    }
 
    private static func isCGOnlyOffscreenGhost(_ record: WindowEnumerationRecord) -> Bool {
        guard record.identifierSource == .cgWindow,
              !record.hasAXBacking,
              !record.isOnscreen,
              !record.isMinimized,
              !record.isFullscreen
        else {
            return false
        }
 
        if !record.spaceIDs.isEmpty {
            return false
        }
 
        return !isKnownCGOnlyOffscreenMainWindow(record)
    }
 
    private static func isKnownCGOnlyOffscreenMainWindow(_ record: WindowEnumerationRecord) -> Bool {
        let trimmedTitle = record.title.trimmingCharacters(in: .whitespacesAndNewlines)
 
        switch record.app.bundleIdentifier {
        case "com.apple.finder":
            return !trimmedTitle.isEmpty && isLargeEnough(record.size)
        case "io.dcloud.HBuilderX":
            return !trimmedTitle.isEmpty && isLargeEnough(record.size)
        default:
            return false
        }
    }
 
    private static func isKnownAccessoryMainWindow(_ record: WindowEnumerationRecord) -> Bool {
        let trimmedTitle = record.title.trimmingCharacters(in: .whitespacesAndNewlines)
 
        return record.app.bundleIdentifier == "cc.ffitch.shottr"
            && trimmedTitle == "Shottr"
            && record.isOnscreen
            && isLargeEnough(record.size)
    }
 
    private static func isKnownClosedShottrCGPlaceholder(_ record: WindowEnumerationRecord) -> Bool {
        let trimmedTitle = record.title.trimmingCharacters(in: .whitespacesAndNewlines)
 
        return record.app.bundleIdentifier == "cc.ffitch.shottr"
            && trimmedTitle == "Shottr"
            && record.identifierSource == .cgWindow
            && !record.hasAXBacking
            && !record.isOnscreen
            && !record.isMinimized
            && !record.isFullscreen
    }
 
    private static func isKnownClosedDocumentPlaceholder(_ record: WindowEnumerationRecord) -> Bool {
        let trimmedTitle = record.title.trimmingCharacters(in: .whitespacesAndNewlines)
        return record.identifierSource == .cgWindow
            && record.app.bundleIdentifier == "app.vmark"
            && trimmedTitle.isEmpty
            && !record.hasAXBacking
            && !record.isOnscreen
            && !record.isMinimized
            && !record.isFullscreen
    }
 
    private static func displayTitle(for record: WindowEnumerationRecord) -> String {
        let trimmedTitle = record.title.trimmingCharacters(in: .whitespacesAndNewlines)
        guard trimmedTitle.isEmpty else { return record.title }
 
        return "\(record.app.name) Untitled"
    }
}