import ParcelCore struct AutomaticRoomReminders { private struct Key: Hashable { let application: RoomReminderApplication let workspace: WorkspaceKey } private struct Context: Equatable { let frames: [WindowID: Rect] let minimums: [WindowID: Size] let failing: Set } private var contexts: [Key: Context] = [:] private var notices: [Key: FeedbackNotice] = [:] var unresolved: [FeedbackNotice] { Array(notices.values) } func contains(_ notice: FeedbackNotice, state: State) -> Bool { guard let key = key(for: notice, state: state), let context = context(for: key, state: state) else { return false } return contexts[key] == context } mutating func record(_ notice: FeedbackNotice, state: State) { guard let key = key(for: notice, state: state), let context = context(for: key, state: state) else { return } contexts[key] = context notices[key] = notice } mutating func reconcile(_ state: State) -> Bool { let before = notices.count contexts = contexts.filter { key, value in context(for: key, state: state) == value } notices = notices.filter { key, notice in guard contexts[key] != nil, let window = notice.window else { return false } return state.windows[window]?.isFloating == false && state.workspace(containing: window) == key.workspace && state.windows[window].map { RoomReminderApplication($0.app) == key.application } == true } return notices.count != before } mutating func forget(_ bundleID: String) { contexts = contexts.filter { $0.key.application != .bundle(bundleID) } notices = notices.filter { $0.key.application != .bundle(bundleID) } } private func key(for notice: FeedbackNotice, state: State) -> Key? { guard let application = RoomReminderApplication(notice, state: state), let workspace = notice.workspace else { return nil } return Key(application: application, workspace: workspace) } private func context(for key: Key, state: State) -> Context? { guard let workspace = state.workspaces[key.workspace], let tree = workspace.tree else { return nil } let windows = Set(tree.windows.filter { state.windows[$0]?.isFloating == false }) guard windows.contains(where: { window in state.windows[window].map { RoomReminderApplication($0.app) == key.application } == true }) else { return nil } return Context( frames: state.frames.filter { windows.contains($0.key) }, minimums: state.windows.compactMapValues(\.minimumSize).filter { windows.contains($0.key) }, failing: Set(state.cannotFit).intersection(windows)) } }