public import Foundation public import ParcelCore public struct FeedbackNotice: Identifiable, Equatable, Codable, Sendable { public enum Kind: String, Codable, Sendable { case retiled, needsRoom, spaceFull, maximumSizeReached, minimumSizeReached, windowCap case layoutCapabilityLimit, tilesMerged case noUndoHistory, undoConflict, minimizedPlacementChanged, message } public let id: UUID public let kind: Kind public let count: Int public let window: WindowID? public let workspace: WorkspaceKey? public let bundleID: String? public let applicationName: String? public let processIdentifier: Int32? public let layout: Layout? public let limit: Int? public let message: String? public let messageKind: String? public let accessibilityMessage: String? public init( id: UUID, kind: Kind, count: Int, window: WindowID?, workspace: WorkspaceKey?, bundleID: String?, applicationName: String?, layout: Layout?, limit: Int?, message: String? = nil, messageKind: String? = nil, accessibilityMessage: String? = nil, processIdentifier: Int32? = nil ) { precondition(kind != .message || message?.isEmpty == false) precondition(accessibilityMessage?.isEmpty != true) self.id = id self.kind = kind self.count = count self.window = window self.workspace = workspace self.bundleID = bundleID self.applicationName = applicationName self.processIdentifier = processIdentifier self.layout = layout self.limit = limit self.message = message self.messageKind = messageKind self.accessibilityMessage = accessibilityMessage } public var accessibilityTitle: String { accessibilityMessage ?? title } public var title: String { switch kind { case .message: guard let message, !message.isEmpty else { preconditionFailure("Transient feedback needs a message") } return message case .retiled: return count == 0 ? FeedbackText.string("Everything is already in place.") : FeedbackText.format("Retiled %lld windows.", Int64(count)) case .needsRoom: return FeedbackText.string("Needs more room") case .spaceFull: return FeedbackText.string("This Space is full") case .maximumSizeReached: return FeedbackText.string("Maximum size reached") case .minimumSizeReached: return FeedbackText.string("Minimum size reached") case .windowCap: return FeedbackText.format("This Space is using its limit of %lld tiles.", Int64(count)) case .layoutCapabilityLimit: guard let layout, let limit else { preconditionFailure("Layout capability feedback needs a layout and limit") } return FeedbackText.format( "%@ Maximum %lld tiled windows on this display. Your requested limit is kept.", FeedbackText.layoutTitle(layout), Int64(limit)) case .tilesMerged: return StackText.stackedWindows(count) case .noUndoHistory: return FeedbackText.string("No window changes to undo.") case .undoConflict: return FeedbackText.string("This layout changed since that action.") case .minimizedPlacementChanged: return FeedbackText.string("Window placed in the current layout") } } var isRoomSummary: Bool { kind == .needsRoom && bundleID == nil && window == nil && count > 0 } public var detail: String? { switch kind { case .needsRoom where isRoomSummary: applicationName case .needsRoom: FeedbackText.format( "%@ does not fit in its tile.", applicationName ?? "") case .spaceFull: FeedbackText.string("The new window is floating in the middle.") case .minimizedPlacementChanged: FeedbackText.string("The layout changed while this window was minimized.") default: nil } } func coalesces(with other: FeedbackNotice) -> Bool { kind != .needsRoom && kind == other.kind && count == other.count && window == other.window && workspace == other.workspace && bundleID == other.bundleID && layout == other.layout && limit == other.limit && message == other.message && messageKind == other.messageKind && accessibilityMessage == other.accessibilityMessage && processIdentifier == other.processIdentifier } public static func map( _ report: Report, state: State, applicationName: (ManagedWindow) -> String = { $0.app.bundleID } ) -> [FeedbackNotice] { func notice(_ kind: Kind, count: Int = 0, window: WindowID? = nil) -> FeedbackNotice { let managed = window.flatMap { state.windows[$0] } return FeedbackNotice( id: UUID(), kind: kind, count: count, window: window, workspace: window.flatMap { state.workspace(containing: $0) }, bundleID: managed.flatMap { $0.app.bundleID.isEmpty ? nil : $0.app.bundleID }, applicationName: managed.map { window in let name = applicationName(window) return name.isEmpty ? SettingsText.string("Application") : name }, layout: nil, limit: nil, processIdentifier: managed.flatMap { $0.app.bundleID.isEmpty ? $0.app.pid : nil }) } switch report { case .retiled(let moved): return [notice(.retiled, count: moved)] case .cannotFit(let windows): var applications: Set = [] return windows.sorted().compactMap { window in guard let managed = state.windows[window], let key = state.workspace(containing: window), state.isActive(key), !managed.isFloating, applications.insert(RoomReminderApplication(managed.app)).inserted else { return nil } return notice(.needsRoom, window: window) } case .spaceFull(let window): return [notice(.spaceFull, window: window)] case .maximumSizeReached: return [notice(.maximumSizeReached)] case .minimumSizeReached: return [notice(.minimumSizeReached)] case .atWindowCap(let count): return [notice(.windowCap, count: count)] case .layoutCapabilityLimit(let layout, let maximum): return [ FeedbackNotice( id: UUID(), kind: .layoutCapabilityLimit, count: 0, window: nil, workspace: state.activeWorkspace, bundleID: nil, applicationName: nil, layout: layout, limit: maximum) ] case .noUndoHistory: return [notice(.noUndoHistory)] case .undoConflict(let window): return [notice(.undoConflict, window: window)] case .minimizedPlacementChanged(let window): return [notice(.minimizedPlacementChanged, window: window)] default: return [] } } public static func map(_ announcement: Announcement, state: State) -> [FeedbackNotice] { guard case .windowsStacked(let window, let count) = announcement, let key = state.workspace(containing: window), state.isActive(key), let stack = state.workspaces[key]?.tree?.stacks.first(where: { $0.windows.contains(window) }), stack.windows.count == count else { return [] } return [ FeedbackNotice( id: UUID(), kind: .tilesMerged, count: count, window: stack.windows.min(), workspace: key, bundleID: nil, applicationName: nil, layout: nil, limit: nil) ] } } enum FeedbackText { static func string(_ value: String.LocalizationValue) -> String { String(localized: value, table: "Feedback", bundle: .module) } static func format(_ key: String, _ arguments: any CVarArg...) -> String { String( format: Bundle.module.localizedString(forKey: key, value: nil, table: "Feedback"), locale: Locale.current, arguments: arguments) } static func layoutTitle(_ layout: Layout) -> String { switch layout { case .grid: FeedbackText.string("Grid layout.") case .columns: FeedbackText.string("Columns layout.") case .rows: FeedbackText.string("Rows layout.") case .side: FeedbackText.string("Side layout.") case .tree: FeedbackText.string("Tree layout.") case .bsp: FeedbackText.string("BSP layout.") case .accordion: FeedbackText.string("Accordion layout.") } } }