From df17d29e4e53607193192b3f9fb0bd2d37f479c6 Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Wed, 11 Nov 2026 21:10:13 +0700 Subject: [PATCH 1/3] feat(macos): color grid values by type from the theme's Values colors --- .../Import/VSCodeThemeMapping.swift | 9 +- .../TableProTheme/ThemeDefinition.swift | 18 + .../TableProTheme/ThemeFileFormat.swift | 31 +- .../TableProTheme/ThemeLoadError.swift | 8 +- .../Sources/TableProTheme/ThemeSlot.swift | 53 ++- .../TableProTheme/ThemeSlotFallback.swift | 5 +- .../TableProTheme/ThemeSlotSection.swift | 4 +- .../Import/VSCodeThemeImportTests.swift | 27 +- .../Import/VSCodeThemeMappingTests.swift | 6 + .../ThemeFileFormatTests.swift | 133 +++++- .../ThemeFileImporterTests.swift | 11 +- .../ThemeSlotFallbackTests.swift | 18 +- .../TableProThemeTests/ThemeSlotTests.swift | 48 ++- .../MariaDBFieldMetadata.swift | 6 +- .../MariaDBTypeNameTests.swift | 9 + .../Core/DataGrid/RowDisplayBox.swift | 51 ++- apple/TablePro/Core/Events/AppEvents.swift | 69 +--- .../RoutineDebugController.swift | 1 + apple/TablePro/Core/Services/ColumnType.swift | 65 +-- .../Core/Services/ColumnTypeClassifier.swift | 32 +- .../Formatting/CellDisplayFormatter.swift | 47 ++- .../Core/Storage/AppSettingsManager.swift | 21 +- .../Core/Storage/AppSettingsStorage.swift | 23 +- .../Extensions/SyncCoordinator+Settings.swift | 111 ++++- .../Core/Sync/SyncChangeTracker.swift | 8 + .../TablePro/Core/Sync/SyncCoordinator.swift | 165 +++----- .../Core/Utilities/SQL/JsonRowConverter.swift | 4 +- .../Models/Settings/AppSettings.swift | 41 +- .../Models/Settings/AppSettingsCategory.swift | 4 +- .../Models/UI/DataGridConfiguration.swift | 6 + .../TablePro/Resources/Localizable.xcstrings | 70 ++++ .../Themes/tablepro.catppuccin-latte.json | 13 +- .../Themes/tablepro.catppuccin-mocha.json | 11 +- .../Resources/Themes/tablepro.dracula.json | 11 +- .../Themes/tablepro.github-dark.json | 11 +- .../Themes/tablepro.github-light.json | 11 +- .../Themes/tablepro.gruvbox-dark.json | 13 +- .../Themes/tablepro.gruvbox-light.json | 13 +- .../Resources/Themes/tablepro.nord.json | 13 +- .../Resources/Themes/tablepro.one-dark.json | 13 +- .../Resources/Themes/tablepro.one-light.json | 15 +- .../Themes/tablepro.solarized-dark.json | 13 +- .../Themes/tablepro.solarized-light.json | 15 +- .../Themes/tablepro.tokyo-night-light.json | 11 +- .../Themes/tablepro.tokyo-night.json | 11 +- apple/TablePro/Theme/ThemeCatalog.swift | 247 ++++++++--- apple/TablePro/Theme/ThemeColor.swift | 4 +- .../Views/Compare/CompareRowGrid.swift | 1 + .../Cells/DataGridCellAppearance.swift | 15 +- .../DataGrid/Cells/DataGridCellKind.swift | 9 +- .../DataGrid/Cells/DataGridCellPalette.swift | 42 ++ .../Cells/DataGridCellPresenter.swift | 97 +++-- .../DataGrid/Cells/DataGridCellRegistry.swift | 8 +- .../DataGrid/Cells/DataGridValueTone.swift | 83 ++++ .../DataGrid/DataGridViewController.swift | 11 + .../MainContentCoordinator+GridModels.swift | 1 + .../Comparison/ResultComparisonGrid.swift | 1 + .../FieldEditors/BooleanPickerView.swift | 24 +- .../Appearance/ThemeEditorPreview.swift | 69 +++- .../Settings/Appearance/ThemeEditorView.swift | 30 +- .../Appearance/ThemeSlotPresentation.swift | 56 ++- .../Settings/DataResultsSettingsView.swift | 4 +- .../Settings/Sections/DataGridSection.swift | 4 + .../Settings/SettingsWindowController.swift | 25 +- .../Core/DataGrid/RowDisplayCacheTests.swift | 30 ++ .../Export/TableTransferServiceTests.swift | 15 +- .../AppSettingsCategoryParityTests.swift | 21 + .../Services/ColumnTypeClassifierTests.swift | 37 +- .../Core/Sync/SyncCategoryRefetchTests.swift | 14 + .../Core/Sync/SyncConflictRebaseTests.swift | 9 +- .../Core/Sync/SyncCoordinatorThemeTests.swift | 183 ++++++++- .../Utilities/JsonRowConverterTests.swift | 11 +- .../Settings/SettingsFieldDecodingTests.swift | 18 + .../Theme/ThemeCatalogSyncTests.swift | 339 ++++++++++++++- .../Theme/ThemeCatalogTests.swift | 47 ++- .../Theme/ThemeContrastTests.swift | 86 +++- .../DataGridCellAppearanceTests.swift | 385 +++++++++++++++++- .../DataGrid/DataGridConfigurationTests.swift | 4 +- .../DataGrid/DataGridValueToneTests.swift | 209 ++++++++++ .../RowInspector/BooleanPickerViewTests.swift | 33 ++ .../Settings/ThemeEditorWindowTests.swift | 131 +++++- 81 files changed, 3005 insertions(+), 576 deletions(-) create mode 100644 apple/TablePro/Views/DataGrid/Cells/DataGridValueTone.swift create mode 100644 apple/TableProTests/Views/DataGrid/DataGridValueToneTests.swift create mode 100644 apple/TableProTests/Views/RowInspector/BooleanPickerViewTests.swift diff --git a/apple/Packages/TableProCore/Sources/TableProTheme/Import/VSCodeThemeMapping.swift b/apple/Packages/TableProCore/Sources/TableProTheme/Import/VSCodeThemeMapping.swift index 6269a02475..5399f05179 100644 --- a/apple/Packages/TableProCore/Sources/TableProTheme/Import/VSCodeThemeMapping.swift +++ b/apple/Packages/TableProCore/Sources/TableProTheme/Import/VSCodeThemeMapping.swift @@ -22,7 +22,7 @@ private extension VSCodeColorSource { } internal extension ThemeSlot { - // A list that stops short leaves the slot to `fallback`, which derives the stripe and the bools the way VS Code paints them. + // A list that stops short leaves the slot to `fallback`, which derives the stripe and the values the way VS Code paints them. var vscodeSources: [VSCodeColorSource] { switch self { case .windowBackdrop: @@ -104,6 +104,13 @@ internal extension ThemeSlot { return [.sqlToken("constant.language.boolean.true.sql")] case .gridBoolFalse: return [.sqlToken("constant.language.boolean.false.sql")] + case .gridString: + return [.sqlToken("string.quoted.single.sql")] + case .gridNumber: + return [.sqlToken("constant.numeric.sql")] + // The SQL grammar scopes no date, binary, JSON, UUID or enum literal. + case .gridDateTime, .gridBinary, .gridJSON, .gridUUID, .gridEnum: + return [] case .gridRowNumber: return [.color("editorLineNumber.foreground")] case .gridModified: diff --git a/apple/Packages/TableProCore/Sources/TableProTheme/ThemeDefinition.swift b/apple/Packages/TableProCore/Sources/TableProTheme/ThemeDefinition.swift index 06ed516b66..a26f872873 100644 --- a/apple/Packages/TableProCore/Sources/TableProTheme/ThemeDefinition.swift +++ b/apple/Packages/TableProCore/Sources/TableProTheme/ThemeDefinition.swift @@ -57,6 +57,24 @@ public struct ThemeDefinition: Identifiable, Equatable, Sendable { isOverridden(slot, highContrast: false) } + public func restricted(toSchema schema: Int) -> ThemeDefinition { + var restricted = self + restricted.overrides = overrides.filter { $0.key.introducedInSchema <= schema } + return restricted + } + + public func setsSlots(introducedIn schema: Int) -> Bool { + overrides.keys.contains { $0.introducedInSchema == schema } + } + + // A slot `source` leaves unset is cleared here too. + public func replacingSlots(from source: ThemeDefinition, where isReplaced: (ThemeSlot) -> Bool) -> ThemeDefinition { + var merged = self + merged.overrides = overrides.filter { !isReplaced($0.key) } + .merging(source.overrides.filter { isReplaced($0.key) }) { _, replacement in replacement } + return merged + } + private func fallbackValue(for slot: ThemeSlot, highContrast: Bool) -> ThemeSlotValue { switch slot.fallback { case .system: diff --git a/apple/Packages/TableProCore/Sources/TableProTheme/ThemeFileFormat.swift b/apple/Packages/TableProCore/Sources/TableProTheme/ThemeFileFormat.swift index 4bdaab4028..1ac6a3fa26 100644 --- a/apple/Packages/TableProCore/Sources/TableProTheme/ThemeFileFormat.swift +++ b/apple/Packages/TableProCore/Sources/TableProTheme/ThemeFileFormat.swift @@ -1,7 +1,9 @@ import Foundation public enum ThemeSchema { - public static let current = 3 + public static let current = 4 + // Every release before format 4 reads and writes format 3 only. + public static let oldestReadable = 3 } public enum ThemeIdentifier { @@ -45,12 +47,12 @@ public enum ThemeFileFormat { guard let schema = root[MetadataKey.schema.rawValue]?.intValue else { if let legacy = root["version"]?.intValue { - throw ThemeLoadError.schemaTooOld(found: legacy, supported: ThemeSchema.current) + throw ThemeLoadError.schemaTooOld(found: legacy, supported: ThemeSchema.oldestReadable) } throw ThemeLoadError.missingSchema } - guard schema >= ThemeSchema.current else { - throw ThemeLoadError.schemaTooOld(found: schema, supported: ThemeSchema.current) + guard schema >= ThemeSchema.oldestReadable else { + throw ThemeLoadError.schemaTooOld(found: schema, supported: ThemeSchema.oldestReadable) } guard schema <= ThemeSchema.current else { throw ThemeLoadError.schemaTooNew(found: schema, supported: ThemeSchema.current) @@ -80,7 +82,7 @@ public enum ThemeFileFormat { name: name, author: author, appearance: appearance, - overrides: try colors(in: root) + overrides: try colors(in: root, schema: schema) ) } @@ -89,7 +91,7 @@ public enum ThemeFileFormat { let name = try validatedName(theme.name, policy: .strict) var root: [String: Any] = [ - MetadataKey.schema.rawValue: ThemeSchema.current, + MetadataKey.schema.rawValue: schema(of: theme), MetadataKey.id.rawValue: theme.id, MetadataKey.name.rawValue: name, MetadataKey.author.rawValue: try validatedAuthor(theme.author, policy: .strict), @@ -108,6 +110,12 @@ public enum ThemeFileFormat { ) } + // The oldest format holding every key the theme sets, so an older TablePro still reads a theme + // that uses none of the newer keys. + private static func schema(of theme: ThemeDefinition) -> Int { + theme.overrides.keys.reduce(ThemeSchema.oldestReadable) { max($0, $1.introducedInSchema) } + } + // MARK: - Reading // `JSONDecoder` over `JSONSerialization`: its error names the character and where it is. @@ -122,7 +130,7 @@ public enum ThemeFileFormat { } } - private static func colors(in root: [String: ThemeJSONNode]) throws -> [ThemeSlot: ThemeColor] { + private static func colors(in root: [String: ThemeJSONNode], schema: Int) throws -> [ThemeSlot: ThemeColor] { let leaves = root .filter { MetadataKey(rawValue: $0.key) == nil } .flatMap { key, node in node.leaves(prefix: key) } @@ -130,12 +138,18 @@ public enum ThemeFileFormat { var parsed: [ThemeSlot: ThemeColor] = [:] var unknown: [String] = [] + var newer: [ThemeSlot] = [] for leaf in leaves { guard let slot = ThemeSlot(rawValue: leaf.path) else { unknown.append(leaf.path) continue } + // Every build that reads the file's format has to read all of it. + guard slot.introducedInSchema <= schema else { + newer.append(slot) + continue + } guard parsed[slot] == nil else { throw ThemeLoadError.duplicateKey(leaf.path) } guard let raw = leaf.value else { throw ThemeLoadError.invalidColor(leaf.path) } let color = try ThemeColor(validating: raw) @@ -143,6 +157,9 @@ public enum ThemeFileFormat { } guard unknown.isEmpty else { throw ThemeLoadError.unknownKeys(unknown) } + if let needed = newer.map(\.introducedInSchema).max() { + throw ThemeLoadError.keysNeedNewerFormat(newer.map(\.rawValue), format: needed) + } return parsed } diff --git a/apple/Packages/TableProCore/Sources/TableProTheme/ThemeLoadError.swift b/apple/Packages/TableProCore/Sources/TableProTheme/ThemeLoadError.swift index 5acd16d076..65d717afc8 100644 --- a/apple/Packages/TableProCore/Sources/TableProTheme/ThemeLoadError.swift +++ b/apple/Packages/TableProCore/Sources/TableProTheme/ThemeLoadError.swift @@ -10,6 +10,7 @@ public enum ThemeLoadError: LocalizedError, Equatable, Sendable { case invalidAuthor case invalidColor(String) case unknownKeys([String]) + case keysNeedNewerFormat([String], format: Int) case duplicateKey(String) case reservedIdentifier(String) case invalidIdentifier(String) @@ -42,13 +43,13 @@ public enum ThemeLoadError: LocalizedError, Equatable, Sendable { return String(localized: "The file does not declare a theme format version.") case let .schemaTooOld(found, supported): return String( - format: String(localized: "Theme format %1$d is no longer supported. This version of TablePro reads format %2$d."), + format: String(localized: "Theme format %1$d is no longer supported. This version of TablePro reads format %2$d and later."), found, supported ) case let .schemaTooNew(found, supported): return String( - format: String(localized: "Theme format %1$d was made for a newer TablePro. This version reads format %2$d."), + format: String(localized: "Theme format %1$d was made for a newer TablePro. This version reads format %2$d and earlier."), found, supported ) @@ -69,6 +70,9 @@ public enum ThemeLoadError: LocalizedError, Equatable, Sendable { case let .unknownKeys(names): let shown = ListFormatter.localizedString(byJoining: names.prefix(3).map { "\"\($0)\"" }) return String(format: String(localized: "TablePro does not use these theme keys: %@."), shown) + case let .keysNeedNewerFormat(names, format): + let shown = ListFormatter.localizedString(byJoining: names.prefix(3).map { "\"\($0)\"" }) + return String(format: String(localized: "These theme keys need theme format %1$d: %2$@."), format, shown) case let .duplicateKey(path): return String(format: String(localized: "The theme sets \"%@\" more than once."), path) case let .reservedIdentifier(identifier): diff --git a/apple/Packages/TableProCore/Sources/TableProTheme/ThemeSlot.swift b/apple/Packages/TableProCore/Sources/TableProTheme/ThemeSlot.swift index 4b4489b75d..a4e018e043 100644 --- a/apple/Packages/TableProCore/Sources/TableProTheme/ThemeSlot.swift +++ b/apple/Packages/TableProCore/Sources/TableProTheme/ThemeSlot.swift @@ -34,6 +34,13 @@ public enum ThemeSlot: String, CaseIterable, Sendable { case gridNullValue = "dataGrid.nullValue" case gridBoolTrue = "dataGrid.boolTrue" case gridBoolFalse = "dataGrid.boolFalse" + case gridString = "dataGrid.string" + case gridNumber = "dataGrid.number" + case gridDateTime = "dataGrid.dateTime" + case gridBinary = "dataGrid.binary" + case gridJSON = "dataGrid.json" + case gridUUID = "dataGrid.uuid" + case gridEnum = "dataGrid.enum" case gridRowNumber = "dataGrid.rowNumber" case gridModified = "dataGrid.modified" case gridInserted = "dataGrid.inserted" @@ -52,8 +59,9 @@ public enum ThemeSlot: String, CaseIterable, Sendable { return .syntax case .gridBackground, .gridText, .gridAlternateRow, .gridHeaderBackground, .gridHeaderText, .gridLine, .gridSelection, .gridSelectionText, .gridInactiveSelection, .gridFocusBorder, - .gridNullValue, .gridBoolTrue, .gridBoolFalse, .gridRowNumber, - .gridModified, .gridInserted, .gridDeleted, .gridDeletedText: + .gridNullValue, .gridBoolTrue, .gridBoolFalse, + .gridString, .gridNumber, .gridDateTime, .gridBinary, .gridJSON, .gridUUID, .gridEnum, + .gridRowNumber, .gridModified, .gridInserted, .gridDeleted, .gridDeletedText: return .dataGrid } } @@ -69,8 +77,11 @@ public enum ThemeSlot: String, CaseIterable, Sendable { .syntaxNull, .syntaxOperator, .syntaxFunction, .syntaxType: return .syntax case .gridBackground, .gridText, .gridAlternateRow, .gridHeaderBackground, .gridHeaderText, - .gridLine, .gridNullValue, .gridBoolTrue, .gridBoolFalse, .gridRowNumber: + .gridLine, .gridRowNumber: return .dataGrid + case .gridNullValue, .gridBoolTrue, .gridBoolFalse, + .gridString, .gridNumber, .gridDateTime, .gridBinary, .gridJSON, .gridUUID, .gridEnum: + return .values case .gridSelection, .gridSelectionText, .gridInactiveSelection, .gridFocusBorder: return .selection case .gridModified, .gridInserted, .gridDeleted, .gridDeletedText: @@ -94,6 +105,7 @@ public enum ThemeSlot: String, CaseIterable, Sendable { .syntaxNull, .syntaxOperator, .syntaxFunction, .syntaxType, .gridText, .gridAlternateRow, .gridHeaderText, .gridLine, .gridSelection, .gridSelectionText, .gridInactiveSelection, .gridFocusBorder, .gridNullValue, .gridBoolTrue, .gridBoolFalse, + .gridString, .gridNumber, .gridDateTime, .gridBinary, .gridJSON, .gridUUID, .gridEnum, .gridRowNumber, .gridModified, .gridInserted, .gridDeleted, .gridDeletedText: return false } @@ -110,6 +122,7 @@ public enum ThemeSlot: String, CaseIterable, Sendable { .syntaxNull, .syntaxOperator, .syntaxFunction, .syntaxType, .gridBackground, .gridText, .gridHeaderBackground, .gridHeaderText, .gridLine, .gridSelectionText, .gridFocusBorder, .gridNullValue, .gridBoolTrue, .gridBoolFalse, + .gridString, .gridNumber, .gridDateTime, .gridBinary, .gridJSON, .gridUUID, .gridEnum, .gridRowNumber: return false } @@ -126,8 +139,8 @@ public enum ThemeSlot: String, CaseIterable, Sendable { case .editorLineNumber: return String(localized: "Line Number") case .editorInvisibles: return String(localized: "Invisibles") case .syntaxKeyword: return String(localized: "Keyword") - case .syntaxString: return String(localized: "String") - case .syntaxNumber: return String(localized: "Number") + case .syntaxString, .gridString: return String(localized: "String") + case .syntaxNumber, .gridNumber: return String(localized: "Number") case .syntaxComment: return String(localized: "Comment") case .syntaxNull: return String(localized: "NULL") case .syntaxOperator: return String(localized: "Operator") @@ -143,6 +156,11 @@ public enum ThemeSlot: String, CaseIterable, Sendable { case .gridNullValue: return String(localized: "NULL Value") case .gridBoolTrue: return String(localized: "Bool True") case .gridBoolFalse: return String(localized: "Bool False") + case .gridDateTime: return String(localized: "Date & Time") + case .gridBinary: return String(localized: "Binary") + case .gridJSON: return String(localized: "JSON") + case .gridUUID: return String(localized: "UUID") + case .gridEnum: return String(localized: "Enum") case .gridRowNumber: return String(localized: "Row Number") case .gridModified: return String(localized: "Modified") case .gridInserted: return String(localized: "Inserted") @@ -184,6 +202,13 @@ public enum ThemeSlot: String, CaseIterable, Sendable { case .gridNullValue: return String(localized: "Data Grid NULL Value") case .gridBoolTrue: return String(localized: "Data Grid Bool True") case .gridBoolFalse: return String(localized: "Data Grid Bool False") + case .gridString: return String(localized: "Data Grid String") + case .gridNumber: return String(localized: "Data Grid Number") + case .gridDateTime: return String(localized: "Data Grid Date & Time") + case .gridBinary: return String(localized: "Data Grid Binary") + case .gridJSON: return String(localized: "Data Grid JSON") + case .gridUUID: return String(localized: "Data Grid UUID") + case .gridEnum: return String(localized: "Data Grid Enum") case .gridRowNumber: return String(localized: "Data Grid Row Number") case .gridModified: return String(localized: "Data Grid Modified") case .gridInserted: return String(localized: "Data Grid Inserted") @@ -191,4 +216,22 @@ public enum ThemeSlot: String, CaseIterable, Sendable { case .gridDeletedText: return String(localized: "Data Grid Deleted Text") } } + + // A file declaring an older format cannot hold the key. + public var introducedInSchema: Int { + switch self { + case .windowBackdrop, + .editorBackground, .editorText, .editorCursor, .editorSelection, + .editorCurrentLine, .editorCurrentStatement, .editorLineNumber, .editorInvisibles, + .syntaxKeyword, .syntaxString, .syntaxNumber, .syntaxComment, + .syntaxNull, .syntaxOperator, .syntaxFunction, .syntaxType, + .gridBackground, .gridText, .gridAlternateRow, .gridHeaderBackground, .gridHeaderText, + .gridLine, .gridSelection, .gridSelectionText, .gridInactiveSelection, .gridFocusBorder, + .gridNullValue, .gridBoolTrue, .gridBoolFalse, .gridRowNumber, + .gridModified, .gridInserted, .gridDeleted, .gridDeletedText: + return 3 + case .gridString, .gridNumber, .gridDateTime, .gridBinary, .gridJSON, .gridUUID, .gridEnum: + return 4 + } + } } diff --git a/apple/Packages/TableProCore/Sources/TableProTheme/ThemeSlotFallback.swift b/apple/Packages/TableProCore/Sources/TableProTheme/ThemeSlotFallback.swift index 83cbe6112d..ed55776c13 100644 --- a/apple/Packages/TableProCore/Sources/TableProTheme/ThemeSlotFallback.swift +++ b/apple/Packages/TableProCore/Sources/TableProTheme/ThemeSlotFallback.swift @@ -34,8 +34,9 @@ public extension ThemeSlot { return .defaultSyntax case .syntaxOperator: return .slot(.editorText) - // A bool in labelColor next to themed cell text reads as a different table. - case .gridBoolTrue, .gridBoolFalse: + // A value in labelColor next to themed cell text reads as a different table. + case .gridBoolTrue, .gridBoolFalse, + .gridString, .gridNumber, .gridDateTime, .gridBinary, .gridJSON, .gridUUID, .gridEnum: return .slot(.gridText) case .gridAlternateRow: return .derived(base: .gridBackground, rule: .stripe(toward: .gridText)) diff --git a/apple/Packages/TableProCore/Sources/TableProTheme/ThemeSlotSection.swift b/apple/Packages/TableProCore/Sources/TableProTheme/ThemeSlotSection.swift index f64c4e6109..adca33977a 100644 --- a/apple/Packages/TableProCore/Sources/TableProTheme/ThemeSlotSection.swift +++ b/apple/Packages/TableProCore/Sources/TableProTheme/ThemeSlotSection.swift @@ -1,11 +1,12 @@ import Foundation -// The theme editor's grouping: selection colors and row change colors, each chosen as a set, get sections of their own. +// The theme editor's grouping: value colors, selection colors and row change colors, each chosen as a set, get sections of their own. public enum ThemeSlotSection: String, CaseIterable, Sendable { case window case editor case syntax case dataGrid + case values case selection case changes @@ -15,6 +16,7 @@ public enum ThemeSlotSection: String, CaseIterable, Sendable { case .editor: return String(localized: "Editor") case .syntax: return String(localized: "Syntax") case .dataGrid: return String(localized: "Data Grid") + case .values: return String(localized: "Values") case .selection: return String(localized: "Selection") case .changes: return String(localized: "Changes") } diff --git a/apple/Packages/TableProCore/Tests/TableProThemeTests/Import/VSCodeThemeImportTests.swift b/apple/Packages/TableProCore/Tests/TableProThemeTests/Import/VSCodeThemeImportTests.swift index c0d16fd667..5462490d66 100644 --- a/apple/Packages/TableProCore/Tests/TableProThemeTests/Import/VSCodeThemeImportTests.swift +++ b/apple/Packages/TableProCore/Tests/TableProThemeTests/Import/VSCodeThemeImportTests.swift @@ -169,13 +169,38 @@ struct VSCodeThemeImportTests { {"colors": { "editor.background": "#202020", "editor.foreground": "#E0E0E0" - }, "tokenColors": [{"scope": "constant.language.boolean", "settings": {"foreground": "#E0E0E0"}}]} + }, "tokenColors": [ + {"scope": "constant.language.boolean", "settings": {"foreground": "#E0E0E0"}}, + {"scope": "string", "settings": {"foreground": "#E0E0E0"}} + ]} """ ) #expect(theme.hex(.gridBoolTrue) == nil) #expect(theme.hex(.gridBoolFalse) == nil) + #expect(theme.hex(.gridString) == nil) #expect(theme.value(for: .gridBoolTrue) == .themed(ThemeColor(red: 0xE0, green: 0xE0, blue: 0xE0))) + #expect(theme.value(for: .gridString) == .themed(ThemeColor(red: 0xE0, green: 0xE0, blue: 0xE0))) + } + + @Test("String and number rules color the grid's strings and numbers, and the other value types follow the text") + func stringAndNumberRulesColorGridValues() throws { + let theme = try Self.theme( + """ + {"colors": {"editor.background": "#1E1E1E", "editor.foreground": "#D4D4D4"}, "tokenColors": [ + {"scope": "string", "settings": {"foreground": "#CE9178"}}, + {"scope": "constant.numeric", "settings": {"foreground": "#B5CEA8"}} + ]} + """ + ) + let text = ThemeColor(red: 0xD4, green: 0xD4, blue: 0xD4) + + #expect(theme.hex(.gridString) == "#CE9178") + #expect(theme.hex(.gridNumber) == "#B5CEA8") + for slot in [ThemeSlot.gridDateTime, .gridBinary, .gridJSON, .gridUUID, .gridEnum] { + #expect(theme.hex(slot) == nil, "\(slot)") + #expect(theme.value(for: slot) == .themed(text), "\(slot)") + } } @Test("A VS Code theme's table header, list selection and focus colors import as set, even where they match the grid") diff --git a/apple/Packages/TableProCore/Tests/TableProThemeTests/Import/VSCodeThemeMappingTests.swift b/apple/Packages/TableProCore/Tests/TableProThemeTests/Import/VSCodeThemeMappingTests.swift index 99a31ad1a2..06d97e6691 100644 --- a/apple/Packages/TableProCore/Tests/TableProThemeTests/Import/VSCodeThemeMappingTests.swift +++ b/apple/Packages/TableProCore/Tests/TableProThemeTests/Import/VSCodeThemeMappingTests.swift @@ -83,6 +83,9 @@ struct VSCodeThemeMappingTests { case .gridNullValue: return "#707070" case .gridBoolTrue: return "#C00009" case .gridBoolFalse: return "#C0000A" + case .gridString: return "#C00002" + case .gridNumber: return "#C00003" + case .gridDateTime, .gridBinary, .gridJSON, .gridUUID, .gridEnum: return nil case .gridRowNumber: return "#808080" // 0xFF scaled by 0.3 and by 0.5, rounded. case .gridModified: return "#E0C0004D" @@ -143,6 +146,9 @@ struct VSCodeThemeMappingTests { case .gridNullValue: return "#40404080" case .gridBoolTrue: return nil case .gridBoolFalse: return nil + case .gridString: return nil + case .gridNumber: return nil + case .gridDateTime, .gridBinary, .gridJSON, .gridUUID, .gridEnum: return nil case .gridRowNumber: return nil case .gridModified: return nil case .gridInserted: return "#00AA0033" diff --git a/apple/Packages/TableProCore/Tests/TableProThemeTests/ThemeFileFormatTests.swift b/apple/Packages/TableProCore/Tests/TableProThemeTests/ThemeFileFormatTests.swift index 25e0faeb27..13175b9a58 100644 --- a/apple/Packages/TableProCore/Tests/TableProThemeTests/ThemeFileFormatTests.swift +++ b/apple/Packages/TableProCore/Tests/TableProThemeTests/ThemeFileFormatTests.swift @@ -53,11 +53,113 @@ struct ThemeFileFormatTests { } } + private static let valueSlots: [ThemeSlot] = [.gridString, .gridNumber, .gridDateTime, .gridBinary, .gridJSON, .gridUUID, .gridEnum] + + private static func encodedSchema(_ theme: ThemeDefinition) throws -> Int? { + let object = try JSONSerialization.jsonObject(with: try ThemeFileFormat.encode(theme)) + return (object as? [String: Any])?["schema"] as? Int + } + // MARK: - What loads - @Test("The current format is 3") - func currentFormatIsThree() { - #expect(ThemeSchema.current == 3) + @Test("The current format is 4, and format 3 still reads") + func currentFormatIsFour() { + #expect(ThemeSchema.current == 4) + #expect(ThemeSchema.oldestReadable == 3) + } + + @Test("A format 3 file and a format 4 file both load", arguments: [3, 4]) + func readableFormatsLoad(schema: Int) throws { + var root = Self.metadata(schema: schema) + Self.insert("#FF0000", at: ["editor", "syntax", "keyword"], into: &root) + Self.insert("#00FF00", at: ["dataGrid", "boolTrue"], into: &root) + + let colors = try Self.decode(root).overrides.mapValues(\.hex) + + #expect(colors == [.syntaxKeyword: "#FF0000", .gridBoolTrue: "#00FF00"]) + } + + @Test("A format 4 file loads its value colors") + func formatFourLoadsValueColors() throws { + var root = Self.metadata(schema: 4) + for slot in Self.valueSlots { + Self.insert("#ABCDEF", at: slot.rawValue.components(separatedBy: "."), into: &root) + } + + let theme = try Self.decode(root) + + #expect(Set(theme.overrides.keys) == Set(Self.valueSlots)) + } + + // A format 3 reader refuses these keys, so a file that claims format 3 cannot hold them. + @Test("A format 3 file holding a format 4 key is refused with the format the key needs") + func newerKeyInOlderFileIsRefused() throws { + #expect( + try Self.refusal(Self.withColor("#F1FA8C", at: ["dataGrid", "string"])) + == .keysNeedNewerFormat(["dataGrid.string"], format: 4) + ) + + var root = Self.metadata(schema: 3) + Self.insert("#F8F8F2", at: ["dataGrid", "text"], into: &root) + for slot in Self.valueSlots { + Self.insert("#ABCDEF", at: slot.rawValue.components(separatedBy: "."), into: &root) + } + let paths = Self.valueSlots.map(\.rawValue).sorted() + #expect(try Self.refusal(root) == .keysNeedNewerFormat(paths, format: 4)) + } + + @Test("A key TablePro does not use outranks a key that needs a newer format") + func unknownKeyOutranksANewerKey() throws { + var root = Self.withColor("#F1FA8C", at: ["dataGrid", "string"]) + Self.insert("#000000", at: ["panel", "text"], into: &root) + + #expect(try Self.refusal(root) == .unknownKeys(["panel.text"])) + } + + @Test("A theme restricted to format 3 drops its value colors and encodes as format 3") + func restrictedThemeEncodesAsFormatThree() throws { + let theme = ThemeDefinition( + id: "user.values", + name: "Values", + author: "", + appearance: .dark, + overrides: [.gridText: try ThemeColor(validating: "#F8F8F2"), .gridString: try ThemeColor(validating: "#F1FA8C")] + ) + + let restricted = theme.restricted(toSchema: ThemeSchema.oldestReadable) + + #expect(restricted.overrides.keys.sorted { $0.rawValue < $1.rawValue } == [.gridText]) + #expect(!restricted.setsSlots(introducedIn: 4)) + #expect(theme.setsSlots(introducedIn: 4)) + #expect(try Self.encodedSchema(restricted) == 3) + } + + @Test("The encoder writes format 3 for a theme with no value color, and format 4 once it has one") + func encoderWritesTheOldestFormatThatHoldsTheTheme() throws { + let theme = ThemeDefinition( + id: "user.values", + name: "Values", + author: "", + appearance: .dark, + overrides: [.gridText: try ThemeColor(validating: "#F8F8F2"), .gridBoolTrue: try ThemeColor(validating: "#50FA7B")] + ) + #expect(try Self.encodedSchema(theme) == 3) + #expect(try Self.encodedSchema(.defaultDark) == 3) + + for slot in Self.valueSlots { + var withValue = theme + withValue.overrides[slot] = try ThemeColor(validating: "#F1FA8C") + #expect(try Self.encodedSchema(withValue) == 4, "\(slot.rawValue)") + #expect(try ThemeFileFormat.decode(try ThemeFileFormat.encode(withValue)) == withValue, "\(slot.rawValue)") + } + } + + @Test("A format 4 file with no value color is saved back as format 3") + func formatFourWithoutValueColorsSavesAsThree() throws { + var root = Self.metadata(schema: 4) + Self.insert("#282A36", at: ["dataGrid", "background"], into: &root) + + #expect(try Self.encodedSchema(try Self.decode(root)) == 3) } @Test("A file with only metadata loads and sets nothing") @@ -85,9 +187,9 @@ struct ThemeFileFormatTests { #expect(colors == [.syntaxKeyword: "#FF0000", .gridAlternateRow: "#00000080"]) } - @Test("Every slot path is accepted") + @Test("Every slot path is accepted in the current format") func everySlotPathLoads() throws { - var root = Self.metadata() + var root = Self.metadata(schema: ThemeSchema.current) for slot in ThemeSlot.allCases { Self.insert("#123456", at: slot.rawValue.components(separatedBy: "."), into: &root) } @@ -271,10 +373,10 @@ struct ThemeFileFormatTests { } } - @Test("Format 2 and a file with no format are refused") + @Test("Format 2, format 5 and a file with no format are refused") func otherFormatsAreRefused() throws { #expect(try Self.refusal(Self.metadata(schema: 2)) == .schemaTooOld(found: 2, supported: 3)) - #expect(try Self.refusal(Self.metadata(schema: 4)) == .schemaTooNew(found: 4, supported: 3)) + #expect(try Self.refusal(Self.metadata(schema: 5)) == .schemaTooNew(found: 5, supported: 4)) #expect(try Self.refusal(Self.metadata(schema: "3")) == .missingSchema) var unversioned = Self.metadata() @@ -479,8 +581,9 @@ struct ThemeFileFormatTests { func errorMessages() { let errors: [ThemeLoadError] = [ .malformedJSON(detail: nil), .missingSchema, .schemaTooOld(found: 2, supported: 3), - .schemaTooNew(found: 4, supported: 3), .missingField("name"), .invalidName, .invalidAuthor, - .invalidColor("#XYZ"), .unknownKeys(["a.b"]), .duplicateKey("a.b"), .reservedIdentifier("tablepro.x"), + .schemaTooNew(found: 5, supported: 4), .missingField("name"), .invalidName, .invalidAuthor, + .invalidColor("#XYZ"), .unknownKeys(["a.b"]), .keysNeedNewerFormat(["a.b"], format: 4), + .duplicateKey("a.b"), .reservedIdentifier("tablepro.x"), .invalidIdentifier("x/y"), .duplicateIdentifier("user.a", existingFile: "a.json"), .changedOnDisk("a.json"), .deletedOnDisk("a.json"), .nameTaken("Ocean"), .fileTooLarge(limit: ThemeFileImporter.maximumFileSize), .notATheme, @@ -489,6 +592,18 @@ struct ThemeFileFormatTests { #expect(!(error.errorDescription ?? "").isEmpty, "\(error)") } #expect(ThemeLoadError.malformedJSON(detail: nil).errorDescription == "The file is not valid JSON.") + #expect( + ThemeLoadError.schemaTooOld(found: 2, supported: 3).errorDescription + == "Theme format 2 is no longer supported. This version of TablePro reads format 3 and later." + ) + #expect( + ThemeLoadError.schemaTooNew(found: 5, supported: 4).errorDescription + == "Theme format 5 was made for a newer TablePro. This version reads format 4 and earlier." + ) + #expect( + ThemeLoadError.keysNeedNewerFormat(["dataGrid.string"], format: 4).errorDescription + == "These theme keys need theme format 4: \"dataGrid.string\"." + ) #expect(ThemeLoadError.deletedOnDisk("Ocean.json").errorDescription == "Ocean.json was deleted outside TablePro.") #expect(ThemeLoadError.nameTaken("Ocean").errorDescription == "A theme named “Ocean” already exists.") #expect(ThemeLoadError.notATheme.errorDescription == "This file is not a TablePro theme or a VS Code color theme.") diff --git a/apple/Packages/TableProCore/Tests/TableProThemeTests/ThemeFileImporterTests.swift b/apple/Packages/TableProCore/Tests/TableProThemeTests/ThemeFileImporterTests.swift index ccec91c0d0..50f972ab17 100644 --- a/apple/Packages/TableProCore/Tests/TableProThemeTests/ThemeFileImporterTests.swift +++ b/apple/Packages/TableProCore/Tests/TableProThemeTests/ThemeFileImporterTests.swift @@ -118,7 +118,16 @@ struct ThemeFileImporterTests { defer { folder.remove() } let url = try folder.write("{\"version\": 1, \"id\": \"user.a\", \"name\": \"x\"}", as: "old.json") - #expect(Self.refusal(importing: url) == .schemaTooOld(found: 1, supported: ThemeSchema.current)) + #expect(Self.refusal(importing: url) == .schemaTooOld(found: 1, supported: ThemeSchema.oldestReadable)) + } + + @Test("A TablePro theme in a newer format names the newest format this version reads") + func newerFormatKeepsItsError() throws { + let folder = try Folder() + defer { folder.remove() } + let url = try folder.write("{\"schema\": 5, \"id\": \"user.a\", \"name\": \"x\", \"appearance\": \"dark\"}", as: "new.json") + + #expect(Self.refusal(importing: url) == .schemaTooNew(found: 5, supported: ThemeSchema.current)) } @Test("A missing file throws the file system's error") diff --git a/apple/Packages/TableProCore/Tests/TableProThemeTests/ThemeSlotFallbackTests.swift b/apple/Packages/TableProCore/Tests/TableProThemeTests/ThemeSlotFallbackTests.swift index 6822fb722c..064034cb8f 100644 --- a/apple/Packages/TableProCore/Tests/TableProThemeTests/ThemeSlotFallbackTests.swift +++ b/apple/Packages/TableProCore/Tests/TableProThemeTests/ThemeSlotFallbackTests.swift @@ -54,6 +54,13 @@ struct ThemeSlotFallbackTests { .gridNullValue: .system, .gridBoolTrue: .slot(.gridText), .gridBoolFalse: .slot(.gridText), + .gridString: .slot(.gridText), + .gridNumber: .slot(.gridText), + .gridDateTime: .slot(.gridText), + .gridBinary: .slot(.gridText), + .gridJSON: .slot(.gridText), + .gridUUID: .slot(.gridText), + .gridEnum: .slot(.gridText), .gridRowNumber: .system, .gridModified: .system, .gridInserted: .system, @@ -189,11 +196,14 @@ struct ThemeSlotFallbackTests { // MARK: - Slot fallbacks - @Test("Bools follow themed grid text, and stay native without it") + @Test("Bools and value types follow themed grid text, and stay native without it") func gridTextFollowers() throws { let themed = try Self.theme(.dark, [.gridText: "#F8F8F2"]) let text = Self.color("#F8F8F2") - for slot in [ThemeSlot.gridBoolTrue, .gridBoolFalse] { + let followers: [ThemeSlot] = [ + .gridBoolTrue, .gridBoolFalse, .gridString, .gridNumber, .gridDateTime, .gridBinary, .gridJSON, .gridUUID, .gridEnum, + ] + for slot in followers { #expect(themed.value(for: slot) == .themed(text), "\(slot.rawValue)") #expect(ThemeDefinition.defaultDark.value(for: slot) == .system(slot), "\(slot.rawValue)") } @@ -201,6 +211,10 @@ struct ThemeSlotFallbackTests { let ownBool = try Self.theme(.dark, [.gridText: "#F8F8F2", .gridBoolTrue: "#50FA7B"]) #expect(ownBool.value(for: .gridBoolTrue) == .themed(Self.color("#50FA7B"))) #expect(ownBool.value(for: .gridBoolFalse) == .themed(text)) + + let ownString = try Self.theme(.dark, [.gridText: "#F8F8F2", .gridString: "#F1FA8C"]) + #expect(ownString.value(for: .gridString) == .themed(Self.color("#F1FA8C"))) + #expect(ownString.value(for: .gridNumber) == .themed(text)) } @Test("Operator falls back to the editor text, themed or native") diff --git a/apple/Packages/TableProCore/Tests/TableProThemeTests/ThemeSlotTests.swift b/apple/Packages/TableProCore/Tests/TableProThemeTests/ThemeSlotTests.swift index a097ffd52c..fdfa516c54 100644 --- a/apple/Packages/TableProCore/Tests/TableProThemeTests/ThemeSlotTests.swift +++ b/apple/Packages/TableProCore/Tests/TableProThemeTests/ThemeSlotTests.swift @@ -23,9 +23,36 @@ struct ThemeSlotTests { } } - @Test("The registry holds 35 slots") + @Test("The registry holds 42 slots") func slotCount() { - #expect(ThemeSlot.allCases.count == 35) + #expect(ThemeSlot.allCases.count == 42) + } + + @Test("The value slots sit under the data grid key") + func valueSlots() { + let expected: [String: ThemeSlot] = [ + "dataGrid.string": .gridString, + "dataGrid.number": .gridNumber, + "dataGrid.dateTime": .gridDateTime, + "dataGrid.binary": .gridBinary, + "dataGrid.json": .gridJSON, + "dataGrid.uuid": .gridUUID, + "dataGrid.enum": .gridEnum, + ] + for (path, slot) in expected { + #expect(ThemeSlot(rawValue: path) == slot, "\(path)") + #expect(slot.group == .dataGrid, "\(path)") + #expect(slot.introducedInSchema == 4, "\(path)") + } + } + + @Test("Every slot came with format 3 or a later one this build reads, and the newest is the current format") + func slotsCarryTheirFormat() { + let formats = ThemeSlot.allCases.map(\.introducedInSchema) + #expect(formats.allSatisfy { (ThemeSchema.oldestReadable...ThemeSchema.current).contains($0) }) + #expect(formats.max() == ThemeSchema.current) + let format3 = ThemeSlot.allCases.filter { $0.introducedInSchema == ThemeSchema.oldestReadable } + #expect(format3.count == 35) } @Test("Every slot belongs to exactly one group, listed in registry order") @@ -44,16 +71,20 @@ struct ThemeSlotTests { #expect(sectioned.count == ThemeSlot.allCases.count) } - @Test("The editor sections split the data grid into rows, selection and changes") + @Test("The editor sections split the data grid into rows, values, selection and changes") func sectionsSplitTheGrid() { - #expect(ThemeSlotSection.allCases == [.window, .editor, .syntax, .dataGrid, .selection, .changes]) + #expect(ThemeSlotSection.allCases == [.window, .editor, .syntax, .dataGrid, .values, .selection, .changes]) #expect(ThemeSlotSection.window.slots == [.windowBackdrop]) #expect(ThemeSlotSection.editor.slots == ThemeSlotGroup.editor.slots) #expect(ThemeSlotSection.syntax.slots == ThemeSlotGroup.syntax.slots) #expect(ThemeSlotSection.dataGrid.slots == [ - .gridBackground, .gridText, .gridAlternateRow, .gridHeaderBackground, .gridHeaderText, - .gridLine, .gridNullValue, .gridBoolTrue, .gridBoolFalse, .gridRowNumber, + .gridBackground, .gridText, .gridAlternateRow, .gridHeaderBackground, .gridHeaderText, .gridLine, .gridRowNumber, + ]) + #expect(ThemeSlotSection.values.slots == [ + .gridNullValue, .gridBoolTrue, .gridBoolFalse, + .gridString, .gridNumber, .gridDateTime, .gridBinary, .gridJSON, .gridUUID, .gridEnum, ]) + #expect(ThemeSlotSection.values.label == "Values") #expect(ThemeSlotSection.selection.slots == [.gridSelection, .gridSelectionText, .gridInactiveSelection, .gridFocusBorder]) #expect(ThemeSlotSection.changes.slots == [.gridModified, .gridInserted, .gridDeleted, .gridDeletedText]) } @@ -96,6 +127,11 @@ struct ThemeSlotTests { #expect(ThemeSlot.gridSelection.accessibilityLabel == "Data Grid Selection") #expect(ThemeSlot.gridSelectionText.accessibilityLabel == "Data Grid Selected Text") #expect(ThemeSlot.windowBackdrop.accessibilityLabel == "Window Background") + #expect(ThemeSlot.gridString.label == "String") + #expect(ThemeSlot.gridString.accessibilityLabel == "Data Grid String") + #expect(ThemeSlot.gridDateTime.label == "Date & Time") + #expect(ThemeSlot.gridDateTime.accessibilityLabel == "Data Grid Date & Time") + #expect(ThemeSlot.gridEnum.accessibilityLabel == "Data Grid Enum") } @Test("Only the fills under everything else require an opaque color") diff --git a/apple/Plugins/MySQLDriverPlugin/MariaDBFieldMetadata.swift b/apple/Plugins/MySQLDriverPlugin/MariaDBFieldMetadata.swift index 9b6fab6db0..eaa84845cb 100644 --- a/apple/Plugins/MySQLDriverPlugin/MariaDBFieldMetadata.swift +++ b/apple/Plugins/MySQLDriverPlugin/MariaDBFieldMetadata.swift @@ -41,7 +41,8 @@ nonisolated internal func mariaDBTypeName( switch typeRaw { case 0: return "DECIMAL" - case 1: return "TINYINT" + // A BOOLEAN column arrives as TINY with length 1. + case 1: return length == 1 ? "TINYINT(1)" : "TINYINT" case 2: return "SMALLINT" case 3: return "INT" case 4: return "FLOAT" @@ -56,7 +57,8 @@ nonisolated internal func mariaDBTypeName( case 13: return "YEAR" case 14: return "NEWDATE" case 15: return "VARCHAR" - case 16: return "BIT" + // The width is what tells a one-bit flag from a BIT(8) that reads as a number. + case 16: return length > 1 ? "BIT(\(length))" : "BIT" case 245: return "JSON" case 246: return "DECIMAL" case 247: return "ENUM" diff --git a/apple/Plugins/MySQLDriverTests/MariaDBTypeNameTests.swift b/apple/Plugins/MySQLDriverTests/MariaDBTypeNameTests.swift index f029fa39ab..f4cdb34e79 100644 --- a/apple/Plugins/MySQLDriverTests/MariaDBTypeNameTests.swift +++ b/apple/Plugins/MySQLDriverTests/MariaDBTypeNameTests.swift @@ -47,6 +47,15 @@ struct MariaDBTypeNameTests { #expect(resolve(typeRaw: 255) == "GEOMETRY") } + @Test("TINY of length 1 is TINYINT(1), and a BIT wider than one bit names its width") + func widthsTheClassifierReads() { + #expect(resolve(typeRaw: 1, length: 1) == "TINYINT(1)") + #expect(resolve(typeRaw: 1, length: 4) == "TINYINT") + #expect(resolve(typeRaw: 16, length: 1) == "BIT") + #expect(resolve(typeRaw: 16, length: 8) == "BIT(8)") + #expect(resolve(typeRaw: 16, length: 64) == "BIT(64)") + } + @Test("Unknown type code returns UNKNOWN") func unknownType() { #expect(resolve(typeRaw: 999) == "UNKNOWN") diff --git a/apple/TablePro/Core/DataGrid/RowDisplayBox.swift b/apple/TablePro/Core/DataGrid/RowDisplayBox.swift index 209b20d4f5..590df90467 100644 --- a/apple/TablePro/Core/DataGrid/RowDisplayBox.swift +++ b/apple/TablePro/Core/DataGrid/RowDisplayBox.swift @@ -7,18 +7,48 @@ import Foundation final class RowDisplayBox { var values: ContiguousArray + // A bit per column whose display format produced its text, so the draw reads it without formatting again. + private var formattedBits: ContiguousArray = [] init(_ values: ContiguousArray) { self.values = values } + + func setText(_ text: String?, at index: Int, isFormatted: Bool) { + values[index] = text + let word = index >> 6 + let bit = UInt64(1) << UInt64(index & 63) + if isFormatted { + if word >= formattedBits.count { + formattedBits.append(contentsOf: repeatElement(0, count: word + 1 - formattedBits.count)) + } + formattedBits[word] |= bit + } else if word < formattedBits.count { + formattedBits[word] &= ~bit + } + } + + func isFormatted(at index: Int) -> Bool { + let word = index >> 6 + return index >= 0 && word < formattedBits.count && formattedBits[word] & (UInt64(1) << UInt64(index & 63)) != 0 + } + + func clearText(at index: Int) { + guard index >= 0, index < values.count else { return } + setText(nil, at: index, isFormatted: false) + } + + func clearAllText() { + for index in values.indices { + values[index] = nil + } + formattedBits.removeAll() + } } @MainActor final class RowDisplayCache { - /// A box is a reference and callers mutate one in place before handing it back, so the cost - /// recorded at insertion is the only number that still describes what was added. Recomputing it - /// from the box on removal subtracts a different figure than was added and drifts `totalCost` - /// away from the budget it is there to enforce. + // Callers mutate a box in place, so only the cost recorded at insertion matches what was added to `totalCost`. private struct Entry { let box: RowDisplayBox let cost: Int @@ -80,17 +110,12 @@ final class RowDisplayCache { totalCost = 0 } - /// Drops one row's formatted values while keeping its box, so the next read - /// reformats from the current cell values. Row ids are positional, so a row - /// whose content changed in place keeps its id and would otherwise be served - /// its pre-edit text. + // Row ids are positional: a row edited in place keeps its id and would otherwise be served its pre-edit text. func clearValues(forID id: RowID) { highlights.removeValue(forKey: id) guard let existing = storage[id] else { return } totalCost -= existing.cost - for index in existing.box.values.indices { - existing.box.values[index] = nil - } + existing.box.clearAllText() storage[id] = Entry(box: existing.box, cost: 0) } @@ -99,8 +124,8 @@ final class RowDisplayCache { guard !dataIndexes.isEmpty else { return } highlights.removeAll(keepingCapacity: true) storage = storage.mapValues { entry in - for index in dataIndexes where index < entry.box.values.count { - entry.box.values[index] = nil + for index in dataIndexes { + entry.box.clearText(at: index) } let cost = Self.rowCost(entry.box.values) totalCost += cost - entry.cost diff --git a/apple/TablePro/Core/Events/AppEvents.swift b/apple/TablePro/Core/Events/AppEvents.swift index eb2894539b..b1c515f67e 100644 --- a/apple/TablePro/Core/Events/AppEvents.swift +++ b/apple/TablePro/Core/Events/AppEvents.swift @@ -13,32 +13,28 @@ final class AppEvents { // MARK: - Theme & Accessibility - /// Carries the palette revision and the effective appearance. A consumer that snapshots - /// colours or fonts needs both: a forced Light/Dark switch never changes the theme, and a - /// theme switch never changes the appearance. Subscribing reaches a view that is alive but - /// detached from every window, which a walk of the window tree would miss. + // Carries both because a Light/Dark switch keeps the theme and a theme switch keeps the appearance; it also reaches views detached from every window. let themeChanged = PassthroughSubject() let accessibilityTextSizeChanged = PassthroughSubject() // MARK: - System Environment - /// The Mac's time zone moved. Formatted text that was derived in the old one is now wrong, and - /// nothing else re-derives it: a grid keeps its cached strings until the result is re-fetched. + // A grid keeps strings formatted in the old time zone until a re-fetch, and nothing else re-derives them. let systemTimeZoneChanged = PassthroughSubject() // MARK: - Settings let editorSettingsChanged = PassthroughSubject() - /// A live session's health monitor is started once, at connect, so a change to how often - /// TablePro checks its connections reaches nothing already open without this. + // A session's health monitor starts once at connect, so an open session sees a new interval only through this. let connectionHealthCheckChanged = PassthroughSubject() let dataGridSettingsChanged = PassthroughSubject() - /// The menu bar re-syncs itself through `MainMenuBuilder`, but a window's toolbar advertises - /// the same shortcuts in its tooltips and overflow menu and AppKit never revisits either. + let valueColorSettingsChanged = PassthroughSubject() + + // The menu bar re-syncs itself, but AppKit never revisits the shortcuts in a toolbar's tooltips or overflow menu. let keyboardSettingsChanged = PassthroughSubject() let currentSchemaChanged = PassthroughSubject() @@ -48,21 +44,16 @@ final class AppEvents { // MARK: - Connections let connectionStatusChanged = PassthroughSubject() - /// A workspace's window phase changed. Carries the connection id; the phase lives on the workspace. + // The phase lives on the workspace, so the payload is only the connection id. let connectionPhaseChanged = PassthroughSubject() // A driver's Vault lease can no longer be renewed, so the driver has to sign in again. let credentialLeaseEnding = PassthroughSubject() - /// The step a connection attempt is currently on. Presentational detail inside the - /// connecting phase, never a second source of truth for which pane a window shows. + // Presentational only: which pane a window shows comes from the phase, never from the stage. let connectionStageChanged = PassthroughSubject() - /// Connection metadata changed (name, color, group, type, etc.). - /// Payload is the affected connection's id, or `nil` for bulk updates - /// (sync pull, multi-import) where the sender doesn't track individual ids. - /// Subscribers scoped to a single connection should filter `payload == id`; - /// list-level subscribers refresh on every event regardless. + // nil for a bulk update (sync pull, multi-import) whose sender does not track ids. let connectionUpdated = PassthroughSubject() let databaseDidConnect = PassthroughSubject() @@ -73,61 +64,38 @@ final class AppEvents { let mainWindowWillClose = PassthroughSubject() - /// A connection window was registered, closed, or became key. - /// Subscribers that present the set of open connections, or which window - /// stands for each one, refresh on every event. + // Also fires when a connection window becomes key, since that can change which window stands for a connection. let connectionWindowsChanged = PassthroughSubject() - /// The user reordered the workspace rail. Every open rail shares one - /// arrangement, so all of them reload. + // Every open rail shares one arrangement, so all of them reload. let workspaceRailOrderChanged = PassthroughSubject() - /// The workspace rail was shown or hidden. Every window carries its own - /// rail, so all of them follow the one setting. + // Every window carries its own rail, and all of them follow the one setting. let workspaceRailVisibilityChanged = PassthroughSubject() - /// A window's tabs changed in a way that changes which containers they hold - /// open. Fired only when the set of held containers can differ, not on every - /// keystroke, so the rail reloads when a workspace appears or goes away. + // Fired only when the set of held containers can differ, not on every keystroke. let workspaceTabsChanged = PassthroughSubject() - /// A connection's browse cursor moved to another database or schema. - /// Payload is the connection's id. let browseContainerChanged = PassthroughSubject() - /// A window put another connections-strip entry in front, so the strip's highlight moves. + // The connections strip moves its highlight on this. let visibleContainerChanged = PassthroughSubject() - /// A session connection started or stopped holding a transaction. Payload is the connection's id. let sessionTransactionsChanged = PassthroughSubject() // MARK: - Data Sources - /// Query history changed (entry added, deleted, or cleared). - /// Payload is the affected connection's id, or `nil` for cross-connection - /// operations (delete-by-id without connection lookup, clear-all). - /// Per-connection subscribers should refresh on `payload == nil || payload == self.connectionId`. + // nil when the sender cannot name one connection (delete by id, clear all), so a per-connection subscriber refreshes on nil too. let queryHistoryDidUpdate = PassthroughSubject() - /// SQL favorites or favorite folders changed. - /// Payload is the connection whose list changed, or `nil` when every connection's did. - /// Per-connection subscribers should refresh on `payload == nil || payload == self.connectionId`. - /// - /// A global favorite is part of every connection's list, so a record moving into or out of - /// global scope changes them all and is sent as `nil`, as are bulk operations and any delete. - /// Only the sender can know this: the scope it moved from is no longer in the record by the - /// time a subscriber sees the event. `SQLFavoriteManager.scopeToAnnounce(for:newConnectionId:)` - /// is where that is decided. + // SQLFavoriteManager.scopeToAnnounce picks the payload, since a subscriber never sees the scope a record moved from. let sqlFavoritesDidUpdate = PassthroughSubject() let linkedFoldersDidUpdate = PassthroughSubject() let teamLibraryDidUpdate = PassthroughSubject() - /// Linked SQL folder rescan completed; cached file index changed. - /// Senders are bulk rescans across all enabled folders, so payload is always `nil`. - /// The shape is kept consistent with `sqlFavoritesDidUpdate` so subscribers can - /// uniformly handle "this update may affect me" via `payload == nil || payload == self.connectionId`. + // Always nil, since every rescan is bulk; typed like sqlFavoritesDidUpdate so subscribers share one filter. let linkedSQLFoldersDidUpdate = PassthroughSubject() // Refresh on a tab that loads its own content rather than through the coordinator, by tab id. @@ -147,8 +115,7 @@ final class AppEvents { let pluginsRejected = PassthroughSubject<[RejectedPlugin], Never>() - /// Not private so a test can hand an isolated bus to the object under test. App code uses - /// `shared`, which is the only instance anything observes. + // Not private so a test can hand an isolated bus to the object under test; app code uses only shared. init() {} } diff --git a/apple/TablePro/Core/RoutineDebugging/RoutineDebugController.swift b/apple/TablePro/Core/RoutineDebugging/RoutineDebugController.swift index 41f98644ee..ad0a0eeccb 100644 --- a/apple/TablePro/Core/RoutineDebugging/RoutineDebugController.swift +++ b/apple/TablePro/Core/RoutineDebugging/RoutineDebugController.swift @@ -770,6 +770,7 @@ internal final class RoutineDebugController: ObservableObject { configuration.traits = DataGridEngineTraits(databaseType: databaseType) configuration.showRowNumbers = true configuration.supportsValueFilter = false + configuration.colorsValuesByType = true configuration.commands = [.freezeColumns, .displayAs] let grid = HeldRowsGridAdapter(configuration: configuration, sortsHeldRows: true) grid.setRows(rows) diff --git a/apple/TablePro/Core/Services/ColumnType.swift b/apple/TablePro/Core/Services/ColumnType.swift index 63145b21f9..38c5696c28 100644 --- a/apple/TablePro/Core/Services/ColumnType.swift +++ b/apple/TablePro/Core/Services/ColumnType.swift @@ -8,17 +8,12 @@ import Foundation -/// Which editor an array's elements get. -/// -/// A Bool could only say that an array has a per-element editor, never which one, and the two are -/// not the same control: a scalar element fits a one-line field, a JSON element is a document and -/// needs the JSON viewer. +// Not a Bool: a scalar element fits a one-line field, a JSON element is a document and needs the JSON viewer. enum ArrayElementEditor: Equatable, Sendable { case scalar case json } -/// Represents the semantic type of a database column enum ColumnType: Equatable, Sendable { case text(rawType: String?) case integer(rawType: String?) @@ -35,7 +30,6 @@ enum ColumnType: Equatable, Sendable { case vector(rawType: String?) indirect case array(rawType: String?, element: ColumnType) - /// Raw database type name (e.g., "LONGTEXT", "VARCHAR(255)", "CLOB") var rawType: String? { switch self { case .text(let raw), .integer(let raw), .decimal(let raw), @@ -50,7 +44,6 @@ enum ColumnType: Equatable, Sendable { // MARK: - Display Properties - /// Human-readable name for this column type var displayName: String { switch self { case .text: return "Text" @@ -70,7 +63,6 @@ enum ColumnType: Equatable, Sendable { } } - /// Whether this type represents a JSON value that should use JSON editor var isJsonType: Bool { switch self { case .json: @@ -80,7 +72,6 @@ enum ColumnType: Equatable, Sendable { } } - /// Whether this type represents a date/time value that should be formatted var isDateType: Bool { switch self { case .date, .timestamp, .datetime: @@ -99,8 +90,6 @@ enum ColumnType: Equatable, Sendable { || base == "TIME WITH TIME ZONE" } - /// Whether this type represents long text that should use multi-line editor - /// Checks for TEXT, LONGTEXT, MEDIUMTEXT, TINYTEXT, CLOB types var isLongText: Bool { guard let raw = rawType?.uppercased() else { return false @@ -119,14 +108,12 @@ enum ColumnType: Equatable, Sendable { return false } - /// Whether this type is a very large text type that should be excluded from browse queries. - /// Only MEDIUMTEXT (16MB), LONGTEXT (4GB), and CLOB — not plain TEXT (65KB) or TINYTEXT (255B). + // Kept out of browse queries: MEDIUMTEXT runs to 16MB and LONGTEXT to 4GB, where TEXT stops at 65KB. var isVeryLongText: Bool { guard let raw = rawType?.uppercased() else { return false } return raw == "MEDIUMTEXT" || raw == "LONGTEXT" || raw == "CLOB" } - /// Whether this type is an enum column var isEnumType: Bool { switch self { case .enumType: @@ -136,7 +123,6 @@ enum ColumnType: Equatable, Sendable { } } - /// Whether this type is a SET column var isSetType: Bool { switch self { case .set: @@ -146,7 +132,7 @@ enum ColumnType: Equatable, Sendable { } } - /// True from the result set alone, before the allowed values arrive on the metadata round trip. + // True from the result set alone, before the allowed values arrive on the metadata round trip. var isEnumOrSetType: Bool { isEnumType || isSetType } @@ -165,13 +151,7 @@ enum ColumnType: Equatable, Sendable { } } - /// Whether `''` is a value of this type, so an emptied cell can be written as one. - /// - /// A date, number, boolean, JSON document, geometry or array has no empty value. Sent `''`, - /// MySQL without strict mode stores its zero value (`0000-00-00 00:00:00`, `0`), SQL Server - /// stores `1900-01-01` or `0`, SQLite keeps the text, and a strict MySQL or PostgreSQL refuses - /// the save. An empty SET is the empty set, and an enum has an empty value only when one of - /// its labels is the empty string. + // Sent '', non-strict MySQL stores a zero value, SQL Server 1900-01-01 or 0, SQLite keeps the text, and strict engines refuse it. var hasEmptyValue: Bool { switch self { case .text(let rawType): @@ -185,14 +165,13 @@ enum ColumnType: Equatable, Sendable { } } - /// Types the classifier files under `.text`, either by name or because nothing else matched, - /// that hold no empty string either. + // Filed under .text by the classifier, by name or as the fallback, yet none of them holds an empty string. private static let nonCharacterTextTypes: Set = [ "YEAR", "UUID", "TIMEUUID", "UNIQUEIDENTIFIER", "INET", "CIDR", "MACADDR", "MACADDR8", "IPV4", "IPV6", "INTERVAL" ] - /// A column the engine types as a UUID. Cassandra's `timeuuid` is one, though it takes version 1 only. + // Cassandra's `timeuuid` counts, though it takes version 1 only. var isUUIDType: Bool { guard case .text(let rawType) = self else { return false } return Self.uuidTypeNames.contains(Self.baseTypeName(rawType)) @@ -205,16 +184,26 @@ enum ColumnType: Equatable, Sendable { private static let uuidTypeNames: Set = ["UUID", "UNIQUEIDENTIFIER", "TIMEUUID"] - /// `Nullable(UUID)`, `year(4)` and `INTERVAL DAY TO SECOND` all name their type in the first word. + // `Nullable(UUID)`, `year(4)` and `INTERVAL DAY TO SECOND` all name their type in the first word. private static func baseTypeName(_ rawType: String?) -> String { guard var name = rawType?.uppercased() else { return "" } - while let wrapper = ["NULLABLE(", "LOWCARDINALITY("].first(where: { name.hasPrefix($0) }) { + while let wrapper = typeWrappers.first(where: { name.hasPrefix($0) }) { name.removeFirst(wrapper.count) } return String(name.prefix { $0.isLetter || $0.isNumber || $0 == "_" }) } - /// The element type of an array column, if this is one + // ClickHouse's `Nullable(String)` and `LowCardinality(Nullable(String))` are the type they wrap. + static func unwrappedTypeName(_ rawType: String?) -> String? { + guard var name = rawType?.trimmingCharacters(in: .whitespaces) else { return nil } + while name.hasSuffix(")"), let wrapper = typeWrappers.first(where: { name.uppercased().hasPrefix($0) }) { + name = String(name.dropFirst(wrapper.count).dropLast()).trimmingCharacters(in: .whitespaces) + } + return name + } + + private static let typeWrappers = ["NULLABLE(", "LOWCARDINALITY("] + var arrayElement: ColumnType? { switch self { case .array(_, let element): return element @@ -222,11 +211,7 @@ enum ColumnType: Equatable, Sendable { } } - /// The editor this array's elements get, or nil where they cannot be edited one at a time. - /// - /// A JSON element is included because PostgreSQL's array quoting round-trips it exactly: the - /// literal a `jsonb[]` cell carries parses to its elements and re-serializes byte for byte, - /// SQL NULL and JSON null included. Binary, spatial and nested arrays stay out. + // JSON elements qualify because PostgreSQL's array quoting round-trips a `jsonb[]` literal byte for byte, NULLs included. var arrayElementEditor: ArrayElementEditor? { guard let element = arrayElement else { return nil } switch element { @@ -239,7 +224,6 @@ enum ColumnType: Equatable, Sendable { } } - /// Whether this array's elements can be edited one at a time var supportsElementEditing: Bool { arrayElementEditor != nil } var isVectorType: Bool { @@ -257,7 +241,6 @@ enum ColumnType: Equatable, Sendable { return dimensions } - /// Compact lowercase badge label for sidebar var badgeLabel: String { switch self { case .boolean: return "bool" @@ -278,12 +261,7 @@ enum ColumnType: Equatable, Sendable { } } - /// The same type with the labels the catalog declared, reaching inside an array to its element. - /// - /// The classifier cannot know them: a column's labels arrive separately in - /// `TableRows.columnEnumValues`. Injecting them on the scalar cases alone left an `ENUM[]` - /// column's element editor with no vocabulary, so it offered free-form text where the grid - /// offers the declared labels. + // Reaches into arrays: setting the labels on the scalar cases alone left an `ENUM[]` element editor offering free text. func withAllowedValues(_ values: [String]) -> ColumnType { switch self { case .enumType(let rawType, _): @@ -297,7 +275,6 @@ enum ColumnType: Equatable, Sendable { } } - /// The allowed enum/set values, if known var enumValues: [String]? { switch self { case .enumType(_, let values), .set(_, let values): diff --git a/apple/TablePro/Core/Services/ColumnTypeClassifier.swift b/apple/TablePro/Core/Services/ColumnTypeClassifier.swift index cb2fb3502d..becc872c02 100644 --- a/apple/TablePro/Core/Services/ColumnTypeClassifier.swift +++ b/apple/TablePro/Core/Services/ColumnTypeClassifier.swift @@ -10,7 +10,7 @@ import Foundation struct ColumnTypeClassifier { - /// The engine's type family, when the caller knows it. Without one a `BIT` is a boolean. + // Without a family a `BIT` of any width is a boolean. let family: SQLTypeFamily? init(family: SQLTypeFamily? = nil) { @@ -22,10 +22,7 @@ struct ColumnTypeClassifier { if Self.isAngleBracketCompositeType(stripped) { return .json(rawType: rawTypeName) } - /// `extractBaseAndParams` splits at the first `(` and keeps nothing after the last `)`, so - /// a parameterized array's `[]` never reached the test below: `numeric(10,2)[]` classified - /// as `.decimal` and `bit(8)[]` as `.boolean`, which put a boolean dropdown on an array - /// column. PostgreSQL spells a domain's array this way through `format_type`. + // `extractBaseAndParams` drops what follows the last `)`, so `bit(8)[]` would read as its element. if stripped.hasSuffix("[]") { let elementRaw = String(stripped.dropLast(2)) guard !elementRaw.isEmpty else { return .text(rawType: rawTypeName) } @@ -34,8 +31,7 @@ struct ColumnTypeClassifier { let (base, params) = extractBaseAndParams(stripped) - /// The app's own spelling for an enum array, `ENUM[](mood)`, puts the `[]` on the base and - /// the labels in the parentheses, so it is the one array form the suffix test above misses. + // The app spells an enum array `ENUM[](mood)`, with the `[]` on the base, which the suffix test misses. if base.hasSuffix("[]") { let elementBase = String(base.dropLast(2)) guard !elementBase.isEmpty else { return .text(rawType: rawTypeName) } @@ -59,6 +55,11 @@ struct ColumnTypeClassifier { return .text(rawType: rawTypeName) } + // A MySQL `BIT(8)` reads back as the number it holds, such as 170; only one bit is a flag. + if upper == "BIT", family?.readsBitsAsDecimal == true, let width = params.flatMap({ Int($0) }), width > 1 { + return .integer(rawType: rawTypeName) + } + if let factory = Self.typeLookup[upper] { return factory(rawTypeName) } @@ -72,8 +73,7 @@ struct ColumnTypeClassifier { private static let caseSensitiveBytesTypeName = "BYTES" - /// Elasticsearch names an object array `nested` and a flat key-value object `flattened`, and - /// both hold JSON the grid should offer its viewer for. + // Elasticsearch's `nested` is an object array and `flattened` a key-value object, both JSON the grid offers its viewer for. private static let structuredTypeNames: Set = ["VARIANT", "OBJECT", "ARRAY", "NESTED", "FLATTENED"] private static func isAngleBracketCompositeType(_ value: String) -> Bool { @@ -95,8 +95,7 @@ struct ColumnTypeClassifier { return value } - /// MySQL's catalog spells a column `INT UNSIGNED` or `INT(10) UNSIGNED ZEROFILL`, while its - /// result metadata says `INT`. Without this the catalog spelling fell through to text. + // MySQL's catalog says `INT(10) UNSIGNED ZEROFILL` where its result metadata says `INT`; unstripped, it fell through to text. private func stripTrailingAttributes(_ value: String) -> String { var stripped = value while let attribute = Self.trailingAttributes.first(where: { stripped.uppercased().hasSuffix(" \($0)") }) { @@ -212,7 +211,7 @@ struct ColumnTypeClassifier { map[key] = { .json(rawType: $0) } } - /// Oracle's `LONG RAW` holds bytes, and no pattern arm below reads the name as binary. + // Oracle's `LONG RAW` holds bytes, and no pattern arm below reads the name as binary. for key in [ "BLOB", "BYTEA", "BINARY", "VARBINARY", "RAW", "IMAGE", "TINYBLOB", "MEDIUMBLOB", "LONGBLOB", "LONG RAW" @@ -226,11 +225,7 @@ struct ColumnTypeClassifier { map["SET"] = { .set(rawType: $0, values: nil) } - /// GEOMCOLLECTION is MySQL 8.0.11's own spelling, which its catalog reports for a column - /// declared either way. GEO_POINT and GEO_SHAPE are Elasticsearch's, SDO_GEOMETRY Oracle's, - /// ST_GEOMETRY Teradata's, and RING ClickHouse's. Every one of them fell through to a - /// pattern arm before this: GEO_POINT reached `hasSuffix("INT")` and classified as an - /// integer, which drove its alignment, its sort comparator and its filter operators. + // Engine spellings that fell to a pattern arm before, where GEO_POINT matched `hasSuffix("INT")` and classified as an integer. for key in [ "GEOMETRY", "POINT", "LINESTRING", "POLYGON", "MULTIPOINT", "MULTILINESTRING", "MULTIPOLYGON", @@ -244,8 +239,7 @@ struct ColumnTypeClassifier { map[key] = { .vector(rawType: $0) } } - /// An Oracle `BFILE` cell is the `BFILENAME('DIR', 'file')` call that points at the file, never its bytes, - /// so it edits as text; the hex editor would write bytes the driver refuses for a BFILE. + // An Oracle `BFILE` cell is the `BFILENAME('DIR', 'file')` call, never its bytes, so it edits as text. for key in [ "TEXT", "VARCHAR", "CHAR", "NVARCHAR", "NCHAR", "NTEXT", "VARCHAR2", "NVARCHAR2", "CLOB", "NCLOB", "ROWID", "UROWID", "REF", "BFILE", diff --git a/apple/TablePro/Core/Services/Formatting/CellDisplayFormatter.swift b/apple/TablePro/Core/Services/Formatting/CellDisplayFormatter.swift index b1794b9b94..7f1945b61e 100644 --- a/apple/TablePro/Core/Services/Formatting/CellDisplayFormatter.swift +++ b/apple/TablePro/Core/Services/Formatting/CellDisplayFormatter.swift @@ -19,24 +19,35 @@ enum CellDisplayFormatter { displayFormat: ValueDisplayFormat? = nil, databaseType: DatabaseType? = nil ) -> String? { + display(rawValue, columnType: columnType, displayFormat: displayFormat, databaseType: databaseType).text + } + + // `isFormatted` is false wherever the cell falls back to how its column reads without the display format. + static func display( + _ rawValue: PluginCellValue, + columnType: ColumnType?, + displayFormat: ValueDisplayFormat?, + databaseType: DatabaseType? + ) -> (text: String?, isFormatted: Bool) { switch rawValue { case .null: - return nil + return (nil, false) case .bytes(let data): if let displayFormat, displayFormat.isApplicable(to: columnType, databaseType: databaseType), let formatted = ValueDisplayFormatter.apply(data, format: displayFormat, columnType: columnType) { - return fitToCell(formatted) + return (fitToCell(formatted), true) } - return BlobFormattingService.shared.format(data, for: .grid) + return (BlobFormattingService.shared.format(data, for: .grid), false) case .text(let value): - guard !value.isEmpty else { return value } - var displayValue = value + guard !value.isEmpty else { return (value, false) } if let displayFormat, displayFormat != .raw, displayFormat.isApplicable(to: columnType, databaseType: databaseType), let formatted = ValueDisplayFormatter.apply(value, format: displayFormat, columnType: columnType) { - displayValue = formatted - } else if let columnType { + return (fitToCell(formatted), rendered(formatted, from: value, by: displayFormat)) + } + var displayValue = value + if let columnType { if columnType.isDateType { if let formatted = DateFormattingService.shared.format( dateString: displayValue, @@ -52,13 +63,27 @@ enum CellDisplayFormatter { ) } } - return fitToCell(displayValue) + return (fitToCell(displayValue), false) + } + } + + // The UUID, timestamp, JSON and PHP formats hand back a text value they cannot read unchanged rather than nil. + private static func rendered(_ formatted: String, from value: String, by format: ValueDisplayFormat) -> Bool { + switch format { + case .uuid, .uuidSwappedTime: + return UUID(uuidString: formatted) != nil + case .unixTimestamp, .unixTimestampMillis: + return formatted != value + case .json: + return value.isValidJson + case .phpSerialized: + return CellValueContentDetector.detect(value) == .phpSerialized + case .raw, .text, .messagePack, .protobuf, .igbinary, .avro, .protobufSchema, .schemaRegistry: + return true } } - /// A row is one line, so anything a format produces is capped and stripped of line breaks - /// before it reaches a cell. Binary read as text is the case that needs it: a blob carries - /// newlines and runs to megabytes where hex never did. A lone CR stays for the cell to mark. + // Binary read as text carries newlines and runs to megabytes, so all output is capped and flattened. A lone CR stays for the cell to mark. private static func fitToCell(_ value: String) -> String { var displayValue = value let nsDisplay = displayValue as NSString diff --git a/apple/TablePro/Core/Storage/AppSettingsManager.swift b/apple/TablePro/Core/Storage/AppSettingsManager.swift index 47b3a24e27..2048ba13f6 100644 --- a/apple/TablePro/Core/Storage/AppSettingsManager.swift +++ b/apple/TablePro/Core/Storage/AppSettingsManager.swift @@ -62,8 +62,7 @@ final class AppSettingsManager: ObservableObject { formatter.options(for: editor) } - /// Device-local: it is deliberately absent from `AppSettingsCategory.synced`, so a zoom press - /// never pushes an iCloud record and a Mac on an older build cannot reset it. + // Device-local, absent from `AppSettingsCategory.synced`: a zoom press never pushes a record an older build could reset. @Published var typography: TypographySettings { didSet { guard typography != oldValue else { return } @@ -107,6 +106,15 @@ final class AppSettingsManager: ObservableObject { } } + @Published var valueColors: ValueColorSettings { + didSet { + guard valueColors != oldValue else { return } + storage.saveValueColors(valueColors) + appEvents.valueColorSettingsChanged.send(()) + syncTracker.markDirty(.settings, id: AppSettingsCategory.valueColors) + } + } + @Published var history: HistorySettings { didSet { guard !isValidating else { return } @@ -221,6 +229,7 @@ final class AppSettingsManager: ObservableObject { self.editor = storage.loadEditor() self.formatter = storage.loadFormatter() self.dataGrid = storage.loadDataGrid() + self.valueColors = storage.loadValueColors() self.history = storage.loadHistory() self.tabs = storage.loadTabs() self.keyboard = storage.loadKeyboard() @@ -242,10 +251,7 @@ final class AppSettingsManager: ObservableObject { dateFormattingService.updateFormat(dataGrid.dateFormat) } - /// Auto-pick the first configured provider as active when nothing is selected. - /// Avoids a "AI suddenly stopped working" upgrade UX when older settings JSON - /// (with multiple providers and no activeProviderID concept) is loaded. - /// Internal so `@testable` tests can exercise it directly. + // Settings saved before there was an active provider would otherwise read as AI turned off. internal static func migrateAI(_ settings: AISettings) -> AISettings { guard settings.activeProviderID == nil, let first = settings.providers.first else { return settings @@ -261,7 +267,7 @@ final class AppSettingsManager: ObservableObject { await queryHistoryManager.applySettingsChange() } - /// Resets Sparkle's update preferences here too: the reset alert promises every section. + // Resets Sparkle's update preferences too: the reset alert promises every section. func resetToDefaults() { general = .default appearance = .default @@ -269,6 +275,7 @@ final class AppSettingsManager: ObservableObject { editor = .default formatter = .default dataGrid = .default + valueColors = .default history = .default tabs = .default keyboard = .default diff --git a/apple/TablePro/Core/Storage/AppSettingsStorage.swift b/apple/TablePro/Core/Storage/AppSettingsStorage.swift index 42809b4038..a67ae5946c 100644 --- a/apple/TablePro/Core/Storage/AppSettingsStorage.swift +++ b/apple/TablePro/Core/Storage/AppSettingsStorage.swift @@ -9,7 +9,6 @@ import Foundation import os -/// Persistent storage for app settings final class AppSettingsStorage: Sendable { static let shared = AppSettingsStorage() private static let logger = Logger(subsystem: "com.TablePro", category: "AppSettingsStorage") @@ -27,6 +26,7 @@ final class AppSettingsStorage: Sendable { static let editor = "com.TablePro.settings.editor" static let formatter = "com.TablePro.settings.formatter" static let dataGrid = "com.TablePro.settings.dataGrid" + static let valueColors = "com.TablePro.settings.valueColors" static let history = "com.TablePro.settings.history" static let tabs = "com.TablePro.settings.tabs" static let keyboard = "com.TablePro.settings.keyboard" @@ -47,11 +47,7 @@ final class AppSettingsStorage: Sendable { ] } - /// The storage environment's defaults, not `.standard`. A UI test runs against a per-sandbox - /// suite, and reading the standard domain here put every setting, the onboarding flag among - /// them, in the machine's real preferences: a developer who had finished onboarding never saw - /// it under test, a fresh runner saw it until the first case skipped it, and every case after - /// that inherited the skip. + // Not `.standard`: a UI test runs against a per-sandbox suite, and the standard domain leaked the onboarding flag between runs. init(userDefaults: UserDefaults = AppStorageEnvironment.shared.defaults) { self.defaults = userDefaults } @@ -127,8 +123,7 @@ final class AppSettingsStorage: Sendable { } } - /// The first launch with no typography setting lifts the fonts out of the old theme files once, - /// so an upgrade does not return everyone to System Mono 13. + // The first launch with no typography setting lifts the fonts out of the old theme files, so an upgrade keeps them. func loadTypography( systemIsDark: Bool, legacyThemesDirectory: URL = ThemeCatalog.defaultUserDirectory @@ -186,6 +181,16 @@ final class AppSettingsStorage: Sendable { save(settings, key: Keys.dataGrid) } + // MARK: - Value Color Settings + + func loadValueColors() -> ValueColorSettings { + load(key: Keys.valueColors, default: .default) + } + + func saveValueColors(_ settings: ValueColorSettings) { + save(settings, key: Keys.valueColors) + } + // MARK: - History Settings func loadHistory() -> HistorySettings { @@ -295,7 +300,6 @@ final class AppSettingsStorage: Sendable { // MARK: - Reset - /// Reset all settings to defaults func resetToDefaults() { saveGeneral(.default) saveAppearance(.default) @@ -303,6 +307,7 @@ final class AppSettingsStorage: Sendable { saveEditor(.default) saveFormatter(.default) saveDataGrid(.default) + saveValueColors(.default) saveHistory(.default) saveTabs(.default) saveKeyboard(.default) diff --git a/apple/TablePro/Core/Sync/Extensions/SyncCoordinator+Settings.swift b/apple/TablePro/Core/Sync/Extensions/SyncCoordinator+Settings.swift index ef318b30cb..26904c6453 100644 --- a/apple/TablePro/Core/Sync/Extensions/SyncCoordinator+Settings.swift +++ b/apple/TablePro/Core/Sync/Extensions/SyncCoordinator+Settings.swift @@ -22,13 +22,14 @@ extension SyncCoordinator { case AppSettingsCategory.ai: return try encoder.encode(storage.loadAI()) case AppSettingsCategory.notifications: return try encoder.encode(storage.loadNotifications()) case AppSettingsCategory.formatter: return try encoder.encode(storage.loadFormatter()) + case AppSettingsCategory.valueColors: return try encoder.encode(storage.loadValueColors()) case CustomSlashCommandStorage.syncCategory: return try encoder.encode(CustomSlashCommandStorage.shared.commands) case let category where category.hasPrefix(FileColumnLayoutPersister.syncCategoryPrefix): return columnLayouts().rawData( forStorageKey: String(category.dropFirst(FileColumnLayoutPersister.syncCategoryPrefix.count)) ) - case let category where category.hasPrefix(ThemeCatalog.syncCategoryPrefix): + case let category where ThemeCatalog.themeId(forSyncCategory: category) != nil: return themeCatalog.syncPayload(forCategory: category) default: return nil } @@ -57,6 +58,8 @@ extension SyncCoordinator { manager.notifications = try decoder.decode(NotificationSettings.self, from: data) case AppSettingsCategory.formatter: manager.formatter = try decoder.decode(SQLFormatterSettings.self, from: data) + case AppSettingsCategory.valueColors: + manager.valueColors = try decoder.decode(ValueColorSettings.self, from: data) case CustomSlashCommandStorage.syncCategory: CustomSlashCommandStorage.shared.applyRemote(try decoder.decode([CustomSlashCommand].self, from: data)) case let category where category.hasPrefix(FileColumnLayoutPersister.syncCategoryPrefix): @@ -74,6 +77,12 @@ extension SyncCoordinator { // MARK: - Themes +// nil for a record the pull deleted or could not read. +struct PulledThemeRecords { + var records: [String: Data?] = [:] + var isFullFetch = false +} + extension SyncCoordinator { // A build that reads a newer theme format names a new kind, so it fetches the zone once and // picks up the theme records an older build skipped. @@ -82,9 +91,17 @@ extension SyncCoordinator { // Fetches the zone once for a formatter record this Mac skipped while on an older build. static let formatterContentKind = "\(SyncRecordType.settings.rawValue)/\(AppSettingsCategory.formatter).v1" + // Fetches the zone once for a value colors record this Mac skipped while on an older build. + static let valueColorsContentKind = "\(SyncRecordType.settings.rawValue)/\(AppSettingsCategory.valueColors).v1" + static var readableContent: Set { Set(SyncRecordType.verifiedInProduction.map(\.rawValue)) - .union([Self.themeContentKind, Self.formatterContentKind, ConnectionSyncField.sshRouteContentKind]) + .union([ + Self.themeContentKind, + Self.formatterContentKind, + Self.valueColorsContentKind, + ConnectionSyncField.sshRouteContentKind, + ]) } // Only what this Mac applies: a pull skips a category that is off and still moves the token past it. @@ -96,15 +113,97 @@ extension SyncCoordinator { if settings.syncs(.settings) { content.insert(Self.themeContentKind) content.insert(Self.formatterContentKind) + content.insert(Self.valueColorsContentKind) } return content } + // Read before any record is applied, so a theme new to this Mac finds its newer formats' records + // whichever comes first in the pull. + func pulledThemeRecords( + _ changedRecords: [CKRecord], + deletedRecordIDs: [CKRecord.ID], + unreadRecordIDs: [CKRecord.ID], + isFullFetch: Bool, + echoGuard: SyncEchoGuard? + ) -> PulledThemeRecords { + var pulled = PulledThemeRecords(isFullFetch: isFullFetch) + for record in changedRecords where record.recordType == SyncRecordType.settings.rawValue { + guard echoGuard?.withholds(record.recordID, tracker: changeTracker) != true, + let category = SyncRecordMapper.settingsCategory(from: record), + Self.isNewerThemeFormat(category), + let data = SyncRecordMapper.settingsData(from: record) + else { continue } + pulled.records[category] = data + } + // An unread record changed since the cached copy, so its keys stay unknown until the pull that reads it. + for recordID in deletedRecordIDs + unreadRecordIDs { + guard let parsed = SyncRecordType.parse(recordName: recordID.recordName), + parsed.type == .settings, + Self.isNewerThemeFormat(parsed.id) + else { continue } + pulled.records.updateValue(nil, forKey: parsed.id) + } + return pulled + } + + // A full fetch lists every record the zone holds, so a cached record it lacks was deleted. + func newerThemeRecord(_ category: String, in pulled: PulledThemeRecords) -> Data? { + if let record = pulled.records[category] { + return record + } + guard !pulled.isFullFetch else { return nil } + return recordCache.record(named: SyncRecordType.settings.recordName(for: category)) + .flatMap(SyncRecordMapper.settingsData(from:)) + } + + // A newer format's record the fetch does not list is gone from the server. Every theme that exists + // anywhere is listed or held here, and only a Mac that holds a theme deletes its newer records. + func themeRecordsGone( + afterFullFetch result: PullResult, + zoneID: CKRecordZone.ID, + pushed: Set + ) -> [CKRecord.ID] { + let listed = Set((result.changedRecords.map(\.recordID) + result.unreadRecordIDs).map(\.recordName)) + let listedIds = listed.compactMap { name -> String? in + guard let parsed = SyncRecordType.parse(recordName: name), + parsed.type == .settings, + ThemeCatalog.syncSchema(forCategory: parsed.id) == ThemeSchema.oldestReadable + else { return nil } + return ThemeCatalog.themeId(forSyncCategory: parsed.id) + } + let heldIds = themeCatalog.syncCategories.compactMap(ThemeCatalog.themeId(forSyncCategory:)) + return Set(listedIds + heldIds).sorted() + .flatMap { id in + ThemeCatalog.newerSyncSchemas.map { schema in + SyncRecordMapper.recordID(type: .settings, id: ThemeCatalog.syncCategory(for: id, schema: schema), in: zoneID) + } + } + .filter { !listed.contains($0.recordName) && !pushed.contains($0) } + } + + private static func isNewerThemeFormat(_ category: String) -> Bool { + ThemeCatalog.syncSchema(forCategory: category).map { ThemeCatalog.newerSyncSchemas.contains($0) } == true + } + func applyRemoteThemeDeletions(_ settingsRecordNames: Set) -> Bool { - let prefix = SyncRecordType.settings.recordNamePrefix + ThemeCatalog.syncCategoryPrefix - let ids = Set(settingsRecordNames.filter { $0.hasPrefix(prefix) }.map { String($0.dropFirst(prefix.count)) }) - guard !ids.isEmpty else { return true } - return themeCatalog.removeWithoutSync(ids: ids) + let prefix = SyncRecordType.settings.recordNamePrefix + let deleted = settingsRecordNames.compactMap { name -> (id: String, schema: Int)? in + guard name.hasPrefix(prefix) else { return nil } + let category = String(name.dropFirst(prefix.count)) + guard let id = ThemeCatalog.themeId(forSyncCategory: category), + let schema = ThemeCatalog.syncSchema(forCategory: category) + else { return nil } + return (id, schema) + } + let trashed = Set(deleted.filter { $0.schema == ThemeSchema.oldestReadable }.map(\.id)) + var isPersisted = trashed.isEmpty || themeCatalog.removeWithoutSync(ids: trashed) + for schema in ThemeCatalog.newerSyncSchemas { + let ids = Set(deleted.filter { $0.schema == schema && !trashed.contains($0.id) }.map(\.id)) + guard !ids.isEmpty else { continue } + isPersisted = themeCatalog.removeSlotsWithoutSync(introducedIn: schema, ids: ids) && isPersisted + } + return isPersisted } // Themes made before theme sync were never marked, and loads never mark. They wait for the diff --git a/apple/TablePro/Core/Sync/SyncChangeTracker.swift b/apple/TablePro/Core/Sync/SyncChangeTracker.swift index cd06aedaa5..f04b6cdd6f 100644 --- a/apple/TablePro/Core/Sync/SyncChangeTracker.swift +++ b/apple/TablePro/Core/Sync/SyncChangeTracker.swift @@ -108,6 +108,14 @@ final class SyncChangeTracker: Sendable { postChangeNotification() } + // For a deletion taken back before it was pushed, such as a trash undone. + @MainActor + func discardTombstones(_ type: SyncRecordType, ids: [String]) { + guard !ids.isEmpty else { return } + metadataStorage.removeTombstones(ids, type: type) + recordEdits(type, ids: ids) + } + @MainActor func discardTombstones(ownedBy owners: Set) { guard !owners.isEmpty else { return } diff --git a/apple/TablePro/Core/Sync/SyncCoordinator.swift b/apple/TablePro/Core/Sync/SyncCoordinator.swift index a1734725af..88320ed86d 100644 --- a/apple/TablePro/Core/Sync/SyncCoordinator.swift +++ b/apple/TablePro/Core/Sync/SyncCoordinator.swift @@ -11,7 +11,6 @@ import Foundation import os import TableProSyncTransport -/// Central coordinator for iCloud sync @MainActor final class SyncCoordinator: ObservableObject { static let shared = SyncCoordinator() @@ -22,7 +21,7 @@ final class SyncCoordinator: ObservableObject { let services: AppServices private let makeTransport: () -> any SyncTransport - /// Created on the first run, so no CloudKit container exists while sync stays off. + // Created on the first run, so no CloudKit container exists while sync stays off. private lazy var transport: any SyncTransport = makeTransport() let changeTracker: SyncChangeTracker private var connectionsCleanedOnPull: Set = [] @@ -31,7 +30,6 @@ final class SyncCoordinator: ObservableObject { let columnLayouts: () -> FileColumnLayoutPersister let themeCatalog: ThemeCatalog private let makeNetworkMonitor: () -> SyncNetworkMonitor? - /// Why the license keeps sync off, or nil while it allows sync. private let licenseGate: @MainActor () -> DisableReason? let recordCreationDate: (CKRecord) -> Date? private var triggers: SyncTriggers? @@ -41,25 +39,19 @@ final class SyncCoordinator: ObservableObject { private var hasStarted = false private var isRunning = false - /// Triggers that arrived while a run was in flight. One more run follows it rather than the - /// trigger cancelling it: a cancelled run reported itself as a failure. + // Queued rather than cancelling the run in flight: a cancelled run reported itself as a failure. private var pendingTriggers: [SyncTrigger] = [] - /// Upload Again pressed while an automatic check was running. The notice stays on screen during - /// that check, so the button does too, and the person's answer must not be lost to it. + // The notice and its button stay up during an automatic check, so a press then waits for the check to end. private var uploadAgainRequested = false - /// Failed runs in a row, which sets how far the next attempt is held back. private var consecutiveFailures = 0 - /// When the upload that failed may be tried again by a trigger that does not change the situation. private var nextAttempt: Date? - /// The end of the wait CloudKit named on its last throttle. No automatic run starts before it. private var throttledUntil: Date? - /// Bumped every time something other than a sync run decides the status, so a run that has been - /// suspended across the network can tell whether its outcome is still the current answer. + // Lets a run suspended across the network tell whether something else decided the status meanwhile. private var statusGeneration = 0 init( @@ -103,8 +95,7 @@ final class SyncCoordinator: ObservableObject { // MARK: - Lifecycle - /// Call from AppDelegate at launch. Only the license is observed while sync is off: it decides - /// whether sync can turn on, and the Welcome window shows a license that does not cover it. + // Only the license is observed while sync is off: it decides whether sync can turn on. func start() { guard !hasStarted else { return } hasStarted = true @@ -115,12 +106,10 @@ final class SyncCoordinator: ObservableObject { Task { await sync(.launch) } } - /// Called when the app comes to the foreground func syncIfNeeded() { requestSync(.activation) } - /// Sync Now: the person asked, so it runs whatever held the last attempt back. func syncNow() async { await sync(.userRequest) } @@ -130,7 +119,6 @@ final class SyncCoordinator: ObservableObject { Task { await sync(trigger) } } - /// Runs as much of a sync as `trigger` is admitted to, given how the last run ended. func sync(_ trigger: SyncTrigger) async { guard canSync() else { Self.logger.info("Sync skipped: not allowed for \(String(describing: trigger), privacy: .public)") @@ -157,9 +145,7 @@ final class SyncCoordinator: ObservableObject { let generation = statusGeneration isRunning = true - /// A standing condition stays on screen through the automatic runs that check on it, so the - /// notice does not blink away at every activation. Only the person's own request shows - /// progress over it. + // A standing error stays on screen through automatic runs, so the notice does not blink away at every activation. if previousError == nil || trigger == .userRequest { syncStatus = .syncing } @@ -188,14 +174,13 @@ final class SyncCoordinator: ObservableObject { case .unavailable(let failure): return CycleResult(admission: admission, failure: failure) case .ready(let isNewAccount): - /// A condition the last account was under says nothing about this one. + // A condition the last account was under says nothing about this one. if isNewAccount { admission = .full } } - /// Only a run that may upload creates the zone. A download looks for it, and finding it - /// again is how this Mac learns another device brought deleted data back. + // Only an upload creates the zone: a download finding it again is how this Mac learns another device brought the data back. if metadataStorage.zoneState.createsZone(in: admission) { do { try await transport.ensureZoneExists() @@ -216,8 +201,7 @@ final class SyncCoordinator: ObservableObject { failure = await refined(decisive) } let nextZone = zone.after(failure: failure, reachedZone: pullFailure == nil) - /// The old zone's records went with it. Kept, their system fields would make every later - /// save into a zone another device recreated fail as an unknown record. + // The removed zone's cached system fields would make every save into a recreated zone fail as an unknown record. if nextZone == .removed, zone != .removed { recordCache.removeAll() metadataStorage.deferredDeletions = [] @@ -227,9 +211,7 @@ final class SyncCoordinator: ObservableObject { return CycleResult(admission: admission, failure: failure) } - /// Reads the account before any request, so a signed-out or restricted account is reported as - /// itself rather than as whatever the first request fails with. Adopting its id is what keeps - /// another account's token, tombstones and cached records from being used against this one. + // Read before any request, so a signed-out or restricted account is reported as itself, not as the first request's failure. private func confirmAccount() async -> AccountCheck { do { if let blocker = SyncBlocker(accountStatus: try await transport.accountStatus()) { @@ -243,7 +225,6 @@ final class SyncCoordinator: ObservableObject { } private enum AccountCheck { - /// Whether the account differs from the one this Mac last synced with. case ready(isNewAccount: Bool) case unavailable(SyncStepFailure) } @@ -262,9 +243,7 @@ final class SyncCoordinator: ObservableObject { return true } - /// CloudKit fails an operation with `notAuthenticated` for every account state that is not - /// available, so a fresh status read says which one it is. Signed in by that read, the account - /// is not ready yet rather than signed out. + // CloudKit answers notAuthenticated for every account state that is not available, so a fresh status read says which one. private func refined(_ failure: SyncStepFailure) async -> SyncStepFailure { guard case .blocked(let blocker) = failure.failure, blocker.isAccountState else { return failure } let status = try? await transport.accountStatus() @@ -272,13 +251,7 @@ final class SyncCoordinator: ObservableObject { return SyncStepFailure(failure: .blocked(actual), error: .blocked(actual), retryAfter: failure.retryAfter) } - /// Publishes the outcome of a sync run, unless something decided the status while it was in - /// flight. - /// - /// A run reads `canSync()` once on entry and then suspends across the whole CloudKit round - /// trip, so turning sync off, or losing the license, used to be overwritten by the returning - /// run: the indicator went back to "Synced" for a sync that would now be refused. Whoever - /// decided last wins, and a stale run reports nothing. + // A returning run used to overwrite a Sync off or a lost license with "Synced", so a stale run reports nothing. private func finish(_ result: CycleResult, previousError: SyncError?, from generation: Int) { guard generation == statusGeneration else { Self.logger.info("Discarding a sync outcome the status moved on from") @@ -312,8 +285,6 @@ final class SyncCoordinator: ObservableObject { ) ?? 30 let until = Date().addingTimeInterval(wait) throttledUntil = until - /// The next attempt comes once the throttle is over, never inside it, and also when - /// nothing else was going to try again. if nextAttempt.map({ $0 < until }) ?? true { nextAttempt = until scheduleRetry(after: wait) @@ -352,11 +323,9 @@ final class SyncCoordinator: ObservableObject { retryTask = nil } - /// Runs once for whatever arrived during the last run, with the trigger that may do the most. private func runPendingTrigger() async { guard !pendingTriggers.isEmpty else { return } let error = syncStatus.error - /// The network coming back mid-run matters only if the run ended unable to reach iCloud. let triggers = pendingTriggers.filter { $0 != .networkRestored || error == .offline } pendingTriggers = [] let attempt = nextAttempt @@ -372,14 +341,13 @@ final class SyncCoordinator: ObservableObject { await sync(next) } - /// Called when user enables sync in settings func enableSync() { Self.logger.info("enableSync() called") // Clear token to force a full fetch on first sync after enabling metadataStorage.saveToken(nil) metadataStorage.deferredDeletions = [] - /// Turning sync on is the person's choice to upload, a removed zone included. + // Turning sync on is the person's choice to upload, a removed zone included. metadataStorage.zoneState = .unknown // Mark ALL existing local data as dirty so it gets pushed on first sync @@ -395,8 +363,7 @@ final class SyncCoordinator: ObservableObject { } } - /// The person's answer after TablePro's data was removed from iCloud: upload everything on this - /// Mac again. Never done without asking, which is Apple's guidance for a purged zone. + // Only on the person's word: Apple's guidance for a purged zone is never to upload again without asking. func uploadAgain() async { guard syncStatus.error == .blocked(.dataDeletedFromICloud) else { return } guard !isRunning else { @@ -409,15 +376,14 @@ final class SyncCoordinator: ObservableObject { recordCache.removeAll() markAllLocalDataDirty() await markSQLFavoritesDirty() - /// Sync may have been turned off while the favorites were read. + // Sync may have been turned off while the favorites were read. guard syncStatus.error == .blocked(.dataDeletedFromICloud) else { return } resetRetryState() decide(.idle) await sync(.userRequest) } - /// Marks existing SQL favorites and folders dirty. Separate from `markAllLocalDataDirty` - /// because the favorite store is an actor and must be read asynchronously. + // Separate from markAllLocalDataDirty because the favorite store is an actor and reads asynchronously. private func markSQLFavoritesDirty() async { let favorites = await services.sqlFavoriteManager.fetchFavorites() changeTracker.markDirty(.favorite, ids: favorites.map { $0.id.uuidString }) @@ -426,12 +392,7 @@ final class SyncCoordinator: ObservableObject { changeTracker.markDirty(.favoriteFolder, ids: folders.map { $0.id.uuidString }) } - /// Marks every synced record dirty so the first sync after enabling pushes the lot. - /// - /// Every type is marked as one batch. Marking record by record posted a change notification per - /// record, and an account with a few hundred saved column layouts built a chain of hundreds of - /// tasks each waiting on its predecessor until the app stopped responding to the switch that - /// started it. + // One batch per type: marking record by record posted a notification each and hung the app on a few hundred column layouts. private func markAllLocalDataDirty() { let connections = services.connectionStorage.loadConnections() changeTracker.markDirty( @@ -486,7 +447,6 @@ final class SyncCoordinator: ObservableObject { Self.logger.info("Marked all local data dirty: \(summary, privacy: .public)") } - /// Called when user disables sync in settings func disableSync() { decide(.disabled(.userDisabled)) } @@ -507,18 +467,13 @@ final class SyncCoordinator: ObservableObject { return } - /// Only a move out of disabled is decided here. An error stays until a run clears it: a - /// license or account notification is no evidence that storage was freed. + // An error stays until a run clears it: a license or account notification is no evidence that storage was freed. if case .disabled = syncStatus { decide(metadataStorage.zoneState.initialStatus) } } - /// Settles the status from outside a sync run, and retires whatever run is in flight. - /// - /// The branch that leaves a running sync alone deliberately does not come through here: it has - /// decided nothing, so invalidating the run would leave the status stuck on Syncing with - /// nothing left to move it. + // Retires the run in flight, so a path that decided nothing must not call it or the status sticks on Syncing. private func decide(_ status: SyncStatus) { statusGeneration += 1 syncStatus = status @@ -625,7 +580,7 @@ final class SyncCoordinator: ObservableObject { let itemFailure = SyncStepFailure(outcome) if let interruption { Self.logger.error("Push stopped part way: \(interruption.localizedDescription)") - /// A blocker an earlier batch hit explains the interruption too; anything less does not. + // A blocker an earlier batch hit explains the interruption too; anything less does not. let failure = itemFailure.flatMap { $0.failure.stopsUpload ? $0 : nil } ?? SyncStepFailure(interruption) return PushReport(echoGuard: echoGuard, failure: failure) } @@ -680,8 +635,7 @@ final class SyncCoordinator: ObservableObject { Self.logger.info("Reading record types the last pull skipped: cleared sync token for full fetch") } - /// Reports a failed download instead of passing over it: a run whose pull failed did not bring - /// this Mac up to date, and must not say Synced or move Last Synced. + // A failed pull is reported, never passed over: the run did not bring this Mac up to date and must not say Synced. private func performPull(echoGuard: SyncEchoGuard?) async -> SyncStepFailure? { // Applied with the categories recorded here, so one turned off during the download is still read once. let settings = services.appSettingsStorage.loadSync() @@ -752,9 +706,17 @@ final class SyncCoordinator: ObservableObject { deletedRecordIDs: result.deletedRecordIDs, newToken: result.newToken ) + let themeRecords = pulledThemeRecords( + admitted.changedRecords, + deletedRecordIDs: deletedRecordIDs, + unreadRecordIDs: result.unreadRecordIDs, + isFullFetch: isFullFetch, + echoGuard: echoGuard + ) let storesPersisted = applyRemoteChanges( admitted, deletedRecordIDs: deletedRecordIDs, + themeRecords: themeRecords, settings: settings, echoGuard: echoGuard ) @@ -763,11 +725,7 @@ final class SyncCoordinator: ObservableObject { echoGuard: echoGuard ) - /// The token and the cache are the record of what this device holds, so neither is - /// committed over a batch a store refused. Saving the token first acknowledged records that - /// were never written and the server never sent them again, and the cached record then - /// stood in as the merge base for an edit that had no local base at all. Not saving it - /// means the next pull replays the batch, which every apply here is written to survive. + // Neither token nor cache is committed over a refused batch: saving the token first lost records the server never sent again. guard storesPersisted, favoritesOutcome != .failed else { Self.logger.error("Pull not acknowledged: a store refused to persist part of the batch") return false @@ -783,6 +741,9 @@ final class SyncCoordinator: ObservableObject { && SyncRecordType(rawValue: record.recordType).map { settings.syncs($0) } == true }) recordCache.remove(result.deletedRecordIDs + deferred.due + missing) + if isFullFetch { + recordCache.remove(themeRecordsGone(afterFullFetch: result, zoneID: zoneID, pushed: pushed)) + } settleDeferredDeletions(deferred, after: result, settings: settings) Self.logger.info( @@ -817,11 +778,11 @@ final class SyncCoordinator: ObservableObject { // Performance: storage reads here (loadSync, loadConnections, loadGroups, etc.) run on // @MainActor and can block the UI on large sync batches. Consider moving to Task.detached // for large payloads. - /// Reports whether every record that can say so was persisted. A pull that answers false must - /// not commit its token: the batch has to arrive again. + // False when a record was not persisted, so the token is withheld and the batch arrives again. private func applyRemoteChanges( _ result: PullResult, deletedRecordIDs: [CKRecord.ID], + themeRecords: PulledThemeRecords, settings: SyncSettings, echoGuard: SyncEchoGuard? ) -> Bool { @@ -831,6 +792,7 @@ final class SyncCoordinator: ObservableObject { let effects = applyRemoteRecords( result.changedRecords, deletedRecordIDs: deletedRecordIDs, + themeRecords: themeRecords, settings: settings, echoGuard: echoGuard ) @@ -846,6 +808,7 @@ final class SyncCoordinator: ObservableObject { private func applyRemoteRecords( _ changedRecords: [CKRecord], deletedRecordIDs: [CKRecord.ID], + themeRecords: PulledThemeRecords, settings: SyncSettings, echoGuard: SyncEchoGuard? ) -> SyncRemoteDeletionEffects { @@ -909,7 +872,7 @@ final class SyncCoordinator: ObservableObject { persistenceFailed = true } case .settings: - if applyRemoteSettings(record, tombstoneIds: settingsTombstoneIds) == .failed { + if applyRemoteSettings(record, tombstoneIds: settingsTombstoneIds, themeRecords: themeRecords) == .failed { persistenceFailed = true } case .tableFavorite: @@ -935,9 +898,9 @@ final class SyncCoordinator: ObservableObject { groupsOrTagsChanged = groupsOrTagsChanged || effects.groupsOrTagsChanged effects.persistenceFailed = persistenceFailed || effects.persistenceFailed - /// After the batch, never per record: a pull carries no dependency order, so a legal - /// hierarchy change spread over two records passes through a state that reads as a cycle - /// until both have landed. + // After the batch, never per record: a pull carries no dependency order, so a legal + // hierarchy change spread over two records passes through a state that reads as a cycle + // until both have landed. if groupsOrTagsChanged { services.groupStorage.repairHierarchy() } @@ -1101,8 +1064,7 @@ final class SyncCoordinator: ObservableObject { return name } - /// False when the profile could not be persisted, which withholds the pull token so the record - /// arrives again rather than being acknowledged and lost. + // False withholds the pull token, so a profile this Mac failed to save arrives again. private func applyRemoteCredentialProfile(_ record: CKRecord, tombstoneIds: Set) -> Bool { let remoteProfile: CredentialProfile do { @@ -1117,18 +1079,14 @@ final class SyncCoordinator: ObservableObject { var profiles = services.credentialProfileStorage.loadProfiles() if let index = profiles.firstIndex(where: { $0.id == remoteProfile.id }) { - /// The password mode's payload never crosses the wire, so a `.source` this Mac already - /// holds is kept rather than replaced by the `prompt` the remote had to send instead. + // The password mode's payload never crosses the wire, so a local `.source` is kept over the `prompt` the remote sends instead. var merged = remoteProfile if case .source = profiles[index].passwordMode, remoteProfile.passwordMode == .prompt { merged.passwordMode = profiles[index].passwordMode } profiles[index] = merged } else { - /// Two Macs can hold same-named profiles with different ids, which is what happens when - /// someone recreated one by hand before they synced. The editor forbids duplicate names - /// and every picker offers a profile by name alone, so the arriving one is renamed - /// rather than landing as an indistinguishable second row. + // Pickers offer a profile by name alone, so a same-named profile from another Mac is renamed rather than shown twice. var arriving = remoteProfile if profiles.contains(where: { $0.name.compare(arriving.name, options: .caseInsensitive) == .orderedSame }) { arriving.name = Self.availableProfileName(basedOn: arriving.name, taken: profiles) @@ -1136,7 +1094,7 @@ final class SyncCoordinator: ObservableObject { profiles.append(arriving) } guard services.credentialProfileStorage.saveProfilesWithoutSync(profiles) else { return false } - /// The connections linked to it carry a copy of the username, which every raw reader uses. + // The connections linked to it carry a copy of the username, which every raw reader uses. services.credentialProfileStorage.writeUsernameThrough(remoteProfile, byUser: false) return true } @@ -1159,19 +1117,24 @@ final class SyncCoordinator: ObservableObject { profiles.append(remoteProfile) } guard services.sshProfileStorage.saveProfilesWithoutSync(profiles) else { return false } - /// A linked connection stores the profile's configuration, so an edit that arrives from - /// another Mac has to reach those connections too or they keep tunnelling to the old host. + // Linked connections store a copy of the profile, so without this they keep tunnelling to the old host. services.sshProfileStorage.refreshLinkedConnections(with: remoteProfile, byUser: false) return true } - private func applyRemoteSettings(_ record: CKRecord, tombstoneIds: Set) -> RemoteApplyOutcome { + private func applyRemoteSettings( + _ record: CKRecord, + tombstoneIds: Set, + themeRecords: PulledThemeRecords + ) -> RemoteApplyOutcome { guard let category = SyncRecordMapper.settingsCategory(from: record), !tombstoneIds.contains(category), let data = SyncRecordMapper.settingsData(from: record) else { return .skipped } if ThemeCatalog.themeId(forSyncCategory: category) != nil { - return themeCatalog.applyRemote(category: category, data: data, merge: remoteThemeMerge) + return themeCatalog.applyRemote(category: category, data: data, merge: remoteThemeMerge) { newer in + tombstoneIds.contains(newer) ? nil : newerThemeRecord(newer, in: themeRecords) + } } do { try applySettingsData(data, for: category) @@ -1205,8 +1168,7 @@ final class SyncCoordinator: ObservableObject { return entry } - /// Upserts rather than inserts. A database favorite carries a mutable payload, the environment - /// tag, so an insert-if-absent apply would keep the local tag and silently drop the remote one. + // Upserts: an insert-if-absent apply would keep the local environment tag and drop the remote one. private func applyRemoteDatabaseFavorite(_ record: CKRecord, tombstoneIds: Set) { let entry: FavoriteDatabaseEntry do { @@ -1224,8 +1186,6 @@ final class SyncCoordinator: ObservableObject { // MARK: - Observers - /// Runs on each turn from off to on. `decide` drops the triggers on the turn back, so nothing - /// listens while sync is off. private func arm() { guard triggers == nil else { return } triggers = SyncTriggers( @@ -1234,7 +1194,7 @@ final class SyncCoordinator: ObservableObject { networkMonitor: observeNetwork() ) - /// Empty local storage (a fresh install or a wipe) needs a full fetch: a delta returns nothing. + // Empty local storage (a fresh install or a wipe) needs a full fetch: a delta returns nothing. if services.connectionStorage.loadConnections().isEmpty { metadataStorage.saveToken(nil) Self.logger.info("No local connections: cleared sync token for full fetch") @@ -1244,8 +1204,7 @@ final class SyncCoordinator: ObservableObject { scheduleThemeSeedIfNeeded() } - /// The run that follows reads the account and adopts its id, so a sign-out, a switch to another - /// account, and an account becoming ready are all handled where every other run handles them. + // Only requests a run: every run reads the account first, so a sign-out or a switch is handled there. private func observeAccountChanges() -> any NSObjectProtocol { NotificationCenter.default.addObserver( forName: .CKAccountChanged, @@ -1258,8 +1217,7 @@ final class SyncCoordinator: ObservableObject { } } - /// A debounce that has elapsed is never cancelled: an edit made while its run is in flight queues - /// one more run instead, because cancelling the run mid-request reported a failure. + // An elapsed debounce is never cancelled: cancelling a run mid-request reported a failure, so an edit during it queues a run. private func observeLocalChanges() -> AnyCancellable { services.appEvents.syncChangeTracked .receive(on: RunLoop.main) @@ -1289,9 +1247,7 @@ final class SyncCoordinator: ObservableObject { } } - /// The path can come back while a run is still retrying, before that run reports it could not - /// reach iCloud, and the monitor reports the return only once. So a return during a run is kept - /// for when it ends. + // The monitor reports a return once, possibly before the run in flight reports offline, so it is kept for when the run ends. private func observeNetwork() -> SyncNetworkMonitor? { let monitor = makeNetworkMonitor() monitor?.start { [weak self] in @@ -1339,8 +1295,7 @@ private final class RemoteConnectionBatch { } } -/// What starts a run while sync is on. Releasing it detaches every observer; the network monitor -/// stops in its own deinit. +// Releasing it detaches every observer; the network monitor stops in its own deinit. private final class SyncTriggers { private let accountObserver: any NSObjectProtocol private let localChanges: AnyCancellable diff --git a/apple/TablePro/Core/Utilities/SQL/JsonRowConverter.swift b/apple/TablePro/Core/Utilities/SQL/JsonRowConverter.swift index efc41e9320..cab332bd6b 100644 --- a/apple/TablePro/Core/Utilities/SQL/JsonRowConverter.swift +++ b/apple/TablePro/Core/Utilities/SQL/JsonRowConverter.swift @@ -116,7 +116,6 @@ internal struct JsonRowConverter { return quotedEscaped(value) } - /// Checks whether a string conforms to JSON number grammar (RFC 8259 §6) private func isValidJsonNumber(_ value: String) -> Bool { let scalars = value.unicodeScalars var iter = scalars.makeIterator() @@ -173,7 +172,8 @@ internal struct JsonRowConverter { case "false", "0", "no", "off": return "false" default: - return quotedEscaped(value) + // MySQL's BOOLEAN is TINYINT(1), which also stores 2 or -1. + return formatInteger(value) } } diff --git a/apple/TablePro/Models/Settings/AppSettings.swift b/apple/TablePro/Models/Settings/AppSettings.swift index a18e4aeb1b..676f9b38c0 100644 --- a/apple/TablePro/Models/Settings/AppSettings.swift +++ b/apple/TablePro/Models/Settings/AppSettings.swift @@ -51,7 +51,6 @@ struct AppearanceSettings: Codable, Equatable { // MARK: - Data Grid Settings -/// Row height options for data grid enum DataGridRowHeight: Int, Codable, CaseIterable, Identifiable { case compact = 20 case normal = 24 @@ -109,7 +108,6 @@ enum RowCopyFormat: String, Codable, CaseIterable, Identifiable { } } -/// Data grid settings struct DataGridSettings: Codable, Equatable { var rowHeight: DataGridRowHeight var dateFormat: DateFormatOption @@ -208,25 +206,22 @@ struct DataGridSettings: Codable, Equatable { // MARK: - Validated Properties - /// Validated and sanitized nullDisplay (max 20 chars, no newlines) var validatedNullDisplay: String { let sanitized = nullDisplay.sanitized let maxLength = SettingsValidationRules.nullDisplayMaxLength if sanitized.isEmpty { - return "NULL" // Fallback to default + return "NULL" } else if sanitized.count > maxLength { return String(sanitized.prefix(maxLength)) } return sanitized } - /// Validated defaultPageSize (10 to 100,000) var validatedDefaultPageSize: Int { defaultPageSize.clamped(to: SettingsValidationRules.defaultPageSizeRange) } - /// Validation error for nullDisplay (for UI feedback) var nullDisplayValidationError: String? { let sanitized = nullDisplay.sanitized let maxLength = SettingsValidationRules.nullDisplayMaxLength @@ -241,7 +236,6 @@ struct DataGridSettings: Codable, Equatable { return nil } - /// Validation error for defaultPageSize (for UI feedback) var defaultPageSizeValidationError: String? { let range = SettingsValidationRules.defaultPageSizeRange if defaultPageSize < range.lowerBound || defaultPageSize > range.upperBound { @@ -250,13 +244,11 @@ struct DataGridSettings: Codable, Equatable { return nil } - /// Validated queryResultRowCap (100 to 500,000; 0 means unlimited) var validatedQueryResultRowCap: Int { if queryResultRowCap == 0 { return 0 } return queryResultRowCap.clamped(to: SettingsValidationRules.queryResultRowCapRange) } - /// Validation error for queryResultRowCap (for UI feedback) var queryResultRowCapValidationError: String? { let range = SettingsValidationRules.queryResultRowCapRange if queryResultRowCap != 0 && (queryResultRowCap < range.lowerBound || queryResultRowCap > range.upperBound) { @@ -270,18 +262,31 @@ struct DataGridSettings: Codable, Equatable { } } +// MARK: - Value Color Settings + +struct ValueColorSettings: Codable, Equatable { + var colorsValuesByType: Bool + + static let `default` = ValueColorSettings() + + init(colorsValuesByType: Bool = false) { + self.colorsValuesByType = colorsValuesByType + } + + init(from decoder: Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + colorsValuesByType = container.decodeSetting(.colorsValuesByType, default: Self.default.colorsValuesByType) + } +} + // MARK: - History Settings -/// History settings struct HistorySettings: Codable, Equatable { var maxEntries: Int // 0 = unlimited var maxDays: Int // 0 = unlimited var autoCleanup: Bool - /// Keep what rows looked like before each save, so a committed save can be restored. - /// - /// This is the only setting in the app that governs storing real row values on disk, which is - /// why it is a switch rather than an assumption. + // The only setting that governs storing real row values on disk, so it is a switch rather than an assumption. var keepRewindHistory: Bool static let `default` = HistorySettings( @@ -308,17 +313,14 @@ struct HistorySettings: Codable, Equatable { // MARK: - Validated Properties - /// Validated maxEntries (>= 0) var validatedMaxEntries: Int { max(0, maxEntries) } - /// Validated maxDays (>= 0) var validatedMaxDays: Int { max(0, maxDays) } - /// Validation error for maxEntries var maxEntriesValidationError: String? { if maxEntries < 0 { return String(localized: "Maximum entries cannot be negative") @@ -326,7 +328,6 @@ struct HistorySettings: Codable, Equatable { return nil } - /// Validation error for maxDays var maxDaysValidationError: String? { if maxDays < 0 { return String(localized: "Maximum days cannot be negative") @@ -337,11 +338,9 @@ struct HistorySettings: Codable, Equatable { // MARK: - Tab Settings -/// Tab behavior settings struct TabSettings: Codable, Equatable { var enablePreviewTabs: Bool = true - /// What the strip does once the tabs stop fitting. `scroll` is the system's answer and the - /// default: every tab bar Apple ships keeps one row and scrolls it. + // `scroll` is the system's answer: every tab bar Apple ships keeps one row and scrolls it. var overflow: EditorTabStripOverflow = .scroll static let `default` = TabSettings() diff --git a/apple/TablePro/Models/Settings/AppSettingsCategory.swift b/apple/TablePro/Models/Settings/AppSettingsCategory.swift index eaab17b259..89aa8b3a76 100644 --- a/apple/TablePro/Models/Settings/AppSettingsCategory.swift +++ b/apple/TablePro/Models/Settings/AppSettingsCategory.swift @@ -20,6 +20,8 @@ internal enum AppSettingsCategory { internal static let notifications = "notifications" // Not a field of `editor`: an older build re-uploads that blob without it, which would reset it here. internal static let formatter = "formatter" + // Not a field of `dataGrid`, for the same reason. + internal static let valueColors = "valueColors" internal static let appearanceMode = "appearanceMode" // `sync` holds the switch that turns syncing on, `mcp` a port and server setup for one machine, @@ -27,6 +29,6 @@ internal enum AppSettingsCategory { internal static let deviceLocal: Set = ["sync", "mcp", appearanceMode] internal static let synced: [String] = [ - general, appearance, editor, dataGrid, history, tabs, keyboard, ai, notifications, formatter + general, appearance, editor, dataGrid, history, tabs, keyboard, ai, notifications, formatter, valueColors ] } diff --git a/apple/TablePro/Models/UI/DataGridConfiguration.swift b/apple/TablePro/Models/UI/DataGridConfiguration.swift index 3f05e7c2b8..f718a776be 100644 --- a/apple/TablePro/Models/UI/DataGridConfiguration.swift +++ b/apple/TablePro/Models/UI/DataGridConfiguration.swift @@ -24,6 +24,12 @@ struct DataGridConfiguration: Equatable { // A result's rows, not a list of columns: the Data Grid sort preferences apply. var appliesRowSortPreferences: Bool = false + // Values typed by the engine draw in their type's theme color; a list of definitions or untyped text stays plain. + var colorsValuesByType: Bool = false + + // The theme editor shows a theme's value colors even while Color values by type is off. + var overridesValueColorSetting: Bool = false + // Off on the Structure grids, whose Copy writes definitions a paste reads back. var appliesRowCopyFormat: Bool = false diff --git a/apple/TablePro/Resources/Localizable.xcstrings b/apple/TablePro/Resources/Localizable.xcstrings index 00059b6aa3..aebc2250db 100644 --- a/apple/TablePro/Resources/Localizable.xcstrings +++ b/apple/TablePro/Resources/Localizable.xcstrings @@ -7016,6 +7016,9 @@ }, "%1$@ column of %2$@" : { + }, + "%1$@ contrast is %2$@:1. Aim for at least 4.5:1." : { + }, "%1$@ could not be read again before generating the script. %2$@" : { "localizations" : { @@ -31805,6 +31808,10 @@ }, "Billing…" : { + }, + "Binary" : { + "extractionState" : "manual", + "generatesSymbol" : false }, "Binary content" : { "localizations" : { @@ -43892,6 +43899,9 @@ } } } + }, + "Color values by type" : { + }, "Color, omitted when none" : { "localizations" : { @@ -58469,6 +58479,10 @@ "extractionState" : "manual", "generatesSymbol" : false }, + "Data Grid Binary" : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "Data Grid Bool False" : { "extractionState" : "manual", "generatesSymbol" : false @@ -58477,6 +58491,10 @@ "extractionState" : "manual", "generatesSymbol" : false }, + "Data Grid Date & Time" : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "Data Grid Deleted" : { "extractionState" : "manual", "generatesSymbol" : false @@ -58485,6 +58503,10 @@ "extractionState" : "manual", "generatesSymbol" : false }, + "Data Grid Enum" : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "Data Grid Focus Border" : { "extractionState" : "manual", "generatesSymbol" : false @@ -58546,6 +58568,10 @@ "extractionState" : "manual", "generatesSymbol" : false }, + "Data Grid JSON" : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "Data Grid Line" : { "extractionState" : "manual", "generatesSymbol" : false @@ -58558,6 +58584,10 @@ "extractionState" : "manual", "generatesSymbol" : false }, + "Data Grid Number" : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "Data Grid Row Number" : { "extractionState" : "manual", "generatesSymbol" : false @@ -58570,10 +58600,18 @@ "extractionState" : "manual", "generatesSymbol" : false }, + "Data Grid String" : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "Data Grid Text" : { "extractionState" : "manual", "generatesSymbol" : false }, + "Data Grid UUID" : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "Data loss" : { "localizations" : { "fr" : { @@ -60758,6 +60796,10 @@ } } }, + "Date & Time" : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "Date and Time" : { }, @@ -67280,6 +67322,9 @@ } } } + }, + "Draw strings, numbers, dates and other typed values in the colors of the theme's Values section" : { + }, "Drawing %1$d shapes in SRID %2$d." : { "localizations" : { @@ -73528,6 +73573,10 @@ } } }, + "Enum" : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "Enum %@ has no symbols." : { }, @@ -75494,6 +75543,9 @@ } } } + }, + "Except NULL and booleans, these colors show in the grid only while Color values by type is on in Settings > Data Grid." : { + }, "exclamation mark" : { "extractionState" : "manual", @@ -196280,10 +196332,18 @@ } } }, + "Theme format %1$d is no longer supported. This version of TablePro reads format %2$d and later." : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "Theme format %1$d is no longer supported. This version of TablePro reads format %2$d." : { "extractionState" : "manual", "generatesSymbol" : false }, + "Theme format %1$d was made for a newer TablePro. This version reads format %2$d and earlier." : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "Theme format %1$d was made for a newer TablePro. This version reads format %2$d." : { "extractionState" : "manual", "generatesSymbol" : false @@ -196784,6 +196844,10 @@ }, "These statements ran inside the transaction already open on this connection, so rolling it back undoes them:" : { + }, + "These theme keys need theme format %1$d: %2$@." : { + "extractionState" : "manual", + "generatesSymbol" : false }, "These two engines cannot share a script." : { "localizations" : { @@ -212269,6 +212333,8 @@ } }, "UUID" : { + "extractionState" : "manual", + "generatesSymbol" : false, "localizations" : { "fr" : { "stringUnit" : { @@ -213194,6 +213260,10 @@ } } }, + "Values" : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "Values from %d loaded rows" : { "extractionState" : "stale", "localizations" : { diff --git a/apple/TablePro/Resources/Themes/tablepro.catppuccin-latte.json b/apple/TablePro/Resources/Themes/tablepro.catppuccin-latte.json index 8cf7be7217..38c7421092 100644 --- a/apple/TablePro/Resources/Themes/tablepro.catppuccin-latte.json +++ b/apple/TablePro/Resources/Themes/tablepro.catppuccin-latte.json @@ -4,22 +4,29 @@ "dataGrid": { "alternateRow": "#E6E9EF", "background": "#EFF1F5", + "binary": "#C81F9B", "boolFalse": "#D20F39", - "boolTrue": "#40A02B", + "boolTrue": "#327D22", + "dateTime": "#1962F5", "deleted": "#D20F3926", "deletedText": "#D20F3980", + "enum": "#8839EF", "focusBorder": "#8839EF", "gridLine": "#BCC0CC", "headerBackground": "#E6E9EF", "headerText": "#4C4F69", "inactiveSelection": "#CCD0DA", "inserted": "#40A02B26", + "json": "#996214", "modified": "#DF8E1D26", "nullValue": "#7C7F93", + "number": "#BF4701", "rowNumber": "#8C8FA1", "selection": "#CCD0DA", "selectionText": "#4C4F69", - "text": "#4C4F69" + "string": "#327D22", + "text": "#4C4F69", + "uuid": "#137A80" }, "editor": { "background": "#EFF1F5", @@ -43,7 +50,7 @@ }, "id": "tablepro.catppuccin-latte", "name": "Catppuccin Latte", - "schema": 3, + "schema": 4, "window": { "background": "#EFF1F5" } diff --git a/apple/TablePro/Resources/Themes/tablepro.catppuccin-mocha.json b/apple/TablePro/Resources/Themes/tablepro.catppuccin-mocha.json index 2ce5a2d0e8..a7f718a134 100644 --- a/apple/TablePro/Resources/Themes/tablepro.catppuccin-mocha.json +++ b/apple/TablePro/Resources/Themes/tablepro.catppuccin-mocha.json @@ -4,22 +4,29 @@ "dataGrid": { "alternateRow": "#181825", "background": "#1E1E2E", + "binary": "#F5C2E7", "boolFalse": "#F38BA8", "boolTrue": "#A6E3A1", + "dateTime": "#89B4FA", "deleted": "#F38BA826", "deletedText": "#F38BA880", + "enum": "#CBA6F7", "focusBorder": "#CBA6F7", "gridLine": "#45475A", "headerBackground": "#181825", "headerText": "#CDD6F4", "inactiveSelection": "#313244", "inserted": "#A6E3A126", + "json": "#F9E2AF", "modified": "#F9E2AF26", "nullValue": "#9399B2", + "number": "#FAB387", "rowNumber": "#7F849C", "selection": "#313244", "selectionText": "#CDD6F4", - "text": "#CDD6F4" + "string": "#A6E3A1", + "text": "#CDD6F4", + "uuid": "#94E2D5" }, "editor": { "background": "#1E1E2E", @@ -43,7 +50,7 @@ }, "id": "tablepro.catppuccin-mocha", "name": "Catppuccin Mocha", - "schema": 3, + "schema": 4, "window": { "background": "#1E1E2E" } diff --git a/apple/TablePro/Resources/Themes/tablepro.dracula.json b/apple/TablePro/Resources/Themes/tablepro.dracula.json index 94a1575329..06f10ce5f1 100644 --- a/apple/TablePro/Resources/Themes/tablepro.dracula.json +++ b/apple/TablePro/Resources/Themes/tablepro.dracula.json @@ -4,22 +4,29 @@ "dataGrid": { "alternateRow": "#21222C", "background": "#282A36", + "binary": "#BD93F9", "boolFalse": "#FF5555", "boolTrue": "#50FA7B", + "dateTime": "#8BE9FD", "deleted": "#FF555526", "deletedText": "#FF555580", + "enum": "#F1FA8C", "focusBorder": "#BD93F9", "gridLine": "#44475A", "headerBackground": "#21222C", "headerText": "#F8F8F2", "inactiveSelection": "#44475A80", "inserted": "#50FA7B26", + "json": "#FFB86C", "modified": "#FFB86C4D", "nullValue": "#6272A4", + "number": "#BD93F9", "rowNumber": "#6272A4", "selection": "#44475A", "selectionText": "#F8F8F2", - "text": "#F8F8F2" + "string": "#F1FA8C", + "text": "#F8F8F2", + "uuid": "#FF79C6" }, "editor": { "background": "#282A36", @@ -43,7 +50,7 @@ }, "id": "tablepro.dracula", "name": "Dracula", - "schema": 3, + "schema": 4, "window": { "background": "#282A36" } diff --git a/apple/TablePro/Resources/Themes/tablepro.github-dark.json b/apple/TablePro/Resources/Themes/tablepro.github-dark.json index cb921d592e..38a83ac134 100644 --- a/apple/TablePro/Resources/Themes/tablepro.github-dark.json +++ b/apple/TablePro/Resources/Themes/tablepro.github-dark.json @@ -4,22 +4,29 @@ "dataGrid": { "alternateRow": "#161B22", "background": "#0D1117", + "binary": "#FF9BCE", "boolFalse": "#F85149", "boolTrue": "#3FB950", + "dateTime": "#D2A8FF", "deleted": "#F8514926", "deletedText": "#F8514980", + "enum": "#E3B341", "focusBorder": "#1F6FEB", "gridLine": "#30363D", "headerBackground": "#161B22", "headerText": "#E6EDF3", "inactiveSelection": "#343941", "inserted": "#2EA04326", + "json": "#FFA657", "modified": "#BB800926", "nullValue": "#8B949E", + "number": "#79C0FF", "rowNumber": "#6E7681", "selection": "#142744", "selectionText": "#E6EDF3", - "text": "#E6EDF3" + "string": "#A5D6FF", + "text": "#E6EDF3", + "uuid": "#FFA28B" }, "editor": { "background": "#0D1117", @@ -43,7 +50,7 @@ }, "id": "tablepro.github-dark", "name": "GitHub Dark", - "schema": 3, + "schema": 4, "window": { "background": "#0D1117" } diff --git a/apple/TablePro/Resources/Themes/tablepro.github-light.json b/apple/TablePro/Resources/Themes/tablepro.github-light.json index b18d295e08..f9184cde5d 100644 --- a/apple/TablePro/Resources/Themes/tablepro.github-light.json +++ b/apple/TablePro/Resources/Themes/tablepro.github-light.json @@ -4,22 +4,29 @@ "dataGrid": { "alternateRow": "#F6F8FA", "background": "#FFFFFF", + "binary": "#BF3989", "boolFalse": "#CF222E", "boolTrue": "#1A7F37", + "dateTime": "#8250DF", "deleted": "#FF818226", "deletedText": "#CF222E80", + "enum": "#9A6700", "focusBorder": "#0969DA", "gridLine": "#D0D7DE", "headerBackground": "#F6F8FA", "headerText": "#1F2328", "inactiveSelection": "#EFF1F3", "inserted": "#4AC26B26", + "json": "#953800", "modified": "#D4A72C26", "nullValue": "#6E7781", + "number": "#0550AE", "rowNumber": "#8C959F", "selection": "#CEE1F8", "selectionText": "#1F2328", - "text": "#1F2328" + "string": "#0A3069", + "text": "#1F2328", + "uuid": "#0072F0" }, "editor": { "background": "#FFFFFF", @@ -43,7 +50,7 @@ }, "id": "tablepro.github-light", "name": "GitHub Light", - "schema": 3, + "schema": 4, "window": { "background": "#FFFFFF" } diff --git a/apple/TablePro/Resources/Themes/tablepro.gruvbox-dark.json b/apple/TablePro/Resources/Themes/tablepro.gruvbox-dark.json index 7cb0d25f12..698e7abafd 100644 --- a/apple/TablePro/Resources/Themes/tablepro.gruvbox-dark.json +++ b/apple/TablePro/Resources/Themes/tablepro.gruvbox-dark.json @@ -4,22 +4,29 @@ "dataGrid": { "alternateRow": "#32302F", "background": "#282828", - "boolFalse": "#FB4934", + "binary": "#D3869B", + "boolFalse": "#FB533F", "boolTrue": "#B8BB26", + "dateTime": "#83A598", "deleted": "#FB493426", "deletedText": "#FB493480", + "enum": "#FE8019", "focusBorder": "#689D6A", "gridLine": "#504945", "headerBackground": "#32302F", "headerText": "#EBDBB2", "inactiveSelection": "#3C3836", "inserted": "#B8BB2626", + "json": "#FABD2F", "modified": "#D7992126", "nullValue": "#928374", + "number": "#D3869B", "rowNumber": "#665C54", "selection": "#384539", "selectionText": "#EBDBB2", - "text": "#EBDBB2" + "string": "#B8BB26", + "text": "#EBDBB2", + "uuid": "#8EC07C" }, "editor": { "background": "#282828", @@ -43,7 +50,7 @@ }, "id": "tablepro.gruvbox-dark", "name": "Gruvbox Dark", - "schema": 3, + "schema": 4, "window": { "background": "#282828" } diff --git a/apple/TablePro/Resources/Themes/tablepro.gruvbox-light.json b/apple/TablePro/Resources/Themes/tablepro.gruvbox-light.json index 2ef6cd9cf9..19e060142f 100644 --- a/apple/TablePro/Resources/Themes/tablepro.gruvbox-light.json +++ b/apple/TablePro/Resources/Themes/tablepro.gruvbox-light.json @@ -4,22 +4,29 @@ "dataGrid": { "alternateRow": "#F2E5BC", "background": "#FBF1C7", + "binary": "#8F3F71", "boolFalse": "#9D0006", - "boolTrue": "#79740E", + "boolTrue": "#75700E", + "dateTime": "#076678", "deleted": "#9D000626", "deletedText": "#9D000680", + "enum": "#B64F0C", "focusBorder": "#427B58", "gridLine": "#D5C4A1", "headerBackground": "#F2E5BC", "headerText": "#3C3836", "inactiveSelection": "#EBDBB2", "inserted": "#79740E26", + "json": "#976311", "modified": "#D7992126", "nullValue": "#928374", + "number": "#8F3F71", "rowNumber": "#BDAE93", "selection": "#D6DCB0", "selectionText": "#3C3836", - "text": "#3C3836" + "string": "#75700E", + "text": "#3C3836", + "uuid": "#417957" }, "editor": { "background": "#FBF1C7", @@ -43,7 +50,7 @@ }, "id": "tablepro.gruvbox-light", "name": "Gruvbox Light", - "schema": 3, + "schema": 4, "window": { "background": "#FBF1C7" } diff --git a/apple/TablePro/Resources/Themes/tablepro.nord.json b/apple/TablePro/Resources/Themes/tablepro.nord.json index a2b30e1b3b..9142a2e453 100644 --- a/apple/TablePro/Resources/Themes/tablepro.nord.json +++ b/apple/TablePro/Resources/Themes/tablepro.nord.json @@ -4,22 +4,29 @@ "dataGrid": { "alternateRow": "#3B4252", "background": "#2E3440", - "boolFalse": "#BF616A", + "binary": "#B590AF", + "boolFalse": "#CF888F", "boolTrue": "#A3BE8C", + "dateTime": "#81A1C1", "deleted": "#BF616A26", "deletedText": "#BF616A80", + "enum": "#D18A73", "focusBorder": "#88C0D0", "gridLine": "#434C5E", "headerBackground": "#3B4252", "headerText": "#D8DEE9", "inactiveSelection": "#434C5E80", "inserted": "#A3BE8C26", + "json": "#EBCB8B", "modified": "#EBCB8B4D", "nullValue": "#4C566A", + "number": "#B590AF", "rowNumber": "#4C566A", "selection": "#434C5E", "selectionText": "#ECEFF4", - "text": "#D8DEE9" + "string": "#A3BE8C", + "text": "#D8DEE9", + "uuid": "#8FBCBB" }, "editor": { "background": "#2E3440", @@ -43,7 +50,7 @@ }, "id": "tablepro.nord", "name": "Nord", - "schema": 3, + "schema": 4, "window": { "background": "#2E3440" } diff --git a/apple/TablePro/Resources/Themes/tablepro.one-dark.json b/apple/TablePro/Resources/Themes/tablepro.one-dark.json index f9098d550a..b5756fca83 100644 --- a/apple/TablePro/Resources/Themes/tablepro.one-dark.json +++ b/apple/TablePro/Resources/Themes/tablepro.one-dark.json @@ -4,22 +4,29 @@ "dataGrid": { "alternateRow": "#21252B", "background": "#282C34", - "boolFalse": "#E06C75", + "binary": "#D19A66", + "boolFalse": "#E17079", "boolTrue": "#98C379", + "dateTime": "#61AFEF", "deleted": "#E0525226", "deletedText": "#E06C7580", + "enum": "#C678DD", "focusBorder": "#528BFF", "gridLine": "#3E4452", "headerBackground": "#21252B", "headerText": "#ABB2BF", "inactiveSelection": "#323842", "inserted": "#43D08A26", + "json": "#E5C07B", "modified": "#E0C28526", "nullValue": "#7F848E", + "number": "#D19A66", "rowNumber": "#636D83", "selection": "#3E4451", "selectionText": "#D7DAE0", - "text": "#ABB2BF" + "string": "#98C379", + "text": "#ABB2BF", + "uuid": "#56B6C2" }, "editor": { "background": "#282C34", @@ -43,7 +50,7 @@ }, "id": "tablepro.one-dark", "name": "One Dark", - "schema": 3, + "schema": 4, "window": { "background": "#282C34" } diff --git a/apple/TablePro/Resources/Themes/tablepro.one-light.json b/apple/TablePro/Resources/Themes/tablepro.one-light.json index 0af9b52720..8a2e370763 100644 --- a/apple/TablePro/Resources/Themes/tablepro.one-light.json +++ b/apple/TablePro/Resources/Themes/tablepro.one-light.json @@ -4,22 +4,29 @@ "dataGrid": { "alternateRow": "#EAEAEB", "background": "#FAFAFA", - "boolFalse": "#E45649", - "boolTrue": "#50A14F", + "binary": "#986801", + "boolFalse": "#DB3021", + "boolTrue": "#40813F", + "dateTime": "#2D6AF1", "deleted": "#FF141426", "deletedText": "#E4564980", + "enum": "#A626A4", "focusBorder": "#526FFF", "gridLine": "#DBDBDC", "headerBackground": "#EAEAEB", "headerText": "#383A42", "inactiveSelection": "#DBDBDC", "inserted": "#2DB44826", + "json": "#CA1243", "modified": "#F2A60D26", "nullValue": "#A0A1A7", + "number": "#986801", "rowNumber": "#9D9D9F", "selection": "#DBDBDC", "selectionText": "#232324", - "text": "#383A42" + "string": "#40813F", + "text": "#383A42", + "uuid": "#017BB0" }, "editor": { "background": "#FAFAFA", @@ -43,7 +50,7 @@ }, "id": "tablepro.one-light", "name": "One Light", - "schema": 3, + "schema": 4, "window": { "background": "#FAFAFA" } diff --git a/apple/TablePro/Resources/Themes/tablepro.solarized-dark.json b/apple/TablePro/Resources/Themes/tablepro.solarized-dark.json index 7f7374fcab..2243227031 100644 --- a/apple/TablePro/Resources/Themes/tablepro.solarized-dark.json +++ b/apple/TablePro/Resources/Themes/tablepro.solarized-dark.json @@ -4,22 +4,29 @@ "dataGrid": { "alternateRow": "#00212B", "background": "#002B36", - "boolFalse": "#DC322F", + "binary": "#DD629D", + "boolFalse": "#E56462", "boolTrue": "#859900", + "dateTime": "#2F93D9", "deleted": "#DC322F26", "deletedText": "#DC322F80", + "enum": "#8387CD", "focusBorder": "#2AA198", "gridLine": "#23464F", "headerBackground": "#00212B", "headerText": "#93A1A1", "inactiveSelection": "#003846", "inserted": "#85990026", + "json": "#B58900", "modified": "#B5890026", "nullValue": "#586E75", + "number": "#DD629D", "rowNumber": "#586E75", "selection": "#005A6F", "selectionText": "#EEE8D5", - "text": "#839496" + "string": "#2AA198", + "text": "#839496", + "uuid": "#E8652E" }, "editor": { "background": "#002B36", @@ -43,7 +50,7 @@ }, "id": "tablepro.solarized-dark", "name": "Solarized Dark", - "schema": 3, + "schema": 4, "window": { "background": "#002B36" } diff --git a/apple/TablePro/Resources/Themes/tablepro.solarized-light.json b/apple/TablePro/Resources/Themes/tablepro.solarized-light.json index 9ff51235cf..c09a5f5fe2 100644 --- a/apple/TablePro/Resources/Themes/tablepro.solarized-light.json +++ b/apple/TablePro/Resources/Themes/tablepro.solarized-light.json @@ -4,22 +4,29 @@ "dataGrid": { "alternateRow": "#EEE8D5", "background": "#FDF6E3", - "boolFalse": "#DC322F", - "boolTrue": "#859900", + "binary": "#CF2D7C", + "boolFalse": "#DA2925", + "boolTrue": "#697800", + "dateTime": "#2076B3", "deleted": "#DC322F26", "deletedText": "#DC322F80", + "enum": "#6369C0", "focusBorder": "#268BD2", "gridLine": "#DDD6C1", "headerBackground": "#EEE8D5", "headerText": "#586E75", "inactiveSelection": "#EEE0B5", "inserted": "#85990026", + "json": "#8F6C00", "modified": "#B5890026", "nullValue": "#93A1A1", + "number": "#CF2D7C", "rowNumber": "#93A1A1", "selection": "#DFCA88", "selectionText": "#073642", - "text": "#657B83" + "string": "#217E77", + "text": "#657B83", + "uuid": "#C44815" }, "editor": { "background": "#FDF6E3", @@ -43,7 +50,7 @@ }, "id": "tablepro.solarized-light", "name": "Solarized Light", - "schema": 3, + "schema": 4, "window": { "background": "#FDF6E3" } diff --git a/apple/TablePro/Resources/Themes/tablepro.tokyo-night-light.json b/apple/TablePro/Resources/Themes/tablepro.tokyo-night-light.json index 02342393c3..c531cab1f0 100644 --- a/apple/TablePro/Resources/Themes/tablepro.tokyo-night-light.json +++ b/apple/TablePro/Resources/Themes/tablepro.tokyo-night-light.json @@ -4,22 +4,29 @@ "dataGrid": { "alternateRow": "#D6D8DF", "background": "#E6E7ED", + "binary": "#0F4B6E", "boolFalse": "#8C4351", "boolTrue": "#385F0D", + "dateTime": "#2959AA", "deleted": "#E8686826", "deletedText": "#8C435180", + "enum": "#65359D", "focusBorder": "#2959AA", "gridLine": "#C1C2C7", "headerBackground": "#D6D8DF", "headerText": "#343B59", "inactiveSelection": "#C9CBD6", "inserted": "#2D9C9126", + "json": "#8F5E15", "modified": "#8F5E1526", "nullValue": "#888B94", + "number": "#965027", "rowNumber": "#9DA0AB", "selection": "#ACB0BF", "selectionText": "#343B59", - "text": "#343B59" + "string": "#385F0D", + "text": "#343B59", + "uuid": "#33635C" }, "editor": { "background": "#E6E7ED", @@ -43,7 +50,7 @@ }, "id": "tablepro.tokyo-night-light", "name": "Tokyo Night Light", - "schema": 3, + "schema": 4, "window": { "background": "#E6E7ED" } diff --git a/apple/TablePro/Resources/Themes/tablepro.tokyo-night.json b/apple/TablePro/Resources/Themes/tablepro.tokyo-night.json index 3fa07811f3..0c9b22d8b7 100644 --- a/apple/TablePro/Resources/Themes/tablepro.tokyo-night.json +++ b/apple/TablePro/Resources/Themes/tablepro.tokyo-night.json @@ -4,22 +4,29 @@ "dataGrid": { "alternateRow": "#16161E", "background": "#1A1B26", + "binary": "#7DCFFF", "boolFalse": "#F7768E", "boolTrue": "#9ECE6A", + "dateTime": "#7AA2F7", "deleted": "#DB4B4B26", "deletedText": "#F7768E80", + "enum": "#BB9AF7", "focusBorder": "#7AA2F7", "gridLine": "#232433", "headerBackground": "#16161E", "headerText": "#A9B1D6", "inactiveSelection": "#222433", "inserted": "#41A6B526", + "json": "#E0AF68", "modified": "#E0AF6826", "nullValue": "#51597D", + "number": "#FF9E64", "rowNumber": "#363B54", "selection": "#2B2F41", "selectionText": "#A9B1D6", - "text": "#A9B1D6" + "string": "#9ECE6A", + "text": "#A9B1D6", + "uuid": "#73DACA" }, "editor": { "background": "#1A1B26", @@ -43,7 +50,7 @@ }, "id": "tablepro.tokyo-night", "name": "Tokyo Night", - "schema": 3, + "schema": 4, "window": { "background": "#1A1B26" } diff --git a/apple/TablePro/Theme/ThemeCatalog.swift b/apple/TablePro/Theme/ThemeCatalog.swift index e53c4be15e..fe1b960b03 100644 --- a/apple/TablePro/Theme/ThemeCatalog.swift +++ b/apple/TablePro/Theme/ThemeCatalog.swift @@ -13,8 +13,8 @@ internal struct ThemeCatalogContents: Equatable, Sendable { internal static let empty = ThemeCatalogContents(themes: BuiltInThemes.all, rejected: [], userFiles: [:]) } -/// Where a user theme was read from and the bytes read, so a save or a move to the Trash can tell -/// that the file changed on disk since and leave the newer version alone. +// Where a user theme was read from and the bytes read, so a save or a move to the Trash can tell +// that the file changed on disk since and leave the newer version alone. internal struct ThemeFile: Equatable, Sendable { internal let url: URL internal let contents: Data @@ -33,6 +33,8 @@ internal struct ThemeTrashReceipt: Equatable, Sendable { internal let themeId: String? internal let originalURL: URL internal let trashedURL: URL + // Only the deletions the trash queued: one queued before it, such as a cleared value color, stays. + internal var tombstones: [String] = [] } internal struct ThemeFileTrash: Sendable { @@ -62,11 +64,11 @@ internal struct ThemeFileTrash: Sendable { } } -/// Owns the list. A user theme is identified by its JSON `id` but lives in whatever file it was -/// loaded from, so a save, rename or move to the Trash acts on that file rather than on `.json`; -/// the two differ for any file the user named themselves. Every mutation updates the in-memory list -/// synchronously, and the list reloads whenever TablePro becomes active, so a theme fixed, dropped -/// or removed in Finder is picked up without a view asking. +// Owns the list. A user theme is identified by its JSON `id` but lives in whatever file it was +// loaded from, so a save, rename or move to the Trash acts on that file rather than on `.json`; +// the two differ for any file the user named themselves. Every mutation updates the in-memory list +// synchronously, and the list reloads whenever TablePro becomes active, so a theme fixed, dropped +// or removed in Finder is picked up without a view asking. @MainActor internal final class ThemeCatalog: ObservableObject { internal static let shared = ThemeCatalog() @@ -77,8 +79,8 @@ internal final class ThemeCatalog: ObservableObject { // What the files hold. `themes` is this with colliding names told apart for display. private var loaded: [ThemeDefinition] = BuiltInThemes.all private var userFiles: [String: ThemeFile] = [:] - /// Themes whose file went away since it was read. A save of one is refused rather than writing a - /// new file, which would bring back a theme deleted in Finder or on another Mac. + // Themes whose file went away since it was read. A save of one is refused rather than writing a + // new file, which would bring back a theme deleted in Finder or on another Mac. private var vanished: [String: URL] = [:] private let userDirectory: URL private let bundleDirectory: URL? @@ -88,8 +90,8 @@ internal final class ThemeCatalog: ObservableObject { nonisolated private static let logger = Logger(subsystem: "com.TablePro", category: "ThemeCatalog") - /// Loaded before the first activation, so a user theme in a slot is in the list the launch - /// activation resolves against. + // Loaded before the first activation, so a user theme in a slot is in the list the launch + // activation resolves against. internal init( userDirectory: URL = ThemeCatalog.defaultUserDirectory, bundleDirectory: URL? = Bundle.main.resourceURL, @@ -125,11 +127,25 @@ internal final class ThemeCatalog: ObservableObject { adopt(Self.loadContents(userDirectory: userDirectory, bundleDirectory: bundleDirectory)) } - /// Writes the theme to the file it came from, or a new theme to `.json`. The list then holds - /// what the file holds, which the format normalizes. + // Writes the theme to the file it came from, or a new theme to `.json`. The list then holds + // what the file holds, which the format normalizes. internal func save(_ theme: ThemeDefinition) throws { + let previous = userFiles[theme.id] != nil ? storedTheme(id: theme.id) : nil try persist(theme) - syncTracker.markDirty(.settings, id: Self.syncCategory(for: theme.id)) + markSyncEdit(of: storedTheme(id: theme.id) ?? theme, previous: previous) + } + + // Each record pushes whole, so one whose keys did not change would revert another Mac's edit of them. + private func markSyncEdit(of theme: ThemeDefinition, previous: ThemeDefinition?) { + guard let previous else { + syncTracker.markDirty(.settings, ids: Self.syncCategories(of: theme)) + return + } + let cleared = Self.newerSyncSchemas.filter { !theme.setsSlots(introducedIn: $0) && previous.setsSlots(introducedIn: $0) } + let changed = ([ThemeSchema.oldestReadable] + Self.newerSyncSchemas) + .filter { !cleared.contains($0) && !Self.ownedKeysMatch(theme, previous, schema: $0) } + syncTracker.markDirty(.settings, ids: changed.map { Self.syncCategory(for: theme.id, schema: $0) }) + syncTracker.markDeleted(.settings, ids: cleared.map { Self.syncCategory(for: theme.id, schema: $0) }) } private func persist(_ theme: ThemeDefinition) throws { @@ -164,8 +180,16 @@ internal final class ThemeCatalog: ObservableObject { userFiles[id] = nil vanished[id] = file.url publish(loaded.filter { $0.id != id }) - syncTracker.markDeleted(.settings, id: Self.syncCategory(for: id)) - return ThemeTrashReceipt(themeId: id, originalURL: file.url, trashedURL: trashed) + // Another Mac may have set a value color this one has not pulled yet. + let categories = Self.allSyncCategories(for: id) + let queued = syncTracker.tombstonedIds(for: .settings) + syncTracker.markDeleted(.settings, ids: categories) + return ThemeTrashReceipt( + themeId: id, + originalURL: file.url, + trashedURL: trashed, + tombstones: categories.filter { !queued.contains($0) } + ) } @discardableResult @@ -180,12 +204,15 @@ internal final class ThemeCatalog: ObservableObject { internal func restore(_ receipt: ThemeTrashReceipt) throws { try FileManager.default.moveItem(at: receipt.trashedURL, to: receipt.originalURL) reloadSynchronously() - guard let id = receipt.themeId, userFiles[id] != nil else { return } - syncTracker.markDirty(.settings, id: Self.syncCategory(for: id)) + guard let id = receipt.themeId, userFiles[id] != nil, let restored = storedTheme(id: id) else { return } + let categories = Self.syncCategories(of: restored) + syncTracker.markDirty(.settings, ids: categories) + // A record the restored theme does not hold may carry another Mac's keys, which the queued deletion would remove. + syncTracker.discardTombstones(.settings, ids: receipt.tombstones.filter { !categories.contains($0) }) } - /// Two themes with one name cannot be told apart in a popup, so a taken name is refused rather - /// than numbered behind the user's back. + // Two themes with one name cannot be told apart in a popup, so a taken name is refused rather + // than numbered behind the user's back. internal func rename(id: String, to name: String) throws { let trimmed = name.trimmingCharacters(in: .whitespacesAndNewlines) guard var theme = storedTheme(id: id) else { @@ -263,8 +290,8 @@ internal final class ThemeCatalog: ObservableObject { } } - /// `.json`, unless that name already holds another theme or a file that failed to load, - /// which a save must never overwrite. + // `.json`, unless that name already holds another theme or a file that failed to load, + // which a save must never overwrite. private func availableURL(for id: String) -> URL { let taken = Set( userFiles.values.map(\.url.standardizedFileURL) @@ -279,8 +306,8 @@ internal final class ThemeCatalog: ObservableObject { return candidate } - /// A file edited, replaced or removed outside TablePro since it was read is not overwritten, - /// moved or recreated: the catalog reloads and the caller reports the conflict. + // A file edited, replaced or removed outside TablePro since it was read is not overwritten, + // moved or recreated: the catalog reloads and the caller reports the conflict. private func ensureUnchanged(_ file: ThemeFile) throws { let current: Data do { @@ -294,7 +321,7 @@ internal final class ThemeCatalog: ObservableObject { throw ThemeLoadError.changedOnDisk(file.url.lastPathComponent) } - /// A file that is still there no longer loads as this theme, so it changed rather than went away. + // A file that is still there no longer loads as this theme, so it changed rather than went away. private func ensureNotVanished(_ id: String) throws { guard let url = vanished[id] else { return } guard FileManager.default.fileExists(atPath: url.path) else { @@ -447,9 +474,17 @@ internal enum ThemeRemoteMerge: Sendable { case keepBoth } -// Each user theme syncs as one settings record, `theme.`, whose payload is the canonical file. +// Each user theme syncs as the settings record `theme.`, holding only the keys format 3 reads, so the +// previous stable reads and edits it. A theme that sets a key a newer format introduced also syncs +// `themeSchema.`. Each record owns its own keys, and every payload is a canonical file. internal extension ThemeCatalog { nonisolated static let syncCategoryPrefix = "theme." + // Never under `theme.`: a build that reads only format 3 passes over a category it does not know. + nonisolated static let schemaSyncCategoryPrefix = "themeSchema" + + nonisolated static let newerSyncSchemas = Array( + stride(from: ThemeSchema.oldestReadable + 1, through: ThemeSchema.current, by: 1) + ) // Themes are about 1.2 KB, and a slot path nests three containers deep, so anything past these // limits is not a theme and only costs parse time. @@ -460,23 +495,69 @@ internal extension ThemeCatalog { syncCategoryPrefix + id } + nonisolated static func syncCategory(for id: String, schema: Int) -> String { + guard schema > ThemeSchema.oldestReadable else { return syncCategory(for: id) } + return "\(schemaSyncCategoryPrefix)\(schema).\(id)" + } + + nonisolated static func syncCategories(of theme: ThemeDefinition) -> [String] { + [syncCategory(for: theme.id)] + + newerSyncSchemas.filter(theme.setsSlots(introducedIn:)).map { syncCategory(for: theme.id, schema: $0) } + } + nonisolated static func themeId(forSyncCategory category: String) -> String? { - guard category.hasPrefix(syncCategoryPrefix) else { return nil } - return String(category.dropFirst(syncCategoryPrefix.count)) + syncRecord(forCategory: category)?.id + } + + nonisolated static func syncSchema(forCategory category: String) -> Int? { + syncRecord(forCategory: category)?.schema + } + + nonisolated private static func syncRecord(forCategory category: String) -> (id: String, schema: Int)? { + if category.hasPrefix(syncCategoryPrefix) { + return (String(category.dropFirst(syncCategoryPrefix.count)), ThemeSchema.oldestReadable) + } + guard category.hasPrefix(schemaSyncCategoryPrefix) else { return nil } + let rest = category.dropFirst(schemaSyncCategoryPrefix.count) + guard let dot = rest.firstIndex(of: "."), + let schema = Int(rest[.. ThemeSchema.oldestReadable + else { return nil } + return (String(rest[rest.index(after: dot)...]), schema) + } + + // `theme.` owns the format 3 slots, the name, the author and the appearance; a newer format's record + // owns only the slots that format introduced. + nonisolated private static func ownedKeysMatch(_ lhs: ThemeDefinition, _ rhs: ThemeDefinition, schema: Int) -> Bool { + let owned: (ThemeSlot) -> Bool = schema > ThemeSchema.oldestReadable + ? { $0.introducedInSchema == schema } + : { $0.introducedInSchema <= ThemeSchema.oldestReadable } + let slotsMatch = lhs.overrides.filter { owned($0.key) } == rhs.overrides.filter { owned($0.key) } + guard schema <= ThemeSchema.oldestReadable else { return slotsMatch } + return slotsMatch && lhs.name == rhs.name && lhs.author == rhs.author && lhs.appearance == rhs.appearance + } + + nonisolated private static func allSyncCategories(for id: String) -> [String] { + [syncCategory(for: id)] + newerSyncSchemas.map { syncCategory(for: id, schema: $0) } } var syncCategories: [String] { - userFiles.keys.sorted().map(Self.syncCategory(for:)) + userFiles.keys.sorted().flatMap { id in + storedTheme(id: id).map(Self.syncCategories(of:)) ?? [Self.syncCategory(for: id)] + } } - // The file's own name, never the one the list shows to tell two themes apart. + // The file's own name, never the one the list shows to tell two themes apart. A newer format's + // record exists only while the theme sets one of its keys. func syncPayload(forCategory category: String) -> Data? { - guard let id = Self.themeId(forSyncCategory: category), - userFiles[id] != nil, - let theme = storedTheme(id: id) + guard let record = Self.syncRecord(forCategory: category), + userFiles[record.id] != nil, + let theme = storedTheme(id: record.id), + record.schema == ThemeSchema.oldestReadable || theme.setsSlots(introducedIn: record.schema) else { return nil } do { - return try ThemeFileFormat.encode(theme) + return try ThemeFileFormat.encode(theme.restricted(toSchema: record.schema)) } catch { Self.logger.error("Could not encode a theme for sync: \(error.publicLogShape, privacy: .public)") return nil @@ -486,10 +567,15 @@ internal extension ThemeCatalog { // A synced theme is untrusted input from another device, so it is validated before anything // touches disk. A record that fails validation is skipped, since the same bytes would fail // again; only a disk failure is `.failed`, so the pull is not acknowledged and replays. - func applyRemote(category: String, data: Data, merge: ThemeRemoteMerge = .replace) -> RemoteApplyOutcome { - let theme: ThemeDefinition + func applyRemote( + category: String, + data: Data, + merge: ThemeRemoteMerge = .replace, + newerRecord: (String) -> Data? = { _ in nil } + ) -> RemoteApplyOutcome { + let record: (schema: Int, theme: ThemeDefinition) do { - theme = try Self.validatedRemoteTheme(category: category, data: data) + record = try Self.validatedRemoteTheme(category: category, data: data) } catch { Self.logger.error( "Skipped synced theme \(category, privacy: .private(mask: .hash)): \(error.publicLogShape, privacy: .public)" @@ -498,7 +584,11 @@ internal extension ThemeCatalog { } do { - return try writeRemote(theme, merge: merge) + reloadIfChangedOnDisk(record.theme.id) + guard record.schema > ThemeSchema.oldestReadable else { + return try writeRemote(mergedTheme(record.theme, merge: merge, newerRecord: newerRecord), merge: merge) + } + return try applyRemoteSlots(of: record.theme, introducedIn: record.schema, merge: merge) } catch { Self.logger.error( "Could not write synced theme \(category, privacy: .private(mask: .hash)): \(error.publicLogShape, privacy: .public)" @@ -532,12 +622,70 @@ internal extension ThemeCatalog { if !removed.isEmpty { publish(loaded.filter { !removed.contains($0.id) }) } - syncTracker.discardDirty(.settings, ids: removed.sorted().map(Self.syncCategory(for:))) + syncTracker.discardDirty(.settings, ids: removed.sorted().flatMap(Self.allSyncCategories(for:))) return isPersisted } + // Another Mac cleared the theme's last key of `schema`, so this one clears that format's keys too. + @discardableResult + func removeSlotsWithoutSync(introducedIn schema: Int, ids: Set) -> Bool { + var isPersisted = true + var settled: [String] = [] + for id in ids.sorted() { + reloadIfChangedOnDisk(id) + guard userFiles[id] != nil, let local = storedTheme(id: id) else { continue } + settled.append(id) + guard local.setsSlots(introducedIn: schema) else { continue } + var cleared = local + cleared.overrides = local.overrides.filter { $0.key.introducedInSchema != schema } + do { + _ = try writeRemote(cleared, merge: .replace) + } catch { + Self.logger.error("Could not clear keys deleted on another Mac: \(error.publicLogShape, privacy: .public)") + settled.removeLast() + isPersisted = false + } + } + syncTracker.discardDirty(.settings, ids: settled.map { Self.syncCategory(for: $0, schema: schema) }) + return isPersisted + } + + // `theme.` owns only the keys format 3 reads, so a theme this Mac holds keeps its newer keys. + // One new here, or one whose local file may never have reached the server, takes them from their records. + private func mergedTheme( + _ record: ThemeDefinition, + merge: ThemeRemoteMerge, + newerRecord: (String) -> Data? + ) -> ThemeDefinition { + let base = record.restricted(toSchema: ThemeSchema.oldestReadable) + if merge == .replace, userFiles[record.id] != nil, let local = storedTheme(id: record.id) { + return base.replacingSlots(from: local) { $0.introducedInSchema > ThemeSchema.oldestReadable } + } + return Self.newerSyncSchemas.reduce(base) { merged, schema in + let category = Self.syncCategory(for: record.id, schema: schema) + guard let data = newerRecord(category), + let newer = try? Self.validatedRemoteTheme(category: category, data: data) + else { return merged } + return merged.replacingSlots(from: newer.theme) { $0.introducedInSchema == schema } + } + } + + // A theme new to this Mac takes these keys when its `theme.` arrives. While the local file may + // never have reached the server, `theme.` decides the whole theme and what is kept as a copy. + private func applyRemoteSlots( + of record: ThemeDefinition, + introducedIn schema: Int, + merge: ThemeRemoteMerge + ) throws -> RemoteApplyOutcome { + guard merge == .replace, userFiles[record.id] != nil, let local = storedTheme(id: record.id) else { + return .skipped + } + let merged = local.replacingSlots(from: record) { $0.introducedInSchema == schema } + guard merged != local else { return .skipped } + return try writeRemote(merged, merge: .replace) + } + private func writeRemote(_ theme: ThemeDefinition, merge: ThemeRemoteMerge) throws -> RemoteApplyOutcome { - reloadIfChangedOnDisk(theme.id) let data = try ThemeFileFormat.encode(theme) let url: URL var keptCopy: ThemeDefinition? @@ -564,7 +712,7 @@ internal extension ThemeCatalog { upsert(try ThemeFileFormat.decode(data)) // A pull suppresses this mark; the seed that follows it uploads the copy, which the server lacks. if let keptCopy { - syncTracker.markDirty(.settings, id: Self.syncCategory(for: keptCopy.id)) + syncTracker.markDirty(.settings, ids: Self.syncCategories(of: keptCopy)) } return .applied } @@ -605,11 +753,15 @@ internal extension ThemeCatalog { reloadSynchronously() } - nonisolated private static func validatedRemoteTheme(category: String, data: Data) throws -> ThemeDefinition { - guard let id = themeId(forSyncCategory: category), - ThemeIdentifier.isValid(id), - !id.hasPrefix(ThemeDefinition.builtInPrefix) + nonisolated private static func validatedRemoteTheme( + category: String, + data: Data + ) throws -> (schema: Int, theme: ThemeDefinition) { + guard let record = syncRecord(forCategory: category), + ThemeIdentifier.isValid(record.id), + !record.id.hasPrefix(ThemeDefinition.builtInPrefix) else { throw ThemeSyncRejection.invalidCategory } + guard record.schema <= ThemeSchema.current else { throw ThemeSyncRejection.newerFormat(record.schema) } guard data.count <= maximumSyncPayloadSize else { throw ThemeSyncRejection.tooLarge(data.count) } guard JSONNesting.depth(of: data, stoppingAfter: maximumSyncPayloadDepth) <= maximumSyncPayloadDepth else { throw ThemeSyncRejection.tooDeep @@ -617,13 +769,14 @@ internal extension ThemeCatalog { // The decoder refuses a newer schema, an unknown key, a bad color, and a name or author with a // control character or past the length cap. let theme = try ThemeFileFormat.decode(data) - guard theme.id == id else { throw ThemeSyncRejection.mismatchedIdentifier } - return theme + guard theme.id == record.id else { throw ThemeSyncRejection.mismatchedIdentifier } + return (record.schema, theme) } } private enum ThemeSyncRejection: Error { case invalidCategory + case newerFormat(Int) case tooLarge(Int) case tooDeep case mismatchedIdentifier diff --git a/apple/TablePro/Theme/ThemeColor.swift b/apple/TablePro/Theme/ThemeColor.swift index 32dc09a58f..26ad51133b 100644 --- a/apple/TablePro/Theme/ThemeColor.swift +++ b/apple/TablePro/Theme/ThemeColor.swift @@ -60,7 +60,9 @@ internal extension ThemeSlot { return .textColor case .gridBackground: return .controlBackgroundColor - case .gridText, .gridBoolTrue, .gridBoolFalse: return .labelColor + case .gridText, .gridBoolTrue, .gridBoolFalse, + .gridString, .gridNumber, .gridDateTime, .gridBinary, .gridJSON, .gridUUID, .gridEnum: + return .labelColor case .gridAlternateRow: return NativeThemeColors.alternateRow case .gridHeaderBackground: return .controlBackgroundColor case .gridHeaderText: return .headerTextColor diff --git a/apple/TablePro/Views/Compare/CompareRowGrid.swift b/apple/TablePro/Views/Compare/CompareRowGrid.swift index 80ed3e5c35..9f651d5ba5 100644 --- a/apple/TablePro/Views/Compare/CompareRowGrid.swift +++ b/apple/TablePro/Views/Compare/CompareRowGrid.swift @@ -52,6 +52,7 @@ internal final class CompareRowGridModel: ObservableObject { configuration.supportsValueFilter = false configuration.supportsColumnSort = false configuration.appliesRowCopyFormat = true + configuration.colorsValuesByType = true configuration.commands = [.freezeColumns, .displayAs] return configuration } diff --git a/apple/TablePro/Views/DataGrid/Cells/DataGridCellAppearance.swift b/apple/TablePro/Views/DataGrid/Cells/DataGridCellAppearance.swift index bb2faafb71..ec6b958728 100644 --- a/apple/TablePro/Views/DataGrid/Cells/DataGridCellAppearance.swift +++ b/apple/TablePro/Views/DataGrid/Cells/DataGridCellAppearance.swift @@ -28,6 +28,7 @@ struct DataGridCellAppearance: Equatable { onEmphasizedSelection: Bool, checkboxMark: DataGridCheckboxMark? = nil, textTone: NSColor? = nil, + valueTone: DataGridValueTone = .plain, revealsInvisibleCharacters: Bool = false ) -> DataGridCellAppearance { let deletedTextColor = state.visualState.isDeleted ? palette.deletedRowText : nil @@ -41,7 +42,12 @@ struct DataGridCellAppearance: Equatable { baseColor = deletedTextColor ?? .systemBlue case .none: font = palette.regularFont - baseColor = deletedTextColor ?? textTone ?? valueTextColor(kind: kind, rawValue: content.rawValue, palette: palette) + baseColor = deletedTextColor ?? textTone ?? valueTextColor( + kind: kind, + rawValue: content.rawValue, + valueTone: valueTone, + palette: palette + ) case .null, .empty, .absent, .unloaded: font = palette.italicFont baseColor = deletedTextColor ?? palette.placeholderText @@ -103,11 +109,12 @@ struct DataGridCellAppearance: Equatable { private static func valueTextColor( kind: DataGridCellKind, rawValue: String?, + valueTone: DataGridValueTone, palette: DataGridCellPalette ) -> NSColor { - guard kind == .boolean, let rawValue, let isTrue = StoredBoolean.value(of: rawValue) else { - return palette.text + if kind == .boolean, let rawValue, let isTrue = StoredBoolean.value(of: rawValue) { + return isTrue ? palette.booleanTrueText : palette.booleanFalseText } - return isTrue ? palette.booleanTrueText : palette.booleanFalseText + return palette.valueColors.color(for: valueTone) ?? palette.text } } diff --git a/apple/TablePro/Views/DataGrid/Cells/DataGridCellKind.swift b/apple/TablePro/Views/DataGrid/Cells/DataGridCellKind.swift index 73b673868e..d6f7cbacde 100644 --- a/apple/TablePro/Views/DataGrid/Cells/DataGridCellKind.swift +++ b/apple/TablePro/Views/DataGrid/Cells/DataGridCellKind.swift @@ -113,8 +113,9 @@ enum DataGridCellAccessory: Equatable { struct DataGridColumnPresentation: Equatable { let kind: DataGridCellKind let accessory: DataGridCellAccessory + let valueTone: DataGridColumnTone - static let plain = DataGridColumnPresentation(kind: .text, accessory: .none) + static let plain = DataGridColumnPresentation(kind: .text, accessory: .none, valueTone: .plain) static func resolve( columnType: ColumnType?, @@ -123,7 +124,8 @@ struct DataGridColumnPresentation: Equatable { isTypePicker: Bool, isEnumOrSet: Bool, isEditable: Bool, - isCheckbox: Bool = false + isCheckbox: Bool = false, + valueTone: DataGridColumnTone = .plain ) -> DataGridColumnPresentation { let configuredPicker = isDropdown || isTypePicker let picksFromList = configuredPicker || isEnumOrSet || columnType?.isEnumOrSetType == true @@ -149,7 +151,8 @@ struct DataGridColumnPresentation: Equatable { return DataGridColumnPresentation( kind: kind, - accessory: DataGridCellAccessory.reserved(for: kind, isEditable: isEditable) + accessory: DataGridCellAccessory.reserved(for: kind, isEditable: isEditable), + valueTone: valueTone ) } } diff --git a/apple/TablePro/Views/DataGrid/Cells/DataGridCellPalette.swift b/apple/TablePro/Views/DataGrid/Cells/DataGridCellPalette.swift index 4f015cde7f..d64aa3b956 100644 --- a/apple/TablePro/Views/DataGrid/Cells/DataGridCellPalette.swift +++ b/apple/TablePro/Views/DataGrid/Cells/DataGridCellPalette.swift @@ -16,6 +16,7 @@ struct DataGridCellPalette: Equatable { let placeholderText: NSColor let booleanTrueText: NSColor let booleanFalseText: NSColor + let valueColors: DataGridValueColors let rowNumberText: NSColor let deletedRowText: NSColor let modifiedColumnTint: NSColor @@ -33,6 +34,7 @@ struct DataGridCellPalette: Equatable { placeholderText: NSColor, booleanTrueText: NSColor, booleanFalseText: NSColor, + valueColors: DataGridValueColors? = nil, rowNumberText: NSColor, deletedRowText: NSColor, modifiedColumnTint: NSColor, @@ -49,6 +51,7 @@ struct DataGridCellPalette: Equatable { self.placeholderText = placeholderText self.booleanTrueText = booleanTrueText self.booleanFalseText = booleanFalseText + self.valueColors = valueColors ?? DataGridValueColors(all: text) self.rowNumberText = rowNumberText self.deletedRowText = deletedRowText self.modifiedColumnTint = modifiedColumnTint @@ -73,6 +76,36 @@ struct DataGridCellPalette: Equatable { } } +// Stored fields, so a cell's color is a switch and never a lookup. +struct DataGridValueColors: Equatable { + let string: NSColor + let number: NSColor + let dateTime: NSColor + let binary: NSColor + let json: NSColor + let uuid: NSColor + let enumeration: NSColor + + func color(for tone: DataGridValueTone) -> NSColor? { + switch tone { + case .plain: return nil + case .string: return string + case .number: return number + case .dateTime: return dateTime + case .binary: return binary + case .json: return json + case .uuid: return uuid + case .enumeration: return enumeration + } + } +} + +extension DataGridValueColors { + init(all color: NSColor) { + self.init(string: color, number: color, dateTime: color, binary: color, json: color, uuid: color, enumeration: color) + } +} + extension ThemeScope { // The row-state tints are dynamic providers that cost four times a catalog color per fill, so they resolve once here. func dataGridCellPalette( @@ -89,6 +122,15 @@ extension ThemeScope { placeholderText: resolved[.gridNullValue], booleanTrueText: resolved[.gridBoolTrue], booleanFalseText: resolved[.gridBoolFalse], + valueColors: DataGridValueColors( + string: resolved[.gridString], + number: resolved[.gridNumber], + dateTime: resolved[.gridDateTime], + binary: resolved[.gridBinary], + json: resolved[.gridJSON], + uuid: resolved[.gridUUID], + enumeration: resolved[.gridEnum] + ), rowNumberText: resolved[.gridRowNumber], deletedRowText: Self.resolvedTint(.gridDeletedText, of: resolved, in: appearance), modifiedColumnTint: Self.resolvedTint(.gridModified, of: resolved, in: appearance), diff --git a/apple/TablePro/Views/DataGrid/Cells/DataGridCellPresenter.swift b/apple/TablePro/Views/DataGrid/Cells/DataGridCellPresenter.swift index 8f7745aac0..ce323a5e3d 100644 --- a/apple/TablePro/Views/DataGrid/Cells/DataGridCellPresenter.swift +++ b/apple/TablePro/Views/DataGrid/Cells/DataGridCellPresenter.swift @@ -21,6 +21,7 @@ final class DataGridCellPresenter { let scope: ThemeScope let databaseType: DatabaseType? let showsInvisibleCharacters: Bool + let colorsValuesByType: Bool let nullDisplayString: String } @@ -30,11 +31,12 @@ final class DataGridCellPresenter { private let visualIndex = RowVisualIndex() private var visualIndexSource: ObjectIdentifier? private var highlightRuleSet = HighlightRuleSet.empty - private var presentations: [Int: DataGridColumnPresentation] = [:] + // Indexed by data index: the draw path reads it once per cell. + private var presentations: [DataGridColumnPresentation] = [] - init(model: DataGridModel) { + init(model: DataGridModel, valueColors: ValueColorSettings = AppSettingsManager.shared.valueColors) { self.model = model - registry = DataGridCellRegistry(scope: model.themeScope) + registry = DataGridCellRegistry(scope: model.themeScope, valueColors: valueColors) } func adopt(_ model: DataGridModel) { @@ -48,19 +50,22 @@ final class DataGridCellPresenter { guard let model else { return nil } registry.preparePalette(in: appearance) let rows = model.rows + let configuration = model.configuration prepareRowState(rows: rows) return Pass( rows: rows, formats: model.store.displayFormats, selection: model.selection, findMatch: model.find.currentMatch, - isEditable: model.configuration.isEditable, - checkboxColumns: model.configuration.checkboxColumns, - marksEmptyText: model.configuration.marksEmptyText, + isEditable: configuration.isEditable, + checkboxColumns: configuration.checkboxColumns, + marksEmptyText: configuration.marksEmptyText, palette: registry.palette, scope: model.themeScope, - databaseType: model.configuration.traits.databaseType, + databaseType: configuration.traits.databaseType, showsInvisibleCharacters: registry.showsInvisibleCharacters, + colorsValuesByType: configuration.colorsValuesByType + && (registry.colorsValuesByType || configuration.overridesValueColorSetting), nullDisplayString: registry.nullDisplayString ) } @@ -125,22 +130,28 @@ final class DataGridCellPresenter { guard let model else { return [] } let rows = model.rows let configuration = model.configuration - var next: [Int: DataGridColumnPresentation] = [:] + var next: [DataGridColumnPresentation] = [] next.reserveCapacity(rows.columns.count) var changed = IndexSet() for (dataIndex, name) in rows.columns.enumerated() { let type = dataIndex < rows.columnTypes.count ? rows.columnTypes[dataIndex] : nil + let enumValues = rows.columnEnumValues[name] + let valueTone = configuration.colorsValuesByType + ? DataGridColumnTone(columnType: type, declaresEnumLabels: enumValues?.isEmpty == false) + : .plain let presentation = DataGridColumnPresentation.resolve( columnType: type, isForeignKey: rows.columnForeignKeys[name] != nil, isDropdown: configuration.dropdownColumns?.contains(dataIndex) == true, isTypePicker: configuration.typePickerColumns?.contains(dataIndex) == true, - isEnumOrSet: Self.picksFromList(type: type, enumValues: rows.columnEnumValues[name]), + isEnumOrSet: Self.picksFromList(type: type, enumValues: enumValues), isEditable: configuration.isEditable, - isCheckbox: configuration.checkboxColumns.contains(dataIndex) + isCheckbox: configuration.checkboxColumns.contains(dataIndex), + valueTone: valueTone ) - next[dataIndex] = presentation - if presentations[dataIndex] != presentation { + next.append(presentation) + let previous = dataIndex < presentations.count ? presentations[dataIndex] : nil + if previous?.kind != presentation.kind || previous?.accessory != presentation.accessory { changed.insert(dataIndex) } } @@ -149,7 +160,7 @@ final class DataGridCellPresenter { } func presentation(forDataIndex dataIndex: Int) -> DataGridColumnPresentation { - presentations[dataIndex] ?? .plain + presentations.indices.contains(dataIndex) ? presentations[dataIndex] : .plain } private static func picksFromList(type: ColumnType?, enumValues: [String]?) -> Bool { @@ -167,10 +178,6 @@ final class DataGridCellPresenter { // MARK: - Text - func displayText(of row: Row, dataIndex: Int, type: ColumnType?, pass: Pass) -> String { - displayText(of: row, dataIndex: dataIndex, type: type, formats: pass.formats, databaseType: pass.databaseType, columnCount: pass.rows.columns.count) - } - func displayText( of row: Row, dataIndex: Int, @@ -179,19 +186,32 @@ final class DataGridCellPresenter { databaseType: DatabaseType?, columnCount: Int ) -> String { - guard dataIndex >= 0, dataIndex < row.values.count, let cache = model?.display.cache else { return "" } + display(of: row, dataIndex: dataIndex, type: type, formats: formats, databaseType: databaseType, columnCount: columnCount).text + } + + // The text and whether its display format produced it come from one cache entry, written and cleared together. + private func display( + of row: Row, + dataIndex: Int, + type: ColumnType?, + formats: [ValueDisplayFormat?], + databaseType: DatabaseType?, + columnCount: Int + ) -> (text: String, isFormatted: Bool) { + guard dataIndex >= 0, dataIndex < row.values.count, let cache = model?.display.cache else { return ("", false) } let existing = cache.box(forID: row.id) if let existing, dataIndex < existing.values.count, let cached = existing.values[dataIndex] { - return cached + return (cached, existing.isFormatted(at: dataIndex)) } let rawValue = row.values[dataIndex] let format = dataIndex < formats.count ? formats[dataIndex] : nil - let formatted = CellDisplayFormatter.format( + let formatted = CellDisplayFormatter.display( rawValue, columnType: type, displayFormat: format, databaseType: databaseType - ) ?? rawValue.asText ?? "" + ) + let text = formatted.text ?? rawValue.asText ?? "" let neededCount = max(dataIndex + 1, columnCount) let box: RowDisplayBox @@ -203,27 +223,28 @@ final class DataGridCellPresenter { } else { box = RowDisplayBox(ContiguousArray(repeating: nil, count: neededCount)) } - box.values[dataIndex] = formatted + box.setText(text, at: dataIndex, isFormatted: formatted.isFormatted) cache.setBox(box, forID: row.id) - return formatted + return (text, formatted.isFormatted) } // Formats a whole row ahead of the viewport, for the background prewarm. func cacheText(of row: Row, rows: TableRows, formats: [ValueDisplayFormat?], databaseType: DatabaseType?) { guard let cache = model?.display.cache, cache.box(forID: row.id) == nil else { return } let columnCount = rows.columns.count - var values = ContiguousArray(repeating: nil, count: columnCount) + let box = RowDisplayBox(ContiguousArray(repeating: nil, count: columnCount)) for dataIndex in 0..= 0, dataIndex < row.values.count else { return nil } let rawValue = row.values[dataIndex] - let text = displayText(of: row, dataIndex: dataIndex, type: column.type, pass: pass) + let shown = display( + of: row, + dataIndex: dataIndex, + type: column.type, + formats: pass.formats, + databaseType: pass.databaseType, + columnCount: pass.rows.columns.count + ) let isFindMatch = pass.findMatch.map { $0.rowID == row.id && $0.columnID == column.id } ?? false + let presentation = self.presentation(forDataIndex: dataIndex) + let valueTone: DataGridValueTone + if pass.colorsValuesByType { + let format = dataIndex < pass.formats.count ? pass.formats[dataIndex] : nil + valueTone = presentation.valueTone.tone(displayedAs: format, isFormatted: shown.isFormatted) + } else { + valueTone = .plain + } return DataGridCellAppearance.resolve( - kind: presentation(forDataIndex: dataIndex).kind, + kind: presentation.kind, content: DataGridCellContent( - displayText: text, + displayText: shown.text, rawValue: rawValue.asText, placeholder: DataGridCellContent.placeholder( for: rawValue, @@ -266,6 +302,7 @@ final class DataGridCellPresenter { onEmphasizedSelection: onEmphasizedSelection, checkboxMark: checkboxMark(of: row, column: column, checkboxColumns: pass.checkboxColumns), textTone: model?.dataSource?.gridTextTone(for: row.id, column: column), + valueTone: valueTone, revealsInvisibleCharacters: pass.showsInvisibleCharacters ) } diff --git a/apple/TablePro/Views/DataGrid/Cells/DataGridCellRegistry.swift b/apple/TablePro/Views/DataGrid/Cells/DataGridCellRegistry.swift index 777417659b..0be7b02add 100644 --- a/apple/TablePro/Views/DataGrid/Cells/DataGridCellRegistry.swift +++ b/apple/TablePro/Views/DataGrid/Cells/DataGridCellRegistry.swift @@ -9,6 +9,7 @@ import AppKit final class DataGridCellRegistry { private(set) var nullDisplayString: String private(set) var showsInvisibleCharacters: Bool + private(set) var colorsValuesByType: Bool private(set) var palette: DataGridCellPalette private(set) var scope: ThemeScope private var paletteKey: PaletteKey @@ -19,11 +20,12 @@ final class DataGridCellRegistry { let appearance: NSAppearance.Name } - init(scope: ThemeScope = .live) { + init(scope: ThemeScope = .live, valueColors: ValueColorSettings = AppSettingsManager.shared.valueColors) { self.scope = scope let settings = AppSettingsManager.shared.dataGrid nullDisplayString = settings.nullDisplay showsInvisibleCharacters = settings.showInvisibleCharacters + colorsValuesByType = valueColors.colorsValuesByType let appearance = NSApp?.effectiveAppearance ?? NSAppearance.currentDrawing() paletteKey = Self.paletteKey(for: appearance) palette = scope.dataGridCellPalette(in: appearance) @@ -34,6 +36,10 @@ final class DataGridCellRegistry { showsInvisibleCharacters = settings.showInvisibleCharacters } + func reloadValueColors(_ settings: ValueColorSettings = AppSettingsManager.shared.valueColors) { + colorsValuesByType = settings.colorsValuesByType + } + // Asked by every row as it draws: the tints resolve in the appearance the row draws in, which only the draw knows. func preparePalette(in appearance: NSAppearance) { let key = Self.paletteKey(for: appearance) diff --git a/apple/TablePro/Views/DataGrid/Cells/DataGridValueTone.swift b/apple/TablePro/Views/DataGrid/Cells/DataGridValueTone.swift new file mode 100644 index 0000000000..e9c60d5645 --- /dev/null +++ b/apple/TablePro/Views/DataGrid/Cells/DataGridValueTone.swift @@ -0,0 +1,83 @@ +// +// DataGridValueTone.swift +// TablePro +// + +import Foundation + +enum DataGridValueTone: Equatable, Sendable { + case plain + case string + case number + case dateTime + case binary + case json + case uuid + case enumeration + + // A `.text` column is whatever the classifier could not place, so only a character type's name makes it a string. + static func base(of type: ColumnType?, declaresEnumLabels: Bool) -> DataGridValueTone { + guard let type else { return .plain } + switch type { + case .integer, .decimal: + return .number + case .date, .timestamp, .datetime: + return .dateTime + case .blob: + return .binary + case .json: + return .json + case .enumType, .set: + return .enumeration + case .text(let rawType): + if declaresEnumLabels { return .enumeration } + if type.isUUIDType { return .uuid } + guard let name = ColumnType.unwrappedTypeName(rawType), !name.isEmpty, + ColumnTypeSQLQuoting.isCharacterType(.text(rawType: name)) else { return .plain } + return .string + case .boolean, .spatial, .vector, .array: + return .plain + } + } + + // nil where the cell shows the column's own value. + static func shown(by format: ValueDisplayFormat) -> DataGridValueTone? { + switch format { + case .raw: + return nil + case .text: + return .string + case .uuid, .uuidSwappedTime: + return .uuid + case .unixTimestamp, .unixTimestampMillis: + return .dateTime + // Each shows a document as text: JSON, PHP serialize or protobuf text format. + case .json, .phpSerialized, .messagePack, .protobuf, .igbinary, .avro, .protobufSchema, .schemaRegistry: + return .json + } + } +} + +struct DataGridColumnTone: Equatable, Sendable { + static let plain = DataGridColumnTone(base: .plain, followsDisplayFormat: false) + + let base: DataGridValueTone + let followsDisplayFormat: Bool + + private init(base: DataGridValueTone, followsDisplayFormat: Bool) { + self.base = base + self.followsDisplayFormat = followsDisplayFormat + } + + init(columnType: ColumnType?, declaresEnumLabels: Bool) { + self.init(base: DataGridValueTone.base(of: columnType, declaresEnumLabels: declaresEnumLabels), followsDisplayFormat: true) + } + + // `isFormatted` comes from the formatter with the cached text: a value the format could not render reads as its column. + func tone(displayedAs format: ValueDisplayFormat?, isFormatted: Bool) -> DataGridValueTone { + guard followsDisplayFormat, isFormatted, let format, let shown = DataGridValueTone.shown(by: format) else { + return base + } + return shown + } +} diff --git a/apple/TablePro/Views/DataGrid/DataGridViewController.swift b/apple/TablePro/Views/DataGrid/DataGridViewController.swift index dd7176f7af..408bd80545 100644 --- a/apple/TablePro/Views/DataGrid/DataGridViewController.swift +++ b/apple/TablePro/Views/DataGrid/DataGridViewController.swift @@ -209,6 +209,10 @@ final class DataGridViewController: NSViewController, DataGridMountedView { .receive(on: RunLoop.main) .sink { [weak self] _ in self?.dataGridSettingsDidChange() } .store(in: &cancellables) + AppEvents.shared.valueColorSettingsChanged + .receive(on: RunLoop.main) + .sink { [weak self] _ in self?.valueColorSettingsDidChange() } + .store(in: &cancellables) AppEvents.shared.systemTimeZoneChanged .receive(on: RunLoop.main) .sink { [weak self] _ in self?.reformatText() } @@ -286,6 +290,13 @@ final class DataGridViewController: NSViewController, DataGridMountedView { } } + private func valueColorSettingsDidChange() { + let settings = AppSettingsManager.shared.valueColors + guard settings.colorsValuesByType != presenter.registry.colorsValuesByType else { return } + presenter.registry.reloadValueColors(settings) + redrawVisibleRows() + } + // Rebuilds every formatted string: the date format, NULL text or smart detection moved, or the Mac's time zone did. func reformatText() { model.display.cache.removeAll() diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+GridModels.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+GridModels.swift index c9ee39a429..7dbdd9665a 100644 --- a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+GridModels.swift +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+GridModels.swift @@ -183,6 +183,7 @@ extension MainContentCoordinator { configuration.showRowNumbers = AppSettingsManager.shared.dataGrid.showRowNumbers configuration.appliesRowSortPreferences = true configuration.appliesRowCopyFormat = true + configuration.colorsValuesByType = true configuration.supportsColumnSort = tab.tabType != .table || supportsColumnSort configuration.isEditable = isEditable configuration.editRefusalMessage = isSelected ? activeResultEditRefusal?.message : nil diff --git a/apple/TablePro/Views/Results/Comparison/ResultComparisonGrid.swift b/apple/TablePro/Views/Results/Comparison/ResultComparisonGrid.swift index 8804aabd50..e51516aecb 100644 --- a/apple/TablePro/Views/Results/Comparison/ResultComparisonGrid.swift +++ b/apple/TablePro/Views/Results/Comparison/ResultComparisonGrid.swift @@ -34,6 +34,7 @@ internal final class ResultComparisonGridModel: ObservableObject { configuration.supportsValueFilter = false configuration.supportsColumnSort = false configuration.appliesRowCopyFormat = true + configuration.colorsValuesByType = true configuration.commands = [.freezeColumns, .displayAs] return configuration } diff --git a/apple/TablePro/Views/RowInspector/FieldEditors/BooleanPickerView.swift b/apple/TablePro/Views/RowInspector/FieldEditors/BooleanPickerView.swift index ec7100e7ec..6a78e00577 100644 --- a/apple/TablePro/Views/RowInspector/FieldEditors/BooleanPickerView.swift +++ b/apple/TablePro/Views/RowInspector/FieldEditors/BooleanPickerView.swift @@ -19,6 +19,9 @@ internal struct BooleanPickerView: View { ) Text("true").tag("1") Text("false").tag("0") + if let stored = Self.unrecognizedValue(of: context.valueState) { + Text(verbatim: stored).tag(stored) + } } label: { EmptyView() } @@ -28,9 +31,7 @@ internal struct BooleanPickerView: View { .disabled(context.isReadOnly) } - /// The selection follows `valueState`, so a value the user has just picked on a NULL column - /// shows that value. Reading the stored value instead is what made the control snap back to - /// NULL over a recorded edit. + // The selection follows `valueState`, so a value just picked on a NULL column shows instead of snapping back to NULL. private var selectionBinding: Binding { Binding( get: { @@ -45,11 +46,16 @@ internal struct BooleanPickerView: View { ) } - private static func normalized(_ value: String) -> String { - let lower = value.lowercased() - if lower == "true" || lower == "1" || lower == "t" || lower == "yes" { - return "1" - } - return "0" + internal static func normalized(_ value: String) -> String { + guard let isTrue = StoredBoolean.value(of: value) else { return value.isEmpty ? "0" : value } + return isTrue ? "1" : "0" + } + + // A TINYINT(1) can hold 2, which is neither choice, so the picker shows it rather than reading it as false. + internal static func unrecognizedValue(of state: FieldValueState) -> String? { + guard FieldPickerSentinel.tag(for: state) == nil else { return nil } + let text = state.editableText + guard !text.isEmpty, StoredBoolean.value(of: text) == nil else { return nil } + return text } } diff --git a/apple/TablePro/Views/Settings/Appearance/ThemeEditorPreview.swift b/apple/TablePro/Views/Settings/Appearance/ThemeEditorPreview.swift index 6e8bdfaa02..7281a9b541 100644 --- a/apple/TablePro/Views/Settings/Appearance/ThemeEditorPreview.swift +++ b/apple/TablePro/Views/Settings/Appearance/ThemeEditorPreview.swift @@ -162,22 +162,56 @@ private final class ThemePreviewStatementBand: TextViewCoordinator { // MARK: - Grid -// Five rows that put every grid slot on screen: header and row numbers, both stripes, NULL, both booleans, -// an unfocused selection, and a modified, a deleted and an inserted row. +// Five rows that put every grid slot on screen: header and row numbers, both stripes, a value of every +// colored type, NULL, both booleans, an unfocused selection, and a modified, a deleted and an inserted row. +// The preview takes no scroll events, so fixed widths keep every column inside the editor window's default +// width, and every value but the UUID fits its column at the default grid font. internal enum ThemePreviewGridSample { internal static let selectedRow = 1 internal static let modifiedRow = 2 - internal static let modifiedColumn = 4 + internal static let modifiedColumn = 2 internal static let deletedRow = 3 internal static let insertedRow = 4 + internal struct Column { + internal let name: String + internal let type: ColumnType + internal let width: CGFloat + } + + internal static let columns: [Column] = [ + Column(name: "id", type: .integer(rawType: "INTEGER"), width: 40), + Column(name: "name", type: .text(rawType: "TEXT"), width: 68), + Column(name: "role", type: .enumType(rawType: "ENUM", values: ["admin", "staff", "guest"]), width: 56), + Column(name: "active", type: .boolean(rawType: "BOOLEAN"), width: 56), + Column(name: "joined", type: .date(rawType: "DATE"), width: 92), + Column(name: "uuid", type: .text(rawType: "UUID"), width: 104), + Column(name: "tags", type: .json(rawType: "JSON"), width: 68), + Column(name: "hash", type: .blob(rawType: "BLOB"), width: 60), + ] + internal static let tableRows: TableRows = { let values: [[PluginCellValue]] = [ - [.text("1"), .text("Ada Lovelace"), .text("ada@example.com"), .text("true"), .text("1250.00")], - [.text("2"), .text("Alan Turing"), .null, .text("false"), .text("980.50")], - [.text("3"), .text("Grace Hopper"), .text("grace@example.com"), .text("true"), .text("3100.75")], - [.text("4"), .text("Edsger Dijkstra"), .text("edsger@example.com"), .text("false"), .text("0.00")], - [.text("5"), .text("Barbara Liskov"), .text("barbara@example.com"), .text("true"), .null], + [ + .text("1"), .text("Ada"), .text("admin"), .text("true"), .text("2023-04-12"), + .text("8f14e45f-ceea-467f-a8f5-2e1c6a3d9b07"), .text(#"["ops"]"#), .bytes(Data([0x9F, 0x3A])), + ], + [ + .text("2"), .text("Alan"), .text("staff"), .text("false"), .text("2023-09-30"), + .text("c9f0f895-fb98-4b91-9c2e-5d7a1e4b3f60"), .null, .bytes(Data([0x4C, 0x21])), + ], + [ + .text("3"), .text("Grace"), .text("admin"), .text("false"), .text("2024-01-08"), + .text("45c48cce-2e2d-4fbd-8a1b-7c3e9d0f6a12"), .text(#"["dev"]"#), .bytes(Data([0x07, 0xE5])), + ], + [ + .text("4"), .text("Edsger"), .text("guest"), .text("true"), .text("2024-06-21"), + .text("d3d94468-02a4-4c3b-b1d6-9e8f7a6c5b43"), .text("[]"), .bytes(Data([0xB2, 0xD0])), + ], + [ + .text("5"), .text("Barbara"), .text("staff"), .text("true"), .text("2025-02-14"), + .text("6512bd43-d9ca-4f6e-9b2a-1c0d8e7f3a95"), .text(#"["ops"]"#), .null, + ], ] let rows = values.enumerated().map { index, row in Row( @@ -187,26 +221,26 @@ internal enum ThemePreviewGridSample { } return TableRows( rows: ContiguousArray(rows), - columns: ["id", "name", "email", "active", "balance"], - columnTypes: [ - .integer(rawType: "INTEGER"), - .text(rawType: "TEXT"), - .text(rawType: "TEXT"), - .boolean(rawType: "BOOLEAN"), - .decimal(rawType: "DECIMAL"), - ] + columns: columns.map(\.name), + columnTypes: columns.map(\.type) ) }() - // A preview: nothing to sort, filter or edit. + // A preview: nothing to sort, filter or edit, and the theme's value colors even while Color values by type is off. internal static var configuration: DataGridConfiguration { var configuration = DataGridConfiguration() configuration.showRowNumbers = true configuration.supportsValueFilter = false configuration.supportsColumnSort = false + configuration.colorsValuesByType = true + configuration.overridesValueColorSetting = true return configuration } + internal static var columnLayout: ColumnLayoutState { + ColumnLayoutState(columnWidths: Dictionary(uniqueKeysWithValues: columns.map { ($0.name, $0.width) })) + } + internal static func visualState(forRow row: Int) -> RowVisualState? { switch row { case modifiedRow: @@ -230,6 +264,7 @@ private struct ThemePreviewGrid: View { self.scope = scope _grid = StateObject(wrappedValue: HeldRowsGridAdapter( configuration: ThemePreviewGridSample.configuration, + columnLayout: ThemePreviewGridSample.columnLayout, themeScope: scope )) } diff --git a/apple/TablePro/Views/Settings/Appearance/ThemeEditorView.swift b/apple/TablePro/Views/Settings/Appearance/ThemeEditorView.swift index e16232a3b6..709db8dc4e 100644 --- a/apple/TablePro/Views/Settings/Appearance/ThemeEditorView.swift +++ b/apple/TablePro/Views/Settings/Appearance/ThemeEditorView.swift @@ -1,8 +1,7 @@ import SwiftUI import TableProTheme -/// The Theme Editor window's content: a live preview over the theme's fields. The window is chrome, -/// so only the preview wears the theme. +// The window is chrome, so only the preview wears the theme. internal struct ThemeEditorView: View { @ObservedObject internal var model: ThemeEditorModel internal let onDuplicate: () -> Void @@ -117,6 +116,8 @@ internal struct ThemeEditorView: View { private func sectionFooter(_ section: ThemeSlotSection) -> some View { if section == .window { Text("With Increase Contrast on, the window uses the system background.") + } else if section == .values { + ThemeValuesSectionFooter(contrastNote: ThemeContrastNote.message(for: section, in: model.draft)) } else if let note = ThemeContrastNote.message(for: section, in: model.draft) { Text(note) .accessibilityIdentifier("theme-editor-contrast-\(section.rawValue)") @@ -163,6 +164,31 @@ internal struct ThemeEditorView: View { } } +// Observes the settings itself, so a settings change re-renders this footer and not the editor or its preview. +private struct ThemeValuesSectionFooter: View { + let contrastNote: String? + @ObservedObject private var settingsManager = AppSettingsManager.shared + + var body: some View { + let settingNote = ThemeValueColorsNote.message( + for: .values, + colorsValuesByType: settingsManager.valueColors.colorsValuesByType + ) + if contrastNote != nil || settingNote != nil { + VStack(alignment: .leading, spacing: 4) { + if let contrastNote { + Text(contrastNote) + .accessibilityIdentifier("theme-editor-contrast-values") + } + if let settingNote { + Text(settingNote) + .accessibilityIdentifier("theme-editor-values-setting-note") + } + } + } + } +} + internal extension ThemeEditorModel.Alert { var title: String { switch self { diff --git a/apple/TablePro/Views/Settings/Appearance/ThemeSlotPresentation.swift b/apple/TablePro/Views/Settings/Appearance/ThemeSlotPresentation.swift index 68a2b3e993..7763bab49e 100644 --- a/apple/TablePro/Views/Settings/Appearance/ThemeSlotPresentation.swift +++ b/apple/TablePro/Views/Settings/Appearance/ThemeSlotPresentation.swift @@ -82,25 +82,57 @@ internal struct ThemeSlotPresentation: Equatable { internal enum ThemeContrastNote { internal static let minimumRatio = 4.5 - internal static func pair(for section: ThemeSlotSection) -> (text: ThemeSlot, background: ThemeSlot)? { + // NULL draws a placeholder, not a value. An unset value color is the grid text, which the Data Grid note checks. + internal static func checks( + for section: ThemeSlotSection, + in theme: ThemeDefinition + ) -> (texts: [ThemeSlot], background: ThemeSlot)? { switch section { - case .editor: return (.editorText, .editorBackground) - case .dataGrid: return (.gridText, .gridBackground) - case .window, .syntax, .selection, .changes: return nil + case .editor: + return ([.editorText], .editorBackground) + case .dataGrid: + return ([.gridText], .gridBackground) + case .values: + let explicit = ThemeSlotSection.values.slots.filter { $0 != .gridNullValue && theme.hasExplicitColor($0) } + return (explicit, .gridBackground) + case .window, .syntax, .selection, .changes: + return nil } } - internal static func ratio(for section: ThemeSlotSection, in theme: ThemeDefinition) -> Double? { - guard let pair = pair(for: section) else { return nil } - let background = ThemeSlotPresentation.displayColor(for: pair.background, in: theme).opaque - let text = ThemeSlotPresentation.displayColor(for: pair.text, in: theme).composited(over: background) - return text.contrastRatio(with: background) + internal static func lowest(for section: ThemeSlotSection, in theme: ThemeDefinition) -> (slot: ThemeSlot, ratio: Double)? { + guard let checks = checks(for: section, in: theme) else { return nil } + let background = ThemeSlotPresentation.displayColor(for: checks.background, in: theme).opaque + return checks.texts + .map { slot in + let text = ThemeSlotPresentation.displayColor(for: slot, in: theme).composited(over: background) + return (slot: slot, ratio: text.contrastRatio(with: background)) + } + .min { $0.ratio < $1.ratio } } internal static func message(for section: ThemeSlotSection, in theme: ThemeDefinition) -> String? { - guard let ratio = ratio(for: section, in: theme), ratio < minimumRatio else { return nil } - let formatted = ratio.formatted(.number.precision(.fractionLength(1))) - return String(format: String(localized: "Text contrast is %@:1. Aim for at least 4.5:1."), formatted) + guard let lowest = lowest(for: section, in: theme), lowest.ratio < minimumRatio else { return nil } + let formatted = lowest.ratio.formatted(.number.precision(.fractionLength(1))) + switch section { + case .values: + return String( + format: String(localized: "%1$@ contrast is %2$@:1. Aim for at least 4.5:1."), + lowest.slot.label, + formatted + ) + case .window, .editor, .syntax, .dataGrid, .selection, .changes: + return String(format: String(localized: "Text contrast is %@:1. Aim for at least 4.5:1."), formatted) + } + } +} + +internal enum ThemeValueColorsNote { + internal static func message(for section: ThemeSlotSection, colorsValuesByType: Bool) -> String? { + guard section == .values, !colorsValuesByType else { return nil } + return String( + localized: "Except NULL and booleans, these colors show in the grid only while Color values by type is on in Settings > Data." + ) } } diff --git a/apple/TablePro/Views/Settings/DataResultsSettingsView.swift b/apple/TablePro/Views/Settings/DataResultsSettingsView.swift index 1bb0f30e3e..bda0adaed7 100644 --- a/apple/TablePro/Views/Settings/DataResultsSettingsView.swift +++ b/apple/TablePro/Views/Settings/DataResultsSettingsView.swift @@ -7,6 +7,7 @@ import SwiftUI struct DataResultsSettingsView: View { @Binding var dataGrid: DataGridSettings + @Binding var valueColors: ValueColorSettings @Binding var history: HistorySettings @Binding var editor: EditorSettings @Binding var typography: TypographySettings @@ -15,7 +16,7 @@ struct DataResultsSettingsView: View { Form { TypographySection(domain: .dataGrid, settings: $typography) - DataGridSection(settings: $dataGrid) + DataGridSection(settings: $dataGrid, valueColors: $valueColors) Section("JSON Viewer") { Picker("Default view:", selection: $editor.jsonViewerPreferredMode) { @@ -35,6 +36,7 @@ struct DataResultsSettingsView: View { #Preview { DataResultsSettingsView( dataGrid: .constant(.default), + valueColors: .constant(.default), history: .constant(.default), editor: .constant(.default), typography: .constant(.default) diff --git a/apple/TablePro/Views/Settings/Sections/DataGridSection.swift b/apple/TablePro/Views/Settings/Sections/DataGridSection.swift index 38533e8a2b..433c38d4d7 100644 --- a/apple/TablePro/Views/Settings/Sections/DataGridSection.swift +++ b/apple/TablePro/Views/Settings/Sections/DataGridSection.swift @@ -7,6 +7,7 @@ import SwiftUI struct DataGridSection: View { @Binding var settings: DataGridSettings + @Binding var valueColors: ValueColorSettings var body: some View { Section { @@ -38,6 +39,9 @@ struct DataGridSection: View { Toggle("Smart value detection", isOn: $settings.enableSmartValueDetection) Toggle("Show invisible characters", isOn: $settings.showInvisibleCharacters) .help(String(localized: "Mark zero-width, control and special space characters in cells and value fields")) + Toggle("Color values by type", isOn: $valueColors.colorsValuesByType) + .help(String(localized: "Draw strings, numbers, dates and other typed values in the colors of the theme's Values section")) + .accessibilityIdentifier("color-values-by-type-toggle") Picker("Default row sort:", selection: $settings.defaultSortBehavior) { ForEach(DefaultSortBehavior.allCases) { behavior in diff --git a/apple/TablePro/Views/Settings/SettingsWindowController.swift b/apple/TablePro/Views/Settings/SettingsWindowController.swift index aa23cfa15c..d4a54f321f 100644 --- a/apple/TablePro/Views/Settings/SettingsWindowController.swift +++ b/apple/TablePro/Views/Settings/SettingsWindowController.swift @@ -7,9 +7,7 @@ import AppKit import os import SwiftUI -/// Hosts the settings panes in an AppKit window so the app no longer needs a SwiftUI `Settings` -/// scene. One instance is kept for the app's lifetime, matching the single-window scene it -/// replaces. +// An AppKit window, so the app needs no SwiftUI Settings scene. @MainActor internal final class SettingsWindowController: NSWindowController { private static var shared: SettingsWindowController? @@ -35,9 +33,7 @@ internal final class SettingsWindowController: NSWindowController { internal convenience init(registryClient: RegistryClient) { let panes = SettingsPaneTabViewController(registryClient: registryClient) - /// `NSWindow(contentViewController:)` binds the window title to the content view - /// controller's own title, so the selected pane publishes the title and nothing writes - /// `window.title` directly. + // NSWindow(contentViewController:) binds the title to the panes controller's, so nothing writes window.title. let window = NSWindow(contentViewController: panes) window.identifier = NSUserInterfaceItemIdentifier(WindowIdentifier.settings) window.keepsKeyViewLoopCurrent() @@ -47,8 +43,7 @@ internal final class SettingsWindowController: NSWindowController { window.contentMinSize = SettingsPaneTabViewController.paneSize window.setContentSize(SettingsPaneTabViewController.paneSize) window.applyAutosaveName(WindowIdentifier.settings) - /// `contentMinSize` only constrains a user drag, so a frame saved before the window had - /// a minimum is grown back to the pane minimum on restore. + // contentMinSize only limits a drag, so a frame saved before the window had a minimum is grown back here. window.setContentSize( NSSize( width: max(window.contentLayoutRect.width, SettingsPaneTabViewController.paneSize.width), @@ -60,8 +55,7 @@ internal final class SettingsWindowController: NSWindowController { } internal final class SettingsPaneTabViewController: NSTabViewController { - /// Wide enough for twelve items in a `.preference` toolbar. At 720 the last of them - /// falls into an overflow chevron, which is where a pane nobody can find comes from. + // Fits twelve items in a .preference toolbar; at 720 the last one hides behind an overflow chevron. internal static let paneSize = NSSize(width: 800, height: 500) internal static let paneOrder: [SettingsPane] = SettingsPane.allCases @@ -85,8 +79,7 @@ internal final class SettingsPaneTabViewController: NSTabViewController { } internal func select(_ pane: SettingsPane?) { - /// `loadViewIfNeeded()` is macOS 14. Reading `view` is what it does: the getter loads - /// the view when it has not been loaded yet. + // Before macOS 14, reading view loads it, which is all loadViewIfNeeded() does. if #available(macOS 14.0, *) { loadViewIfNeeded() } else { @@ -103,8 +96,7 @@ internal final class SettingsPaneTabViewController: NSTabViewController { override func viewDidLoad() { super.viewDidLoad() tabStyle = .toolbar - /// Adding the first item selects it, which persists `.general` over the pane a caller - /// asked for, so the pane to restore is read while no item exists yet. + // Adding the first item selects and persists .general, so the saved pane is read before any item exists. let restored = persistedPane for pane in Self.paneOrder { addTabViewItem(makeTabViewItem(for: pane)) @@ -129,9 +121,7 @@ internal final class SettingsPaneTabViewController: NSTabViewController { ) .environment(\.appServices, .live) let hosting = NSHostingController(rootView: content) - /// A tab child publishes no size to the window, which owns its minimum through - /// `contentMinSize`. Its title does travel: `NSTabViewController` mirrors the selected - /// child's title into its own, and the window's title is bound to that. + // The window owns the minimum size; the title still reaches it, as NSTabViewController mirrors the selected child's. hosting.sizingOptions = [] hosting.title = pane.title @@ -174,6 +164,7 @@ private struct SettingsPaneContent: View { case .data: DataResultsSettingsView( dataGrid: $settingsManager.dataGrid, + valueColors: $settingsManager.valueColors, history: $settingsManager.history, editor: $settingsManager.editor, typography: $settingsManager.typography diff --git a/apple/TableProTests/Core/DataGrid/RowDisplayCacheTests.swift b/apple/TableProTests/Core/DataGrid/RowDisplayCacheTests.swift index 0710c71a8e..8dabe05674 100644 --- a/apple/TableProTests/Core/DataGrid/RowDisplayCacheTests.swift +++ b/apple/TableProTests/Core/DataGrid/RowDisplayCacheTests.swift @@ -211,6 +211,36 @@ struct RowDisplayCacheTests { #expect(cache.highlight(forID: .existing(3)) == nil) } + @Test("A cell's formatted flag is written with its text and cleared with it") + func formattedFlagFollowsTheText() { + let cache = RowDisplayCache() + let box = makeBox(Array(repeating: nil, count: 70)) + box.setText("uuid", at: 1, isFormatted: true) + box.setText("hex", at: 2, isFormatted: false) + box.setText("date", at: 65, isFormatted: true) + cache.setBox(box, forID: .existing(0)) + + #expect(box.isFormatted(at: 1)) + #expect(!box.isFormatted(at: 2)) + #expect(box.isFormatted(at: 65)) + #expect(!box.isFormatted(at: 69)) + #expect(!box.isFormatted(at: 200)) + #expect(!box.isFormatted(at: -1)) + + box.setText("raw", at: 1, isFormatted: false) + #expect(!box.isFormatted(at: 1)) + box.setText("uuid", at: 1, isFormatted: true) + + cache.clearValues(forDataIndexes: [1]) + #expect(box.values[1] == nil) + #expect(!box.isFormatted(at: 1)) + #expect(box.isFormatted(at: 65)) + + cache.clearValues(forID: .existing(0)) + #expect(box.values[65] == nil) + #expect(!box.isFormatted(at: 65)) + } + @Test("Inserted row IDs of both kinds round-trip") func mixedRowIDKinds() { let cache = RowDisplayCache() diff --git a/apple/TableProTests/Core/Export/TableTransferServiceTests.swift b/apple/TableProTests/Core/Export/TableTransferServiceTests.swift index 127e7522a9..61c2ddb5b6 100644 --- a/apple/TableProTests/Core/Export/TableTransferServiceTests.swift +++ b/apple/TableProTests/Core/Export/TableTransferServiceTests.swift @@ -9,7 +9,6 @@ import Testing @testable import TablePro -/// Serves one table's rows as a source, and records what reaches it as a destination. private final class TransferStubDriver: PluginDriverStub, @unchecked Sendable { let header: [String] let rows: [PluginRow] @@ -104,8 +103,7 @@ struct TableTransferServiceTests { #expect(values["email"] == .text("ada@example.com")) } - /// A driver that stops sending values once the rest of a row is null would otherwise lose the - /// whole row: the sink writes by column name, and a missing key is not a null. + // A driver can stop sending values once the rest of a row is null, and the sink writes by column name: a missing key is not a null. @Test("A row shorter than its header is padded with nulls") func shortRowIsPadded() { let values = TableTransferService.dictionary( @@ -143,8 +141,7 @@ struct TableTransferServiceTests { #expect(values["missing"] == .null) } - /// The transfer moves rows, so a request naming only definition objects has nothing to do and - /// must say so rather than reporting a successful transfer of nothing. + // Refused rather than reported as a successful transfer of nothing. @MainActor @Test("A request with no row-carrying object is refused") func requestWithoutTablesIsRefused() { let service = TableTransferService() @@ -195,8 +192,7 @@ struct TableTransferServiceTests { } } - /// The server refused the INSERT only after "Delete existing rows first" had run, and with no - /// transaction around the table the deletion stood: the destination was left empty. + // The server refused the INSERT after "Delete existing rows first" ran outside a transaction, which left the destination empty. @MainActor @Test("A contested mapping deletes nothing on the destination") func contestedMappingDeletesNothing() async { let source = TransferStubDriver(header: ["first_name", "last_name"], rows: [[.text("Ada"), .text("Lovelace")]]) @@ -226,8 +222,7 @@ struct TableTransferServiceTests { #expect(destination.executedQueries.isEmpty) } - /// `Name` and `name` both matched a lone `name`, and even with only one of them mapped the - /// sink folded the other onto it, so the INSERT named the column twice. + // `Name` and `name` both matched a lone `name`, and the sink folded the unmapped one onto it, so the INSERT named the column twice. @MainActor @Test("A source column that differs only by case from a mapped one is not written twice") func caseTwinIsNotWrittenTwice() async throws { let source = TransferStubDriver( @@ -351,7 +346,7 @@ struct TableTransferServiceTests { #expect(destination.executedParameters.last == [.text("5"), .text("1")]) } - // MySQL's header says BIT without the width, so the digits take the catalog's BIT(8). + // The digits take the catalog's BIT(8), whatever width the header names. @MainActor @Test("A MySQL BIT transferred to a PostgreSQL bit varying arrives as its digits, at the source's width") func mysqlBitReachesPostgresAsItsDigits() async throws { let source = TransferStubDriver( diff --git a/apple/TableProTests/Core/Operations/AppSettingsCategoryParityTests.swift b/apple/TableProTests/Core/Operations/AppSettingsCategoryParityTests.swift index 29f8559502..9c1b6f2d21 100644 --- a/apple/TableProTests/Core/Operations/AppSettingsCategoryParityTests.swift +++ b/apple/TableProTests/Core/Operations/AppSettingsCategoryParityTests.swift @@ -23,4 +23,25 @@ struct AppSettingsCategoryParityTests { func notificationsSync() { #expect(AppSettingsCategory.synced.contains(AppSettingsCategory.notifications)) } + + @Test("Value colors sync as their own category, apart from the Data Grid blob an older build rewrites") + func valueColorsSyncAsTheirOwnCategory() { + #expect(AppSettingsCategory.synced.contains(AppSettingsCategory.valueColors)) + #expect(AppSettingsCategory.valueColors != AppSettingsCategory.dataGrid) + } + + @MainActor + @Test("Value colors persist under their own key, read off when never saved, and reset with the rest") + func valueColorsStorageRoundTrip() throws { + let pool = EphemeralDefaultsPool() + let storage = AppSettingsStorage(userDefaults: try pool.make().defaults) + #expect(storage.loadValueColors() == .default) + + storage.saveValueColors(ValueColorSettings(colorsValuesByType: true)) + #expect(storage.loadValueColors().colorsValuesByType) + #expect(storage.loadDataGrid() == .default) + + storage.resetToDefaults() + #expect(storage.loadValueColors() == .default) + } } diff --git a/apple/TableProTests/Core/Services/ColumnTypeClassifierTests.swift b/apple/TableProTests/Core/Services/ColumnTypeClassifierTests.swift index 29832b00ab..1d9304d6a8 100644 --- a/apple/TableProTests/Core/Services/ColumnTypeClassifierTests.swift +++ b/apple/TableProTests/Core/Services/ColumnTypeClassifierTests.swift @@ -50,6 +50,20 @@ struct ColumnTypeClassifierTests { #expect(ColumnTypeClassifier(family: .postgres).classify(rawTypeName: "boolean").isBooleanType) } + // The MySQL driver names a BIT with its width and a BOOLEAN as TINYINT(1). + @Test("A MySQL BIT wider than one bit is a number; BIT, BIT(1) and TINYINT(1) are booleans") + func mysqlBitWidthDecidesTheKind() { + let mysql = ColumnTypeClassifier(family: .mysql) + #expect(mysql.classify(rawTypeName: "BIT(8)") == .integer(rawType: "BIT(8)")) + #expect(mysql.classify(rawTypeName: "bit(64)") == .integer(rawType: "bit(64)")) + #expect(mysql.classify(rawTypeName: "BIT(1)").isBooleanType) + #expect(mysql.classify(rawTypeName: "BIT").isBooleanType) + #expect(mysql.classify(rawTypeName: "TINYINT(1)").isBooleanType) + #expect(isInteger(mysql.classify(rawTypeName: "TINYINT"))) + #expect(ColumnTypeClassifier(family: .postgres).classify(rawTypeName: "bit(8)") == .text(rawType: "bit(8)")) + #expect(ColumnTypeClassifier().classify(rawTypeName: "BIT(8)").isBooleanType) + } + @Test("A compared column reads BIT by each side's family") func comparedColumnReadsBitBySide() { let column = CompareColumn( @@ -787,8 +801,7 @@ struct ColumnTypeClassifierTests { #expect(isTimestamp(classifier.classify(rawTypeName: rawTypeName))) } - /// Every other engine's DATE is a calendar date, so Oracle's own DATE reaches the classifier as a hint - /// rather than through this name. + // Oracle's DATE reaches the classifier as a hint, so the bare name stays the calendar date every other engine means. @Test("DATE stays a calendar date") func dateStaysADate() { #expect(classifier.classify(rawTypeName: "DATE") == .date(rawType: "DATE")) @@ -959,8 +972,7 @@ struct ColumnTypeClassifierTests { #expect(classifier.classify(rawTypeName: "jsonb").arrayElementEditor == nil) } - /// The badge vocabulary is semantic rather than the SQL spelling, so `jsonb[]` badges as - /// `json[]` exactly as scalar `jsonb` badges as `json`. #2897 read that as a lost `b`. + // The badge is semantic, so `jsonb[]` badges as `json[]` as scalar `jsonb` badges as `json`. #2897 read that as a lost `b`. @Test("A jsonb array keeps its raw type name behind the semantic badge") func keepsRawTypeBehindBadge() { let type = classifier.classify(rawTypeName: "jsonb[]") @@ -976,10 +988,7 @@ struct ColumnTypeClassifierTests { #expect(classifier.classify(rawTypeName: "text[]").displayName == "Text Array") } - /// PostgreSQL spells a domain's array through `format_type`, which puts the parameters - /// before the brackets. The base/params split dropped everything after the last `)`, so the - /// `[]` was lost and the column classified as its element: a `bit(8)[]` got the boolean - /// dropdown, which writes a scalar into an array column. + // PostgreSQL's format_type puts the parameters before the brackets, and the old split dropped the `[]`: `bit(8)[]` got the boolean dropdown. @Test("A parameterized array keeps its brackets") func classifiesParameterizedArrays() { let numeric = classifier.classify(rawTypeName: "numeric(10,2)[]") @@ -996,8 +1005,7 @@ struct ColumnTypeClassifierTests { #expect(classifier.classify(rawTypeName: "timestamp(3) with time zone[]").arrayElement != nil) } - /// The app's own spelling for an enum array puts the `[]` on the base and the labels in the - /// parentheses, which is the one array form the suffix test cannot see. + // The app's own `ENUM[](labels)` spelling is the one array form the suffix test cannot see. @Test("The enum array spelling still resolves its labels") func classifiesEnumArraySpelling() { let type = classifier.classify(rawTypeName: "ENUM[](mood)") @@ -1069,10 +1077,7 @@ struct ColumnTypeClassifierTests { return false } - /// `GEO_POINT` ends in `INT`, and `classifyByPattern` tests `hasSuffix("INT")` before any - /// spatial arm, so an Elasticsearch `geo_point` column classified as an integer. That drove - /// its cell alignment, its sort comparator and its filter operators, all wrong, with or - /// without a map. + // `GEO_POINT` ends in `INT` and the `hasSuffix("INT")` arm ran before any spatial one, so `geo_point` classified as an integer. @Test("geo_point is spatial, not an integer") func geoPointIsNotAnInteger() { for raw in ["geo_point", "GEO_POINT", "Geo_Point"] { @@ -1081,8 +1086,7 @@ struct ColumnTypeClassifierTests { } } - /// MySQL 8.0.11 renamed GEOMETRYCOLLECTION, and its catalog reports `geomcollection` for a - /// column declared with either spelling. Measured on MySQL 8.4.11. + // MySQL 8.0.11 renamed GEOMETRYCOLLECTION, and its catalog reports `geomcollection` for either spelling. Measured on MySQL 8.4.11. @Test("MySQL's own GEOMCOLLECTION spelling is spatial") func geomCollectionSpelling() { for raw in ["geomcollection", "GEOMCOLLECTION", "GEOMETRYCOLLECTION"] { @@ -1105,7 +1109,6 @@ struct ColumnTypeClassifierTests { #expect(isSpatial(classifier.classify(rawTypeName: "Nullable(Point)"))) } - /// The new entries must not drag neighbouring names into the spatial family. @Test("Neighbouring names keep their classification") func neighboursUnchanged() { #expect(classifier.classify(rawTypeName: "INT") == .integer(rawType: "INT")) diff --git a/apple/TableProTests/Core/Sync/SyncCategoryRefetchTests.swift b/apple/TableProTests/Core/Sync/SyncCategoryRefetchTests.swift index f3d157d2eb..9a058af349 100644 --- a/apple/TableProTests/Core/Sync/SyncCategoryRefetchTests.swift +++ b/apple/TableProTests/Core/Sync/SyncCategoryRefetchTests.swift @@ -27,9 +27,23 @@ struct SyncCategoryRefetchTests { #expect(!content.contains(SyncRecordType.settings.rawValue)) #expect(!content.contains(SyncCoordinator.themeContentKind)) #expect(!content.contains(SyncCoordinator.formatterContentKind)) + #expect(!content.contains(SyncCoordinator.valueColorsContentKind)) #expect(content.contains(SyncRecordType.connection.rawValue)) } + @Test("A Mac upgraded from a build that skipped value colors records fetches the zone once") + func upgradeRefetchesSkippedValueColorsRecords() throws { + let pool = EphemeralDefaultsPool() + let metadata = SyncMetadataStorage(userDefaults: try pool.make().defaults) + var older = SyncCoordinator.readableContent + older.remove(SyncCoordinator.valueColorsContentKind) + _ = metadata.adoptReadableRecordTypes(older) + + #expect(SyncCoordinator.readableContent(for: .default).contains(SyncCoordinator.valueColorsContentKind)) + #expect(metadata.adoptReadableRecordTypes(SyncCoordinator.readableContent)) + #expect(!metadata.adoptReadableRecordTypes(SyncCoordinator.readableContent)) + } + @Test("Turning Saved Queries on fetches the zone once and brings back a query saved elsewhere while it was off") func turningACategoryOnRefetches() async throws { let favorite = SQLFavorite(name: "Revenue", query: "SELECT 1") diff --git a/apple/TableProTests/Core/Sync/SyncConflictRebaseTests.swift b/apple/TableProTests/Core/Sync/SyncConflictRebaseTests.swift index a3fc339e00..35c9cfe0d8 100644 --- a/apple/TableProTests/Core/Sync/SyncConflictRebaseTests.swift +++ b/apple/TableProTests/Core/Sync/SyncConflictRebaseTests.swift @@ -196,8 +196,13 @@ struct SyncConflictRebaseTests { } @Test( - "Shortcuts, AI providers and a theme both Macs changed go up whole from this Mac, never mixed", - arguments: [AppSettingsCategory.keyboard, AppSettingsCategory.ai, ThemeCatalog.syncCategory(for: "ocean")] + "Shortcuts, AI providers and a theme's records both Macs changed go up whole from this Mac, never mixed", + arguments: [ + AppSettingsCategory.keyboard, + AppSettingsCategory.ai, + ThemeCatalog.syncCategory(for: "ocean"), + ThemeCatalog.syncCategory(for: "ocean", schema: 4), + ] ) func wholeCategoriesAreNotMixed(category: String) throws { let base = SyncRecordMapper.toCKRecord(category: category, settingsData: Data(#"{"a":1,"b":1}"#.utf8), in: Self.zoneID) diff --git a/apple/TableProTests/Core/Sync/SyncCoordinatorThemeTests.swift b/apple/TableProTests/Core/Sync/SyncCoordinatorThemeTests.swift index 06087c619b..78fc9f18df 100644 --- a/apple/TableProTests/Core/Sync/SyncCoordinatorThemeTests.swift +++ b/apple/TableProTests/Core/Sync/SyncCoordinatorThemeTests.swift @@ -99,9 +99,24 @@ struct SyncCoordinatorThemeTests { ) } + nonisolated private static func valueTheme(_ id: String, background: String = "#224466") throws -> ThemeDefinition { + var values = try theme(id, background: background) + values.overrides[.gridString] = try ThemeColor(validating: "#F1FA8C") + values.overrides[.gridNumber] = try ThemeColor(validating: "#BD93F9") + return values + } + private static func themeRecord(_ theme: ThemeDefinition) throws -> CKRecord { SyncRecordMapper.toCKRecord( category: ThemeCatalog.syncCategory(for: theme.id), + settingsData: try ThemeFileFormat.encode(theme.restricted(toSchema: ThemeSchema.oldestReadable)), + in: SyncTestEnvironment.zoneID + ) + } + + private static func valueRecord(_ theme: ThemeDefinition) throws -> CKRecord { + SyncRecordMapper.toCKRecord( + category: ThemeCatalog.syncCategory(for: theme.id, schema: 4), settingsData: try ThemeFileFormat.encode(theme), in: SyncTestEnvironment.zoneID ) @@ -118,6 +133,13 @@ struct SyncCoordinatorThemeTests { ) } + private static func deletion(ofValuesOf id: String) -> CKRecord.ID { + CKRecord.ID( + recordName: SyncRecordType.settings.recordName(for: ThemeCatalog.syncCategory(for: id, schema: 4)), + zoneID: SyncTestEnvironment.zoneID + ) + } + private static func pull(_ records: [CKRecord], deleting deletions: [CKRecord.ID] = []) -> PullResult { PullResult(changedRecords: records, deletedRecordIDs: deletions, newToken: nil) } @@ -143,6 +165,108 @@ struct SyncCoordinatorThemeTests { #expect(harness.tracker.dirtyRecords(for: .settings).isEmpty) } + @Test("A new theme that arrives with its value record in one pull lands whole, whichever comes first", arguments: [true, false]) + func newThemeArrivesWithItsValueRecord(themeFirst: Bool) async throws { + let harness = try Harness(label: "theme-values-arrival") + let synced = try Self.valueTheme("user.values") + let records = [try Self.themeRecord(synced), try Self.valueRecord(synced)] + + let acknowledged = await harness.coordinator.applyPullResult(Self.pull(themeFirst ? records : Array(records.reversed()))) + + #expect(acknowledged) + #expect(harness.catalog.storedTheme(id: synced.id) == synced) + #expect(harness.tracker.dirtyRecords(for: .settings).isEmpty) + } + + @Test("A value record that came before its theme waits in the cache for it") + func valueRecordWaitsInTheCache() async throws { + let harness = try Harness(label: "theme-values-cache") + let synced = try Self.valueTheme("user.values") + + let valuesFirst = await harness.coordinator.applyPullResult(Self.pull([try Self.valueRecord(synced)])) + #expect(valuesFirst) + #expect(!harness.fileExists(synced.id)) + let themeLater = await harness.coordinator.applyPullResult(Self.pull([try Self.themeRecord(synced)])) + + #expect(themeLater) + #expect(harness.catalog.storedTheme(id: synced.id) == synced) + } + + // A full fetch lists every record the zone holds, so the cached value record was deleted since. + @Test("A full fetch that lacks a value record leaves a new theme without the cached one's colors") + func fullFetchIgnoresACachedValueRecord() async throws { + let harness = try Harness(label: "theme-values-full-fetch") + let synced = try Self.valueTheme("user.values") + harness.environment.recordCache.store([try Self.valueRecord(synced)]) + + let acknowledged = await harness.coordinator.applyPullResult( + Self.pull([try Self.themeRecord(synced)]), + isFullFetch: true + ) + + #expect(acknowledged) + #expect(harness.catalog.storedTheme(id: synced.id) == synced.restricted(toSchema: ThemeSchema.oldestReadable)) + } + + // Without the drop, a delta pull that brings the theme back would take its colors from the stale copy. + @Test("A full fetch drops a cached value record it does not list, so a theme made again later takes no stale colors") + func fullFetchDropsAStaleValueRecord() async throws { + let harness = try Harness(label: "theme-values-stale-cache") + let synced = try Self.valueTheme("user.values") + let stale = try Self.valueRecord(synced) + harness.environment.recordCache.store([stale]) + + let fetched = await harness.coordinator.applyPullResult(Self.pull([try Self.themeRecord(synced)]), isFullFetch: true) + #expect(fetched) + #expect(!harness.environment.recordCache.holdsRecord(named: stale.recordID.recordName)) + + let deleted = await harness.coordinator.applyPullResult(Self.pull([], deleting: [Self.deletion(ofTheme: synced.id)])) + #expect(deleted) + #expect(harness.catalog.storedTheme(id: synced.id) == nil) + let remade = await harness.coordinator.applyPullResult(Self.pull([try Self.themeRecord(synced)])) + + #expect(remade) + #expect(harness.catalog.storedTheme(id: synced.id) == synced.restricted(toSchema: ThemeSchema.oldestReadable)) + } + + @Test("A value record the pull listed but could not read is not filled in from the cache") + func unreadValueRecordIsNotTakenFromTheCache() async throws { + let harness = try Harness(label: "theme-values-unread") + let synced = try Self.valueTheme("user.values") + let cached = try Self.valueRecord(synced) + harness.environment.recordCache.store([cached]) + + let acknowledged = await harness.coordinator.applyPullResult( + PullResult( + changedRecords: [try Self.themeRecord(synced)], + deletedRecordIDs: [], + newToken: nil, + unreadRecordIDs: [cached.recordID] + ) + ) + + #expect(acknowledged) + #expect(harness.catalog.storedTheme(id: synced.id) == synced.restricted(toSchema: ThemeSchema.oldestReadable)) + } + + // 0.86 reads only `theme.`, so its edit carries no value color and must not clear the ones set here. + @Test("An edit from a Mac that reads only format 3 keeps the value colors set here") + func formatThreeEditKeepsValueColors() async throws { + let harness = try Harness(label: "theme-values-older-edit") + let local = try Self.valueTheme("user.values") + try harness.writeOnDisk(local) + harness.metadata.themeSeedState = .done + let edited = try Self.theme("user.values", background: "#336699") + + let acknowledged = await harness.coordinator.applyPullResult(Self.pull([try Self.themeRecord(edited)])) + + #expect(acknowledged) + var expected = local + expected.overrides[.editorBackground] = edited.overrides[.editorBackground] + #expect(harness.catalog.storedTheme(id: local.id) == expected) + #expect(harness.tracker.dirtyRecords(for: .settings).isEmpty) + } + @Test("A theme record that fails validation is skipped, and the pull is still acknowledged") func invalidThemeIsSkipped() async throws { let harness = try Harness(label: "theme-invalid") @@ -195,13 +319,30 @@ struct SyncCoordinatorThemeTests { try await waitUntil { harness.engine.pair.dark == BuiltInThemes.dark } } + @Test("A value record deleted on another Mac clears the value colors here, with no tombstone sent back") + func pulledValueDeletionClearsValueColors() async throws { + let harness = try Harness(label: "theme-values-deletion") + let values = try Self.valueTheme("user.values") + try harness.writeOnDisk(values) + + let acknowledged = await harness.coordinator.applyPullResult(Self.pull([], deleting: [Self.deletion(ofValuesOf: values.id)])) + + #expect(acknowledged) + #expect(harness.catalog.storedTheme(id: values.id) == values.restricted(toSchema: ThemeSchema.oldestReadable)) + #expect(harness.tracker.tombstonedIds(for: .settings).isEmpty) + #expect(harness.tracker.dirtyRecords(for: .settings).isEmpty) + } + @Test("A theme this Mac moved to the Trash stays gone when an edit of it arrives") func localTombstoneBeatsARemoteEdit() async throws { let harness = try Harness(label: "theme-tombstone") let night = try Self.theme("user.night") try harness.writeOnDisk(night) try harness.catalog.trash(id: night.id) - #expect(harness.tracker.tombstonedIds(for: .settings) == [ThemeCatalog.syncCategory(for: night.id)]) + #expect( + harness.tracker.tombstonedIds(for: .settings) + == [ThemeCatalog.syncCategory(for: night.id), ThemeCatalog.syncCategory(for: night.id, schema: 4)] + ) let edited = try Self.theme("user.night", background: "#336699") let acknowledged = await harness.coordinator.applyPullResult(Self.pull([try Self.themeRecord(edited)])) @@ -234,6 +375,46 @@ struct SyncCoordinatorThemeTests { #expect(harness.tracker.tombstonedIds(for: .settings).isEmpty) } + // A Mac on 0.86 reads only format 3, so every theme reaches it as format 3 and the value colors go apart. + @Test("A theme goes out as format 3 and its value colors as a format 4 record, and its trash deletes both") + func valueColorsPushInTheirOwnRecord() async throws { + let harness = try Harness(label: "theme-push-values") + let night = try Self.theme("user.night") + let values = try Self.valueTheme("user.values") + try harness.catalog.save(night) + try harness.catalog.save(values) + + #expect(await harness.coordinator.runSyncCycle() == nil) + let pushed = await harness.transport.pushedRecords + + let base = try Self.pushedTheme(ThemeCatalog.syncCategory(for: values.id), in: pushed) + #expect(base.schema == 3) + #expect(base.theme == values.restricted(toSchema: ThemeSchema.oldestReadable)) + let newer = try Self.pushedTheme(ThemeCatalog.syncCategory(for: values.id, schema: 4), in: pushed) + #expect(newer.schema == 4) + #expect(newer.theme == values) + #expect(try Self.pushedTheme(ThemeCatalog.syncCategory(for: night.id), in: pushed).schema == 3) + let nightValues = ThemeCatalog.syncCategory(for: night.id, schema: 4) + #expect(!pushed.contains { SyncRecordMapper.settingsCategory(from: $0) == nightValues }) + + try harness.catalog.trash(id: values.id) + #expect(await harness.coordinator.runSyncCycle() == nil) + let deletions = await harness.transport.pushedDeletions + #expect(deletions.contains(Self.deletion(ofTheme: values.id))) + #expect(deletions.contains(Self.deletion(ofValuesOf: values.id))) + #expect(harness.tracker.tombstonedIds(for: .settings).isEmpty) + } + + private static func pushedTheme( + _ category: String, + in records: [CKRecord] + ) throws -> (schema: Int?, theme: ThemeDefinition) { + let record = try #require(records.last { SyncRecordMapper.settingsCategory(from: $0) == category }) + let data = try #require(SyncRecordMapper.settingsData(from: record)) + let root = try JSONSerialization.jsonObject(with: data) as? [String: Any] + return (root?["schema"] as? Int, try ThemeFileFormat.decode(data)) + } + @Test("Turning sync on marks every theme on this Mac") func enablingSyncMarksThemes() throws { let harness = try Harness(label: "theme-enable") diff --git a/apple/TableProTests/Core/Utilities/JsonRowConverterTests.swift b/apple/TableProTests/Core/Utilities/JsonRowConverterTests.swift index 759614a413..3d2f554148 100644 --- a/apple/TableProTests/Core/Utilities/JsonRowConverterTests.swift +++ b/apple/TableProTests/Core/Utilities/JsonRowConverterTests.swift @@ -250,6 +250,14 @@ struct JsonRowConverterTests { #expect(result.contains("\"maybe\"")) } + @Test("A boolean column holding another integer writes that number") + func booleanColumnOtherInteger() { + let converter = makeConverter(columns: ["flag"], columnTypes: [.boolean(rawType: "TINYINT(1)")]) + let result = converter.generateJson(rows: [["2"]]) + #expect(result.contains(": 2")) + #expect(!result.contains("\"2\"")) + } + // MARK: - JSON @Test("Valid JSON column is embedded verbatim") @@ -384,8 +392,7 @@ struct JsonRowConverterTests { // MARK: - Default-value marker - /// A pending inserted row carries the default marker for columns the server fills in. The grid - /// draws it as a placeholder, so JSON has to agree instead of printing the internal token. + // A pending insert carries the default marker for server-filled columns; the grid draws a placeholder, so JSON must not print the token. @Test("The default-value marker renders as null, not as literal text") func defaultMarkerRendersAsNull() { let converter = makeConverter( diff --git a/apple/TableProTests/Models/Settings/SettingsFieldDecodingTests.swift b/apple/TableProTests/Models/Settings/SettingsFieldDecodingTests.swift index 56e576b32f..61d44164de 100644 --- a/apple/TableProTests/Models/Settings/SettingsFieldDecodingTests.swift +++ b/apple/TableProTests/Models/Settings/SettingsFieldDecodingTests.swift @@ -66,6 +66,24 @@ struct SettingsFieldDecodingTests { #expect(decoded.rowCopyFormat == .csvWithHeaders) } + @Test("Color values by type is off by default and in stored value colors without it, and a choice round-trips") + func valueColorsDefaultOff() throws { + #expect(ValueColorSettings.default.colorsValuesByType == false) + #expect(try decode(ValueColorSettings.self, "{}").colorsValuesByType == false) + #expect(try decode(ValueColorSettings.self, #"{"colorsValuesByType":"yes"}"#).colorsValuesByType == false) + + let chosen = ValueColorSettings(colorsValuesByType: true) + let encoded = String(decoding: try JSONEncoder().encode(chosen), as: UTF8.self) + #expect(try decode(ValueColorSettings.self, encoded) == chosen) + } + + // An older build rewrites the Data Grid blob with only the keys it knows. + @Test("The Data Grid settings no longer carry Color values by type") + func dataGridBlobLeavesValueColorsOut() throws { + let encoded = String(decoding: try JSONEncoder().encode(DataGridSettings.default), as: UTF8.self) + #expect(!encoded.contains("colorsValuesByType")) + } + @Test("A bad MCP value falls back alone and keeps the secure default") func mcpKeepsOtherFields() throws { let settings = try decode( diff --git a/apple/TableProTests/Theme/ThemeCatalogSyncTests.swift b/apple/TableProTests/Theme/ThemeCatalogSyncTests.swift index 21bf318034..8487796b04 100644 --- a/apple/TableProTests/Theme/ThemeCatalogSyncTests.swift +++ b/apple/TableProTests/Theme/ThemeCatalogSyncTests.swift @@ -2,10 +2,6 @@ // ThemeCatalogSyncTests.swift // TableProTests // -// Each user theme syncs as the settings record `theme.`. The catalog marks it on every edit, -// tombstones it after a move to the Trash, and writes what another Mac sent without marking -// anything, after treating it as untrusted input. -// import AppKit import Foundation @@ -94,10 +90,30 @@ struct ThemeCatalogSyncTests { ) } + nonisolated private static func valueTheme( + _ id: String, + name: String? = nil, + background: String = "#101010", + string: String = "#F1FA8C" + ) throws -> ThemeDefinition { + var values = try theme(id, name: name, background: background) + values.overrides[.gridString] = try ThemeColor(validating: string) + values.overrides[.gridNumber] = try ThemeColor(validating: "#BD93F9") + return values + } + nonisolated private static func category(_ id: String) -> String { ThemeCatalog.syncCategory(for: id) } + nonisolated private static func valuesCategory(_ id: String) -> String { + ThemeCatalog.syncCategory(for: id, schema: 4) + } + + nonisolated private static func schema(of payload: Data) throws -> Int? { + (try JSONSerialization.jsonObject(with: payload) as? [String: Any])?["schema"] as? Int + } + // MARK: - Local changes @Test("Save, rename, duplicate and import each mark the theme's record dirty") @@ -125,6 +141,77 @@ struct ThemeCatalogSyncTests { #expect(harness.tombstoned.isEmpty) } + @Test("A theme with value colors marks its value record too, and only its theme record stays format 3") + func valueColorsSyncInTheirOwnRecord() throws { + let harness = try Harness() + defer { harness.remove() } + let values = try Self.valueTheme("user.values") + + try harness.catalog.save(values) + + let both: Set = [Self.category(values.id), Self.valuesCategory(values.id)] + #expect(harness.dirty == both) + #expect(harness.catalog.syncCategories == [Self.category(values.id), Self.valuesCategory(values.id)]) + let base = try #require(harness.catalog.syncPayload(forCategory: Self.category(values.id))) + #expect(try Self.schema(of: base) == 3) + #expect(try ThemeFileFormat.decode(base) == values.restricted(toSchema: ThemeSchema.oldestReadable)) + let newer = try #require(harness.catalog.syncPayload(forCategory: Self.valuesCategory(values.id))) + #expect(try Self.schema(of: newer) == 4) + #expect(try ThemeFileFormat.decode(newer) == values) + } + + @Test("Clearing a theme's last value color deletes its value record, and a theme that never had one deletes nothing") + func clearingValueColorsDeletesTheirRecord() throws { + let harness = try Harness() + defer { harness.remove() } + let values = try Self.valueTheme("user.values") + try harness.catalog.save(values) + + try harness.catalog.save(values.restricted(toSchema: ThemeSchema.oldestReadable)) + + #expect(harness.dirty == [Self.category(values.id)]) + #expect(harness.tombstoned == [Self.valuesCategory(values.id)]) + #expect(harness.catalog.syncPayload(forCategory: Self.valuesCategory(values.id)) == nil) + + let night = try Self.theme("user.night") + try harness.catalog.save(night) + try harness.catalog.save(try Self.theme("user.night", background: "#202020")) + #expect(harness.tombstoned == [Self.valuesCategory(values.id)]) + } + + enum OwnedEdit: String, CaseIterable, Sendable { + case nothing, rename, editorColor, valueColor + } + + // Each record pushes whole, so one marked for keys it does not own would revert another Mac's edit of them. + @Test("An edit of a theme marks only the record that owns the keys it changed", arguments: OwnedEdit.allCases) + func editMarksOnlyTheRecordThatOwnsItsKeys(_ edit: OwnedEdit) throws { + let harness = try Harness() + defer { harness.remove() } + let values = try Self.valueTheme("user.values") + try harness.write(values) + harness.reopen() + var edited = values + + switch edit { + case .nothing: + try harness.catalog.save(edited) + #expect(harness.dirty.isEmpty) + case .rename: + try harness.catalog.rename(id: values.id, to: "Renamed") + #expect(harness.dirty == [Self.category(values.id)]) + case .editorColor: + edited.overrides[.editorBackground] = try ThemeColor(validating: "#202020") + try harness.catalog.save(edited) + #expect(harness.dirty == [Self.category(values.id)]) + case .valueColor: + edited.overrides[.gridNumber] = try ThemeColor(validating: "#FF5555") + try harness.catalog.save(edited) + #expect(harness.dirty == [Self.valuesCategory(values.id)]) + } + #expect(harness.tombstoned.isEmpty) + } + @Test("Loading the folder and reloading on activation mark nothing") func loadsMarkNothing() throws { let harness = try Harness() @@ -140,7 +227,7 @@ struct ThemeCatalogSyncTests { #expect(harness.tombstoned.isEmpty) } - @Test("Moving a theme to the Trash tombstones its record only once the file is gone") + @Test("Moving a theme to the Trash tombstones its records only once the file is gone") func trashTombstonesAfterTheMove() throws { let tombstonedDuringMove = OSAllocatedUnfairLock(initialState: nil) let probe = TrackerProbe() @@ -161,11 +248,62 @@ struct ThemeCatalogSyncTests { try harness.catalog.trash(id: night.id) #expect(tombstonedDuringMove.withLock { $0 } == false) - #expect(harness.tombstoned == [Self.category(night.id)]) + #expect(harness.tombstoned == [Self.category(night.id), Self.valuesCategory(night.id)]) #expect(!harness.dirty.contains(Self.category(night.id))) #expect(try harness.fileNames().isEmpty) } + // Another Mac may have set a value color this one has not pulled, so its record goes too. + @Test("A trash deletes the value record even when the local copy sets no value color") + func trashDeletesTheValueRecordOfAThemeWithoutValueColors() throws { + let harness = try Harness() + defer { harness.remove() } + let night = try Self.theme("user.night") + try harness.write(night) + harness.reopen() + #expect(!night.setsSlots(introducedIn: 4)) + + try harness.catalog.trash(id: night.id) + + #expect(harness.tombstoned.contains(Self.valuesCategory(night.id))) + } + + @Test("Moving a theme with value colors to the Trash tombstones both records, and putting it back marks both") + func trashTombstonesTheValueRecordToo() throws { + let harness = try Harness() + defer { harness.remove() } + let values = try Self.valueTheme("user.values") + try harness.write(values) + harness.reopen() + let both: Set = [Self.category(values.id), Self.valuesCategory(values.id)] + + let receipt = try harness.catalog.trash(id: values.id) + #expect(harness.tombstoned == both) + #expect(harness.dirty.isEmpty) + + try harness.catalog.restore(receipt) + #expect(harness.dirty == both) + } + + // A queued deletion of a record the restored theme does not hold would remove value colors another Mac set. + @Test("Putting a trashed theme back takes back only the deletions the trash queued") + func restoreTakesBackTheTrashDeletions() throws { + let harness = try Harness() + defer { harness.remove() } + let night = try Self.theme("user.night") + try harness.write(night) + let values = try Self.valueTheme("user.values") + try harness.write(values) + harness.reopen() + + try harness.catalog.restore(try harness.catalog.trash(id: night.id)) + #expect(!harness.tombstoned.contains(Self.valuesCategory(night.id))) + + try harness.catalog.save(values.restricted(toSchema: ThemeSchema.oldestReadable)) + try harness.catalog.restore(try harness.catalog.trash(id: values.id)) + #expect(harness.tombstoned.contains(Self.valuesCategory(values.id))) + } + @Test("Putting a trashed theme back marks its record dirty again") func restoreMarksDirty() throws { let harness = try Harness() @@ -233,6 +371,70 @@ struct ThemeCatalogSyncTests { #expect(harness.catalog.syncPayload(forCategory: Self.category(synced.id)) == payload) } + @Test("A value record for a theme this Mac does not hold waits for the theme record, which lands as format 4") + func valueRecordWaitsForItsTheme() throws { + let harness = try Harness() + defer { harness.remove() } + let synced = try Self.valueTheme("user.values") + let newer = try ThemeFileFormat.encode(synced) + + #expect(harness.catalog.applyRemote(category: Self.valuesCategory(synced.id), data: newer) == .skipped) + #expect(try harness.fileNames().isEmpty) + + let base = try ThemeFileFormat.encode(synced.restricted(toSchema: ThemeSchema.oldestReadable)) + let outcome = harness.catalog.applyRemote(category: Self.category(synced.id), data: base) { category in + category == Self.valuesCategory(synced.id) ? newer : nil + } + + #expect(outcome == .applied) + #expect(harness.catalog.storedTheme(id: synced.id) == synced) + #expect(try Self.schema(of: Data(contentsOf: harness.themes.appendingPathComponent("user.values.json"))) == 4) + #expect(harness.catalog.syncPayload(forCategory: Self.category(synced.id)) == base) + #expect(harness.dirty.isEmpty) + } + + @Test("A synced theme record keeps the value colors this Mac holds") + func themeRecordKeepsLocalValueColors() throws { + let harness = try Harness() + defer { harness.remove() } + let local = try Self.valueTheme("user.values") + try harness.write(local) + harness.reopen() + + let edited = try Self.theme("user.values", name: "Edited elsewhere", background: "#202020") + let outcome = harness.catalog.applyRemote(category: Self.category(edited.id), data: try ThemeFileFormat.encode(edited)) + + #expect(outcome == .applied) + var expected = edited + expected.overrides[.gridString] = local.overrides[.gridString] + expected.overrides[.gridNumber] = local.overrides[.gridNumber] + #expect(harness.catalog.storedTheme(id: local.id) == expected) + #expect(harness.dirty.isEmpty) + } + + @Test("A synced value record changes only the value colors") + func valueRecordChangesOnlyValueColors() throws { + let harness = try Harness() + defer { harness.remove() } + let local = try Self.valueTheme("user.values") + try harness.write(local) + harness.reopen() + + var incoming = try Self.theme("user.values", name: "Other name", background: "#202020") + incoming.overrides[.gridDateTime] = try ThemeColor(validating: "#8BE9FD") + let payload = try ThemeFileFormat.encode(incoming) + let outcome = harness.catalog.applyRemote(category: Self.valuesCategory(incoming.id), data: payload) + + #expect(outcome == .applied) + var expected = local + expected.overrides[.gridString] = nil + expected.overrides[.gridNumber] = nil + expected.overrides[.gridDateTime] = incoming.overrides[.gridDateTime] + #expect(harness.catalog.storedTheme(id: local.id) == expected) + #expect(harness.dirty.isEmpty) + #expect(harness.catalog.applyRemote(category: Self.valuesCategory(incoming.id), data: payload) == .skipped) + } + @Test("A synced copy of the file already on disk writes nothing") func identicalRemoteThemeIsSkipped() throws { let harness = try Harness() @@ -291,6 +493,34 @@ struct ThemeCatalogSyncTests { #expect(try harness.fileNames().count == 2) } + @Test("While the local file may be unsynced, value colors come from their record and the local version stays as a copy") + func keepBothTakesValueColorsFromTheirRecord() throws { + let harness = try Harness() + defer { harness.remove() } + let local = try Self.valueTheme("user.x", name: "Ocean", string: "#111111") + try harness.write(local) + harness.reopen() + let incoming = try Self.valueTheme("user.x", name: "Ocean", string: "#222222") + let newer = try ThemeFileFormat.encode(incoming) + + #expect(harness.catalog.applyRemote(category: Self.valuesCategory(incoming.id), data: newer, merge: .keepBoth) == .skipped) + #expect(harness.catalog.storedTheme(id: local.id) == local) + + let outcome = harness.catalog.applyRemote( + category: Self.category(incoming.id), + data: try ThemeFileFormat.encode(incoming.restricted(toSchema: ThemeSchema.oldestReadable)), + merge: .keepBoth + ) { category in + category == Self.valuesCategory(incoming.id) ? newer : nil + } + + #expect(outcome == .applied) + #expect(harness.catalog.storedTheme(id: incoming.id) == incoming) + let kept = try #require(harness.catalog.themes.first { !$0.isBuiltIn && $0.id != incoming.id }) + #expect(kept.overrides == local.overrides) + #expect(harness.dirty == [Self.category(kept.id), Self.valuesCategory(kept.id)]) + } + @Test("A kept copy keeps its name when the record arrives under another name") func keptCopyKeepsAFreeName() throws { let harness = try Harness() @@ -412,6 +642,9 @@ struct ThemeCatalogSyncTests { Rejection(label: "newer format", category: "theme.user.a") { Data(#"{"schema":\#(ThemeSchema.current + 1),"id":"user.a","name":"a","appearance":"dark"}"#.utf8) }, + Rejection(label: "format 4 key in a format 3 record", category: "theme.user.a") { + Data(##"{"schema":3,"id":"user.a","name":"a","appearance":"dark","dataGrid":{"string":"#F1FA8C"}}"##.utf8) + }, Rejection(label: "unknown key", category: "theme.user.a") { Data(#"{"schema":3,"id":"user.a","name":"a","appearance":"dark","script":"run"}"#.utf8) }, @@ -440,6 +673,65 @@ struct ThemeCatalogSyncTests { #expect(harness.dirty.isEmpty) } + nonisolated static let valueRecordRejections: [Rejection] = [ + Rejection(label: "document for another id", category: "themeSchema4.user.a") { + try ThemeFileFormat.encode(try valueTheme("user.b")) + }, + Rejection(label: "built-in id", category: "themeSchema4.tablepro.dracula") { + Data(##"{"schema":4,"id":"tablepro.dracula","name":"Fake","appearance":"dark","dataGrid":{"string":"#F1FA8C"}}"##.utf8) + }, + Rejection(label: "format this build cannot read", category: "themeSchema\(ThemeSchema.current + 1).user.a") { + try ThemeFileFormat.encode(try valueTheme("user.a")) + }, + Rejection(label: "format 3 under the value prefix", category: "themeSchema3.user.a") { + try ThemeFileFormat.encode(try valueTheme("user.a")) + }, + Rejection(label: "format number with a leading zero", category: "themeSchema04.user.a") { + try ThemeFileFormat.encode(try valueTheme("user.a")) + }, + Rejection(label: "payload past the size cap", category: "themeSchema4.user.a") { + let body = ##"{"schema":4,"id":"user.a","name":"a","appearance":"dark","dataGrid":{"string":"#F1FA8C"}}"## + return Data((body + String(repeating: " ", count: ThemeCatalog.maximumSyncPayloadSize)).utf8) + }, + Rejection(label: "payload nested past the depth cap", category: "themeSchema4.user.a") { + Data(##"{"schema":4,"id":"user.a","name":"a","appearance":"dark","dataGrid":{"a":{"b":{"c":{"d":"#000000"}}}}}"##.utf8) + }, + Rejection(label: "unknown key", category: "themeSchema4.user.a") { + Data(##"{"schema":4,"id":"user.a","name":"a","appearance":"dark","dataGrid":{"string":"#F1FA8C"},"script":"run"}"##.utf8) + }, + ] + + @Test("A value record that fails validation leaves the local theme alone", arguments: valueRecordRejections) + func invalidValueRecordIsSkipped(_ rejection: Rejection) throws { + let harness = try Harness() + defer { harness.remove() } + let local = try Self.theme("user.a") + let url = try harness.write(local) + harness.reopen() + let before = try Data(contentsOf: url) + + let outcome = harness.catalog.applyRemote(category: rejection.category, data: try rejection.payload()) + + #expect(outcome == .skipped) + #expect(try Data(contentsOf: url) == before) + #expect(harness.catalog.storedTheme(id: local.id) == local) + #expect(harness.dirty.isEmpty) + } + + @Test("Both record kinds name their theme, and the value record's prefix is not the theme record's") + func syncCategoriesNameTheirTheme() { + #expect(ThemeCatalog.syncCategory(for: "user.a", schema: 4) == "themeSchema4.user.a") + #expect(ThemeCatalog.syncCategory(for: "user.a", schema: ThemeSchema.oldestReadable) == "theme.user.a") + #expect(ThemeCatalog.themeId(forSyncCategory: "themeSchema4.user.a") == "user.a") + #expect(ThemeCatalog.syncSchema(forCategory: "themeSchema4.user.a") == 4) + #expect(ThemeCatalog.syncSchema(forCategory: "theme.user.a") == ThemeSchema.oldestReadable) + for category in ["themeSchema04.user.a", "themeSchema3.user.a", "themeSchema+4.user.a", "themeSchema4", "themeSchemaX.user.a"] { + #expect(ThemeCatalog.themeId(forSyncCategory: category) == nil, "\(category)") + } + // 0.86 treats any category under `theme.` as a theme it must read. + #expect(!ThemeCatalog.syncCategory(for: "user.a", schema: 4).hasPrefix("theme.")) + } + @Test("A synced edit of a theme removed in Finder brings it back, since only a deletion record removes it") func remoteEditAfterAFinderDelete() throws { let harness = try Harness() @@ -479,6 +771,40 @@ struct ThemeCatalogSyncTests { #expect(harness.tombstoned.isEmpty) } + @Test("A theme deleted on another Mac drops the pending marks of its value record too") + func remoteDeletionDropsTheValueRecordMark() throws { + let harness = try Harness() + defer { harness.remove() } + let values = try Self.valueTheme("user.values") + try harness.catalog.save(values) + #expect(harness.dirty == [Self.category(values.id), Self.valuesCategory(values.id)]) + + #expect(harness.catalog.removeWithoutSync(ids: [values.id])) + + #expect(harness.catalog.theme(id: values.id) == nil) + #expect(harness.dirty.isEmpty) + #expect(harness.tombstoned.isEmpty) + } + + @Test("A value record deleted on another Mac clears the value colors here and marks nothing") + func remoteValueDeletionClearsValueColors() throws { + let harness = try Harness() + defer { harness.remove() } + let values = try Self.valueTheme("user.values") + try harness.write(values) + let night = try Self.theme("user.night") + let nightURL = try harness.write(night) + harness.reopen() + let nightBytes = try Data(contentsOf: nightURL) + + #expect(harness.catalog.removeSlotsWithoutSync(introducedIn: 4, ids: [values.id, night.id, "user.unknown"])) + + #expect(harness.catalog.storedTheme(id: values.id) == values.restricted(toSchema: ThemeSchema.oldestReadable)) + #expect(try Data(contentsOf: nightURL) == nightBytes) + #expect(harness.dirty.isEmpty) + #expect(harness.tombstoned.isEmpty) + } + @Test("A remote deletion leaves alone a file that was replaced by another theme since it loaded") func remoteDeletionSparesAReplacedFile() throws { let harness = try Harness() @@ -521,6 +847,7 @@ struct ThemeCatalogSyncTests { #expect(harness.catalog.syncPayload(forCategory: Self.category(BuiltInThemes.defaultDarkId)) == nil) #expect(harness.catalog.syncPayload(forCategory: Self.category("user.missing")) == nil) #expect(harness.catalog.syncPayload(forCategory: "appearance") == nil) + #expect(harness.catalog.syncPayload(forCategory: Self.valuesCategory(night.id)) == nil) #expect(harness.catalog.syncCategories == [Self.category(night.id)]) } diff --git a/apple/TableProTests/Theme/ThemeCatalogTests.swift b/apple/TableProTests/Theme/ThemeCatalogTests.swift index 7ee89d9389..30527c765b 100644 --- a/apple/TableProTests/Theme/ThemeCatalogTests.swift +++ b/apple/TableProTests/Theme/ThemeCatalogTests.swift @@ -31,7 +31,7 @@ struct ThemeCatalogTests { } } - /// A trash inside the sandbox, so a test never puts a file in the user's own Trash. + // A trash inside the sandbox, so a test never puts a file in the user's own Trash. var fileTrash: ThemeFileTrash { let trash = trash return ThemeFileTrash { url in @@ -133,8 +133,8 @@ struct ThemeCatalogTests { // MARK: - Deleted on disk - /// The file is gone, so writing it again would bring back a theme deleted in Finder or on - /// another Mac. + // The file is gone, so writing it again would bring back a theme deleted in Finder or on + // another Mac. @Test("A save of a theme whose file was deleted outside TablePro is refused and recreates nothing") func saveRefusesAFileDeletedOnDisk() throws { let sandbox = try Sandbox() @@ -170,8 +170,8 @@ struct ThemeCatalogTests { #expect(try sandbox.fileNames().isEmpty) } - /// A file edited into one that no longer loads keeps its name, so a stale save would otherwise - /// write `-2.json` and leave two files with one id once the first is fixed. + // A file edited into one that no longer loads keeps its name, so a stale save would otherwise + // write `-2.json` and leave two files with one id once the first is fixed. @Test("A save of a theme whose file no longer loads is refused, and writes no second file") func saveRefusesAFileThatNoLongerLoads() throws { let sandbox = try Sandbox() @@ -430,6 +430,8 @@ struct ThemeCatalogTests { let saved = try sandbox.read("legacy.json") #expect(saved.name == String(repeating: "N", count: cap)) #expect(saved.author == "Someone on two lines") + let savedRoot = try JSONSerialization.jsonObject(with: try Data(contentsOf: url)) as? [String: Any] + #expect(savedRoot?["schema"] as? Int == 3) let picked = sandbox.incoming.appendingPathComponent("legacy.json") try bytes.write(to: picked) @@ -446,7 +448,36 @@ struct ThemeCatalogTests { let catalog = sandbox.catalog() #expect(catalog.theme(id: "user.old") == nil) - #expect(catalog.rejected.first?.reason == ThemeLoadError.schemaTooOld(found: 2, supported: 3).localizedDescription) + #expect( + catalog.rejected.first?.reason + == ThemeLoadError.schemaTooOld(found: 2, supported: ThemeSchema.oldestReadable).localizedDescription + ) + } + + @Test("A format 4 file loads its value colors, and a format 3 file holding one is listed with the key") + func valueColorsNeedFormatFour() throws { + let sandbox = try Sandbox() + defer { sandbox.remove() } + let current: [String: Any] = [ + "schema": 4, "id": "user.current", "name": "Current", "appearance": "dark", + "dataGrid": ["text": "#F8F8F2", "string": "#F1FA8C"], + ] + let mislabeled: [String: Any] = [ + "schema": 3, "id": "user.mislabeled", "name": "Mislabeled", "appearance": "dark", + "dataGrid": ["text": "#F8F8F2", "string": "#F1FA8C"], + ] + try JSONSerialization.data(withJSONObject: current).write(to: sandbox.themes.appendingPathComponent("current.json")) + try JSONSerialization.data(withJSONObject: mislabeled).write(to: sandbox.themes.appendingPathComponent("mislabeled.json")) + + let catalog = sandbox.catalog() + + #expect(catalog.storedTheme(id: "user.current")?.overrides[.gridString]?.hex == "#F1FA8C") + #expect(catalog.theme(id: "user.mislabeled") == nil) + #expect(catalog.rejected.map(\.fileName) == ["mislabeled.json"]) + #expect( + catalog.rejected.first?.reason + == ThemeLoadError.keysNeedNewerFormat(["dataGrid.string"], format: 4).localizedDescription + ) } @Test("The bundled themes load after the two defaults, in the shared order") @@ -568,8 +599,8 @@ struct ThemeCatalogTests { #expect(catalog.theme(id: BuiltInThemes.defaultDarkId) == BuiltInThemes.dark) } - /// The id in a file says nothing about which theme on this Mac it is: the same file imported - /// twice, or on two Macs, would otherwise collide in sync. + // The id in a file says nothing about which theme on this Mac it is: the same file imported + // twice, or on two Macs, would otherwise collide in sync. @Test("Import mints a new id even when the file's id is free, and keeps a free name") func importMintsAFreshId() throws { let sandbox = try Sandbox() diff --git a/apple/TableProTests/Theme/ThemeContrastTests.swift b/apple/TableProTests/Theme/ThemeContrastTests.swift index b016c80629..ba10190bbc 100644 --- a/apple/TableProTests/Theme/ThemeContrastTests.swift +++ b/apple/TableProTests/Theme/ThemeContrastTests.swift @@ -1,13 +1,3 @@ -// -// ThemeContrastTests.swift -// TableProTests -// -// Sidebar sections, status lines and empty states put the system's secondary label on the window -// background, so a bundled theme that sets one has to keep that label legible. The system's own -// secondary label sits under 4.5:1 on every light window (3.95:1 on white), so a light theme is held -// to the WCAG floor for secondary text, 3:1. -// - import AppKit import Foundation import SwiftUI @@ -23,9 +13,10 @@ struct ThemeContrastTests { .map { try ThemeFileFormat.decode(try Data(contentsOf: $0)) } } + // The system's secondary label is 3.95:1 on white, under 4.5:1 on every light window: WCAG's 3:1 secondary floor. private static let lightSecondaryContrast: CGFloat = 3 - /// `secondaryLabelColor` is translucent, so what the eye reads is the label over the fill. + // secondaryLabelColor is translucent, so what the eye reads is the label over the fill. private static func composite(_ foreground: NSColor, over background: NSColor) -> NSColor { let alpha = foreground.alphaComponent return NSColor( @@ -63,4 +54,77 @@ struct ThemeContrastTests { #expect(ratio >= bar, "\(theme.id): \(ratio), bar \(bar)") } } + + // MARK: - Value colors + + private static let valueSlots = ThemeSlotSection.values.slots.filter { $0 != .gridNullValue } + + @Test("Every bundled theme sets each value color at 4.5:1 or more on its grid background") + func bundledValueColorsOnGridBackground() throws { + let themes = try Self.bundledThemes() + #expect(themes.count >= 14) + #expect(Self.valueSlots.count == 9) + + for theme in themes { + let background = try #require(theme.overrides[.gridBackground], "\(theme.id)") + for slot in Self.valueSlots { + let color = try #require(theme.overrides[slot], "\(theme.id) sets no \(slot.rawValue)") + let ratio = color.contrastRatio(with: background) + #expect(ratio >= ThemeContrastNote.minimumRatio, "\(theme.id) \(slot.rawValue) \(color.hex): \(ratio)") + } + #expect(ThemeContrastNote.message(for: .values, in: theme) == nil, "\(theme.id)") + } + } + + private static let valuesTheme = ThemeDefinition( + id: "user.aaaa0004", + name: "Ledger", + author: "", + appearance: .dark, + overrides: [ + .gridBackground: ThemeColor(red: 0x28, green: 0x2A, blue: 0x36), + .gridText: ThemeColor(red: 0xF8, green: 0xF8, blue: 0xF2), + .gridBoolTrue: ThemeColor(red: 0x50, green: 0xFA, blue: 0x7B), + .gridNumber: ThemeColor(red: 0xBD, green: 0x93, blue: 0xF9), + ] + ) + + @Test("The values note names the value color with the lowest contrast") + func valuesNoteNamesTheLowest() throws { + var theme = Self.valuesTheme + #expect(ThemeContrastNote.message(for: .values, in: theme) == nil) + + theme.overrides[.gridJSON] = ThemeColor(red: 0x6B, green: 0x5A, blue: 0x30) + theme.overrides[.gridBoolFalse] = ThemeColor(red: 0x80, green: 0x30, blue: 0x30) + let background = try #require(theme.overrides[.gridBackground]) + let falseRatio = try #require(theme.overrides[.gridBoolFalse]).contrastRatio(with: background) + let jsonRatio = try #require(theme.overrides[.gridJSON]).contrastRatio(with: background) + #expect(falseRatio < jsonRatio) + #expect(jsonRatio < ThemeContrastNote.minimumRatio) + + let lowest = try #require(ThemeContrastNote.lowest(for: .values, in: theme)) + #expect(lowest.slot == .gridBoolFalse) + #expect(lowest.ratio == falseRatio) + + let formatted = falseRatio.formatted(.number.precision(.fractionLength(1))) + #expect( + ThemeContrastNote.message(for: .values, in: theme) + == "Bool False contrast is \(formatted):1. Aim for at least 4.5:1." + ) + } + + @Test("The values note skips NULL and the value colors that follow the grid text") + func valuesNoteSkipsNullAndUnsetColors() { + var theme = Self.valuesTheme + theme.overrides[.gridNullValue] = ThemeColor(red: 0x30, green: 0x32, blue: 0x3E) + #expect(ThemeContrastNote.message(for: .values, in: theme) == nil) + + theme.overrides[.gridText] = ThemeColor(red: 0x44, green: 0x47, blue: 0x5A) + #expect(ThemeContrastNote.message(for: .values, in: theme) == nil) + #expect(ThemeContrastNote.message(for: .dataGrid, in: theme)?.hasPrefix("Text contrast is ") == true) + + theme.overrides[.gridBoolTrue] = nil + theme.overrides[.gridNumber] = nil + #expect(ThemeContrastNote.lowest(for: .values, in: theme) == nil) + } } diff --git a/apple/TableProTests/Views/DataGrid/DataGridCellAppearanceTests.swift b/apple/TableProTests/Views/DataGrid/DataGridCellAppearanceTests.swift index c8ea6aa420..77d4b55c62 100644 --- a/apple/TableProTests/Views/DataGrid/DataGridCellAppearanceTests.swift +++ b/apple/TableProTests/Views/DataGrid/DataGridCellAppearanceTests.swift @@ -55,6 +55,7 @@ struct DataGridCellAppearanceTests { onEmphasizedSelection: Bool = false, checkboxMark: DataGridCheckboxMark? = nil, textTone: NSColor? = nil, + valueTone: DataGridValueTone = .plain, palette: DataGridCellPalette? = nil ) -> DataGridCellAppearance { DataGridCellAppearance.resolve( @@ -71,7 +72,8 @@ struct DataGridCellAppearanceTests { nullDisplayString: "NULL", onEmphasizedSelection: onEmphasizedSelection, checkboxMark: checkboxMark, - textTone: textTone + textTone: textTone, + valueTone: valueTone ) } @@ -397,6 +399,387 @@ struct DataGridCellAppearanceTests { #expect(found.textColor == themedPalette.findMatchText) } + // MARK: - Value tones + + private let tonedPalette = DataGridCellPalette( + regularFont: .systemFont(ofSize: 13), + italicFont: .systemFont(ofSize: 13), + mediumFont: .systemFont(ofSize: 13, weight: .medium), + text: .systemPurple, + placeholderText: .systemBrown, + booleanTrueText: .systemGreen, + booleanFalseText: .systemPink, + valueColors: DataGridValueColors( + string: NSColor(srgbRed: 0.80, green: 0.20, blue: 0.10, alpha: 1), + number: NSColor(srgbRed: 0.10, green: 0.40, blue: 0.80, alpha: 1), + dateTime: NSColor(srgbRed: 0.50, green: 0.20, blue: 0.70, alpha: 1), + binary: NSColor(srgbRed: 0.40, green: 0.40, blue: 0.40, alpha: 1), + json: NSColor(srgbRed: 0.10, green: 0.50, blue: 0.30, alpha: 1), + uuid: NSColor(srgbRed: 0.60, green: 0.40, blue: 0.10, alpha: 1), + enumeration: NSColor(srgbRed: 0.70, green: 0.10, blue: 0.40, alpha: 1) + ), + rowNumberText: .systemTeal, + deletedRowText: .systemRed, + modifiedColumnTint: .systemYellow, + findMatchTint: .systemOrange, + selectionText: .systemIndigo, + focusBorder: .systemMint + ) + + @Test("Each value tone draws its theme color, a plain value the text color") + func eachToneDrawsItsColor() { + let colors = tonedPalette.valueColors + let expected: [(DataGridValueTone, NSColor)] = [ + (.string, colors.string), + (.number, colors.number), + (.dateTime, colors.dateTime), + (.binary, colors.binary), + (.json, colors.json), + (.uuid, colors.uuid), + (.enumeration, colors.enumeration), + (.plain, .systemPurple), + ] + let distinctColors = Set(expected.map { ThemeTestFixture.hex($0.1) }) + #expect(distinctColors.count == expected.count) + for (tone, color) in expected { + #expect(resolve(valueTone: tone, palette: tonedPalette).textColor == color, "\(tone)") + } + } + + @Test("A find match, an emphasized selection, a deleted row and an owner tone outrank the value color") + func stateOutranksTheValueColor() { + let found = resolve(isCurrentFindMatch: true, valueTone: .number, palette: tonedPalette) + let selected = resolve(onEmphasizedSelection: true, valueTone: .number, palette: tonedPalette) + let deleted = resolve( + visualState: RowVisualState(isDeleted: true, isInserted: false, modifiedColumns: []), + valueTone: .number, + palette: tonedPalette + ) + let toned = resolve(textTone: .systemGreen, valueTone: .number, palette: tonedPalette) + let modified = resolve( + visualState: RowVisualState(isDeleted: false, isInserted: false, modifiedColumns: [0]), + valueTone: .number, + palette: tonedPalette + ) + + #expect(found.textColor == tonedPalette.findMatchText) + #expect(selected.textColor == .systemIndigo) + #expect(deleted.textColor == .systemRed) + #expect(toned.textColor == .systemGreen) + #expect(modified.textColor == tonedPalette.valueColors.number) + #expect(modified.backgroundTint == tonedPalette.modifiedColumnTint) + } + + @Test("A placeholder, a pending DEFAULT and a pending expression keep their own colors over a value tone") + func placeholdersAndPendingValuesIgnoreTheTone() { + let null = resolve(text: "", rawValue: nil, placeholder: .null, valueTone: .number, palette: tonedPalette) + let pendingDefault = resolve(text: "", placeholder: .defaultMarker, valueTone: .number, palette: tonedPalette) + let expression = resolve( + text: "now()", + rawValue: "now()", + visualState: RowVisualState(isDeleted: false, isInserted: false, modifiedColumns: [0], expressionColumns: [0]), + valueTone: .dateTime, + palette: tonedPalette + ) + + #expect(null.textColor == .systemBrown) + #expect(pendingDefault.textColor == .systemBlue) + #expect(expression.textColor == .systemBlue) + } + + @Test("A boolean keeps its true and false colors whatever the tone, and an unreadable one draws plain") + func booleanBranchComesFirst() { + let isTrue = resolve(kind: .boolean, text: "1", rawValue: "1", valueTone: .number, palette: tonedPalette) + let isFalse = resolve(kind: .boolean, text: "f", rawValue: "f", palette: tonedPalette) + let unreadable = resolve(kind: .boolean, text: "maybe", rawValue: "maybe", palette: tonedPalette) + + #expect(isTrue.textColor == .systemGreen) + #expect(isFalse.textColor == .systemPink) + #expect(unreadable.textColor == .systemPurple) + } + + @Test("A palette built without value colors draws every tone in its text color") + func paletteWithoutValueColorsDrawsText() { + for tone in [DataGridValueTone.string, .number, .dateTime, .binary, .json, .uuid, .enumeration] { + #expect(resolve(valueTone: tone, palette: themedPalette).textColor == .systemPurple, "\(tone)") + } + } + + @Test("A theme's value colors reach the grid's palette, and unset ones draw the grid text") + func themeReachesValueColors() throws { + let aqua = try #require(NSAppearance(named: .aqua)) + let themed = ThemeScope.fixed(try ThemeTestFixture.theme(.light, [ + .gridString: "#AA1100", + .gridNumber: "#0044AA", + .gridDateTime: "#7722AA", + .gridBinary: "#555555", + .gridJSON: "#117744", + .gridUUID: "#996611", + .gridEnum: "#AA1166", + ])).dataGridCellPalette(in: aqua) + let unset = ThemeScope.fixed(BuiltInThemes.light).dataGridCellPalette(in: aqua) + + #expect(ThemeTestFixture.hex(themed.valueColors.string) == "#AA1100") + #expect(ThemeTestFixture.hex(themed.valueColors.number) == "#0044AA") + #expect(ThemeTestFixture.hex(themed.valueColors.dateTime) == "#7722AA") + #expect(ThemeTestFixture.hex(themed.valueColors.binary) == "#555555") + #expect(ThemeTestFixture.hex(themed.valueColors.json) == "#117744") + #expect(ThemeTestFixture.hex(themed.valueColors.uuid) == "#996611") + #expect(ThemeTestFixture.hex(themed.valueColors.enumeration) == "#AA1166") + #expect(unset.valueColors == DataGridValueColors(all: unset.text)) + } + + private static let typedTheme: [ThemeSlot: String] = [ + .gridText: "#111111", + .gridString: "#AA1100", + .gridNumber: "#0044AA", + .gridDateTime: "#7722AA", + .gridBinary: "#555555", + .gridJSON: "#117744", + .gridUUID: "#996611", + .gridEnum: "#AA1166", + .gridBoolTrue: "#118811", + .gridBoolFalse: "#881111", + .gridNullValue: "#777777", + ] + + @MainActor + private struct TypedGrid { + let owner: GridTestOwner + let model: DataGridModel + let presenter: DataGridCellPresenter + + init( + values: [PluginCellValue], + type: ColumnType, + colorsValuesByType: Bool, + settingIsOn: Bool = true, + overridesSetting: Bool = false, + format: ValueDisplayFormat? + ) throws { + owner = GridTestOwner(TableRows.from(queryRows: values.map { [$0] }, columns: ["value"], columnTypes: [type])) + var configuration = DataGridConfiguration() + configuration.colorsValuesByType = colorsValuesByType + configuration.overridesValueColorSetting = overridesSetting + model = DataGridModel( + configuration: configuration, + themeScope: .fixed(try ThemeTestFixture.theme(.light, DataGridCellAppearanceTests.typedTheme)) + ) + model.dataSource = owner + model.reloadRows() + if let format { model.store.displayFormats = [format] } + presenter = DataGridCellPresenter(model: model, valueColors: ValueColorSettings(colorsValuesByType: settingIsOn)) + presenter.refreshColumnPresentations() + } + + func colors() throws -> [String?] { + let aqua = try #require(NSAppearance(named: .aqua)) + let pass = try #require(presenter.makePass(in: aqua)) + let column = try #require(model.columns.column(at: 0)) + return try (0.. [String?] { + let grid = try TypedGrid( + values: values, + type: type, + colorsValuesByType: colorsValuesByType, + settingIsOn: settingIsOn, + overridesSetting: overridesSetting, + format: format + ) + return try withExtendedLifetime(grid.owner) { try grid.colors() } + } + + private static let uuidBytes = Data([ + 0xAF, 0x49, 0x45, 0x3B, 0x7F, 0x2F, 0xFB, 0x58, + 0xFC, 0xD3, 0x2B, 0xD3, 0x99, 0x59, 0x9F, 0xA5, + ]) + + @Test("A grid that does not opt in draws every value in the grid text") + func gridOffDrawsPlain() throws { + #expect(try presentedColors(of: [.text("42")], type: .integer(rawType: "INT"), colorsValuesByType: false) == ["#111111"]) + #expect(try presentedColors(of: [.text("ok")], type: .text(rawType: "VARCHAR(8)"), colorsValuesByType: false) == ["#111111"]) + #expect(try presentedColors( + of: [.text("1700000000")], + type: .integer(rawType: "BIGINT"), + colorsValuesByType: false, + format: .unixTimestamp + ) == ["#111111"]) + } + + private static let everyTypedColumn: [(value: PluginCellValue, type: ColumnType, format: ValueDisplayFormat?, hex: String)] = [ + (.text("42"), .integer(rawType: "INT"), nil, "#0044AA"), + (.text("ok"), .text(rawType: "VARCHAR(8)"), nil, "#AA1100"), + (.text("2024-01-08"), .date(rawType: "DATE"), nil, "#7722AA"), + (.bytes(Data([0x9F, 0x3A])), .blob(rawType: "BLOB"), nil, "#555555"), + (.text(#"["ops"]"#), .json(rawType: "JSON"), nil, "#117744"), + (.text("8f14e45f-ceea-467f-a8f5-2e1c6a3d9b07"), .text(rawType: "UUID"), nil, "#996611"), + (.text("admin"), .enumType(rawType: "ENUM", values: ["admin", "staff"]), nil, "#AA1166"), + (.text("1700000000"), .integer(rawType: "BIGINT"), .unixTimestamp, "#7722AA"), + ] + + @Test("With Color values by type off, an opted-in grid draws every typed value in the grid text") + func settingOffDrawsPlain() throws { + for column in Self.everyTypedColumn { + let on = try presentedColors(of: [column.value], type: column.type, format: column.format) + let off = try presentedColors(of: [column.value], type: column.type, settingIsOn: false, format: column.format) + #expect(on == [column.hex], "\(column.type)") + #expect(off == ["#111111"], "\(column.type)") + } + } + + @Test("Booleans and NULL keep their colors whether Color values by type is on or off") + func booleansAndNullIgnoreTheSetting() throws { + let values: [PluginCellValue] = [.text("true"), .text("false"), .null] + let type = ColumnType.boolean(rawType: "BOOLEAN") + let expected: [String?] = ["#118811", "#881111", "#777777"] + + #expect(try presentedColors(of: values, type: type, settingIsOn: true) == expected) + #expect(try presentedColors(of: values, type: type, settingIsOn: false) == expected) + #expect(try presentedColors(of: [.null], type: .integer(rawType: "INT"), settingIsOn: false) == ["#777777"]) + } + + @Test("Turning Color values by type on or off recolors the grid it drew, with no reload") + func settingChangeRecolorsWithoutReload() throws { + let grid = try TypedGrid( + values: [.text("42")], + type: .integer(rawType: "INT"), + colorsValuesByType: true, + settingIsOn: false, + format: nil + ) + let off = try grid.colors() + grid.presenter.registry.reloadValueColors(ValueColorSettings(colorsValuesByType: true)) + let on = try grid.colors() + grid.presenter.registry.reloadValueColors(ValueColorSettings(colorsValuesByType: false)) + let offAgain = try grid.colors() + withExtendedLifetime(grid.owner) {} + + #expect(off == ["#111111"]) + #expect(on == ["#0044AA"]) + #expect(offAgain == ["#111111"]) + } + + @Test("A grid that overrides the setting colors values by type while it is off, and only if it opted in") + func overrideIgnoresTheSetting() throws { + let value: [PluginCellValue] = [.text("42")] + let type = ColumnType.integer(rawType: "INT") + + #expect(try presentedColors(of: value, type: type, settingIsOn: false, overridesSetting: true) == ["#0044AA"]) + #expect(try presentedColors( + of: value, + type: type, + colorsValuesByType: false, + settingIsOn: false, + overridesSetting: true + ) == ["#111111"]) + } + + @Test("An opted-in grid colors a value by its column's type and by the format it is shown in") + func gridOnColorsByTypeAndFormat() throws { + #expect(try presentedColors(of: [.text("42")], type: .integer(rawType: "INT")) == ["#0044AA"]) + #expect(try presentedColors(of: [.text("ok")], type: .text(rawType: "VARCHAR(8)")) == ["#AA1100"]) + #expect(try presentedColors(of: [.text("10.0.0.1")], type: .text(rawType: "INET")) == ["#111111"]) + #expect(try presentedColors(of: [.text("1700000000")], type: .integer(rawType: "BIGINT"), format: .unixTimestamp) == ["#7722AA"]) + #expect(try presentedColors(of: [.text("1700000000")], type: .integer(rawType: "BIGINT"), format: .json) == ["#0044AA"]) + } + + @Test("Under a UUID format a 16-byte value takes the UUID color and an 8-byte one in the same column stays binary") + func uuidFormatColorsOnlyTheValuesItRenders() throws { + let colors = try presentedColors( + of: [.bytes(Self.uuidBytes), .bytes(Self.uuidBytes.prefix(8))], + type: .blob(rawType: "VARBINARY(16)"), + format: .uuid + ) + + #expect(colors == ["#996611", "#555555"]) + } + + @Test("A value its decoder cannot read keeps the column's color, one it reads takes the JSON color") + func failedDecoderKeepsTheBaseColor() throws { + let colors = try presentedColors( + of: [.bytes(Data([0x82, 0xA7, 0x63, 0x6F, 0x6D, 0x70])), .bytes(Data([0x81, 0xA1, 0x61, 0x01]))], + type: .blob(rawType: "BLOB"), + format: .messagePack + ) + + #expect(colors == ["#555555", "#117744"]) + } + + @Test("A text column under a UUID format keeps the string color for a value that is not a UUID") + func textThatIsNotAUUIDStaysAString() throws { + let colors = try presentedColors( + of: [.text("not a uuid"), .text("AF49453B7F2FFB58FCD32BD399599FA5")], + type: .text(rawType: "VARCHAR(40)"), + format: .uuid + ) + + #expect(colors == ["#AA1100", "#996611"]) + } + + @Test("The prewarmed text carries the same color as text formatted at draw time") + func prewarmedTextKeepsTheFormattedFlag() throws { + let grid = try TypedGrid( + values: [.bytes(Self.uuidBytes), .bytes(Self.uuidBytes.prefix(8))], + type: .blob(rawType: "VARBINARY(16)"), + colorsValuesByType: true, + format: .uuid + ) + for displayRow in 0.. Undo did nothing to the theme. + // The preview's editor and grid have undo histories of their own, so while either held focus Edit > Undo missed the theme. @Test("The preview takes no clicks and gives up focus, so Undo reaches the theme") func previewNeverHoldsFocus() throws { let fixture = try Fixture() @@ -71,8 +71,7 @@ struct ThemeEditorWindowTests { windows.closeEditor(for: "user.aaaa0002") } - /// AppKit's key view loop reached the preview's editor and grid, and Undo then went to their empty - /// histories instead of the theme's. + // AppKit's key view loop reached the preview's editor and grid, and Undo then went to their empty histories. @Test("Tab and Shift-Tab pass over the preview") func tabPassesOverThePreview() throws { let fixture = try Fixture() @@ -117,7 +116,7 @@ struct ThemeEditorWindowTests { return view.isInsideThemePreview } - /// Quitting closes no window, so the field's text reached no commit. + // Quitting closes no window, so the field's text reached no commit. @Test("Quitting writes the name still being typed") func quitWritesTheTypedName() async throws { let fixture = try Fixture() @@ -198,8 +197,7 @@ struct ThemeEditorWindowTests { #expect(try fixture.onDisk("Midnight.json").name == "Night Owl") } - /// The close button takes no focus, so the rename was still pending, refused, and dropped as the - /// window closed in the same event. + // The close button takes no focus, so the pending rename was refused and dropped as the window closed in the same event. @Test("Closing with a taken name keeps the window open with the reason") func closeWithATakenNameKeepsTheWindow() throws { let fixture = try Fixture() @@ -287,4 +285,123 @@ struct ThemeEditorWindowTests { #expect(controller.window?.representedURL?.lastPathComponent == "\(newId).json") windows.closeEditor(for: newId) } + + // MARK: - Preview grid + + @Test("The preview grid has a column for every value color, both booleans and a NULL") + func previewSampleShowsEveryValueColor() throws { + let rows = ThemePreviewGridSample.tableRows + let tones = Set(rows.columnTypes.map { DataGridValueTone.base(of: $0, declaresEnumLabels: false) }) + #expect(tones == [.plain, .string, .number, .dateTime, .binary, .json, .uuid, .enumeration]) + #expect(ThemePreviewGridSample.configuration.colorsValuesByType) + #expect(ThemePreviewGridSample.configuration.overridesValueColorSetting) + + let boolean = try #require(rows.columnTypes.firstIndex { $0.isBooleanType }) + #expect(Set(rows.rows.map { $0.values[boolean] }) == [.text("true"), .text("false")]) + #expect(rows.rows.contains { $0.values.contains(.null) }) + + let marked = [ + ThemePreviewGridSample.selectedRow, + ThemePreviewGridSample.modifiedRow, + ThemePreviewGridSample.deletedRow, + ThemePreviewGridSample.insertedRow, + ] + #expect(Set(marked).count == marked.count) + #expect(marked.allSatisfy { rows.rows.indices.contains($0) }) + let modified = rows.rows[ThemePreviewGridSample.modifiedRow].values[ThemePreviewGridSample.modifiedColumn] + #expect(modified != .null) + } + + // The preview shows the theme being edited, so it never waits for the grid setting to be turned on. + @Test("The preview grid draws the theme's value colors while Color values by type is off") + func previewColorsValuesWithTheSettingOff() throws { + let owner = GridTestOwner(ThemePreviewGridSample.tableRows) + let model = DataGridModel( + configuration: ThemePreviewGridSample.configuration, + themeScope: .fixed(try ThemeTestFixture.theme(.light, [ + .gridText: "#111111", + .gridString: "#AA1100", + .gridNumber: "#0044AA", + ])) + ) + model.dataSource = owner + model.reloadRows() + let presenter = DataGridCellPresenter(model: model, valueColors: ValueColorSettings(colorsValuesByType: false)) + presenter.refreshColumnPresentations() + let aqua = try #require(NSAppearance(named: .aqua)) + let pass = try #require(presenter.makePass(in: aqua)) + let row = try #require(model.row(at: 0)) + + let colors = try ["id", "name"].map { name in + let index = try #require(ThemePreviewGridSample.columns.firstIndex { $0.name == name }) + let column = try #require(model.columns.column(at: index)) + let appearance = try #require(presenter.appearance( + of: row, + displayRow: 0, + column: column, + visualState: .empty, + pass: pass, + onEmphasizedSelection: false + )) + return ThemeTestFixture.hex(appearance.textColor) + } + withExtendedLifetime(owner) {} + + #expect(colors == ["#0044AA", "#AA1100"]) + } + + @Test("The Values section says where to turn value colors on, only while Color values by type is off") + func valuesNoteFollowsTheSetting() { + for section in ThemeSlotSection.allCases { + #expect(ThemeValueColorsNote.message(for: section, colorsValuesByType: true) == nil, "\(section)") + let off = ThemeValueColorsNote.message(for: section, colorsValuesByType: false) + #expect((off != nil) == (section == .values), "\(section)") + } + let note = ThemeValueColorsNote.message(for: .values, colorsValuesByType: false) + #expect(note?.hasSuffix("Settings > Data.") == true) + } + + // A UUID is wider than any column the window has room for, so it alone ends in an ellipsis. + @Test("Every sample value but the UUID fits its column at the default grid font") + func previewSampleValuesFitTheirColumns() { + let font = DataGridFontCache(from: .default).regular + let null = DataGridSettings.default.nullDisplay + let rows = ThemePreviewGridSample.tableRows + for (index, column) in ThemePreviewGridSample.columns.enumerated() where !column.type.isUUIDType { + for row in rows.rows { + let shown = CellDisplayFormatter.format(row.values[index], columnType: column.type) ?? null + let width = (shown as NSString).size(withAttributes: [.font: font]).width + + 2 * DataGridMetrics.cellHorizontalInset + #expect(width <= column.width, "\(column.name) \(shown): \(width)") + } + } + } + + // The preview takes no scroll events, so a column past the window's edge never shows. + @Test("The preview grid shows every sample column inside the editor window") + func previewGridFitsTheWindow() async throws { + let fixture = try Fixture() + try fixture.install(Fixture.theme("user.aaaa0002", "Midnight"), as: "Midnight.json") + let windows = makeWindows(fixture) + windows.openEditor(for: "user.aaaa0002") + let controller = try #require(windows.controller(for: "user.aaaa0002")) + defer { windows.closeEditor(for: "user.aaaa0002") } + let content = try #require(controller.window?.contentView) + + try await waitUntil { + content.layoutSubtreeIfNeeded() + return Self.previewGrid(in: content) != nil + } + let grid = try #require(Self.previewGrid(in: content)) + let clip = try #require(grid.enclosingScrollView?.contentView) + #expect(grid.rect(ofColumn: grid.numberOfColumns - 1).maxX <= clip.bounds.width) + } + + // The main pane holds the row numbers and every column; the frozen pane only the row numbers. + private static func previewGrid(in root: NSView) -> NSTableView? { + let columnCount = ThemePreviewGridSample.columns.count + 1 + return subviews(of: root) + .compactMap { $0 as? NSTableView } + .first { $0.isInsideThemePreview && $0.numberOfColumns == columnCount } + } } From 26a3fc3b02853503e071837b68954bbb7eb95812 Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Wed, 11 Nov 2026 23:04:24 +0700 Subject: [PATCH 2/3] fix(macos): sync each theme as one record at the oldest schema that holds it --- .../TableProTheme/ThemeDefinition.swift | 18 - .../ThemeFileFormatTests.swift | 18 - .../Extensions/SyncCoordinator+Settings.swift | 97 +---- .../Core/Sync/SyncChangeTracker.swift | 8 - .../TablePro/Core/Sync/SyncCoordinator.swift | 165 +++++--- .../TablePro/Resources/Localizable.xcstrings | 2 +- apple/TablePro/Theme/ThemeCatalog.swift | 247 +++-------- .../Core/Sync/SyncConflictRebaseTests.swift | 9 +- .../Core/Sync/SyncCoordinatorThemeTests.swift | 188 ++------- .../Theme/ThemeCatalogSyncTests.swift | 385 +++--------------- 10 files changed, 248 insertions(+), 889 deletions(-) diff --git a/apple/Packages/TableProCore/Sources/TableProTheme/ThemeDefinition.swift b/apple/Packages/TableProCore/Sources/TableProTheme/ThemeDefinition.swift index a26f872873..06ed516b66 100644 --- a/apple/Packages/TableProCore/Sources/TableProTheme/ThemeDefinition.swift +++ b/apple/Packages/TableProCore/Sources/TableProTheme/ThemeDefinition.swift @@ -57,24 +57,6 @@ public struct ThemeDefinition: Identifiable, Equatable, Sendable { isOverridden(slot, highContrast: false) } - public func restricted(toSchema schema: Int) -> ThemeDefinition { - var restricted = self - restricted.overrides = overrides.filter { $0.key.introducedInSchema <= schema } - return restricted - } - - public func setsSlots(introducedIn schema: Int) -> Bool { - overrides.keys.contains { $0.introducedInSchema == schema } - } - - // A slot `source` leaves unset is cleared here too. - public func replacingSlots(from source: ThemeDefinition, where isReplaced: (ThemeSlot) -> Bool) -> ThemeDefinition { - var merged = self - merged.overrides = overrides.filter { !isReplaced($0.key) } - .merging(source.overrides.filter { isReplaced($0.key) }) { _, replacement in replacement } - return merged - } - private func fallbackValue(for slot: ThemeSlot, highContrast: Bool) -> ThemeSlotValue { switch slot.fallback { case .system: diff --git a/apple/Packages/TableProCore/Tests/TableProThemeTests/ThemeFileFormatTests.swift b/apple/Packages/TableProCore/Tests/TableProThemeTests/ThemeFileFormatTests.swift index 13175b9a58..b8c0d77846 100644 --- a/apple/Packages/TableProCore/Tests/TableProThemeTests/ThemeFileFormatTests.swift +++ b/apple/Packages/TableProCore/Tests/TableProThemeTests/ThemeFileFormatTests.swift @@ -116,24 +116,6 @@ struct ThemeFileFormatTests { #expect(try Self.refusal(root) == .unknownKeys(["panel.text"])) } - @Test("A theme restricted to format 3 drops its value colors and encodes as format 3") - func restrictedThemeEncodesAsFormatThree() throws { - let theme = ThemeDefinition( - id: "user.values", - name: "Values", - author: "", - appearance: .dark, - overrides: [.gridText: try ThemeColor(validating: "#F8F8F2"), .gridString: try ThemeColor(validating: "#F1FA8C")] - ) - - let restricted = theme.restricted(toSchema: ThemeSchema.oldestReadable) - - #expect(restricted.overrides.keys.sorted { $0.rawValue < $1.rawValue } == [.gridText]) - #expect(!restricted.setsSlots(introducedIn: 4)) - #expect(theme.setsSlots(introducedIn: 4)) - #expect(try Self.encodedSchema(restricted) == 3) - } - @Test("The encoder writes format 3 for a theme with no value color, and format 4 once it has one") func encoderWritesTheOldestFormatThatHoldsTheTheme() throws { let theme = ThemeDefinition( diff --git a/apple/TablePro/Core/Sync/Extensions/SyncCoordinator+Settings.swift b/apple/TablePro/Core/Sync/Extensions/SyncCoordinator+Settings.swift index 26904c6453..5f058b5d0a 100644 --- a/apple/TablePro/Core/Sync/Extensions/SyncCoordinator+Settings.swift +++ b/apple/TablePro/Core/Sync/Extensions/SyncCoordinator+Settings.swift @@ -29,7 +29,7 @@ extension SyncCoordinator { return columnLayouts().rawData( forStorageKey: String(category.dropFirst(FileColumnLayoutPersister.syncCategoryPrefix.count)) ) - case let category where ThemeCatalog.themeId(forSyncCategory: category) != nil: + case let category where category.hasPrefix(ThemeCatalog.syncCategoryPrefix): return themeCatalog.syncPayload(forCategory: category) default: return nil } @@ -77,12 +77,6 @@ extension SyncCoordinator { // MARK: - Themes -// nil for a record the pull deleted or could not read. -struct PulledThemeRecords { - var records: [String: Data?] = [:] - var isFullFetch = false -} - extension SyncCoordinator { // A build that reads a newer theme format names a new kind, so it fetches the zone once and // picks up the theme records an older build skipped. @@ -118,92 +112,11 @@ extension SyncCoordinator { return content } - // Read before any record is applied, so a theme new to this Mac finds its newer formats' records - // whichever comes first in the pull. - func pulledThemeRecords( - _ changedRecords: [CKRecord], - deletedRecordIDs: [CKRecord.ID], - unreadRecordIDs: [CKRecord.ID], - isFullFetch: Bool, - echoGuard: SyncEchoGuard? - ) -> PulledThemeRecords { - var pulled = PulledThemeRecords(isFullFetch: isFullFetch) - for record in changedRecords where record.recordType == SyncRecordType.settings.rawValue { - guard echoGuard?.withholds(record.recordID, tracker: changeTracker) != true, - let category = SyncRecordMapper.settingsCategory(from: record), - Self.isNewerThemeFormat(category), - let data = SyncRecordMapper.settingsData(from: record) - else { continue } - pulled.records[category] = data - } - // An unread record changed since the cached copy, so its keys stay unknown until the pull that reads it. - for recordID in deletedRecordIDs + unreadRecordIDs { - guard let parsed = SyncRecordType.parse(recordName: recordID.recordName), - parsed.type == .settings, - Self.isNewerThemeFormat(parsed.id) - else { continue } - pulled.records.updateValue(nil, forKey: parsed.id) - } - return pulled - } - - // A full fetch lists every record the zone holds, so a cached record it lacks was deleted. - func newerThemeRecord(_ category: String, in pulled: PulledThemeRecords) -> Data? { - if let record = pulled.records[category] { - return record - } - guard !pulled.isFullFetch else { return nil } - return recordCache.record(named: SyncRecordType.settings.recordName(for: category)) - .flatMap(SyncRecordMapper.settingsData(from:)) - } - - // A newer format's record the fetch does not list is gone from the server. Every theme that exists - // anywhere is listed or held here, and only a Mac that holds a theme deletes its newer records. - func themeRecordsGone( - afterFullFetch result: PullResult, - zoneID: CKRecordZone.ID, - pushed: Set - ) -> [CKRecord.ID] { - let listed = Set((result.changedRecords.map(\.recordID) + result.unreadRecordIDs).map(\.recordName)) - let listedIds = listed.compactMap { name -> String? in - guard let parsed = SyncRecordType.parse(recordName: name), - parsed.type == .settings, - ThemeCatalog.syncSchema(forCategory: parsed.id) == ThemeSchema.oldestReadable - else { return nil } - return ThemeCatalog.themeId(forSyncCategory: parsed.id) - } - let heldIds = themeCatalog.syncCategories.compactMap(ThemeCatalog.themeId(forSyncCategory:)) - return Set(listedIds + heldIds).sorted() - .flatMap { id in - ThemeCatalog.newerSyncSchemas.map { schema in - SyncRecordMapper.recordID(type: .settings, id: ThemeCatalog.syncCategory(for: id, schema: schema), in: zoneID) - } - } - .filter { !listed.contains($0.recordName) && !pushed.contains($0) } - } - - private static func isNewerThemeFormat(_ category: String) -> Bool { - ThemeCatalog.syncSchema(forCategory: category).map { ThemeCatalog.newerSyncSchemas.contains($0) } == true - } - func applyRemoteThemeDeletions(_ settingsRecordNames: Set) -> Bool { - let prefix = SyncRecordType.settings.recordNamePrefix - let deleted = settingsRecordNames.compactMap { name -> (id: String, schema: Int)? in - guard name.hasPrefix(prefix) else { return nil } - let category = String(name.dropFirst(prefix.count)) - guard let id = ThemeCatalog.themeId(forSyncCategory: category), - let schema = ThemeCatalog.syncSchema(forCategory: category) - else { return nil } - return (id, schema) - } - let trashed = Set(deleted.filter { $0.schema == ThemeSchema.oldestReadable }.map(\.id)) - var isPersisted = trashed.isEmpty || themeCatalog.removeWithoutSync(ids: trashed) - for schema in ThemeCatalog.newerSyncSchemas { - let ids = Set(deleted.filter { $0.schema == schema && !trashed.contains($0.id) }.map(\.id)) - guard !ids.isEmpty else { continue } - isPersisted = themeCatalog.removeSlotsWithoutSync(introducedIn: schema, ids: ids) && isPersisted - } - return isPersisted + let prefix = SyncRecordType.settings.recordNamePrefix + ThemeCatalog.syncCategoryPrefix + let ids = Set(settingsRecordNames.filter { $0.hasPrefix(prefix) }.map { String($0.dropFirst(prefix.count)) }) + guard !ids.isEmpty else { return true } + return themeCatalog.removeWithoutSync(ids: ids) } // Themes made before theme sync were never marked, and loads never mark. They wait for the diff --git a/apple/TablePro/Core/Sync/SyncChangeTracker.swift b/apple/TablePro/Core/Sync/SyncChangeTracker.swift index f04b6cdd6f..cd06aedaa5 100644 --- a/apple/TablePro/Core/Sync/SyncChangeTracker.swift +++ b/apple/TablePro/Core/Sync/SyncChangeTracker.swift @@ -108,14 +108,6 @@ final class SyncChangeTracker: Sendable { postChangeNotification() } - // For a deletion taken back before it was pushed, such as a trash undone. - @MainActor - func discardTombstones(_ type: SyncRecordType, ids: [String]) { - guard !ids.isEmpty else { return } - metadataStorage.removeTombstones(ids, type: type) - recordEdits(type, ids: ids) - } - @MainActor func discardTombstones(ownedBy owners: Set) { guard !owners.isEmpty else { return } diff --git a/apple/TablePro/Core/Sync/SyncCoordinator.swift b/apple/TablePro/Core/Sync/SyncCoordinator.swift index 88320ed86d..a1734725af 100644 --- a/apple/TablePro/Core/Sync/SyncCoordinator.swift +++ b/apple/TablePro/Core/Sync/SyncCoordinator.swift @@ -11,6 +11,7 @@ import Foundation import os import TableProSyncTransport +/// Central coordinator for iCloud sync @MainActor final class SyncCoordinator: ObservableObject { static let shared = SyncCoordinator() @@ -21,7 +22,7 @@ final class SyncCoordinator: ObservableObject { let services: AppServices private let makeTransport: () -> any SyncTransport - // Created on the first run, so no CloudKit container exists while sync stays off. + /// Created on the first run, so no CloudKit container exists while sync stays off. private lazy var transport: any SyncTransport = makeTransport() let changeTracker: SyncChangeTracker private var connectionsCleanedOnPull: Set = [] @@ -30,6 +31,7 @@ final class SyncCoordinator: ObservableObject { let columnLayouts: () -> FileColumnLayoutPersister let themeCatalog: ThemeCatalog private let makeNetworkMonitor: () -> SyncNetworkMonitor? + /// Why the license keeps sync off, or nil while it allows sync. private let licenseGate: @MainActor () -> DisableReason? let recordCreationDate: (CKRecord) -> Date? private var triggers: SyncTriggers? @@ -39,19 +41,25 @@ final class SyncCoordinator: ObservableObject { private var hasStarted = false private var isRunning = false - // Queued rather than cancelling the run in flight: a cancelled run reported itself as a failure. + /// Triggers that arrived while a run was in flight. One more run follows it rather than the + /// trigger cancelling it: a cancelled run reported itself as a failure. private var pendingTriggers: [SyncTrigger] = [] - // The notice and its button stay up during an automatic check, so a press then waits for the check to end. + /// Upload Again pressed while an automatic check was running. The notice stays on screen during + /// that check, so the button does too, and the person's answer must not be lost to it. private var uploadAgainRequested = false + /// Failed runs in a row, which sets how far the next attempt is held back. private var consecutiveFailures = 0 + /// When the upload that failed may be tried again by a trigger that does not change the situation. private var nextAttempt: Date? + /// The end of the wait CloudKit named on its last throttle. No automatic run starts before it. private var throttledUntil: Date? - // Lets a run suspended across the network tell whether something else decided the status meanwhile. + /// Bumped every time something other than a sync run decides the status, so a run that has been + /// suspended across the network can tell whether its outcome is still the current answer. private var statusGeneration = 0 init( @@ -95,7 +103,8 @@ final class SyncCoordinator: ObservableObject { // MARK: - Lifecycle - // Only the license is observed while sync is off: it decides whether sync can turn on. + /// Call from AppDelegate at launch. Only the license is observed while sync is off: it decides + /// whether sync can turn on, and the Welcome window shows a license that does not cover it. func start() { guard !hasStarted else { return } hasStarted = true @@ -106,10 +115,12 @@ final class SyncCoordinator: ObservableObject { Task { await sync(.launch) } } + /// Called when the app comes to the foreground func syncIfNeeded() { requestSync(.activation) } + /// Sync Now: the person asked, so it runs whatever held the last attempt back. func syncNow() async { await sync(.userRequest) } @@ -119,6 +130,7 @@ final class SyncCoordinator: ObservableObject { Task { await sync(trigger) } } + /// Runs as much of a sync as `trigger` is admitted to, given how the last run ended. func sync(_ trigger: SyncTrigger) async { guard canSync() else { Self.logger.info("Sync skipped: not allowed for \(String(describing: trigger), privacy: .public)") @@ -145,7 +157,9 @@ final class SyncCoordinator: ObservableObject { let generation = statusGeneration isRunning = true - // A standing error stays on screen through automatic runs, so the notice does not blink away at every activation. + /// A standing condition stays on screen through the automatic runs that check on it, so the + /// notice does not blink away at every activation. Only the person's own request shows + /// progress over it. if previousError == nil || trigger == .userRequest { syncStatus = .syncing } @@ -174,13 +188,14 @@ final class SyncCoordinator: ObservableObject { case .unavailable(let failure): return CycleResult(admission: admission, failure: failure) case .ready(let isNewAccount): - // A condition the last account was under says nothing about this one. + /// A condition the last account was under says nothing about this one. if isNewAccount { admission = .full } } - // Only an upload creates the zone: a download finding it again is how this Mac learns another device brought the data back. + /// Only a run that may upload creates the zone. A download looks for it, and finding it + /// again is how this Mac learns another device brought deleted data back. if metadataStorage.zoneState.createsZone(in: admission) { do { try await transport.ensureZoneExists() @@ -201,7 +216,8 @@ final class SyncCoordinator: ObservableObject { failure = await refined(decisive) } let nextZone = zone.after(failure: failure, reachedZone: pullFailure == nil) - // The removed zone's cached system fields would make every save into a recreated zone fail as an unknown record. + /// The old zone's records went with it. Kept, their system fields would make every later + /// save into a zone another device recreated fail as an unknown record. if nextZone == .removed, zone != .removed { recordCache.removeAll() metadataStorage.deferredDeletions = [] @@ -211,7 +227,9 @@ final class SyncCoordinator: ObservableObject { return CycleResult(admission: admission, failure: failure) } - // Read before any request, so a signed-out or restricted account is reported as itself, not as the first request's failure. + /// Reads the account before any request, so a signed-out or restricted account is reported as + /// itself rather than as whatever the first request fails with. Adopting its id is what keeps + /// another account's token, tombstones and cached records from being used against this one. private func confirmAccount() async -> AccountCheck { do { if let blocker = SyncBlocker(accountStatus: try await transport.accountStatus()) { @@ -225,6 +243,7 @@ final class SyncCoordinator: ObservableObject { } private enum AccountCheck { + /// Whether the account differs from the one this Mac last synced with. case ready(isNewAccount: Bool) case unavailable(SyncStepFailure) } @@ -243,7 +262,9 @@ final class SyncCoordinator: ObservableObject { return true } - // CloudKit answers notAuthenticated for every account state that is not available, so a fresh status read says which one. + /// CloudKit fails an operation with `notAuthenticated` for every account state that is not + /// available, so a fresh status read says which one it is. Signed in by that read, the account + /// is not ready yet rather than signed out. private func refined(_ failure: SyncStepFailure) async -> SyncStepFailure { guard case .blocked(let blocker) = failure.failure, blocker.isAccountState else { return failure } let status = try? await transport.accountStatus() @@ -251,7 +272,13 @@ final class SyncCoordinator: ObservableObject { return SyncStepFailure(failure: .blocked(actual), error: .blocked(actual), retryAfter: failure.retryAfter) } - // A returning run used to overwrite a Sync off or a lost license with "Synced", so a stale run reports nothing. + /// Publishes the outcome of a sync run, unless something decided the status while it was in + /// flight. + /// + /// A run reads `canSync()` once on entry and then suspends across the whole CloudKit round + /// trip, so turning sync off, or losing the license, used to be overwritten by the returning + /// run: the indicator went back to "Synced" for a sync that would now be refused. Whoever + /// decided last wins, and a stale run reports nothing. private func finish(_ result: CycleResult, previousError: SyncError?, from generation: Int) { guard generation == statusGeneration else { Self.logger.info("Discarding a sync outcome the status moved on from") @@ -285,6 +312,8 @@ final class SyncCoordinator: ObservableObject { ) ?? 30 let until = Date().addingTimeInterval(wait) throttledUntil = until + /// The next attempt comes once the throttle is over, never inside it, and also when + /// nothing else was going to try again. if nextAttempt.map({ $0 < until }) ?? true { nextAttempt = until scheduleRetry(after: wait) @@ -323,9 +352,11 @@ final class SyncCoordinator: ObservableObject { retryTask = nil } + /// Runs once for whatever arrived during the last run, with the trigger that may do the most. private func runPendingTrigger() async { guard !pendingTriggers.isEmpty else { return } let error = syncStatus.error + /// The network coming back mid-run matters only if the run ended unable to reach iCloud. let triggers = pendingTriggers.filter { $0 != .networkRestored || error == .offline } pendingTriggers = [] let attempt = nextAttempt @@ -341,13 +372,14 @@ final class SyncCoordinator: ObservableObject { await sync(next) } + /// Called when user enables sync in settings func enableSync() { Self.logger.info("enableSync() called") // Clear token to force a full fetch on first sync after enabling metadataStorage.saveToken(nil) metadataStorage.deferredDeletions = [] - // Turning sync on is the person's choice to upload, a removed zone included. + /// Turning sync on is the person's choice to upload, a removed zone included. metadataStorage.zoneState = .unknown // Mark ALL existing local data as dirty so it gets pushed on first sync @@ -363,7 +395,8 @@ final class SyncCoordinator: ObservableObject { } } - // Only on the person's word: Apple's guidance for a purged zone is never to upload again without asking. + /// The person's answer after TablePro's data was removed from iCloud: upload everything on this + /// Mac again. Never done without asking, which is Apple's guidance for a purged zone. func uploadAgain() async { guard syncStatus.error == .blocked(.dataDeletedFromICloud) else { return } guard !isRunning else { @@ -376,14 +409,15 @@ final class SyncCoordinator: ObservableObject { recordCache.removeAll() markAllLocalDataDirty() await markSQLFavoritesDirty() - // Sync may have been turned off while the favorites were read. + /// Sync may have been turned off while the favorites were read. guard syncStatus.error == .blocked(.dataDeletedFromICloud) else { return } resetRetryState() decide(.idle) await sync(.userRequest) } - // Separate from markAllLocalDataDirty because the favorite store is an actor and reads asynchronously. + /// Marks existing SQL favorites and folders dirty. Separate from `markAllLocalDataDirty` + /// because the favorite store is an actor and must be read asynchronously. private func markSQLFavoritesDirty() async { let favorites = await services.sqlFavoriteManager.fetchFavorites() changeTracker.markDirty(.favorite, ids: favorites.map { $0.id.uuidString }) @@ -392,7 +426,12 @@ final class SyncCoordinator: ObservableObject { changeTracker.markDirty(.favoriteFolder, ids: folders.map { $0.id.uuidString }) } - // One batch per type: marking record by record posted a notification each and hung the app on a few hundred column layouts. + /// Marks every synced record dirty so the first sync after enabling pushes the lot. + /// + /// Every type is marked as one batch. Marking record by record posted a change notification per + /// record, and an account with a few hundred saved column layouts built a chain of hundreds of + /// tasks each waiting on its predecessor until the app stopped responding to the switch that + /// started it. private func markAllLocalDataDirty() { let connections = services.connectionStorage.loadConnections() changeTracker.markDirty( @@ -447,6 +486,7 @@ final class SyncCoordinator: ObservableObject { Self.logger.info("Marked all local data dirty: \(summary, privacy: .public)") } + /// Called when user disables sync in settings func disableSync() { decide(.disabled(.userDisabled)) } @@ -467,13 +507,18 @@ final class SyncCoordinator: ObservableObject { return } - // An error stays until a run clears it: a license or account notification is no evidence that storage was freed. + /// Only a move out of disabled is decided here. An error stays until a run clears it: a + /// license or account notification is no evidence that storage was freed. if case .disabled = syncStatus { decide(metadataStorage.zoneState.initialStatus) } } - // Retires the run in flight, so a path that decided nothing must not call it or the status sticks on Syncing. + /// Settles the status from outside a sync run, and retires whatever run is in flight. + /// + /// The branch that leaves a running sync alone deliberately does not come through here: it has + /// decided nothing, so invalidating the run would leave the status stuck on Syncing with + /// nothing left to move it. private func decide(_ status: SyncStatus) { statusGeneration += 1 syncStatus = status @@ -580,7 +625,7 @@ final class SyncCoordinator: ObservableObject { let itemFailure = SyncStepFailure(outcome) if let interruption { Self.logger.error("Push stopped part way: \(interruption.localizedDescription)") - // A blocker an earlier batch hit explains the interruption too; anything less does not. + /// A blocker an earlier batch hit explains the interruption too; anything less does not. let failure = itemFailure.flatMap { $0.failure.stopsUpload ? $0 : nil } ?? SyncStepFailure(interruption) return PushReport(echoGuard: echoGuard, failure: failure) } @@ -635,7 +680,8 @@ final class SyncCoordinator: ObservableObject { Self.logger.info("Reading record types the last pull skipped: cleared sync token for full fetch") } - // A failed pull is reported, never passed over: the run did not bring this Mac up to date and must not say Synced. + /// Reports a failed download instead of passing over it: a run whose pull failed did not bring + /// this Mac up to date, and must not say Synced or move Last Synced. private func performPull(echoGuard: SyncEchoGuard?) async -> SyncStepFailure? { // Applied with the categories recorded here, so one turned off during the download is still read once. let settings = services.appSettingsStorage.loadSync() @@ -706,17 +752,9 @@ final class SyncCoordinator: ObservableObject { deletedRecordIDs: result.deletedRecordIDs, newToken: result.newToken ) - let themeRecords = pulledThemeRecords( - admitted.changedRecords, - deletedRecordIDs: deletedRecordIDs, - unreadRecordIDs: result.unreadRecordIDs, - isFullFetch: isFullFetch, - echoGuard: echoGuard - ) let storesPersisted = applyRemoteChanges( admitted, deletedRecordIDs: deletedRecordIDs, - themeRecords: themeRecords, settings: settings, echoGuard: echoGuard ) @@ -725,7 +763,11 @@ final class SyncCoordinator: ObservableObject { echoGuard: echoGuard ) - // Neither token nor cache is committed over a refused batch: saving the token first lost records the server never sent again. + /// The token and the cache are the record of what this device holds, so neither is + /// committed over a batch a store refused. Saving the token first acknowledged records that + /// were never written and the server never sent them again, and the cached record then + /// stood in as the merge base for an edit that had no local base at all. Not saving it + /// means the next pull replays the batch, which every apply here is written to survive. guard storesPersisted, favoritesOutcome != .failed else { Self.logger.error("Pull not acknowledged: a store refused to persist part of the batch") return false @@ -741,9 +783,6 @@ final class SyncCoordinator: ObservableObject { && SyncRecordType(rawValue: record.recordType).map { settings.syncs($0) } == true }) recordCache.remove(result.deletedRecordIDs + deferred.due + missing) - if isFullFetch { - recordCache.remove(themeRecordsGone(afterFullFetch: result, zoneID: zoneID, pushed: pushed)) - } settleDeferredDeletions(deferred, after: result, settings: settings) Self.logger.info( @@ -778,11 +817,11 @@ final class SyncCoordinator: ObservableObject { // Performance: storage reads here (loadSync, loadConnections, loadGroups, etc.) run on // @MainActor and can block the UI on large sync batches. Consider moving to Task.detached // for large payloads. - // False when a record was not persisted, so the token is withheld and the batch arrives again. + /// Reports whether every record that can say so was persisted. A pull that answers false must + /// not commit its token: the batch has to arrive again. private func applyRemoteChanges( _ result: PullResult, deletedRecordIDs: [CKRecord.ID], - themeRecords: PulledThemeRecords, settings: SyncSettings, echoGuard: SyncEchoGuard? ) -> Bool { @@ -792,7 +831,6 @@ final class SyncCoordinator: ObservableObject { let effects = applyRemoteRecords( result.changedRecords, deletedRecordIDs: deletedRecordIDs, - themeRecords: themeRecords, settings: settings, echoGuard: echoGuard ) @@ -808,7 +846,6 @@ final class SyncCoordinator: ObservableObject { private func applyRemoteRecords( _ changedRecords: [CKRecord], deletedRecordIDs: [CKRecord.ID], - themeRecords: PulledThemeRecords, settings: SyncSettings, echoGuard: SyncEchoGuard? ) -> SyncRemoteDeletionEffects { @@ -872,7 +909,7 @@ final class SyncCoordinator: ObservableObject { persistenceFailed = true } case .settings: - if applyRemoteSettings(record, tombstoneIds: settingsTombstoneIds, themeRecords: themeRecords) == .failed { + if applyRemoteSettings(record, tombstoneIds: settingsTombstoneIds) == .failed { persistenceFailed = true } case .tableFavorite: @@ -898,9 +935,9 @@ final class SyncCoordinator: ObservableObject { groupsOrTagsChanged = groupsOrTagsChanged || effects.groupsOrTagsChanged effects.persistenceFailed = persistenceFailed || effects.persistenceFailed - // After the batch, never per record: a pull carries no dependency order, so a legal - // hierarchy change spread over two records passes through a state that reads as a cycle - // until both have landed. + /// After the batch, never per record: a pull carries no dependency order, so a legal + /// hierarchy change spread over two records passes through a state that reads as a cycle + /// until both have landed. if groupsOrTagsChanged { services.groupStorage.repairHierarchy() } @@ -1064,7 +1101,8 @@ final class SyncCoordinator: ObservableObject { return name } - // False withholds the pull token, so a profile this Mac failed to save arrives again. + /// False when the profile could not be persisted, which withholds the pull token so the record + /// arrives again rather than being acknowledged and lost. private func applyRemoteCredentialProfile(_ record: CKRecord, tombstoneIds: Set) -> Bool { let remoteProfile: CredentialProfile do { @@ -1079,14 +1117,18 @@ final class SyncCoordinator: ObservableObject { var profiles = services.credentialProfileStorage.loadProfiles() if let index = profiles.firstIndex(where: { $0.id == remoteProfile.id }) { - // The password mode's payload never crosses the wire, so a local `.source` is kept over the `prompt` the remote sends instead. + /// The password mode's payload never crosses the wire, so a `.source` this Mac already + /// holds is kept rather than replaced by the `prompt` the remote had to send instead. var merged = remoteProfile if case .source = profiles[index].passwordMode, remoteProfile.passwordMode == .prompt { merged.passwordMode = profiles[index].passwordMode } profiles[index] = merged } else { - // Pickers offer a profile by name alone, so a same-named profile from another Mac is renamed rather than shown twice. + /// Two Macs can hold same-named profiles with different ids, which is what happens when + /// someone recreated one by hand before they synced. The editor forbids duplicate names + /// and every picker offers a profile by name alone, so the arriving one is renamed + /// rather than landing as an indistinguishable second row. var arriving = remoteProfile if profiles.contains(where: { $0.name.compare(arriving.name, options: .caseInsensitive) == .orderedSame }) { arriving.name = Self.availableProfileName(basedOn: arriving.name, taken: profiles) @@ -1094,7 +1136,7 @@ final class SyncCoordinator: ObservableObject { profiles.append(arriving) } guard services.credentialProfileStorage.saveProfilesWithoutSync(profiles) else { return false } - // The connections linked to it carry a copy of the username, which every raw reader uses. + /// The connections linked to it carry a copy of the username, which every raw reader uses. services.credentialProfileStorage.writeUsernameThrough(remoteProfile, byUser: false) return true } @@ -1117,24 +1159,19 @@ final class SyncCoordinator: ObservableObject { profiles.append(remoteProfile) } guard services.sshProfileStorage.saveProfilesWithoutSync(profiles) else { return false } - // Linked connections store a copy of the profile, so without this they keep tunnelling to the old host. + /// A linked connection stores the profile's configuration, so an edit that arrives from + /// another Mac has to reach those connections too or they keep tunnelling to the old host. services.sshProfileStorage.refreshLinkedConnections(with: remoteProfile, byUser: false) return true } - private func applyRemoteSettings( - _ record: CKRecord, - tombstoneIds: Set, - themeRecords: PulledThemeRecords - ) -> RemoteApplyOutcome { + private func applyRemoteSettings(_ record: CKRecord, tombstoneIds: Set) -> RemoteApplyOutcome { guard let category = SyncRecordMapper.settingsCategory(from: record), !tombstoneIds.contains(category), let data = SyncRecordMapper.settingsData(from: record) else { return .skipped } if ThemeCatalog.themeId(forSyncCategory: category) != nil { - return themeCatalog.applyRemote(category: category, data: data, merge: remoteThemeMerge) { newer in - tombstoneIds.contains(newer) ? nil : newerThemeRecord(newer, in: themeRecords) - } + return themeCatalog.applyRemote(category: category, data: data, merge: remoteThemeMerge) } do { try applySettingsData(data, for: category) @@ -1168,7 +1205,8 @@ final class SyncCoordinator: ObservableObject { return entry } - // Upserts: an insert-if-absent apply would keep the local environment tag and drop the remote one. + /// Upserts rather than inserts. A database favorite carries a mutable payload, the environment + /// tag, so an insert-if-absent apply would keep the local tag and silently drop the remote one. private func applyRemoteDatabaseFavorite(_ record: CKRecord, tombstoneIds: Set) { let entry: FavoriteDatabaseEntry do { @@ -1186,6 +1224,8 @@ final class SyncCoordinator: ObservableObject { // MARK: - Observers + /// Runs on each turn from off to on. `decide` drops the triggers on the turn back, so nothing + /// listens while sync is off. private func arm() { guard triggers == nil else { return } triggers = SyncTriggers( @@ -1194,7 +1234,7 @@ final class SyncCoordinator: ObservableObject { networkMonitor: observeNetwork() ) - // Empty local storage (a fresh install or a wipe) needs a full fetch: a delta returns nothing. + /// Empty local storage (a fresh install or a wipe) needs a full fetch: a delta returns nothing. if services.connectionStorage.loadConnections().isEmpty { metadataStorage.saveToken(nil) Self.logger.info("No local connections: cleared sync token for full fetch") @@ -1204,7 +1244,8 @@ final class SyncCoordinator: ObservableObject { scheduleThemeSeedIfNeeded() } - // Only requests a run: every run reads the account first, so a sign-out or a switch is handled there. + /// The run that follows reads the account and adopts its id, so a sign-out, a switch to another + /// account, and an account becoming ready are all handled where every other run handles them. private func observeAccountChanges() -> any NSObjectProtocol { NotificationCenter.default.addObserver( forName: .CKAccountChanged, @@ -1217,7 +1258,8 @@ final class SyncCoordinator: ObservableObject { } } - // An elapsed debounce is never cancelled: cancelling a run mid-request reported a failure, so an edit during it queues a run. + /// A debounce that has elapsed is never cancelled: an edit made while its run is in flight queues + /// one more run instead, because cancelling the run mid-request reported a failure. private func observeLocalChanges() -> AnyCancellable { services.appEvents.syncChangeTracked .receive(on: RunLoop.main) @@ -1247,7 +1289,9 @@ final class SyncCoordinator: ObservableObject { } } - // The monitor reports a return once, possibly before the run in flight reports offline, so it is kept for when the run ends. + /// The path can come back while a run is still retrying, before that run reports it could not + /// reach iCloud, and the monitor reports the return only once. So a return during a run is kept + /// for when it ends. private func observeNetwork() -> SyncNetworkMonitor? { let monitor = makeNetworkMonitor() monitor?.start { [weak self] in @@ -1295,7 +1339,8 @@ private final class RemoteConnectionBatch { } } -// Releasing it detaches every observer; the network monitor stops in its own deinit. +/// What starts a run while sync is on. Releasing it detaches every observer; the network monitor +/// stops in its own deinit. private final class SyncTriggers { private let accountObserver: any NSObjectProtocol private let localChanges: AnyCancellable diff --git a/apple/TablePro/Resources/Localizable.xcstrings b/apple/TablePro/Resources/Localizable.xcstrings index 20e63de08e..5912243f45 100644 --- a/apple/TablePro/Resources/Localizable.xcstrings +++ b/apple/TablePro/Resources/Localizable.xcstrings @@ -75556,7 +75556,7 @@ } } }, - "Except NULL and booleans, these colors show in the grid only while Color values by type is on in Settings > Data Grid." : { + "Except NULL and booleans, these colors show in the grid only while Color values by type is on in Settings > Data." : { }, "exclamation mark" : { diff --git a/apple/TablePro/Theme/ThemeCatalog.swift b/apple/TablePro/Theme/ThemeCatalog.swift index fe1b960b03..e53c4be15e 100644 --- a/apple/TablePro/Theme/ThemeCatalog.swift +++ b/apple/TablePro/Theme/ThemeCatalog.swift @@ -13,8 +13,8 @@ internal struct ThemeCatalogContents: Equatable, Sendable { internal static let empty = ThemeCatalogContents(themes: BuiltInThemes.all, rejected: [], userFiles: [:]) } -// Where a user theme was read from and the bytes read, so a save or a move to the Trash can tell -// that the file changed on disk since and leave the newer version alone. +/// Where a user theme was read from and the bytes read, so a save or a move to the Trash can tell +/// that the file changed on disk since and leave the newer version alone. internal struct ThemeFile: Equatable, Sendable { internal let url: URL internal let contents: Data @@ -33,8 +33,6 @@ internal struct ThemeTrashReceipt: Equatable, Sendable { internal let themeId: String? internal let originalURL: URL internal let trashedURL: URL - // Only the deletions the trash queued: one queued before it, such as a cleared value color, stays. - internal var tombstones: [String] = [] } internal struct ThemeFileTrash: Sendable { @@ -64,11 +62,11 @@ internal struct ThemeFileTrash: Sendable { } } -// Owns the list. A user theme is identified by its JSON `id` but lives in whatever file it was -// loaded from, so a save, rename or move to the Trash acts on that file rather than on `.json`; -// the two differ for any file the user named themselves. Every mutation updates the in-memory list -// synchronously, and the list reloads whenever TablePro becomes active, so a theme fixed, dropped -// or removed in Finder is picked up without a view asking. +/// Owns the list. A user theme is identified by its JSON `id` but lives in whatever file it was +/// loaded from, so a save, rename or move to the Trash acts on that file rather than on `.json`; +/// the two differ for any file the user named themselves. Every mutation updates the in-memory list +/// synchronously, and the list reloads whenever TablePro becomes active, so a theme fixed, dropped +/// or removed in Finder is picked up without a view asking. @MainActor internal final class ThemeCatalog: ObservableObject { internal static let shared = ThemeCatalog() @@ -79,8 +77,8 @@ internal final class ThemeCatalog: ObservableObject { // What the files hold. `themes` is this with colliding names told apart for display. private var loaded: [ThemeDefinition] = BuiltInThemes.all private var userFiles: [String: ThemeFile] = [:] - // Themes whose file went away since it was read. A save of one is refused rather than writing a - // new file, which would bring back a theme deleted in Finder or on another Mac. + /// Themes whose file went away since it was read. A save of one is refused rather than writing a + /// new file, which would bring back a theme deleted in Finder or on another Mac. private var vanished: [String: URL] = [:] private let userDirectory: URL private let bundleDirectory: URL? @@ -90,8 +88,8 @@ internal final class ThemeCatalog: ObservableObject { nonisolated private static let logger = Logger(subsystem: "com.TablePro", category: "ThemeCatalog") - // Loaded before the first activation, so a user theme in a slot is in the list the launch - // activation resolves against. + /// Loaded before the first activation, so a user theme in a slot is in the list the launch + /// activation resolves against. internal init( userDirectory: URL = ThemeCatalog.defaultUserDirectory, bundleDirectory: URL? = Bundle.main.resourceURL, @@ -127,25 +125,11 @@ internal final class ThemeCatalog: ObservableObject { adopt(Self.loadContents(userDirectory: userDirectory, bundleDirectory: bundleDirectory)) } - // Writes the theme to the file it came from, or a new theme to `.json`. The list then holds - // what the file holds, which the format normalizes. + /// Writes the theme to the file it came from, or a new theme to `.json`. The list then holds + /// what the file holds, which the format normalizes. internal func save(_ theme: ThemeDefinition) throws { - let previous = userFiles[theme.id] != nil ? storedTheme(id: theme.id) : nil try persist(theme) - markSyncEdit(of: storedTheme(id: theme.id) ?? theme, previous: previous) - } - - // Each record pushes whole, so one whose keys did not change would revert another Mac's edit of them. - private func markSyncEdit(of theme: ThemeDefinition, previous: ThemeDefinition?) { - guard let previous else { - syncTracker.markDirty(.settings, ids: Self.syncCategories(of: theme)) - return - } - let cleared = Self.newerSyncSchemas.filter { !theme.setsSlots(introducedIn: $0) && previous.setsSlots(introducedIn: $0) } - let changed = ([ThemeSchema.oldestReadable] + Self.newerSyncSchemas) - .filter { !cleared.contains($0) && !Self.ownedKeysMatch(theme, previous, schema: $0) } - syncTracker.markDirty(.settings, ids: changed.map { Self.syncCategory(for: theme.id, schema: $0) }) - syncTracker.markDeleted(.settings, ids: cleared.map { Self.syncCategory(for: theme.id, schema: $0) }) + syncTracker.markDirty(.settings, id: Self.syncCategory(for: theme.id)) } private func persist(_ theme: ThemeDefinition) throws { @@ -180,16 +164,8 @@ internal final class ThemeCatalog: ObservableObject { userFiles[id] = nil vanished[id] = file.url publish(loaded.filter { $0.id != id }) - // Another Mac may have set a value color this one has not pulled yet. - let categories = Self.allSyncCategories(for: id) - let queued = syncTracker.tombstonedIds(for: .settings) - syncTracker.markDeleted(.settings, ids: categories) - return ThemeTrashReceipt( - themeId: id, - originalURL: file.url, - trashedURL: trashed, - tombstones: categories.filter { !queued.contains($0) } - ) + syncTracker.markDeleted(.settings, id: Self.syncCategory(for: id)) + return ThemeTrashReceipt(themeId: id, originalURL: file.url, trashedURL: trashed) } @discardableResult @@ -204,15 +180,12 @@ internal final class ThemeCatalog: ObservableObject { internal func restore(_ receipt: ThemeTrashReceipt) throws { try FileManager.default.moveItem(at: receipt.trashedURL, to: receipt.originalURL) reloadSynchronously() - guard let id = receipt.themeId, userFiles[id] != nil, let restored = storedTheme(id: id) else { return } - let categories = Self.syncCategories(of: restored) - syncTracker.markDirty(.settings, ids: categories) - // A record the restored theme does not hold may carry another Mac's keys, which the queued deletion would remove. - syncTracker.discardTombstones(.settings, ids: receipt.tombstones.filter { !categories.contains($0) }) + guard let id = receipt.themeId, userFiles[id] != nil else { return } + syncTracker.markDirty(.settings, id: Self.syncCategory(for: id)) } - // Two themes with one name cannot be told apart in a popup, so a taken name is refused rather - // than numbered behind the user's back. + /// Two themes with one name cannot be told apart in a popup, so a taken name is refused rather + /// than numbered behind the user's back. internal func rename(id: String, to name: String) throws { let trimmed = name.trimmingCharacters(in: .whitespacesAndNewlines) guard var theme = storedTheme(id: id) else { @@ -290,8 +263,8 @@ internal final class ThemeCatalog: ObservableObject { } } - // `.json`, unless that name already holds another theme or a file that failed to load, - // which a save must never overwrite. + /// `.json`, unless that name already holds another theme or a file that failed to load, + /// which a save must never overwrite. private func availableURL(for id: String) -> URL { let taken = Set( userFiles.values.map(\.url.standardizedFileURL) @@ -306,8 +279,8 @@ internal final class ThemeCatalog: ObservableObject { return candidate } - // A file edited, replaced or removed outside TablePro since it was read is not overwritten, - // moved or recreated: the catalog reloads and the caller reports the conflict. + /// A file edited, replaced or removed outside TablePro since it was read is not overwritten, + /// moved or recreated: the catalog reloads and the caller reports the conflict. private func ensureUnchanged(_ file: ThemeFile) throws { let current: Data do { @@ -321,7 +294,7 @@ internal final class ThemeCatalog: ObservableObject { throw ThemeLoadError.changedOnDisk(file.url.lastPathComponent) } - // A file that is still there no longer loads as this theme, so it changed rather than went away. + /// A file that is still there no longer loads as this theme, so it changed rather than went away. private func ensureNotVanished(_ id: String) throws { guard let url = vanished[id] else { return } guard FileManager.default.fileExists(atPath: url.path) else { @@ -474,17 +447,9 @@ internal enum ThemeRemoteMerge: Sendable { case keepBoth } -// Each user theme syncs as the settings record `theme.`, holding only the keys format 3 reads, so the -// previous stable reads and edits it. A theme that sets a key a newer format introduced also syncs -// `themeSchema.`. Each record owns its own keys, and every payload is a canonical file. +// Each user theme syncs as one settings record, `theme.`, whose payload is the canonical file. internal extension ThemeCatalog { nonisolated static let syncCategoryPrefix = "theme." - // Never under `theme.`: a build that reads only format 3 passes over a category it does not know. - nonisolated static let schemaSyncCategoryPrefix = "themeSchema" - - nonisolated static let newerSyncSchemas = Array( - stride(from: ThemeSchema.oldestReadable + 1, through: ThemeSchema.current, by: 1) - ) // Themes are about 1.2 KB, and a slot path nests three containers deep, so anything past these // limits is not a theme and only costs parse time. @@ -495,69 +460,23 @@ internal extension ThemeCatalog { syncCategoryPrefix + id } - nonisolated static func syncCategory(for id: String, schema: Int) -> String { - guard schema > ThemeSchema.oldestReadable else { return syncCategory(for: id) } - return "\(schemaSyncCategoryPrefix)\(schema).\(id)" - } - - nonisolated static func syncCategories(of theme: ThemeDefinition) -> [String] { - [syncCategory(for: theme.id)] - + newerSyncSchemas.filter(theme.setsSlots(introducedIn:)).map { syncCategory(for: theme.id, schema: $0) } - } - nonisolated static func themeId(forSyncCategory category: String) -> String? { - syncRecord(forCategory: category)?.id - } - - nonisolated static func syncSchema(forCategory category: String) -> Int? { - syncRecord(forCategory: category)?.schema - } - - nonisolated private static func syncRecord(forCategory category: String) -> (id: String, schema: Int)? { - if category.hasPrefix(syncCategoryPrefix) { - return (String(category.dropFirst(syncCategoryPrefix.count)), ThemeSchema.oldestReadable) - } - guard category.hasPrefix(schemaSyncCategoryPrefix) else { return nil } - let rest = category.dropFirst(schemaSyncCategoryPrefix.count) - guard let dot = rest.firstIndex(of: "."), - let schema = Int(rest[.. ThemeSchema.oldestReadable - else { return nil } - return (String(rest[rest.index(after: dot)...]), schema) - } - - // `theme.` owns the format 3 slots, the name, the author and the appearance; a newer format's record - // owns only the slots that format introduced. - nonisolated private static func ownedKeysMatch(_ lhs: ThemeDefinition, _ rhs: ThemeDefinition, schema: Int) -> Bool { - let owned: (ThemeSlot) -> Bool = schema > ThemeSchema.oldestReadable - ? { $0.introducedInSchema == schema } - : { $0.introducedInSchema <= ThemeSchema.oldestReadable } - let slotsMatch = lhs.overrides.filter { owned($0.key) } == rhs.overrides.filter { owned($0.key) } - guard schema <= ThemeSchema.oldestReadable else { return slotsMatch } - return slotsMatch && lhs.name == rhs.name && lhs.author == rhs.author && lhs.appearance == rhs.appearance - } - - nonisolated private static func allSyncCategories(for id: String) -> [String] { - [syncCategory(for: id)] + newerSyncSchemas.map { syncCategory(for: id, schema: $0) } + guard category.hasPrefix(syncCategoryPrefix) else { return nil } + return String(category.dropFirst(syncCategoryPrefix.count)) } var syncCategories: [String] { - userFiles.keys.sorted().flatMap { id in - storedTheme(id: id).map(Self.syncCategories(of:)) ?? [Self.syncCategory(for: id)] - } + userFiles.keys.sorted().map(Self.syncCategory(for:)) } - // The file's own name, never the one the list shows to tell two themes apart. A newer format's - // record exists only while the theme sets one of its keys. + // The file's own name, never the one the list shows to tell two themes apart. func syncPayload(forCategory category: String) -> Data? { - guard let record = Self.syncRecord(forCategory: category), - userFiles[record.id] != nil, - let theme = storedTheme(id: record.id), - record.schema == ThemeSchema.oldestReadable || theme.setsSlots(introducedIn: record.schema) + guard let id = Self.themeId(forSyncCategory: category), + userFiles[id] != nil, + let theme = storedTheme(id: id) else { return nil } do { - return try ThemeFileFormat.encode(theme.restricted(toSchema: record.schema)) + return try ThemeFileFormat.encode(theme) } catch { Self.logger.error("Could not encode a theme for sync: \(error.publicLogShape, privacy: .public)") return nil @@ -567,15 +486,10 @@ internal extension ThemeCatalog { // A synced theme is untrusted input from another device, so it is validated before anything // touches disk. A record that fails validation is skipped, since the same bytes would fail // again; only a disk failure is `.failed`, so the pull is not acknowledged and replays. - func applyRemote( - category: String, - data: Data, - merge: ThemeRemoteMerge = .replace, - newerRecord: (String) -> Data? = { _ in nil } - ) -> RemoteApplyOutcome { - let record: (schema: Int, theme: ThemeDefinition) + func applyRemote(category: String, data: Data, merge: ThemeRemoteMerge = .replace) -> RemoteApplyOutcome { + let theme: ThemeDefinition do { - record = try Self.validatedRemoteTheme(category: category, data: data) + theme = try Self.validatedRemoteTheme(category: category, data: data) } catch { Self.logger.error( "Skipped synced theme \(category, privacy: .private(mask: .hash)): \(error.publicLogShape, privacy: .public)" @@ -584,11 +498,7 @@ internal extension ThemeCatalog { } do { - reloadIfChangedOnDisk(record.theme.id) - guard record.schema > ThemeSchema.oldestReadable else { - return try writeRemote(mergedTheme(record.theme, merge: merge, newerRecord: newerRecord), merge: merge) - } - return try applyRemoteSlots(of: record.theme, introducedIn: record.schema, merge: merge) + return try writeRemote(theme, merge: merge) } catch { Self.logger.error( "Could not write synced theme \(category, privacy: .private(mask: .hash)): \(error.publicLogShape, privacy: .public)" @@ -622,70 +532,12 @@ internal extension ThemeCatalog { if !removed.isEmpty { publish(loaded.filter { !removed.contains($0.id) }) } - syncTracker.discardDirty(.settings, ids: removed.sorted().flatMap(Self.allSyncCategories(for:))) + syncTracker.discardDirty(.settings, ids: removed.sorted().map(Self.syncCategory(for:))) return isPersisted } - // Another Mac cleared the theme's last key of `schema`, so this one clears that format's keys too. - @discardableResult - func removeSlotsWithoutSync(introducedIn schema: Int, ids: Set) -> Bool { - var isPersisted = true - var settled: [String] = [] - for id in ids.sorted() { - reloadIfChangedOnDisk(id) - guard userFiles[id] != nil, let local = storedTheme(id: id) else { continue } - settled.append(id) - guard local.setsSlots(introducedIn: schema) else { continue } - var cleared = local - cleared.overrides = local.overrides.filter { $0.key.introducedInSchema != schema } - do { - _ = try writeRemote(cleared, merge: .replace) - } catch { - Self.logger.error("Could not clear keys deleted on another Mac: \(error.publicLogShape, privacy: .public)") - settled.removeLast() - isPersisted = false - } - } - syncTracker.discardDirty(.settings, ids: settled.map { Self.syncCategory(for: $0, schema: schema) }) - return isPersisted - } - - // `theme.` owns only the keys format 3 reads, so a theme this Mac holds keeps its newer keys. - // One new here, or one whose local file may never have reached the server, takes them from their records. - private func mergedTheme( - _ record: ThemeDefinition, - merge: ThemeRemoteMerge, - newerRecord: (String) -> Data? - ) -> ThemeDefinition { - let base = record.restricted(toSchema: ThemeSchema.oldestReadable) - if merge == .replace, userFiles[record.id] != nil, let local = storedTheme(id: record.id) { - return base.replacingSlots(from: local) { $0.introducedInSchema > ThemeSchema.oldestReadable } - } - return Self.newerSyncSchemas.reduce(base) { merged, schema in - let category = Self.syncCategory(for: record.id, schema: schema) - guard let data = newerRecord(category), - let newer = try? Self.validatedRemoteTheme(category: category, data: data) - else { return merged } - return merged.replacingSlots(from: newer.theme) { $0.introducedInSchema == schema } - } - } - - // A theme new to this Mac takes these keys when its `theme.` arrives. While the local file may - // never have reached the server, `theme.` decides the whole theme and what is kept as a copy. - private func applyRemoteSlots( - of record: ThemeDefinition, - introducedIn schema: Int, - merge: ThemeRemoteMerge - ) throws -> RemoteApplyOutcome { - guard merge == .replace, userFiles[record.id] != nil, let local = storedTheme(id: record.id) else { - return .skipped - } - let merged = local.replacingSlots(from: record) { $0.introducedInSchema == schema } - guard merged != local else { return .skipped } - return try writeRemote(merged, merge: .replace) - } - private func writeRemote(_ theme: ThemeDefinition, merge: ThemeRemoteMerge) throws -> RemoteApplyOutcome { + reloadIfChangedOnDisk(theme.id) let data = try ThemeFileFormat.encode(theme) let url: URL var keptCopy: ThemeDefinition? @@ -712,7 +564,7 @@ internal extension ThemeCatalog { upsert(try ThemeFileFormat.decode(data)) // A pull suppresses this mark; the seed that follows it uploads the copy, which the server lacks. if let keptCopy { - syncTracker.markDirty(.settings, ids: Self.syncCategories(of: keptCopy)) + syncTracker.markDirty(.settings, id: Self.syncCategory(for: keptCopy.id)) } return .applied } @@ -753,15 +605,11 @@ internal extension ThemeCatalog { reloadSynchronously() } - nonisolated private static func validatedRemoteTheme( - category: String, - data: Data - ) throws -> (schema: Int, theme: ThemeDefinition) { - guard let record = syncRecord(forCategory: category), - ThemeIdentifier.isValid(record.id), - !record.id.hasPrefix(ThemeDefinition.builtInPrefix) + nonisolated private static func validatedRemoteTheme(category: String, data: Data) throws -> ThemeDefinition { + guard let id = themeId(forSyncCategory: category), + ThemeIdentifier.isValid(id), + !id.hasPrefix(ThemeDefinition.builtInPrefix) else { throw ThemeSyncRejection.invalidCategory } - guard record.schema <= ThemeSchema.current else { throw ThemeSyncRejection.newerFormat(record.schema) } guard data.count <= maximumSyncPayloadSize else { throw ThemeSyncRejection.tooLarge(data.count) } guard JSONNesting.depth(of: data, stoppingAfter: maximumSyncPayloadDepth) <= maximumSyncPayloadDepth else { throw ThemeSyncRejection.tooDeep @@ -769,14 +617,13 @@ internal extension ThemeCatalog { // The decoder refuses a newer schema, an unknown key, a bad color, and a name or author with a // control character or past the length cap. let theme = try ThemeFileFormat.decode(data) - guard theme.id == record.id else { throw ThemeSyncRejection.mismatchedIdentifier } - return (record.schema, theme) + guard theme.id == id else { throw ThemeSyncRejection.mismatchedIdentifier } + return theme } } private enum ThemeSyncRejection: Error { case invalidCategory - case newerFormat(Int) case tooLarge(Int) case tooDeep case mismatchedIdentifier diff --git a/apple/TableProTests/Core/Sync/SyncConflictRebaseTests.swift b/apple/TableProTests/Core/Sync/SyncConflictRebaseTests.swift index 35c9cfe0d8..a3fc339e00 100644 --- a/apple/TableProTests/Core/Sync/SyncConflictRebaseTests.swift +++ b/apple/TableProTests/Core/Sync/SyncConflictRebaseTests.swift @@ -196,13 +196,8 @@ struct SyncConflictRebaseTests { } @Test( - "Shortcuts, AI providers and a theme's records both Macs changed go up whole from this Mac, never mixed", - arguments: [ - AppSettingsCategory.keyboard, - AppSettingsCategory.ai, - ThemeCatalog.syncCategory(for: "ocean"), - ThemeCatalog.syncCategory(for: "ocean", schema: 4), - ] + "Shortcuts, AI providers and a theme both Macs changed go up whole from this Mac, never mixed", + arguments: [AppSettingsCategory.keyboard, AppSettingsCategory.ai, ThemeCatalog.syncCategory(for: "ocean")] ) func wholeCategoriesAreNotMixed(category: String) throws { let base = SyncRecordMapper.toCKRecord(category: category, settingsData: Data(#"{"a":1,"b":1}"#.utf8), in: Self.zoneID) diff --git a/apple/TableProTests/Core/Sync/SyncCoordinatorThemeTests.swift b/apple/TableProTests/Core/Sync/SyncCoordinatorThemeTests.swift index 78fc9f18df..33d20c49a2 100644 --- a/apple/TableProTests/Core/Sync/SyncCoordinatorThemeTests.swift +++ b/apple/TableProTests/Core/Sync/SyncCoordinatorThemeTests.swift @@ -99,24 +99,20 @@ struct SyncCoordinatorThemeTests { ) } - nonisolated private static func valueTheme(_ id: String, background: String = "#224466") throws -> ThemeDefinition { - var values = try theme(id, background: background) + nonisolated private static func valueTheme(_ id: String) throws -> ThemeDefinition { + var values = try theme(id) values.overrides[.gridString] = try ThemeColor(validating: "#F1FA8C") values.overrides[.gridNumber] = try ThemeColor(validating: "#BD93F9") return values } - private static func themeRecord(_ theme: ThemeDefinition) throws -> CKRecord { - SyncRecordMapper.toCKRecord( - category: ThemeCatalog.syncCategory(for: theme.id), - settingsData: try ThemeFileFormat.encode(theme.restricted(toSchema: ThemeSchema.oldestReadable)), - in: SyncTestEnvironment.zoneID - ) + nonisolated private static func schema(of payload: Data) throws -> Int? { + try (JSONSerialization.jsonObject(with: payload) as? [String: Any])?["schema"] as? Int } - private static func valueRecord(_ theme: ThemeDefinition) throws -> CKRecord { + private static func themeRecord(_ theme: ThemeDefinition) throws -> CKRecord { SyncRecordMapper.toCKRecord( - category: ThemeCatalog.syncCategory(for: theme.id, schema: 4), + category: ThemeCatalog.syncCategory(for: theme.id), settingsData: try ThemeFileFormat.encode(theme), in: SyncTestEnvironment.zoneID ) @@ -133,13 +129,6 @@ struct SyncCoordinatorThemeTests { ) } - private static func deletion(ofValuesOf id: String) -> CKRecord.ID { - CKRecord.ID( - recordName: SyncRecordType.settings.recordName(for: ThemeCatalog.syncCategory(for: id, schema: 4)), - zoneID: SyncTestEnvironment.zoneID - ) - } - private static func pull(_ records: [CKRecord], deleting deletions: [CKRecord.ID] = []) -> PullResult { PullResult(changedRecords: records, deletedRecordIDs: deletions, newToken: nil) } @@ -165,105 +154,17 @@ struct SyncCoordinatorThemeTests { #expect(harness.tracker.dirtyRecords(for: .settings).isEmpty) } - @Test("A new theme that arrives with its value record in one pull lands whole, whichever comes first", arguments: [true, false]) - func newThemeArrivesWithItsValueRecord(themeFirst: Bool) async throws { + @Test("A pulled format 4 theme record lands with its value colors") + func pulledFormatFourThemeKeepsItsValueColors() async throws { let harness = try Harness(label: "theme-values-arrival") let synced = try Self.valueTheme("user.values") - let records = [try Self.themeRecord(synced), try Self.valueRecord(synced)] - let acknowledged = await harness.coordinator.applyPullResult(Self.pull(themeFirst ? records : Array(records.reversed()))) + let acknowledged = await harness.coordinator.applyPullResult(Self.pull([try Self.themeRecord(synced)])) #expect(acknowledged) #expect(harness.catalog.storedTheme(id: synced.id) == synced) - #expect(harness.tracker.dirtyRecords(for: .settings).isEmpty) - } - - @Test("A value record that came before its theme waits in the cache for it") - func valueRecordWaitsInTheCache() async throws { - let harness = try Harness(label: "theme-values-cache") - let synced = try Self.valueTheme("user.values") - - let valuesFirst = await harness.coordinator.applyPullResult(Self.pull([try Self.valueRecord(synced)])) - #expect(valuesFirst) - #expect(!harness.fileExists(synced.id)) - let themeLater = await harness.coordinator.applyPullResult(Self.pull([try Self.themeRecord(synced)])) - - #expect(themeLater) - #expect(harness.catalog.storedTheme(id: synced.id) == synced) - } - - // A full fetch lists every record the zone holds, so the cached value record was deleted since. - @Test("A full fetch that lacks a value record leaves a new theme without the cached one's colors") - func fullFetchIgnoresACachedValueRecord() async throws { - let harness = try Harness(label: "theme-values-full-fetch") - let synced = try Self.valueTheme("user.values") - harness.environment.recordCache.store([try Self.valueRecord(synced)]) - - let acknowledged = await harness.coordinator.applyPullResult( - Self.pull([try Self.themeRecord(synced)]), - isFullFetch: true - ) - - #expect(acknowledged) - #expect(harness.catalog.storedTheme(id: synced.id) == synced.restricted(toSchema: ThemeSchema.oldestReadable)) - } - - // Without the drop, a delta pull that brings the theme back would take its colors from the stale copy. - @Test("A full fetch drops a cached value record it does not list, so a theme made again later takes no stale colors") - func fullFetchDropsAStaleValueRecord() async throws { - let harness = try Harness(label: "theme-values-stale-cache") - let synced = try Self.valueTheme("user.values") - let stale = try Self.valueRecord(synced) - harness.environment.recordCache.store([stale]) - - let fetched = await harness.coordinator.applyPullResult(Self.pull([try Self.themeRecord(synced)]), isFullFetch: true) - #expect(fetched) - #expect(!harness.environment.recordCache.holdsRecord(named: stale.recordID.recordName)) - - let deleted = await harness.coordinator.applyPullResult(Self.pull([], deleting: [Self.deletion(ofTheme: synced.id)])) - #expect(deleted) - #expect(harness.catalog.storedTheme(id: synced.id) == nil) - let remade = await harness.coordinator.applyPullResult(Self.pull([try Self.themeRecord(synced)])) - - #expect(remade) - #expect(harness.catalog.storedTheme(id: synced.id) == synced.restricted(toSchema: ThemeSchema.oldestReadable)) - } - - @Test("A value record the pull listed but could not read is not filled in from the cache") - func unreadValueRecordIsNotTakenFromTheCache() async throws { - let harness = try Harness(label: "theme-values-unread") - let synced = try Self.valueTheme("user.values") - let cached = try Self.valueRecord(synced) - harness.environment.recordCache.store([cached]) - - let acknowledged = await harness.coordinator.applyPullResult( - PullResult( - changedRecords: [try Self.themeRecord(synced)], - deletedRecordIDs: [], - newToken: nil, - unreadRecordIDs: [cached.recordID] - ) - ) - - #expect(acknowledged) - #expect(harness.catalog.storedTheme(id: synced.id) == synced.restricted(toSchema: ThemeSchema.oldestReadable)) - } - - // 0.86 reads only `theme.`, so its edit carries no value color and must not clear the ones set here. - @Test("An edit from a Mac that reads only format 3 keeps the value colors set here") - func formatThreeEditKeepsValueColors() async throws { - let harness = try Harness(label: "theme-values-older-edit") - let local = try Self.valueTheme("user.values") - try harness.writeOnDisk(local) - harness.metadata.themeSeedState = .done - let edited = try Self.theme("user.values", background: "#336699") - - let acknowledged = await harness.coordinator.applyPullResult(Self.pull([try Self.themeRecord(edited)])) - - #expect(acknowledged) - var expected = local - expected.overrides[.editorBackground] = edited.overrides[.editorBackground] - #expect(harness.catalog.storedTheme(id: local.id) == expected) + let onDisk = try Data(contentsOf: harness.themes.appendingPathComponent("\(synced.id).json")) + #expect(try Self.schema(of: onDisk) == 4) #expect(harness.tracker.dirtyRecords(for: .settings).isEmpty) } @@ -319,30 +220,13 @@ struct SyncCoordinatorThemeTests { try await waitUntil { harness.engine.pair.dark == BuiltInThemes.dark } } - @Test("A value record deleted on another Mac clears the value colors here, with no tombstone sent back") - func pulledValueDeletionClearsValueColors() async throws { - let harness = try Harness(label: "theme-values-deletion") - let values = try Self.valueTheme("user.values") - try harness.writeOnDisk(values) - - let acknowledged = await harness.coordinator.applyPullResult(Self.pull([], deleting: [Self.deletion(ofValuesOf: values.id)])) - - #expect(acknowledged) - #expect(harness.catalog.storedTheme(id: values.id) == values.restricted(toSchema: ThemeSchema.oldestReadable)) - #expect(harness.tracker.tombstonedIds(for: .settings).isEmpty) - #expect(harness.tracker.dirtyRecords(for: .settings).isEmpty) - } - @Test("A theme this Mac moved to the Trash stays gone when an edit of it arrives") func localTombstoneBeatsARemoteEdit() async throws { let harness = try Harness(label: "theme-tombstone") let night = try Self.theme("user.night") try harness.writeOnDisk(night) try harness.catalog.trash(id: night.id) - #expect( - harness.tracker.tombstonedIds(for: .settings) - == [ThemeCatalog.syncCategory(for: night.id), ThemeCatalog.syncCategory(for: night.id, schema: 4)] - ) + #expect(harness.tracker.tombstonedIds(for: .settings) == [ThemeCatalog.syncCategory(for: night.id)]) let edited = try Self.theme("user.night", background: "#336699") let acknowledged = await harness.coordinator.applyPullResult(Self.pull([try Self.themeRecord(edited)])) @@ -375,44 +259,30 @@ struct SyncCoordinatorThemeTests { #expect(harness.tracker.tombstonedIds(for: .settings).isEmpty) } - // A Mac on 0.86 reads only format 3, so every theme reaches it as format 3 and the value colors go apart. - @Test("A theme goes out as format 3 and its value colors as a format 4 record, and its trash deletes both") - func valueColorsPushInTheirOwnRecord() async throws { - let harness = try Harness(label: "theme-push-values") + @Test("Each theme pushes one record, theme.: format 4 holding its value colors, or format 3 without them") + func pushWritesTheOldestFormatHoldingTheTheme() async throws { + let harness = try Harness(label: "theme-push-format") let night = try Self.theme("user.night") let values = try Self.valueTheme("user.values") try harness.catalog.save(night) try harness.catalog.save(values) #expect(await harness.coordinator.runSyncCycle() == nil) - let pushed = await harness.transport.pushedRecords - - let base = try Self.pushedTheme(ThemeCatalog.syncCategory(for: values.id), in: pushed) - #expect(base.schema == 3) - #expect(base.theme == values.restricted(toSchema: ThemeSchema.oldestReadable)) - let newer = try Self.pushedTheme(ThemeCatalog.syncCategory(for: values.id, schema: 4), in: pushed) - #expect(newer.schema == 4) - #expect(newer.theme == values) - #expect(try Self.pushedTheme(ThemeCatalog.syncCategory(for: night.id), in: pushed).schema == 3) - let nightValues = ThemeCatalog.syncCategory(for: night.id, schema: 4) - #expect(!pushed.contains { SyncRecordMapper.settingsCategory(from: $0) == nightValues }) - - try harness.catalog.trash(id: values.id) - #expect(await harness.coordinator.runSyncCycle() == nil) - let deletions = await harness.transport.pushedDeletions - #expect(deletions.contains(Self.deletion(ofTheme: values.id))) - #expect(deletions.contains(Self.deletion(ofValuesOf: values.id))) - #expect(harness.tracker.tombstonedIds(for: .settings).isEmpty) - } - private static func pushedTheme( - _ category: String, - in records: [CKRecord] - ) throws -> (schema: Int?, theme: ThemeDefinition) { - let record = try #require(records.last { SyncRecordMapper.settingsCategory(from: $0) == category }) - let data = try #require(SyncRecordMapper.settingsData(from: record)) - let root = try JSONSerialization.jsonObject(with: data) as? [String: Any] - return (root?["schema"] as? Int, try ThemeFileFormat.decode(data)) + let pushed = await harness.transport.pushedRecords + let themes = Dictionary(pushed.compactMap { record -> (String, Data)? in + guard let category = SyncRecordMapper.settingsCategory(from: record), + ThemeCatalog.themeId(forSyncCategory: category) != nil, + let data = SyncRecordMapper.settingsData(from: record) + else { return nil } + return (category, data) + }, uniquingKeysWith: { _, last in last }) + #expect(Set(themes.keys) == [ThemeCatalog.syncCategory(for: night.id), ThemeCatalog.syncCategory(for: values.id)]) + let plain = try #require(themes[ThemeCatalog.syncCategory(for: night.id)]) + #expect(try Self.schema(of: plain) == 3) + let colored = try #require(themes[ThemeCatalog.syncCategory(for: values.id)]) + #expect(try Self.schema(of: colored) == 4) + #expect(try ThemeFileFormat.decode(colored) == values) } @Test("Turning sync on marks every theme on this Mac") diff --git a/apple/TableProTests/Theme/ThemeCatalogSyncTests.swift b/apple/TableProTests/Theme/ThemeCatalogSyncTests.swift index 8487796b04..29ec2a9024 100644 --- a/apple/TableProTests/Theme/ThemeCatalogSyncTests.swift +++ b/apple/TableProTests/Theme/ThemeCatalogSyncTests.swift @@ -2,6 +2,10 @@ // ThemeCatalogSyncTests.swift // TableProTests // +// Each user theme syncs as the settings record `theme.`. The catalog marks it on every edit, +// tombstones it after a move to the Trash, and writes what another Mac sent without marking +// anything, after treating it as untrusted input. +// import AppKit import Foundation @@ -90,14 +94,9 @@ struct ThemeCatalogSyncTests { ) } - nonisolated private static func valueTheme( - _ id: String, - name: String? = nil, - background: String = "#101010", - string: String = "#F1FA8C" - ) throws -> ThemeDefinition { - var values = try theme(id, name: name, background: background) - values.overrides[.gridString] = try ThemeColor(validating: string) + nonisolated private static func valueTheme(_ id: String) throws -> ThemeDefinition { + var values = try theme(id) + values.overrides[.gridString] = try ThemeColor(validating: "#F1FA8C") values.overrides[.gridNumber] = try ThemeColor(validating: "#BD93F9") return values } @@ -106,12 +105,8 @@ struct ThemeCatalogSyncTests { ThemeCatalog.syncCategory(for: id) } - nonisolated private static func valuesCategory(_ id: String) -> String { - ThemeCatalog.syncCategory(for: id, schema: 4) - } - nonisolated private static func schema(of payload: Data) throws -> Int? { - (try JSONSerialization.jsonObject(with: payload) as? [String: Any])?["schema"] as? Int + try (JSONSerialization.jsonObject(with: payload) as? [String: Any])?["schema"] as? Int } // MARK: - Local changes @@ -141,77 +136,6 @@ struct ThemeCatalogSyncTests { #expect(harness.tombstoned.isEmpty) } - @Test("A theme with value colors marks its value record too, and only its theme record stays format 3") - func valueColorsSyncInTheirOwnRecord() throws { - let harness = try Harness() - defer { harness.remove() } - let values = try Self.valueTheme("user.values") - - try harness.catalog.save(values) - - let both: Set = [Self.category(values.id), Self.valuesCategory(values.id)] - #expect(harness.dirty == both) - #expect(harness.catalog.syncCategories == [Self.category(values.id), Self.valuesCategory(values.id)]) - let base = try #require(harness.catalog.syncPayload(forCategory: Self.category(values.id))) - #expect(try Self.schema(of: base) == 3) - #expect(try ThemeFileFormat.decode(base) == values.restricted(toSchema: ThemeSchema.oldestReadable)) - let newer = try #require(harness.catalog.syncPayload(forCategory: Self.valuesCategory(values.id))) - #expect(try Self.schema(of: newer) == 4) - #expect(try ThemeFileFormat.decode(newer) == values) - } - - @Test("Clearing a theme's last value color deletes its value record, and a theme that never had one deletes nothing") - func clearingValueColorsDeletesTheirRecord() throws { - let harness = try Harness() - defer { harness.remove() } - let values = try Self.valueTheme("user.values") - try harness.catalog.save(values) - - try harness.catalog.save(values.restricted(toSchema: ThemeSchema.oldestReadable)) - - #expect(harness.dirty == [Self.category(values.id)]) - #expect(harness.tombstoned == [Self.valuesCategory(values.id)]) - #expect(harness.catalog.syncPayload(forCategory: Self.valuesCategory(values.id)) == nil) - - let night = try Self.theme("user.night") - try harness.catalog.save(night) - try harness.catalog.save(try Self.theme("user.night", background: "#202020")) - #expect(harness.tombstoned == [Self.valuesCategory(values.id)]) - } - - enum OwnedEdit: String, CaseIterable, Sendable { - case nothing, rename, editorColor, valueColor - } - - // Each record pushes whole, so one marked for keys it does not own would revert another Mac's edit of them. - @Test("An edit of a theme marks only the record that owns the keys it changed", arguments: OwnedEdit.allCases) - func editMarksOnlyTheRecordThatOwnsItsKeys(_ edit: OwnedEdit) throws { - let harness = try Harness() - defer { harness.remove() } - let values = try Self.valueTheme("user.values") - try harness.write(values) - harness.reopen() - var edited = values - - switch edit { - case .nothing: - try harness.catalog.save(edited) - #expect(harness.dirty.isEmpty) - case .rename: - try harness.catalog.rename(id: values.id, to: "Renamed") - #expect(harness.dirty == [Self.category(values.id)]) - case .editorColor: - edited.overrides[.editorBackground] = try ThemeColor(validating: "#202020") - try harness.catalog.save(edited) - #expect(harness.dirty == [Self.category(values.id)]) - case .valueColor: - edited.overrides[.gridNumber] = try ThemeColor(validating: "#FF5555") - try harness.catalog.save(edited) - #expect(harness.dirty == [Self.valuesCategory(values.id)]) - } - #expect(harness.tombstoned.isEmpty) - } - @Test("Loading the folder and reloading on activation mark nothing") func loadsMarkNothing() throws { let harness = try Harness() @@ -227,7 +151,7 @@ struct ThemeCatalogSyncTests { #expect(harness.tombstoned.isEmpty) } - @Test("Moving a theme to the Trash tombstones its records only once the file is gone") + @Test("Moving a theme to the Trash tombstones its record only once the file is gone") func trashTombstonesAfterTheMove() throws { let tombstonedDuringMove = OSAllocatedUnfairLock(initialState: nil) let probe = TrackerProbe() @@ -248,62 +172,11 @@ struct ThemeCatalogSyncTests { try harness.catalog.trash(id: night.id) #expect(tombstonedDuringMove.withLock { $0 } == false) - #expect(harness.tombstoned == [Self.category(night.id), Self.valuesCategory(night.id)]) + #expect(harness.tombstoned == [Self.category(night.id)]) #expect(!harness.dirty.contains(Self.category(night.id))) #expect(try harness.fileNames().isEmpty) } - // Another Mac may have set a value color this one has not pulled, so its record goes too. - @Test("A trash deletes the value record even when the local copy sets no value color") - func trashDeletesTheValueRecordOfAThemeWithoutValueColors() throws { - let harness = try Harness() - defer { harness.remove() } - let night = try Self.theme("user.night") - try harness.write(night) - harness.reopen() - #expect(!night.setsSlots(introducedIn: 4)) - - try harness.catalog.trash(id: night.id) - - #expect(harness.tombstoned.contains(Self.valuesCategory(night.id))) - } - - @Test("Moving a theme with value colors to the Trash tombstones both records, and putting it back marks both") - func trashTombstonesTheValueRecordToo() throws { - let harness = try Harness() - defer { harness.remove() } - let values = try Self.valueTheme("user.values") - try harness.write(values) - harness.reopen() - let both: Set = [Self.category(values.id), Self.valuesCategory(values.id)] - - let receipt = try harness.catalog.trash(id: values.id) - #expect(harness.tombstoned == both) - #expect(harness.dirty.isEmpty) - - try harness.catalog.restore(receipt) - #expect(harness.dirty == both) - } - - // A queued deletion of a record the restored theme does not hold would remove value colors another Mac set. - @Test("Putting a trashed theme back takes back only the deletions the trash queued") - func restoreTakesBackTheTrashDeletions() throws { - let harness = try Harness() - defer { harness.remove() } - let night = try Self.theme("user.night") - try harness.write(night) - let values = try Self.valueTheme("user.values") - try harness.write(values) - harness.reopen() - - try harness.catalog.restore(try harness.catalog.trash(id: night.id)) - #expect(!harness.tombstoned.contains(Self.valuesCategory(night.id))) - - try harness.catalog.save(values.restricted(toSchema: ThemeSchema.oldestReadable)) - try harness.catalog.restore(try harness.catalog.trash(id: values.id)) - #expect(harness.tombstoned.contains(Self.valuesCategory(values.id))) - } - @Test("Putting a trashed theme back marks its record dirty again") func restoreMarksDirty() throws { let harness = try Harness() @@ -371,70 +244,6 @@ struct ThemeCatalogSyncTests { #expect(harness.catalog.syncPayload(forCategory: Self.category(synced.id)) == payload) } - @Test("A value record for a theme this Mac does not hold waits for the theme record, which lands as format 4") - func valueRecordWaitsForItsTheme() throws { - let harness = try Harness() - defer { harness.remove() } - let synced = try Self.valueTheme("user.values") - let newer = try ThemeFileFormat.encode(synced) - - #expect(harness.catalog.applyRemote(category: Self.valuesCategory(synced.id), data: newer) == .skipped) - #expect(try harness.fileNames().isEmpty) - - let base = try ThemeFileFormat.encode(synced.restricted(toSchema: ThemeSchema.oldestReadable)) - let outcome = harness.catalog.applyRemote(category: Self.category(synced.id), data: base) { category in - category == Self.valuesCategory(synced.id) ? newer : nil - } - - #expect(outcome == .applied) - #expect(harness.catalog.storedTheme(id: synced.id) == synced) - #expect(try Self.schema(of: Data(contentsOf: harness.themes.appendingPathComponent("user.values.json"))) == 4) - #expect(harness.catalog.syncPayload(forCategory: Self.category(synced.id)) == base) - #expect(harness.dirty.isEmpty) - } - - @Test("A synced theme record keeps the value colors this Mac holds") - func themeRecordKeepsLocalValueColors() throws { - let harness = try Harness() - defer { harness.remove() } - let local = try Self.valueTheme("user.values") - try harness.write(local) - harness.reopen() - - let edited = try Self.theme("user.values", name: "Edited elsewhere", background: "#202020") - let outcome = harness.catalog.applyRemote(category: Self.category(edited.id), data: try ThemeFileFormat.encode(edited)) - - #expect(outcome == .applied) - var expected = edited - expected.overrides[.gridString] = local.overrides[.gridString] - expected.overrides[.gridNumber] = local.overrides[.gridNumber] - #expect(harness.catalog.storedTheme(id: local.id) == expected) - #expect(harness.dirty.isEmpty) - } - - @Test("A synced value record changes only the value colors") - func valueRecordChangesOnlyValueColors() throws { - let harness = try Harness() - defer { harness.remove() } - let local = try Self.valueTheme("user.values") - try harness.write(local) - harness.reopen() - - var incoming = try Self.theme("user.values", name: "Other name", background: "#202020") - incoming.overrides[.gridDateTime] = try ThemeColor(validating: "#8BE9FD") - let payload = try ThemeFileFormat.encode(incoming) - let outcome = harness.catalog.applyRemote(category: Self.valuesCategory(incoming.id), data: payload) - - #expect(outcome == .applied) - var expected = local - expected.overrides[.gridString] = nil - expected.overrides[.gridNumber] = nil - expected.overrides[.gridDateTime] = incoming.overrides[.gridDateTime] - #expect(harness.catalog.storedTheme(id: local.id) == expected) - #expect(harness.dirty.isEmpty) - #expect(harness.catalog.applyRemote(category: Self.valuesCategory(incoming.id), data: payload) == .skipped) - } - @Test("A synced copy of the file already on disk writes nothing") func identicalRemoteThemeIsSkipped() throws { let harness = try Harness() @@ -493,34 +302,6 @@ struct ThemeCatalogSyncTests { #expect(try harness.fileNames().count == 2) } - @Test("While the local file may be unsynced, value colors come from their record and the local version stays as a copy") - func keepBothTakesValueColorsFromTheirRecord() throws { - let harness = try Harness() - defer { harness.remove() } - let local = try Self.valueTheme("user.x", name: "Ocean", string: "#111111") - try harness.write(local) - harness.reopen() - let incoming = try Self.valueTheme("user.x", name: "Ocean", string: "#222222") - let newer = try ThemeFileFormat.encode(incoming) - - #expect(harness.catalog.applyRemote(category: Self.valuesCategory(incoming.id), data: newer, merge: .keepBoth) == .skipped) - #expect(harness.catalog.storedTheme(id: local.id) == local) - - let outcome = harness.catalog.applyRemote( - category: Self.category(incoming.id), - data: try ThemeFileFormat.encode(incoming.restricted(toSchema: ThemeSchema.oldestReadable)), - merge: .keepBoth - ) { category in - category == Self.valuesCategory(incoming.id) ? newer : nil - } - - #expect(outcome == .applied) - #expect(harness.catalog.storedTheme(id: incoming.id) == incoming) - let kept = try #require(harness.catalog.themes.first { !$0.isBuiltIn && $0.id != incoming.id }) - #expect(kept.overrides == local.overrides) - #expect(harness.dirty == [Self.category(kept.id), Self.valuesCategory(kept.id)]) - } - @Test("A kept copy keeps its name when the record arrives under another name") func keptCopyKeepsAFreeName() throws { let harness = try Harness() @@ -608,6 +389,37 @@ struct ThemeCatalogSyncTests { #expect(harness.catalog.theme(id: synced.id) == nil) } + @Test("A synced format 4 theme lands with its value colors and marks nothing") + func remoteFormatFourThemeKeepsItsValueColors() throws { + let harness = try Harness() + defer { harness.remove() } + let synced = try Self.valueTheme("user.values") + let payload = try ThemeFileFormat.encode(synced) + #expect(try Self.schema(of: payload) == 4) + + #expect(harness.catalog.applyRemote(category: Self.category(synced.id), data: payload) == .applied) + + #expect(harness.catalog.storedTheme(id: synced.id) == synced) + #expect(try Data(contentsOf: harness.themes.appendingPathComponent("user.values.json")) == payload) + #expect(harness.dirty.isEmpty) + } + + @Test("A synced format 3 record holding a value color is skipped, since that format cannot hold the key") + func remoteFormatThreeRecordWithAValueColorIsSkipped() throws { + let harness = try Harness() + defer { harness.remove() } + let payload = Data(##"{"schema":3,"id":"user.a","name":"a","appearance":"dark","dataGrid":{"string":"#F1FA8C"}}"##.utf8) + #expect(throws: ThemeLoadError.keysNeedNewerFormat(["dataGrid.string"], format: 4)) { + try ThemeFileFormat.decode(payload) + } + + #expect(harness.catalog.applyRemote(category: Self.category("user.a"), data: payload) == .skipped) + + #expect(try harness.fileNames().isEmpty) + #expect(harness.catalog.storedTheme(id: "user.a") == nil) + #expect(harness.dirty.isEmpty) + } + struct Rejection: CustomTestStringConvertible, Sendable { let label: String let category: String @@ -642,9 +454,6 @@ struct ThemeCatalogSyncTests { Rejection(label: "newer format", category: "theme.user.a") { Data(#"{"schema":\#(ThemeSchema.current + 1),"id":"user.a","name":"a","appearance":"dark"}"#.utf8) }, - Rejection(label: "format 4 key in a format 3 record", category: "theme.user.a") { - Data(##"{"schema":3,"id":"user.a","name":"a","appearance":"dark","dataGrid":{"string":"#F1FA8C"}}"##.utf8) - }, Rejection(label: "unknown key", category: "theme.user.a") { Data(#"{"schema":3,"id":"user.a","name":"a","appearance":"dark","script":"run"}"#.utf8) }, @@ -673,65 +482,6 @@ struct ThemeCatalogSyncTests { #expect(harness.dirty.isEmpty) } - nonisolated static let valueRecordRejections: [Rejection] = [ - Rejection(label: "document for another id", category: "themeSchema4.user.a") { - try ThemeFileFormat.encode(try valueTheme("user.b")) - }, - Rejection(label: "built-in id", category: "themeSchema4.tablepro.dracula") { - Data(##"{"schema":4,"id":"tablepro.dracula","name":"Fake","appearance":"dark","dataGrid":{"string":"#F1FA8C"}}"##.utf8) - }, - Rejection(label: "format this build cannot read", category: "themeSchema\(ThemeSchema.current + 1).user.a") { - try ThemeFileFormat.encode(try valueTheme("user.a")) - }, - Rejection(label: "format 3 under the value prefix", category: "themeSchema3.user.a") { - try ThemeFileFormat.encode(try valueTheme("user.a")) - }, - Rejection(label: "format number with a leading zero", category: "themeSchema04.user.a") { - try ThemeFileFormat.encode(try valueTheme("user.a")) - }, - Rejection(label: "payload past the size cap", category: "themeSchema4.user.a") { - let body = ##"{"schema":4,"id":"user.a","name":"a","appearance":"dark","dataGrid":{"string":"#F1FA8C"}}"## - return Data((body + String(repeating: " ", count: ThemeCatalog.maximumSyncPayloadSize)).utf8) - }, - Rejection(label: "payload nested past the depth cap", category: "themeSchema4.user.a") { - Data(##"{"schema":4,"id":"user.a","name":"a","appearance":"dark","dataGrid":{"a":{"b":{"c":{"d":"#000000"}}}}}"##.utf8) - }, - Rejection(label: "unknown key", category: "themeSchema4.user.a") { - Data(##"{"schema":4,"id":"user.a","name":"a","appearance":"dark","dataGrid":{"string":"#F1FA8C"},"script":"run"}"##.utf8) - }, - ] - - @Test("A value record that fails validation leaves the local theme alone", arguments: valueRecordRejections) - func invalidValueRecordIsSkipped(_ rejection: Rejection) throws { - let harness = try Harness() - defer { harness.remove() } - let local = try Self.theme("user.a") - let url = try harness.write(local) - harness.reopen() - let before = try Data(contentsOf: url) - - let outcome = harness.catalog.applyRemote(category: rejection.category, data: try rejection.payload()) - - #expect(outcome == .skipped) - #expect(try Data(contentsOf: url) == before) - #expect(harness.catalog.storedTheme(id: local.id) == local) - #expect(harness.dirty.isEmpty) - } - - @Test("Both record kinds name their theme, and the value record's prefix is not the theme record's") - func syncCategoriesNameTheirTheme() { - #expect(ThemeCatalog.syncCategory(for: "user.a", schema: 4) == "themeSchema4.user.a") - #expect(ThemeCatalog.syncCategory(for: "user.a", schema: ThemeSchema.oldestReadable) == "theme.user.a") - #expect(ThemeCatalog.themeId(forSyncCategory: "themeSchema4.user.a") == "user.a") - #expect(ThemeCatalog.syncSchema(forCategory: "themeSchema4.user.a") == 4) - #expect(ThemeCatalog.syncSchema(forCategory: "theme.user.a") == ThemeSchema.oldestReadable) - for category in ["themeSchema04.user.a", "themeSchema3.user.a", "themeSchema+4.user.a", "themeSchema4", "themeSchemaX.user.a"] { - #expect(ThemeCatalog.themeId(forSyncCategory: category) == nil, "\(category)") - } - // 0.86 treats any category under `theme.` as a theme it must read. - #expect(!ThemeCatalog.syncCategory(for: "user.a", schema: 4).hasPrefix("theme.")) - } - @Test("A synced edit of a theme removed in Finder brings it back, since only a deletion record removes it") func remoteEditAfterAFinderDelete() throws { let harness = try Harness() @@ -771,40 +521,6 @@ struct ThemeCatalogSyncTests { #expect(harness.tombstoned.isEmpty) } - @Test("A theme deleted on another Mac drops the pending marks of its value record too") - func remoteDeletionDropsTheValueRecordMark() throws { - let harness = try Harness() - defer { harness.remove() } - let values = try Self.valueTheme("user.values") - try harness.catalog.save(values) - #expect(harness.dirty == [Self.category(values.id), Self.valuesCategory(values.id)]) - - #expect(harness.catalog.removeWithoutSync(ids: [values.id])) - - #expect(harness.catalog.theme(id: values.id) == nil) - #expect(harness.dirty.isEmpty) - #expect(harness.tombstoned.isEmpty) - } - - @Test("A value record deleted on another Mac clears the value colors here and marks nothing") - func remoteValueDeletionClearsValueColors() throws { - let harness = try Harness() - defer { harness.remove() } - let values = try Self.valueTheme("user.values") - try harness.write(values) - let night = try Self.theme("user.night") - let nightURL = try harness.write(night) - harness.reopen() - let nightBytes = try Data(contentsOf: nightURL) - - #expect(harness.catalog.removeSlotsWithoutSync(introducedIn: 4, ids: [values.id, night.id, "user.unknown"])) - - #expect(harness.catalog.storedTheme(id: values.id) == values.restricted(toSchema: ThemeSchema.oldestReadable)) - #expect(try Data(contentsOf: nightURL) == nightBytes) - #expect(harness.dirty.isEmpty) - #expect(harness.tombstoned.isEmpty) - } - @Test("A remote deletion leaves alone a file that was replaced by another theme since it loaded") func remoteDeletionSparesAReplacedFile() throws { let harness = try Harness() @@ -847,10 +563,27 @@ struct ThemeCatalogSyncTests { #expect(harness.catalog.syncPayload(forCategory: Self.category(BuiltInThemes.defaultDarkId)) == nil) #expect(harness.catalog.syncPayload(forCategory: Self.category("user.missing")) == nil) #expect(harness.catalog.syncPayload(forCategory: "appearance") == nil) - #expect(harness.catalog.syncPayload(forCategory: Self.valuesCategory(night.id)) == nil) #expect(harness.catalog.syncCategories == [Self.category(night.id)]) } + @Test("A theme with a value color syncs as one format 4 record, and a theme without one as format 3") + func payloadFormatFollowsTheKeysSet() throws { + let harness = try Harness() + defer { harness.remove() } + let night = try Self.theme("user.night") + let values = try Self.valueTheme("user.values") + try harness.catalog.save(night) + try harness.catalog.save(values) + + #expect(harness.dirty == [Self.category(night.id), Self.category(values.id)]) + #expect(harness.catalog.syncCategories == [Self.category(night.id), Self.category(values.id)]) + let plain = try #require(harness.catalog.syncPayload(forCategory: Self.category(night.id))) + #expect(try Self.schema(of: plain) == 3) + let colored = try #require(harness.catalog.syncPayload(forCategory: Self.category(values.id))) + #expect(try Self.schema(of: colored) == 4) + #expect(try ThemeFileFormat.decode(colored) == values) + } + // MARK: - Names @Test("Two themes with one name show apart in the list, while their files and records keep the name") From 54a4f38d022e7e6aff825c8762967524f55e5d23 Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Wed, 11 Nov 2026 23:06:40 +0700 Subject: [PATCH 3/3] docs(changelog): add fragments for #4205 --- apple/changelog.d/4205.added.1.md | 1 + apple/changelog.d/4205.added.md | 1 + apple/changelog.d/4205.changed.md | 1 + apple/changelog.d/4205.fixed.md | 1 + 4 files changed, 4 insertions(+) create mode 100644 apple/changelog.d/4205.added.1.md create mode 100644 apple/changelog.d/4205.added.md create mode 100644 apple/changelog.d/4205.changed.md create mode 100644 apple/changelog.d/4205.fixed.md diff --git a/apple/changelog.d/4205.added.1.md b/apple/changelog.d/4205.added.1.md new file mode 100644 index 0000000000..fe6fe25f96 --- /dev/null +++ b/apple/changelog.d/4205.added.1.md @@ -0,0 +1 @@ +A Values section in the Theme Editor sets the NULL, boolean and per-type value colors. diff --git a/apple/changelog.d/4205.added.md b/apple/changelog.d/4205.added.md new file mode 100644 index 0000000000..0def50c40b --- /dev/null +++ b/apple/changelog.d/4205.added.md @@ -0,0 +1 @@ +Color values by type in Settings > Data colors strings, numbers, dates, binary, JSON, UUID and enum values. diff --git a/apple/changelog.d/4205.changed.md b/apple/changelog.d/4205.changed.md new file mode 100644 index 0000000000..2dc37713bb --- /dev/null +++ b/apple/changelog.d/4205.changed.md @@ -0,0 +1 @@ +Built-in themes set a color for each value type, and their boolean colors now reach 4.5:1 contrast. diff --git a/apple/changelog.d/4205.fixed.md b/apple/changelog.d/4205.fixed.md new file mode 100644 index 0000000000..a1f611d878 --- /dev/null +++ b/apple/changelog.d/4205.fixed.md @@ -0,0 +1 @@ +MySQL and MariaDB BOOLEAN columns showed as numbers in query results, and BIT(8) columns as booleans.