public import AppKit import Observation import ParcelConfig public import ParcelCore import SwiftUI public struct FloatingWindowInspection: Equatable, Sendable { public let id: UInt32 public let bundleID: String public let title: String? public let reason: String public let role: String? public let subrole: String? public let minimumSize: Size? public let canTile: Bool public init( id: UInt32, bundleID: String, title: String? = nil, reason: String, role: String? = nil, subrole: String? = nil, minimumSize: Size? = nil, canTile: Bool ) { self.id = id self.bundleID = bundleID self.title = title self.reason = reason self.role = role self.subrole = subrole self.minimumSize = minimumSize self.canTile = canTile } var suggestedRule: FloatRule { FloatRule( bundleID: bundleID, titleRegex: title.flatMap { $0.isEmpty ? nil : "^" + NSRegularExpression.escapedPattern(for: $0) + "$" }, role: role) } } @MainActor @Observable public final class FloatingWindowInspectorModel { public private(set) var inspection: FloatingWindowInspection? public private(set) var failure: String? public private(set) var isBusy = false private var selectedID: UInt32? private let inspect: @MainActor (UInt32?) async throws -> FloatingWindowInspection private let tile: @MainActor (UInt32) async throws -> Void public init( inspect: @escaping @MainActor (UInt32?) async throws -> FloatingWindowInspection, tile: @escaping @MainActor (UInt32) async throws -> Void ) { self.inspect = inspect self.tile = tile } public func refresh() async { guard !isBusy else { return } isBusy = true defer { isBusy = false } do { inspection = try await inspect(selectedID) selectedID = inspection?.id failure = nil } catch { inspection = nil failure = error.localizedDescription } } public func tileWindow() async { guard !isBusy, let inspection, inspection.canTile else { return } isBusy = true defer { isBusy = false } do { try await tile(inspection.id) self.inspection = try await inspect(inspection.id) failure = nil } catch { failure = error.localizedDescription } } } @MainActor public final class FloatingWindowInspectorController { private let model: FloatingWindowInspectorModel private let settings: SettingsModel private var window: NSWindow? public init(model: FloatingWindowInspectorModel, settings: SettingsModel) { self.model = model self.settings = settings } public func show() { if window == nil { let controller = NSHostingController( rootView: FloatingWindowInspectorView(model: model, settings: settings)) let window = NSWindow(contentViewController: controller) window.title = SettingsText.string("Why is this window floating?") window.styleMask = [.titled, .closable, .resizable] window.isReleasedWhenClosed = false AppWindowPresentation.configure(window) window.center() self.window = window } window?.makeKeyAndOrderFront(nil) NSApplication.shared.activate(ignoringOtherApps: true) } public func close() { window?.close() window = nil } } struct FloatingWindowInspectorView: View { let model: FloatingWindowInspectorModel let settings: SettingsModel @SwiftUI.State private var ruleDraft: RuleDraft? var body: some View { Form { if let inspection = model.inspection { LabeledContent(SettingsText.string("Application"), value: inspection.bundleID) if let title = inspection.title { LabeledContent(SettingsText.string("Window title"), value: title) } Text(inspection.reason).textSelection(.enabled) .accessibilityLabel(SettingsText.string("Classification reason")) .accessibilityValue(inspection.reason) if let role = inspection.role { LabeledContent(SettingsText.string("Accessibility role"), value: role) } if let subrole = inspection.subrole { LabeledContent(SettingsText.string("Accessibility subrole"), value: subrole) } if let size = inspection.minimumSize { LabeledContent( SettingsText.string("Minimum size"), value: "\(size.width.formatted()) × \(size.height.formatted())") } Button(SettingsText.string("Tile this window")) { Task { await model.tileWindow() } } .disabled(!inspection.canTile || model.isBusy) .accessibilityIdentifier("settings.inspector.tile") Button(SettingsText.string("Add floating rule…")) { ruleDraft = RuleDraft(index: nil, original: inspection.suggestedRule) } .disabled(model.isBusy) .accessibilityIdentifier("settings.inspector.rule") } if let failure = model.failure { Text(failure).textSelection(.enabled) } Button(SettingsText.string("Refresh inspection")) { Task { await model.refresh() } } .disabled(model.isBusy) if model.isBusy { ProgressView().accessibilityLabel(SettingsText.string("Inspecting window")) } } .formStyle(.grouped) .frame(minWidth: 460, minHeight: 320) .task { await model.refresh() await settings.start() } .sheet(item: $ruleDraft) { draft in RuleEditor(model: settings, draft: draft) } } } @MainActor public final class FloatingInspectorMenuItem: NSMenuItem { private let invoke: @MainActor () -> Void public init(invoke: @escaping @MainActor () -> Void) { self.invoke = invoke let title = SettingsText.string("Why is this window floating?") super.init(title: title, action: #selector(openInspector), keyEquivalent: "") target = self setAccessibilityLabel(title) } required init(coder: NSCoder) { fatalError("init(coder:) is unavailable") } @objc private func openInspector() { invoke() } }