extension ManagedWindow { enum CodingKeys: String, CodingKey { case id, app, title, role, subrole, minimum, isFloating, managementReason, facts, initialPosition } public init(from decoder: any Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) self.init( id: try container.decode(WindowID.self, forKey: .id), app: try container.decode(AppID.self, forKey: .app), title: try container.decode(String.self, forKey: .title), role: try container.decode(String.self, forKey: .role), subrole: try container.decodeIfPresent(String.self, forKey: .subrole), isFloating: try container.decode(Bool.self, forKey: .isFloating), facts: try container.decodeIfPresent(WindowFacts.self, forKey: .facts), initialPosition: try container.decodeIfPresent(Int.self, forKey: .initialPosition), managementReason: try container.decodeIfPresent(WindowManagementReason.self, forKey: .managementReason) ?? .automatic) minimum = try container.decodeIfPresent(LearnedMinimumSize.self, forKey: .minimum) } } extension TileTree { var isValid: Bool { let validFractions = fold( { _ in true }, { node, first, second in switch node { case .tile: preconditionFailure("A folded branch must be a split or partition") case .split(_, let fraction, _, _), .partition(_, let fraction, _, _): return fraction.isFinite && (0...1).contains(fraction) && first && second } }) guard validFractions else { return false } let members = windows return Set(members).count == members.count } } extension Workspace { enum CodingKeys: String, CodingKey { case key, layout, ratio, configuredRatio, gaps, reservedEdges, whenFull, windowCap, order, openingOrder, focus, tree, pendingSplitAxis case layoutVariant, displaySize, capabilitySize, layoutDefaults, alternateSideTree, adaptiveTileMemory } public init(from decoder: any Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) let key = try container.decode(WorkspaceKey.self, forKey: .key) let layout = try container.decode(Layout.self, forKey: .layout) let ratio = try container.decode(MainRatio.self, forKey: .ratio) let gaps = try container.decode(Gaps.self, forKey: .gaps) let reserved = try container.decode(Insets.self, forKey: .reservedEdges) let cap = try container.decode(Int.self, forKey: .windowCap) let order = try container.decode([WindowID].self, forKey: .order) let whenFull = try container.decodeIfPresent(WhenFull.self, forKey: .whenFull) ?? .stack let openingOrder = try container.decodeIfPresent([WindowID].self, forKey: .openingOrder) ?? order let focus = try container.decodeIfPresent(WindowID.self, forKey: .focus) let tree = try container.decodeIfPresent(TileTree.self, forKey: .tree) guard cap > 0, Set(order).count == order.count, focus.map(order.contains) != false, Set(openingOrder) == Set(order), Set(openingOrder).count == openingOrder.count, tree?.isValid != false, (tree?.tileCount ?? 0) <= cap, Set(tree?.windows ?? []).isSubset(of: Set(order)) else { throw DecodingError.dataCorrupted( .init( codingPath: decoder.codingPath, debugDescription: "A workspace has inconsistent tiles or membership" )) } self.init( key: key, layout: layout, ratio: ratio, gaps: gaps, reservedEdges: reserved, windowCap: cap, whenFull: whenFull) configuredRatio = try container.decodeIfPresent(MainRatio.self, forKey: .configuredRatio) ?? ratio self.order = order self.openingOrder = openingOrder self.focus = focus self.tree = tree pendingSplitAxis = try container.decodeIfPresent(Axis.self, forKey: .pendingSplitAxis) layoutVariant = try container.decodeIfPresent(LayoutVariant.self, forKey: .layoutVariant) ?? .standard displaySize = try container.decodeIfPresent(Size.self, forKey: .displaySize) capabilitySize = try container.decodeIfPresent(Size.self, forKey: .capabilitySize) ?? displaySize layoutDefaults = try container.decodeIfPresent(LayoutDefaults.self, forKey: .layoutDefaults) alternateSideTree = try container.decodeIfPresent(TileTree.self, forKey: .alternateSideTree) adaptiveTileMemory = try container.decodeIfPresent(AdaptiveTileMemory.self, forKey: .adaptiveTileMemory) guard displaySize?.isValid != false, capabilitySize?.isValid != false, alternateSideTree?.isValid != false, adaptiveTileMemory.map({ memory in memory.original.isValid && memory.projection.isValid && Set(memory.original.windows) == Set(tree?.windows ?? []) && Set(memory.projection.windows) == Set(tree?.windows ?? []) }) != false, capabilitySize.map({ layout.capabilities(in: Rect(origin: Point(), size: $0)).variant == layoutVariant }) != false, (tree?.tileCount ?? 0) <= effectiveWindowCap else { throw DecodingError.dataCorrupted( .init(codingPath: decoder.codingPath, debugDescription: "A workspace has invalid display adaptation")) } } }