public import Foundation public enum SavedSetupError: Error, Sendable, Equatable { case invalidName case invalidEmoji case invalidShortcut case duplicateID(UUID) case duplicateName(String) } public struct SavedSetup: Sendable, Equatable, Codable, Identifiable { public var id: UUID public var name: String public var emoji: String? public var shortcut: String? public var hideOtherApps: Bool public var memory: WorkspaceMemory public init( id: UUID = UUID(), name: String, emoji: String? = nil, shortcut: String? = nil, hideOtherApps: Bool = true, memory: WorkspaceMemory ) { self.id = id self.name = name self.emoji = emoji self.shortcut = shortcut self.hideOtherApps = hideOtherApps self.memory = memory } public func validate() throws { guard Self.safeMetadata(name, maximumBytes: 512), name.count <= 128, normalizedName.unicodeScalars.contains(where: { !$0.properties.isWhitespace && $0.properties.generalCategory != .format }) else { throw SavedSetupError.invalidName } if let emoji { guard Self.safeMetadata(emoji, maximumBytes: 128, allowingEmojiTags: true), emoji.count == 1, emoji.unicodeScalars.contains(where: { $0.properties.isEmoji }), emoji.unicodeScalars.contains(where: { $0.properties.isEmojiPresentation || $0.value == 0xFE0F || $0.value == 0x20E3 }) else { throw SavedSetupError.invalidEmoji } } if let shortcut { guard Self.safeMetadata(shortcut, maximumBytes: 256), shortcut.count <= 128, !shortcut.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else { throw SavedSetupError.invalidShortcut } } try memory.validate() } /// The caller supplies fresh entropy; saving never reads a clock or persists raw titles. public static func capture( id: UUID = UUID(), name: String, emoji: String? = nil, shortcut: String? = nil, hideOtherApps: Bool = true, state: State, titleSalt: Data ) throws -> SavedSetup { guard titleSalt.count == 32 else { throw WorkspaceMemoryError.invalidTitleSalt } let memory = try WorkspaceMemory.capture(state, titleSalt: titleSalt, maximumCount: Int.max) let setup = SavedSetup( id: id, name: name, emoji: emoji, shortcut: shortcut, hideOtherApps: hideOtherApps, memory: memory) try setup.validate() return setup } public func matches(_ state: State) -> Bool { guard !memory.records.isEmpty else { return state.windows.isEmpty } let mapping = memory.windowMapping(in: state) guard mapping.count == memory.records.reduce(0, { $0 + $1.windows.count }) else { return false } let expected = memory.restoring(state, mapping: mapping, restoringTiling: true) let expectedFrames = expected.layoutFrames().frames for record in memory.records { let key = record.workspace.key guard state.isActive(key), state.displays[key.display]?.hasUsableGeometry == true, state.workspaces[key]?.order.count == record.windows.count, state.workspaces[key]?.undoPlacement == expected.workspaces[key]?.undoPlacement else { return false } for saved in record.windows { guard let id = mapping[saved.token], state.windows[id]?.isFloating == saved.isFloating else { return false } let expectedFrame = saved.isFloating ? expected.frames[id] : expectedFrames[id] if !saved.isFloating && expectedFrame == nil { return false } if let frame = expectedFrame { guard let actual = state.frames[id] ?? state.observedFrames[id], abs(actual.minX - frame.minX) < 0.5, abs(actual.minY - frame.minY) < 0.5, abs(actual.size.width - frame.size.width) < 0.5, abs(actual.size.height - frame.size.height) < 0.5 else { return false } } } } return true } public var normalizedName: String { Self.normalizedName(name) } public static func normalizedName(_ name: String) -> String { name.trimmingCharacters(in: .whitespacesAndNewlines).precomposedStringWithCanonicalMapping .folding(options: .caseInsensitive, locale: Locale(identifier: "en_US_POSIX")) } private static func safeMetadata(_ value: String, maximumBytes: Int, allowingEmojiTags: Bool = false) -> Bool { value.utf8.count <= maximumBytes && !value.unicodeScalars.contains { $0.properties.generalCategory == .control || $0.properties.generalCategory == .lineSeparator || $0.properties.generalCategory == .paragraphSeparator || ($0.properties.generalCategory == .format && $0.value != 0x200C && $0.value != 0x200D && !(allowingEmojiTags && (0xE0020...0xE007F).contains($0.value))) } } private enum CodingKeys: String, CodingKey { case id, name, emoji, shortcut, hideOtherApps, memory } public init(from decoder: any Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) self.init( id: try values.decode(UUID.self, forKey: .id), name: try values.decode(String.self, forKey: .name), emoji: try values.decodeIfPresent(String.self, forKey: .emoji), shortcut: try values.decodeIfPresent(String.self, forKey: .shortcut), hideOtherApps: try values.decode(Bool.self, forKey: .hideOtherApps), memory: try values.decode(WorkspaceMemory.self, forKey: .memory)) try validate() } }