diff --git a/apple/Packages/TableProCore/Sources/TableProTheme/ThemeFileFormat.swift b/apple/Packages/TableProCore/Sources/TableProTheme/ThemeFileFormat.swift index 1ac6a3fa26..c0938288d5 100644 --- a/apple/Packages/TableProCore/Sources/TableProTheme/ThemeFileFormat.swift +++ b/apple/Packages/TableProCore/Sources/TableProTheme/ThemeFileFormat.swift @@ -110,8 +110,7 @@ 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. + // The oldest format holding the theme's keys, so an older build still reads a theme that sets no newer key. private static func schema(of theme: ThemeDefinition) -> Int { theme.overrides.keys.reduce(ThemeSchema.oldestReadable) { max($0, $1.introducedInSchema) } } diff --git a/apple/Packages/TableProCore/Sources/TableProTheme/ThemeSlot.swift b/apple/Packages/TableProCore/Sources/TableProTheme/ThemeSlot.swift index a4e018e043..41d9ac2b39 100644 --- a/apple/Packages/TableProCore/Sources/TableProTheme/ThemeSlot.swift +++ b/apple/Packages/TableProCore/Sources/TableProTheme/ThemeSlot.swift @@ -217,7 +217,6 @@ public enum ThemeSlot: String, CaseIterable, Sendable { } } - // A file declaring an older format cannot hold the key. public var introducedInSchema: Int { switch self { case .windowBackdrop, diff --git a/apple/TablePro/Core/Events/AppEvents.swift b/apple/TablePro/Core/Events/AppEvents.swift index 9c44d56b40..084d9ef454 100644 --- a/apple/TablePro/Core/Events/AppEvents.swift +++ b/apple/TablePro/Core/Events/AppEvents.swift @@ -80,7 +80,6 @@ final class AppEvents { let browseContainerChanged = PassthroughSubject() - // The connections strip moves its highlight on this. let visibleContainerChanged = PassthroughSubject() let sessionTransactionsChanged = PassthroughSubject() diff --git a/apple/TablePro/Core/Services/Export/TableTransferService.swift b/apple/TablePro/Core/Services/Export/TableTransferService.swift index 98d3a27644..8806cac233 100644 --- a/apple/TablePro/Core/Services/Export/TableTransferService.swift +++ b/apple/TablePro/Core/Services/Export/TableTransferService.swift @@ -273,7 +273,7 @@ final class TableTransferService: ObservableObject { // PostgreSQL and DuckDB read a BIT as its digits and MySQL as its number, so 00000101 bound to a MySQL BIT stores 101. // Read before the row stream opens: a catalog read queued behind the source's own open stream waits for the whole // table, and behind DuckDB's bounded one never ends. The catalog, because a fresh PostgreSQL session's header names - // a bit column `unknown` and MySQL's drops the width. + // a bit column `unknown` and MySQL's drops the width of a BIT(1). static func bitColumnKinds( for object: ExportObjectItem, source: ExportDataSourceAdapter, diff --git a/apple/TablePro/Core/Services/Formatting/CellDisplayFormatter.swift b/apple/TablePro/Core/Services/Formatting/CellDisplayFormatter.swift index bd42bef09e..2fd4e12284 100644 --- a/apple/TablePro/Core/Services/Formatting/CellDisplayFormatter.swift +++ b/apple/TablePro/Core/Services/Formatting/CellDisplayFormatter.swift @@ -2,9 +2,6 @@ // CellDisplayFormatter.swift // TablePro // -// Pure formatter that transforms raw cell values into display-ready strings. -// Used by the data grid coordinator's display cache to compute values once per cell. -// import Foundation import TableProPluginKit @@ -19,7 +16,14 @@ enum CellDisplayFormatter { displayFormat: ValueDisplayFormat? = nil, databaseType: DatabaseType? = nil ) -> String? { - display(rawValue, columnType: columnType, displayFormat: displayFormat, databaseType: databaseType).text + resolve( + rawValue, + columnType: columnType, + displayFormat: displayFormat, + databaseType: databaseType, + dates: DateFormattingService.shared.renderer, + checksRendering: false + ).text } static func format( @@ -29,7 +33,14 @@ enum CellDisplayFormatter { databaseType: DatabaseType?, dates: DateDisplayRenderer ) -> String? { - display(rawValue, columnType: columnType, displayFormat: displayFormat, databaseType: databaseType, dates: dates).text + resolve( + rawValue, + columnType: columnType, + displayFormat: displayFormat, + databaseType: databaseType, + dates: dates, + checksRendering: false + ).text } @MainActor @@ -39,12 +50,13 @@ enum CellDisplayFormatter { displayFormat: ValueDisplayFormat?, databaseType: DatabaseType? ) -> (text: String?, isFormatted: Bool) { - display( + resolve( rawValue, columnType: columnType, displayFormat: displayFormat, databaseType: databaseType, - dates: DateFormattingService.shared.renderer + dates: DateFormattingService.shared.renderer, + checksRendering: true ) } @@ -55,6 +67,25 @@ enum CellDisplayFormatter { displayFormat: ValueDisplayFormat?, databaseType: DatabaseType?, dates: DateDisplayRenderer + ) -> (text: String?, isFormatted: Bool) { + resolve( + rawValue, + columnType: columnType, + displayFormat: displayFormat, + databaseType: databaseType, + dates: dates, + checksRendering: true + ) + } + + // Find, the value filter and the display order discard the flag, and the JSON and PHP checks read the whole value. + private static func resolve( + _ rawValue: PluginCellValue, + columnType: ColumnType?, + displayFormat: ValueDisplayFormat?, + databaseType: DatabaseType?, + dates: DateDisplayRenderer, + checksRendering: Bool ) -> (text: String?, isFormatted: Bool) { switch rawValue { case .null: @@ -71,7 +102,7 @@ enum CellDisplayFormatter { if let displayFormat, displayFormat != .raw, displayFormat.isApplicable(to: columnType, databaseType: databaseType), let formatted = ValueDisplayFormatter.apply(value, format: displayFormat, columnType: columnType, dates: dates) { - return (fitToCell(formatted), rendered(formatted, from: value, by: displayFormat)) + return (fitToCell(formatted), checksRendering && rendered(formatted, from: value, by: displayFormat)) } var displayValue = value if let columnType { diff --git a/apple/TablePro/Core/Services/Highlight/HighlightCondition.swift b/apple/TablePro/Core/Services/Highlight/HighlightCondition.swift index 094ad197df..82181fb47c 100644 --- a/apple/TablePro/Core/Services/Highlight/HighlightCondition.swift +++ b/apple/TablePro/Core/Services/Highlight/HighlightCondition.swift @@ -10,8 +10,8 @@ struct HighlightCondition { static let searchLimit = 10_000 private enum ValueKind { - case numeric - case boolean + // A boolean column can hold numbers: MySQL's TINYINT(1) stores any integer from -128 to 127. + case numberOrBoolean case text } @@ -130,10 +130,8 @@ struct HighlightCondition { private var prefersNumbers: Bool { switch valueKind { - case .numeric: + case .numberOrBoolean: return true - case .boolean: - return false case .text: return isOrderingOperator } @@ -141,7 +139,7 @@ struct HighlightCondition { private var prefersBooleans: Bool { switch valueKind { - case .numeric, .boolean: + case .numberOrBoolean: return true case .text: return false @@ -194,12 +192,7 @@ struct HighlightCondition { regularExpression(pattern, ignoresCase: false) != nil } - /// `searchLimit` caps the haystack in `searchable(_:)`, not the pattern. Capping the pattern - /// looked like a guard against a runaway expression and is not one: cost comes from a pattern's - /// shape rather than its length, and `(a+)+$` compiles in 0.01ms. Measured against this cap, - /// a valid 160,000-unit alternation compiles in 8.85ms, once per rule set rather than per row, - /// so the only thing the cap achieved was to refuse a pattern the user wrote and then report it - /// as invalid. Nil now means the expression will not compile, and nothing else. + // No length cap: cost follows a pattern's shape, and a valid 160,000-unit alternation compiled in 8.85ms. private static func regularExpression(_ pattern: String, ignoresCase: Bool) -> NSRegularExpression? { guard !pattern.isEmpty else { return nil } return try? NSRegularExpression(pattern: pattern, options: ignoresCase ? [.caseInsensitive] : []) @@ -207,10 +200,8 @@ struct HighlightCondition { private static func valueKind(for columnType: ColumnType?) -> ValueKind { switch columnType { - case .integer, .decimal: - return .numeric - case .boolean: - return .boolean + case .integer, .decimal, .boolean: + return .numberOrBoolean case .text, .date, .timestamp, .datetime, .blob, .json, .enumType, .set, .spatial, .vector, .array, .none: return .text } diff --git a/apple/TablePro/Core/Services/Masking/MaskingPolicy.swift b/apple/TablePro/Core/Services/Masking/MaskingPolicy.swift index d14aeb5aa4..f0fac9b9eb 100644 --- a/apple/TablePro/Core/Services/Masking/MaskingPolicy.swift +++ b/apple/TablePro/Core/Services/Masking/MaskingPolicy.swift @@ -24,7 +24,10 @@ struct MaskingPolicy: Equatable, Sendable { for rule in rules where Self.matches(rule, table: table, column: column, typeName: typeName) { return rule.style } - return nil + // Only after no rule matched as written, so a later column rule keeps beating a type rule that names no width. + guard let typeName else { return nil } + let base = Self.baseTypeName(typeName) + return rules.first { $0.target == .type && MaskingGlob.matches($0.trimmedPattern, base) }?.style } func columnMasks(table: String?, columns: [String], typeNames: [String?]) -> ColumnMasks { @@ -111,6 +114,12 @@ struct MaskingPolicy: Equatable, Sendable { return MaskingGlob.matches(tablePattern, String(table[table.index(after: bareStart)...])) } } + + // A result names `TINYINT(1)`, `BIT(8)` or `Nullable(String)` where the rule says `tinyint`, `bit` or `string`. + private static func baseTypeName(_ typeName: String) -> String { + let unwrapped = ColumnType.unwrappedTypeName(typeName) ?? typeName + return unwrapped.prefix { $0 != "(" }.trimmingCharacters(in: .whitespaces) + } } extension MaskingStyle { diff --git a/apple/TablePro/Core/Utilities/SQL/RowSortComparator.swift b/apple/TablePro/Core/Utilities/SQL/RowSortComparator.swift index f6282825df..4a49e1ae78 100644 --- a/apple/TablePro/Core/Utilities/SQL/RowSortComparator.swift +++ b/apple/TablePro/Core/Utilities/SQL/RowSortComparator.swift @@ -2,24 +2,25 @@ // RowSortComparator.swift // TablePro // -// Type-aware row value comparator for grid sorting. -// import Foundation -/// Type-aware row value comparator for grid sorting. -/// Uses String.compare with .numeric option and type-specific fast paths for integer/decimal columns. internal enum RowSortComparator { internal static func compare(_ lhs: String, _ rhs: String, columnType: ColumnType?) -> ComparisonResult { if let columnType { switch columnType { case .integer: if let l = Int64(lhs), let r = Int64(rhs) { - return l < r ? .orderedAscending : (l > r ? .orderedDescending : .orderedSame) + return order(l, r) } case .decimal: if let l = Double(lhs), let r = Double(rhs) { - return l < r ? .orderedAscending : (l > r ? .orderedDescending : .orderedSame) + return order(l, r) + } + case .boolean: + // MySQL's TINYINT(1) is a boolean that stores any integer from -128 to 127. + if let l = booleanNumber(lhs), let r = booleanNumber(rhs) { + return order(l, r) } default: break @@ -27,4 +28,14 @@ internal enum RowSortComparator { } return lhs.compare(rhs, options: [.numeric]) } + + // One number per value, so `1`, `2` and `true` order the same way whichever pair is compared. + private static func booleanNumber(_ text: String) -> Int64? { + if let number = Int64(text) { return number } + return StoredBoolean.value(of: text).map { $0 ? 1 : 0 } + } + + private static func order(_ lhs: Value, _ rhs: Value) -> ComparisonResult { + lhs < rhs ? .orderedAscending : (lhs > rhs ? .orderedDescending : .orderedSame) + } } diff --git a/apple/TablePro/Models/UI/DataGridConfiguration.swift b/apple/TablePro/Models/UI/DataGridConfiguration.swift index f718a776be..7e2cfd4dc4 100644 --- a/apple/TablePro/Models/UI/DataGridConfiguration.swift +++ b/apple/TablePro/Models/UI/DataGridConfiguration.swift @@ -24,7 +24,6 @@ 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. diff --git a/apple/TablePro/Resources/Themes/tablepro.catppuccin-latte.json b/apple/TablePro/Resources/Themes/tablepro.catppuccin-latte.json index 38c7421092..6ba703010b 100644 --- a/apple/TablePro/Resources/Themes/tablepro.catppuccin-latte.json +++ b/apple/TablePro/Resources/Themes/tablepro.catppuccin-latte.json @@ -4,29 +4,29 @@ "dataGrid": { "alternateRow": "#E6E9EF", "background": "#EFF1F5", - "binary": "#C81F9B", - "boolFalse": "#D20F39", - "boolTrue": "#327D22", - "dateTime": "#1962F5", + "binary": "#B4008A", + "boolFalse": "#C10031", + "boolTrue": "#226E0E", + "dateTime": "#0551E4", "deleted": "#D20F3926", "deletedText": "#D20F3980", - "enum": "#8839EF", + "enum": "#7E29E2", "focusBorder": "#8839EF", "gridLine": "#BCC0CC", "headerBackground": "#E6E9EF", "headerText": "#4C4F69", "inactiveSelection": "#CCD0DA", "inserted": "#40A02B26", - "json": "#996214", + "json": "#875400", "modified": "#DF8E1D26", "nullValue": "#7C7F93", - "number": "#BF4701", + "number": "#A73D00", "rowNumber": "#8C8FA1", "selection": "#CCD0DA", "selectionText": "#4C4F69", - "string": "#327D22", + "string": "#4653C5", "text": "#4C4F69", - "uuid": "#137A80" + "uuid": "#006A70" }, "editor": { "background": "#EFF1F5", diff --git a/apple/TablePro/Resources/Themes/tablepro.catppuccin-mocha.json b/apple/TablePro/Resources/Themes/tablepro.catppuccin-mocha.json index a7f718a134..929bf39cf4 100644 --- a/apple/TablePro/Resources/Themes/tablepro.catppuccin-mocha.json +++ b/apple/TablePro/Resources/Themes/tablepro.catppuccin-mocha.json @@ -24,7 +24,7 @@ "rowNumber": "#7F849C", "selection": "#313244", "selectionText": "#CDD6F4", - "string": "#A6E3A1", + "string": "#B4BEFE", "text": "#CDD6F4", "uuid": "#94E2D5" }, diff --git a/apple/TablePro/Resources/Themes/tablepro.dracula.json b/apple/TablePro/Resources/Themes/tablepro.dracula.json index 06f10ce5f1..06197213f5 100644 --- a/apple/TablePro/Resources/Themes/tablepro.dracula.json +++ b/apple/TablePro/Resources/Themes/tablepro.dracula.json @@ -4,13 +4,13 @@ "dataGrid": { "alternateRow": "#21222C", "background": "#282A36", - "binary": "#BD93F9", - "boolFalse": "#FF5555", + "binary": "#9DAFE5", + "boolFalse": "#FF928B", "boolTrue": "#50FA7B", "dateTime": "#8BE9FD", "deleted": "#FF555526", "deletedText": "#FF555580", - "enum": "#F1FA8C", + "enum": "#A4FFFF", "focusBorder": "#BD93F9", "gridLine": "#44475A", "headerBackground": "#21222C", @@ -18,15 +18,15 @@ "inactiveSelection": "#44475A80", "inserted": "#50FA7B26", "json": "#FFB86C", - "modified": "#FFB86C4D", + "modified": "#FFB86C26", "nullValue": "#6272A4", - "number": "#BD93F9", + "number": "#C59EFF", "rowNumber": "#6272A4", "selection": "#44475A", "selectionText": "#F8F8F2", "string": "#F1FA8C", "text": "#F8F8F2", - "uuid": "#FF79C6" + "uuid": "#FF89CB" }, "editor": { "background": "#282A36", diff --git a/apple/TablePro/Resources/Themes/tablepro.github-dark.json b/apple/TablePro/Resources/Themes/tablepro.github-dark.json index 38a83ac134..3e747ef26b 100644 --- a/apple/TablePro/Resources/Themes/tablepro.github-dark.json +++ b/apple/TablePro/Resources/Themes/tablepro.github-dark.json @@ -5,7 +5,7 @@ "alternateRow": "#161B22", "background": "#0D1117", "binary": "#FF9BCE", - "boolFalse": "#F85149", + "boolFalse": "#FF574F", "boolTrue": "#3FB950", "dateTime": "#D2A8FF", "deleted": "#F8514926", diff --git a/apple/TablePro/Resources/Themes/tablepro.github-light.json b/apple/TablePro/Resources/Themes/tablepro.github-light.json index f9184cde5d..424566e106 100644 --- a/apple/TablePro/Resources/Themes/tablepro.github-light.json +++ b/apple/TablePro/Resources/Themes/tablepro.github-light.json @@ -4,13 +4,13 @@ "dataGrid": { "alternateRow": "#F6F8FA", "background": "#FFFFFF", - "binary": "#BF3989", - "boolFalse": "#CF222E", - "boolTrue": "#1A7F37", - "dateTime": "#8250DF", + "binary": "#B93384", + "boolFalse": "#CE212E", + "boolTrue": "#127A33", + "dateTime": "#7D4BD9", "deleted": "#FF818226", "deletedText": "#CF222E80", - "enum": "#9A6700", + "enum": "#916000", "focusBorder": "#0969DA", "gridLine": "#D0D7DE", "headerBackground": "#F6F8FA", @@ -26,7 +26,7 @@ "selectionText": "#1F2328", "string": "#0A3069", "text": "#1F2328", - "uuid": "#0072F0" + "uuid": "#0066D8" }, "editor": { "background": "#FFFFFF", diff --git a/apple/TablePro/Resources/Themes/tablepro.gruvbox-dark.json b/apple/TablePro/Resources/Themes/tablepro.gruvbox-dark.json index 698e7abafd..1ddabd1674 100644 --- a/apple/TablePro/Resources/Themes/tablepro.gruvbox-dark.json +++ b/apple/TablePro/Resources/Themes/tablepro.gruvbox-dark.json @@ -4,13 +4,13 @@ "dataGrid": { "alternateRow": "#32302F", "background": "#282828", - "binary": "#D3869B", - "boolFalse": "#FB533F", + "binary": "#FFB7CA", + "boolFalse": "#FF9381", "boolTrue": "#B8BB26", - "dateTime": "#83A598", + "dateTime": "#96B8AB", "deleted": "#FB493426", "deletedText": "#FB493480", - "enum": "#FE8019", + "enum": "#FF9651", "focusBorder": "#689D6A", "gridLine": "#504945", "headerBackground": "#32302F", @@ -20,11 +20,11 @@ "json": "#FABD2F", "modified": "#D7992126", "nullValue": "#928374", - "number": "#D3869B", + "number": "#E99BB0", "rowNumber": "#665C54", "selection": "#384539", "selectionText": "#EBDBB2", - "string": "#B8BB26", + "string": "#A7DA95", "text": "#EBDBB2", "uuid": "#8EC07C" }, diff --git a/apple/TablePro/Resources/Themes/tablepro.gruvbox-light.json b/apple/TablePro/Resources/Themes/tablepro.gruvbox-light.json index 19e060142f..6d220a2e33 100644 --- a/apple/TablePro/Resources/Themes/tablepro.gruvbox-light.json +++ b/apple/TablePro/Resources/Themes/tablepro.gruvbox-light.json @@ -4,29 +4,29 @@ "dataGrid": { "alternateRow": "#F2E5BC", "background": "#FBF1C7", - "binary": "#8F3F71", + "binary": "#76285A", "boolFalse": "#9D0006", - "boolTrue": "#75700E", - "dateTime": "#076678", + "boolTrue": "#625E00", + "dateTime": "#036476", "deleted": "#9D000626", "deletedText": "#9D000680", - "enum": "#B64F0C", + "enum": "#9A4000", "focusBorder": "#427B58", "gridLine": "#D5C4A1", "headerBackground": "#F2E5BC", "headerText": "#3C3836", "inactiveSelection": "#EBDBB2", "inserted": "#79740E26", - "json": "#976311", + "json": "#805100", "modified": "#D7992126", "nullValue": "#928374", "number": "#8F3F71", "rowNumber": "#BDAE93", "selection": "#D6DCB0", "selectionText": "#3C3836", - "string": "#75700E", + "string": "#135031", "text": "#3C3836", - "uuid": "#417957" + "uuid": "#2E6646" }, "editor": { "background": "#FBF1C7", diff --git a/apple/TablePro/Resources/Themes/tablepro.nord.json b/apple/TablePro/Resources/Themes/tablepro.nord.json index 9142a2e453..d9d89063d9 100644 --- a/apple/TablePro/Resources/Themes/tablepro.nord.json +++ b/apple/TablePro/Resources/Themes/tablepro.nord.json @@ -4,13 +4,13 @@ "dataGrid": { "alternateRow": "#3B4252", "background": "#2E3440", - "binary": "#B590AF", - "boolFalse": "#CF888F", - "boolTrue": "#A3BE8C", - "dateTime": "#81A1C1", + "binary": "#FFDBF8", + "boolFalse": "#FFBAC0", + "boolTrue": "#B8D4A1", + "dateTime": "#ADCFF0", "deleted": "#BF616A26", "deletedText": "#BF616A80", - "enum": "#D18A73", + "enum": "#FFBCA6", "focusBorder": "#88C0D0", "gridLine": "#434C5E", "headerBackground": "#3B4252", @@ -18,15 +18,15 @@ "inactiveSelection": "#434C5E80", "inserted": "#A3BE8C26", "json": "#EBCB8B", - "modified": "#EBCB8B4D", + "modified": "#EBCB8B26", "nullValue": "#4C566A", - "number": "#B590AF", + "number": "#E7C0E0", "rowNumber": "#4C566A", "selection": "#434C5E", "selectionText": "#ECEFF4", - "string": "#A3BE8C", + "string": "#9CD4E4", "text": "#D8DEE9", - "uuid": "#8FBCBB" + "uuid": "#A6D4D3" }, "editor": { "background": "#2E3440", diff --git a/apple/TablePro/Resources/Themes/tablepro.one-dark.json b/apple/TablePro/Resources/Themes/tablepro.one-dark.json index b5756fca83..82eb6a8567 100644 --- a/apple/TablePro/Resources/Themes/tablepro.one-dark.json +++ b/apple/TablePro/Resources/Themes/tablepro.one-dark.json @@ -4,13 +4,13 @@ "dataGrid": { "alternateRow": "#21252B", "background": "#282C34", - "binary": "#D19A66", - "boolFalse": "#E17079", + "binary": "#EEB5FF", + "boolFalse": "#FF8D94", "boolTrue": "#98C379", - "dateTime": "#61AFEF", + "dateTime": "#67B5F5", "deleted": "#E0525226", "deletedText": "#E06C7580", - "enum": "#C678DD", + "enum": "#DF90F6", "focusBorder": "#528BFF", "gridLine": "#3E4452", "headerBackground": "#21252B", @@ -20,13 +20,13 @@ "json": "#E5C07B", "modified": "#E0C28526", "nullValue": "#7F848E", - "number": "#D19A66", + "number": "#DBA36F", "rowNumber": "#636D83", "selection": "#3E4451", "selectionText": "#D7DAE0", - "string": "#98C379", + "string": "#77D6E2", "text": "#ABB2BF", - "uuid": "#56B6C2" + "uuid": "#5CBCC8" }, "editor": { "background": "#282C34", diff --git a/apple/TablePro/Resources/Themes/tablepro.one-light.json b/apple/TablePro/Resources/Themes/tablepro.one-light.json index 8a2e370763..431c9075f8 100644 --- a/apple/TablePro/Resources/Themes/tablepro.one-light.json +++ b/apple/TablePro/Resources/Themes/tablepro.one-light.json @@ -4,29 +4,29 @@ "dataGrid": { "alternateRow": "#EAEAEB", "background": "#FAFAFA", - "binary": "#986801", - "boolFalse": "#DB3021", - "boolTrue": "#40813F", - "dateTime": "#2D6AF1", + "binary": "#674500", + "boolFalse": "#C40C01", + "boolTrue": "#2D6E2D", + "dateTime": "#1B56DC", "deleted": "#FF141426", "deletedText": "#E4564980", - "enum": "#A626A4", + "enum": "#A525A3", "focusBorder": "#526FFF", "gridLine": "#DBDBDC", "headerBackground": "#EAEAEB", "headerText": "#383A42", "inactiveSelection": "#DBDBDC", "inserted": "#2DB44826", - "json": "#CA1243", + "json": "#C3003E", "modified": "#F2A60D26", "nullValue": "#A0A1A7", - "number": "#986801", + "number": "#845900", "rowNumber": "#9D9D9F", "selection": "#DBDBDC", "selectionText": "#232324", - "string": "#40813F", + "string": "#005074", "text": "#383A42", - "uuid": "#017BB0" + "uuid": "#006895" }, "editor": { "background": "#FAFAFA", diff --git a/apple/TablePro/Resources/Themes/tablepro.solarized-dark.json b/apple/TablePro/Resources/Themes/tablepro.solarized-dark.json index 2243227031..8d93a15b93 100644 --- a/apple/TablePro/Resources/Themes/tablepro.solarized-dark.json +++ b/apple/TablePro/Resources/Themes/tablepro.solarized-dark.json @@ -4,29 +4,29 @@ "dataGrid": { "alternateRow": "#00212B", "background": "#002B36", - "binary": "#DD629D", - "boolFalse": "#E56462", - "boolTrue": "#859900", - "dateTime": "#2F93D9", + "binary": "#FF96C5", + "boolFalse": "#F77471", + "boolTrue": "#91A61F", + "dateTime": "#42A3EB", "deleted": "#DC322F26", "deletedText": "#DC322F80", - "enum": "#8387CD", + "enum": "#9297DE", "focusBorder": "#2AA198", "gridLine": "#23464F", "headerBackground": "#00212B", "headerText": "#93A1A1", "inactiveSelection": "#003846", "inserted": "#85990026", - "json": "#B58900", + "json": "#C29620", "modified": "#B5890026", "nullValue": "#586E75", - "number": "#DD629D", + "number": "#EF72AD", "rowNumber": "#586E75", "selection": "#005A6F", "selectionText": "#EEE8D5", - "string": "#2AA198", + "string": "#3AADA3", "text": "#839496", - "uuid": "#E8652E" + "uuid": "#FA7540" }, "editor": { "background": "#002B36", diff --git a/apple/TablePro/Resources/Themes/tablepro.solarized-light.json b/apple/TablePro/Resources/Themes/tablepro.solarized-light.json index c09a5f5fe2..4fd142b231 100644 --- a/apple/TablePro/Resources/Themes/tablepro.solarized-light.json +++ b/apple/TablePro/Resources/Themes/tablepro.solarized-light.json @@ -4,29 +4,29 @@ "dataGrid": { "alternateRow": "#EEE8D5", "background": "#FDF6E3", - "binary": "#CF2D7C", - "boolFalse": "#DA2925", - "boolTrue": "#697800", - "dateTime": "#2076B3", + "binary": "#940053", + "boolFalse": "#C2000D", + "boolTrue": "#586500", + "dateTime": "#00639E", "deleted": "#DC322F26", "deletedText": "#DC322F80", - "enum": "#6369C0", + "enum": "#5257AC", "focusBorder": "#268BD2", "gridLine": "#DDD6C1", "headerBackground": "#EEE8D5", "headerText": "#586E75", "inactiveSelection": "#EEE0B5", "inserted": "#85990026", - "json": "#8F6C00", + "json": "#795B00", "modified": "#B5890026", "nullValue": "#93A1A1", - "number": "#CF2D7C", + "number": "#B90B6A", "rowNumber": "#93A1A1", "selection": "#DFCA88", "selectionText": "#073642", - "string": "#217E77", + "string": "#006B65", "text": "#657B83", - "uuid": "#C44815" + "uuid": "#AC3800" }, "editor": { "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 c531cab1f0..f5795fcf6d 100644 --- a/apple/TablePro/Resources/Themes/tablepro.tokyo-night-light.json +++ b/apple/TablePro/Resources/Themes/tablepro.tokyo-night-light.json @@ -5,9 +5,9 @@ "alternateRow": "#D6D8DF", "background": "#E6E7ED", "binary": "#0F4B6E", - "boolFalse": "#8C4351", - "boolTrue": "#385F0D", - "dateTime": "#2959AA", + "boolFalse": "#843C4A", + "boolTrue": "#375D0A", + "dateTime": "#2150A1", "deleted": "#E8686826", "deletedText": "#8C435180", "enum": "#65359D", @@ -17,16 +17,16 @@ "headerText": "#343B59", "inactiveSelection": "#C9CBD6", "inserted": "#2D9C9126", - "json": "#8F5E15", + "json": "#754A00", "modified": "#8F5E1526", "nullValue": "#888B94", - "number": "#965027", + "number": "#844015", "rowNumber": "#9DA0AB", "selection": "#ACB0BF", "selectionText": "#343B59", - "string": "#385F0D", + "string": "#5A4A78", "text": "#343B59", - "uuid": "#33635C" + "uuid": "#2B5B55" }, "editor": { "background": "#E6E7ED", diff --git a/apple/TablePro/Resources/Themes/tablepro.tokyo-night.json b/apple/TablePro/Resources/Themes/tablepro.tokyo-night.json index 0c9b22d8b7..84e809759f 100644 --- a/apple/TablePro/Resources/Themes/tablepro.tokyo-night.json +++ b/apple/TablePro/Resources/Themes/tablepro.tokyo-night.json @@ -24,7 +24,7 @@ "rowNumber": "#363B54", "selection": "#2B2F41", "selectionText": "#A9B1D6", - "string": "#9ECE6A", + "string": "#B4F9F8", "text": "#A9B1D6", "uuid": "#73DACA" }, diff --git a/apple/TablePro/Views/DataGrid/Cells/DataGridCellPalette.swift b/apple/TablePro/Views/DataGrid/Cells/DataGridCellPalette.swift index d64aa3b956..a70264b8c3 100644 --- a/apple/TablePro/Views/DataGrid/Cells/DataGridCellPalette.swift +++ b/apple/TablePro/Views/DataGrid/Cells/DataGridCellPalette.swift @@ -34,7 +34,7 @@ struct DataGridCellPalette: Equatable { placeholderText: NSColor, booleanTrueText: NSColor, booleanFalseText: NSColor, - valueColors: DataGridValueColors? = nil, + valueColors: DataGridValueColors, rowNumberText: NSColor, deletedRowText: NSColor, modifiedColumnTint: NSColor, @@ -51,7 +51,7 @@ struct DataGridCellPalette: Equatable { self.placeholderText = placeholderText self.booleanTrueText = booleanTrueText self.booleanFalseText = booleanFalseText - self.valueColors = valueColors ?? DataGridValueColors(all: text) + self.valueColors = valueColors self.rowNumberText = rowNumberText self.deletedRowText = deletedRowText self.modifiedColumnTint = modifiedColumnTint diff --git a/apple/TablePro/Views/DataGrid/Cells/DataGridCellRegistry.swift b/apple/TablePro/Views/DataGrid/Cells/DataGridCellRegistry.swift index 0be7b02add..fa1d9bf01a 100644 --- a/apple/TablePro/Views/DataGrid/Cells/DataGridCellRegistry.swift +++ b/apple/TablePro/Views/DataGrid/Cells/DataGridCellRegistry.swift @@ -20,7 +20,7 @@ final class DataGridCellRegistry { let appearance: NSAppearance.Name } - init(scope: ThemeScope = .live, valueColors: ValueColorSettings = AppSettingsManager.shared.valueColors) { + init(scope: ThemeScope, valueColors: ValueColorSettings) { self.scope = scope let settings = AppSettingsManager.shared.dataGrid nullDisplayString = settings.nullDisplay @@ -31,12 +31,12 @@ final class DataGridCellRegistry { palette = scope.dataGridCellPalette(in: appearance) } - func reloadSettings(_ settings: DataGridSettings = AppSettingsManager.shared.dataGrid) { + func reloadSettings(_ settings: DataGridSettings) { nullDisplayString = settings.nullDisplay showsInvisibleCharacters = settings.showInvisibleCharacters } - func reloadValueColors(_ settings: ValueColorSettings = AppSettingsManager.shared.valueColors) { + func reloadValueColors(_ settings: ValueColorSettings) { colorsValuesByType = settings.colorsValuesByType } diff --git a/apple/TablePro/Views/DataGrid/Cells/DataGridValueTone.swift b/apple/TablePro/Views/DataGrid/Cells/DataGridValueTone.swift index e9c60d5645..f8a46606f8 100644 --- a/apple/TablePro/Views/DataGrid/Cells/DataGridValueTone.swift +++ b/apple/TablePro/Views/DataGrid/Cells/DataGridValueTone.swift @@ -33,14 +33,22 @@ enum DataGridValueTone: Equatable, Sendable { 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 } + ColumnTypeSQLQuoting.isCharacterType(.text(rawType: name)) || isCharacterTypeName(name) else { return .plain } return .string case .boolean, .spatial, .vector, .array: return .plain } } - // nil where the cell shows the column's own value. + // Character data `isCharacterType` leaves out: it also decides which columns a filter casts before LIKE. + private static let characterTypeNames: Set = [ + "KEYWORD", "WILDCARD", "CONSTANT_KEYWORD", "ASCII", "LONG", "SYSNAME", + ] + + private static func isCharacterTypeName(_ name: String) -> Bool { + characterTypeNames.contains(name.prefix { $0 != "(" }.trimmingCharacters(in: .whitespaces).uppercased()) + } + static func shown(by format: ValueDisplayFormat) -> DataGridValueTone? { switch format { case .raw: diff --git a/apple/TablePro/Views/Settings/Appearance/ThemeEditorPreview.swift b/apple/TablePro/Views/Settings/Appearance/ThemeEditorPreview.swift index 7281a9b541..a0053264c7 100644 --- a/apple/TablePro/Views/Settings/Appearance/ThemeEditorPreview.swift +++ b/apple/TablePro/Views/Settings/Appearance/ThemeEditorPreview.swift @@ -162,10 +162,7 @@ private final class ThemePreviewStatementBand: TextViewCoordinator { // MARK: - Grid -// 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. +// The preview takes no scroll events, so fixed widths keep every column in the editor window; only the UUID overflows. internal enum ThemePreviewGridSample { internal static let selectedRow = 1 internal static let modifiedRow = 2 diff --git a/apple/TablePro/Views/Settings/Appearance/ThemeSlotPresentation.swift b/apple/TablePro/Views/Settings/Appearance/ThemeSlotPresentation.swift index 7763bab49e..a40a4d5fa6 100644 --- a/apple/TablePro/Views/Settings/Appearance/ThemeSlotPresentation.swift +++ b/apple/TablePro/Views/Settings/Appearance/ThemeSlotPresentation.swift @@ -83,34 +83,46 @@ internal enum ThemeContrastNote { internal static let minimumRatio = 4.5 // 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( + private static func checks( for section: ThemeSlotSection, in theme: ThemeDefinition - ) -> (texts: [ThemeSlot], background: ThemeSlot)? { + ) -> (texts: [ThemeSlot], surfaces: [ThemeColor])? { switch section { case .editor: - return ([.editorText], .editorBackground) + return ([.editorText], [color(.editorBackground, in: theme).opaque]) case .dataGrid: - return ([.gridText], .gridBackground) + return ([.gridText], [color(.gridBackground, in: theme).opaque]) case .values: let explicit = ThemeSlotSection.values.slots.filter { $0 != .gridNullValue && theme.hasExplicitColor($0) } - return (explicit, .gridBackground) + return (explicit, rowSurfaces(in: theme)) case .window, .syntax, .selection, .changes: return nil } } + // Alternate rows are on by default, and a modified cell or an inserted row washes either row. + private static func rowSurfaces(in theme: ThemeDefinition) -> [ThemeColor] { + let background = color(.gridBackground, in: theme).opaque + let rows = [background, color(.gridAlternateRow, in: theme).composited(over: background)] + let washes = [color(.gridModified, in: theme), color(.gridInserted, in: theme)] + return rows + washes.flatMap { wash in rows.map { wash.composited(over: $0) } } + } + 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)) + let text = color(slot, in: theme) + let ratios = checks.surfaces.map { text.composited(over: $0).contrastRatio(with: $0) } + return (slot: slot, ratio: ratios.min() ?? .infinity) } .min { $0.ratio < $1.ratio } } + private static func color(_ slot: ThemeSlot, in theme: ThemeDefinition) -> ThemeColor { + ThemeSlotPresentation.displayColor(for: slot, in: theme) + } + internal static func message(for section: ThemeSlotSection, in theme: ThemeDefinition) -> String? { guard let lowest = lowest(for: section, in: theme), lowest.ratio < minimumRatio else { return nil } let formatted = lowest.ratio.formatted(.number.precision(.fractionLength(1))) diff --git a/apple/TablePro/Views/TableListing/TableListingTabView.swift b/apple/TablePro/Views/TableListing/TableListingTabView.swift index 44fd926e3b..a5b15db1b4 100644 --- a/apple/TablePro/Views/TableListing/TableListingTabView.swift +++ b/apple/TablePro/Views/TableListing/TableListingTabView.swift @@ -75,10 +75,10 @@ internal struct TableListingTabView: View { private var rowsGrid: some View { DataGridView(model: model.grid.model) - .onAppear { model.grid.model.configuration = configuration } + .onAppear { model.grid.model.configuration = Self.gridConfiguration(databaseType: databaseType) } } - private var configuration: DataGridConfiguration { + static func gridConfiguration(databaseType: DatabaseType) -> DataGridConfiguration { var configuration = DataGridConfiguration() configuration.traits = DataGridEngineTraits(databaseType: databaseType) configuration.showRowNumbers = true diff --git a/apple/TableProTests/Core/Compare/CompareSQLLiteralTests.swift b/apple/TableProTests/Core/Compare/CompareSQLLiteralTests.swift index 04baec1f63..d662447ad7 100644 --- a/apple/TableProTests/Core/Compare/CompareSQLLiteralTests.swift +++ b/apple/TableProTests/Core/Compare/CompareSQLLiteralTests.swift @@ -48,23 +48,34 @@ struct CompareSQLLiteralTests { @Test func mysqlBitIsWrittenAsItsNumber() { let driver = NoBackslashEscapesDriver() - let bit = ColumnType.boolean(rawType: "bit(8)") + let bits = ColumnTypeClassifier(family: .mysql).classify(rawTypeName: "BIT(8)") + #expect(bits == .integer(rawType: "BIT(8)")) for type in [DatabaseType.mysql, .mariadb] { - #expect(CompareSQLLiteral.textLiteral("5", columnType: bit, databaseType: type, driver: driver) == "5") + #expect(CompareSQLLiteral.textLiteral("5", columnType: bits, databaseType: type, driver: driver) == "5") #expect(CompareSQLLiteral.literal( - for: .text("170"), columnType: bit, databaseType: type, driver: driver, keptInScript: false + for: .text("170"), columnType: bits, databaseType: type, driver: driver, keptInScript: false ) == "170") } - #expect(CompareSQLLiteral.textLiteral("abc", columnType: bit, databaseType: .mysql, driver: driver) == "'abc'") - #expect(CompareSQLLiteral.textLiteral("+5", columnType: bit, databaseType: .mysql, driver: driver) == "'+5'") + #expect(CompareSQLLiteral.textLiteral("abc", columnType: bits, databaseType: .mysql, driver: driver) == "'abc'") + #expect(CompareSQLLiteral.textLiteral("+5", columnType: bits, databaseType: .mysql, driver: driver) == "'+5'") + #expect(CompareSQLLiteral.textLiteral("-1", columnType: bits, databaseType: .mysql, driver: driver) == "-1") + } + + @Test + func mysqlOneBitFlagIsWrittenAsItsNumber() { + let driver = NoBackslashEscapesDriver() + let flag = ColumnTypeClassifier(family: .mysql).classify(rawTypeName: "BIT") + #expect(flag.isBooleanType) + #expect(CompareSQLLiteral.textLiteral("1", columnType: flag, databaseType: .mysql, driver: driver) == "1") + #expect(CompareSQLLiteral.textLiteral("-1", columnType: flag, databaseType: .mysql, driver: driver) == "'-1'") } @Test func otherBooleansAndBitStringsStayQuoted() { let driver = NoBackslashEscapesDriver() - let tinyint = ColumnType.boolean(rawType: "tinyint(1)") + let tinyint = ColumnTypeClassifier(family: .mysql).classify(rawTypeName: "TINYINT(1)") #expect(CompareSQLLiteral.textLiteral("1", columnType: tinyint, databaseType: .mysql, driver: driver) == "'1'") - let bit = ColumnType.boolean(rawType: "bit(8)") + let bit = ColumnTypeClassifier(family: .postgres).classify(rawTypeName: "bit(8)") #expect(CompareSQLLiteral.textLiteral("5", columnType: bit, databaseType: .postgresql, driver: driver) != "5") } diff --git a/apple/TableProTests/Core/Export/TableTransferServiceTests.swift b/apple/TableProTests/Core/Export/TableTransferServiceTests.swift index 61c2ddb5b6..c268bb1847 100644 --- a/apple/TableProTests/Core/Export/TableTransferServiceTests.swift +++ b/apple/TableProTests/Core/Export/TableTransferServiceTests.swift @@ -346,7 +346,6 @@ struct TableTransferServiceTests { #expect(destination.executedParameters.last == [.text("5"), .text("1")]) } - // 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( @@ -354,7 +353,7 @@ struct TableTransferServiceTests { rows: [[.text("1"), .text("5")]], columns: ["id", "flags"], types: ["id": "int", "flags": "bit(8)"], - headerTypes: ["id": "INT", "flags": "BIT"] + headerTypes: ["id": "INT", "flags": "BIT(8)"] ) let destination = TransferStubDriver(columns: ["id", "flags"], types: ["id": "integer", "flags": "varbit"]) let request = TableTransferService.Request( diff --git a/apple/TableProTests/Core/Operations/AppSettingsCategoryParityTests.swift b/apple/TableProTests/Core/Operations/AppSettingsCategoryParityTests.swift index 9c1b6f2d21..4c191de08a 100644 --- a/apple/TableProTests/Core/Operations/AppSettingsCategoryParityTests.swift +++ b/apple/TableProTests/Core/Operations/AppSettingsCategoryParityTests.swift @@ -24,10 +24,17 @@ struct AppSettingsCategoryParityTests { #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() { + @MainActor + @Test("Value colors sync as their own category, and a rewrite of the Data Grid blob keeps them") + func valueColorsSyncAsTheirOwnCategory() throws { #expect(AppSettingsCategory.synced.contains(AppSettingsCategory.valueColors)) - #expect(AppSettingsCategory.valueColors != AppSettingsCategory.dataGrid) + let pool = EphemeralDefaultsPool() + let storage = AppSettingsStorage(userDefaults: try pool.make().defaults) + storage.saveValueColors(ValueColorSettings(colorsValuesByType: true)) + + storage.saveDataGrid(.default) + + #expect(storage.loadValueColors().colorsValuesByType) } @MainActor diff --git a/apple/TableProTests/Core/Services/Highlight/HighlightConditionTests.swift b/apple/TableProTests/Core/Services/Highlight/HighlightConditionTests.swift index c5ed69ea8e..bf058d176a 100644 --- a/apple/TableProTests/Core/Services/Highlight/HighlightConditionTests.swift +++ b/apple/TableProTests/Core/Services/Highlight/HighlightConditionTests.swift @@ -91,6 +91,20 @@ struct HighlightConditionTests { #expect(matches("1", .equal, "true", type: .integer(rawType: "TINYINT(1)"))) } + @Test("A boolean column holding integers compares them as numbers") + func booleanColumnHoldingIntegers() { + let tinyint = ColumnTypeClassifier(family: .mysql).classify(rawTypeName: "TINYINT(1)") + #expect(tinyint.isBooleanType) + #expect(matches("10", .greaterThan, "9", type: tinyint)) + #expect(!matches("9", .greaterThan, "10", type: tinyint)) + #expect(matches("-2", .lessThan, "-1", type: tinyint)) + #expect(matches("5", .between, "2", second: "10", type: tinyint)) + #expect(matches("1", .equal, "1.0", type: tinyint)) + #expect(matches("1", .equal, "true", type: tinyint)) + #expect(!matches("5", .equal, "true", type: tinyint)) + #expect(matches("t", .greaterThan, "0", type: .boolean(rawType: "BOOLEAN"))) + } + @Test("Pattern operators ignore case by default and honour Match Case") func patterns() { #expect(matches("Hello World", .contains, "world")) diff --git a/apple/TableProTests/Core/Services/Masking/MaskingPolicyTests.swift b/apple/TableProTests/Core/Services/Masking/MaskingPolicyTests.swift index 482b9a163b..830f8ec5be 100644 --- a/apple/TableProTests/Core/Services/Masking/MaskingPolicyTests.swift +++ b/apple/TableProTests/Core/Services/Masking/MaskingPolicyTests.swift @@ -43,6 +43,57 @@ struct MaskingPolicyTests { #expect(policy.style(table: "hosts", column: "address", typeName: nil) == nil) } + // MySQL results name `TINYINT(1)` and `BIT(8)`, and a `tinyint` rule showed those columns in the clear. + @Test("A type rule matches the type a result names with its parameters or a wrapper, ignoring case") + func typeRuleMatchesTheBaseTypeName() { + let policy = MaskingPolicy(rules: [ + MaskingRule(pattern: "tinyint", target: .type), + MaskingRule(pattern: "bit", target: .type, style: .null), + MaskingRule(pattern: "String", target: .type, style: .hash), + MaskingRule(pattern: "var*", target: .type, style: .lastFour), + ]) + #expect(policy.style(table: "users", column: "active", typeName: "TINYINT(1)") == .full) + #expect(policy.style(table: "users", column: "age", typeName: "tinyint(4) unsigned") == .full) + #expect(policy.style(table: "users", column: "flags", typeName: "BIT(8)") == .null) + #expect(policy.style(table: "users", column: "flag", typeName: "BIT") == .null) + #expect(policy.style(table: "events", column: "token", typeName: "LowCardinality(Nullable(String))") == .hash) + #expect(policy.style(table: "users", column: "name", typeName: "VARCHAR(255)") == .lastFour) + #expect(policy.style(table: "users", column: "id", typeName: "INT") == nil) + #expect(policy.style(table: "users", column: "bits", typeName: "BITSTRING(8)") == nil) + } + + @Test("A type rule that names parameters still matches only those parameters") + func parameterizedTypeRuleStaysExact() { + let policy = MaskingPolicy(rules: [MaskingRule(pattern: "tinyint(1)", target: .type)]) + #expect(policy.style(table: "users", column: "active", typeName: "TINYINT(1)") == .full) + #expect(policy.style(table: "users", column: "age", typeName: "TINYINT(4)") == nil) + #expect(policy.style(table: "users", column: "age", typeName: "TINYINT") == nil) + } + + @Test("A column rule after a type rule still wins when the type rule matches only the base name") + func exactMatchesBeatBaseNameMatches() { + let policy = MaskingPolicy(rules: [ + MaskingRule(pattern: "varchar", target: .type, style: .lastFour), + MaskingRule(pattern: "ssn", target: .column), + ]) + #expect(policy.style(table: "people", column: "ssn", typeName: "VARCHAR(11)") == .full) + #expect(policy.style(table: "people", column: "name", typeName: "VARCHAR(11)") == .lastFour) + #expect(policy.style(table: "people", column: "ssn", typeName: "varchar") == .lastFour) + } + + @Test("Column masks match a type rule against the classified type's raw name") + func columnMasksMatchTypeRulesOnParameterizedTypes() { + let policy = MaskingPolicy(rules: [MaskingRule(pattern: "tinyint", target: .type)]) + let classifier = ColumnTypeClassifier(family: .mysql) + let masks = policy.columnMasks( + table: "users", + columns: ["id", "active"], + columnTypes: [classifier.classify(rawTypeName: "INT"), classifier.classify(rawTypeName: "TINYINT(1)")] + ) + #expect(masks.maskedColumns == [1]) + #expect(masks.row([.text("7"), .text("1")]) == [.text("7"), .text("••••")]) + } + @Test("The first matching rule wins, and an empty pattern is ignored") func firstRuleWins() { let policy = MaskingPolicy(rules: [ diff --git a/apple/TableProTests/Core/Storage/AppSettingsValueColorsTests.swift b/apple/TableProTests/Core/Storage/AppSettingsValueColorsTests.swift new file mode 100644 index 0000000000..314bccfa82 --- /dev/null +++ b/apple/TableProTests/Core/Storage/AppSettingsValueColorsTests.swift @@ -0,0 +1,170 @@ +// +// AppSettingsValueColorsTests.swift +// TableProTests +// + +import CloudKit +import Combine +import Foundation +@testable import TablePro +import TableProSyncTransport +import Testing + +@MainActor +struct AppSettingsValueColorsTests { + @MainActor + private final class Harness { + let environment: SyncTestEnvironment + let storage: AppSettingsStorage + let settings: AppSettingsManager + let coordinator: SyncCoordinator + private(set) var sentEvents = 0 + private var subscription: AnyCancellable? + + var tracker: SyncChangeTracker { environment.tracker } + + init(label: String, saved: ValueColorSettings? = nil) throws { + let environment = try SyncTestEnvironment(label: label) + let storage = AppSettingsStorage(userDefaults: environment.defaults) + if let saved { storage.saveValueColors(saved) } + let events = AppEvents() + let catalog = ThemeCatalog( + userDirectory: environment.directory.appendingPathComponent("Themes", isDirectory: true), + bundleDirectory: nil, + notificationCenter: NotificationCenter(), + fileTrash: .folder(environment.directory.appendingPathComponent("Trash", isDirectory: true)), + syncTracker: environment.tracker + ) + let settings = AppSettingsManager( + storage: storage, + themeEngine: ThemeEngine(catalog: catalog), + syncTracker: environment.tracker, + appEvents: events + ) + let transport = ScriptedSyncTransport(zoneID: SyncTestEnvironment.zoneID) + let columnLayouts = environment.columnLayouts + let base = environment.makeCoordinator(transport: transport) + self.environment = environment + self.storage = storage + self.settings = settings + coordinator = SyncCoordinator( + services: base.services.withAppSettings(settings, storage: storage), + recordCache: environment.recordCache, + transport: transport, + makeNetworkMonitor: { nil }, + columnLayouts: columnLayouts, + themeCatalog: catalog, + licenseGate: { nil } + ) + subscription = events.valueColorSettingsChanged.sink { [weak self] in self?.sentEvents += 1 } + } + + func pull(_ category: String, _ data: Data) async -> Bool { + let record = SyncRecordMapper.toCKRecord(category: category, settingsData: data, in: SyncTestEnvironment.zoneID) + return await coordinator.applyPullResult(PullResult(changedRecords: [record], deletedRecordIDs: [], newToken: nil)) + } + } + + // MARK: - Manager + + @Test("Turning Color values by type on saves it, tells the open grids and marks its record for sync") + func changeSavesSendsAndMarks() throws { + let harness = try Harness(label: "value-colors-change") + + harness.settings.valueColors = ValueColorSettings(colorsValuesByType: true) + + #expect(harness.storage.loadValueColors().colorsValuesByType) + #expect(harness.sentEvents == 1) + #expect(harness.tracker.dirtyRecords(for: .settings) == [AppSettingsCategory.valueColors]) + } + + @Test("Writing the value colors it already holds tells no grid and marks nothing") + func unchangedWriteDoesNothing() throws { + let harness = try Harness(label: "value-colors-same", saved: ValueColorSettings(colorsValuesByType: true)) + + harness.settings.valueColors = ValueColorSettings(colorsValuesByType: true) + + #expect(harness.sentEvents == 0) + #expect(harness.tracker.dirtyRecords(for: .settings).isEmpty) + } + + // MARK: - Sync + + @Test("The outgoing value colors record carries what this Mac saved") + func outgoingRecordCarriesTheSavedSetting() throws { + let harness = try Harness(label: "value-colors-out", saved: ValueColorSettings(colorsValuesByType: true)) + + let data = try #require(harness.coordinator.settingsData(for: AppSettingsCategory.valueColors)) + + #expect(try JSONDecoder().decode(ValueColorSettings.self, from: data) == ValueColorSettings(colorsValuesByType: true)) + } + + @Test("A pulled value colors record turns the setting on and recolors the grids, without marking it for push") + func pulledRecordAppliesWithoutMarking() async throws { + let harness = try Harness(label: "value-colors-in") + let data = try JSONEncoder().encode(ValueColorSettings(colorsValuesByType: true)) + + let acknowledged = await harness.pull(AppSettingsCategory.valueColors, data) + + #expect(acknowledged) + #expect(harness.settings.valueColors.colorsValuesByType) + #expect(harness.storage.loadValueColors().colorsValuesByType) + #expect(harness.sentEvents == 1) + #expect(harness.tracker.dirtyRecords(for: .settings).isEmpty) + } + + @Test("A Data Grid record from a Mac that predates value colors leaves them as they are") + func olderDataGridRecordKeepsValueColors() async throws { + let harness = try Harness(label: "value-colors-older-grid", saved: ValueColorSettings(colorsValuesByType: true)) + var dataGrid = DataGridSettings.default + dataGrid.showRowNumbers.toggle() + + let acknowledged = await harness.pull(AppSettingsCategory.dataGrid, try JSONEncoder().encode(dataGrid)) + + #expect(acknowledged) + #expect(harness.settings.dataGrid.showRowNumbers == dataGrid.showRowNumbers) + #expect(harness.settings.valueColors.colorsValuesByType) + #expect(harness.storage.loadValueColors().colorsValuesByType) + #expect(harness.sentEvents == 0) + } +} + +@MainActor +private extension AppServices { + func withAppSettings(_ appSettings: AppSettingsManager, storage: AppSettingsStorage) -> AppServices { + AppServices( + appEvents: appEvents, + appSettings: appSettings, + appSettingsStorage: storage, + connectionStorage: connectionStorage, + databaseManager: databaseManager, + pluginManager: pluginManager, + schemaService: schemaService, + schemaRefreshService: schemaRefreshService, + schemaProviderRegistry: schemaProviderRegistry, + catalogChangeService: catalogChangeService, + sqlFavoriteManager: sqlFavoriteManager, + favoriteTablesStorage: favoriteTablesStorage, + favoriteDatabasesStorage: favoriteDatabasesStorage, + tableFolderStorage: tableFolderStorage, + aiChatStorage: aiChatStorage, + aiKeyStorage: aiKeyStorage, + aiAccessApprovals: aiAccessApprovals, + groupStorage: groupStorage, + tagStorage: tagStorage, + sshProfileStorage: sshProfileStorage, + credentialProfileStorage: credentialProfileStorage, + featureAccess: featureAccess, + syncMetadataStorage: syncMetadataStorage, + favoritesExpansionState: favoritesExpansionState, + linkedFolderWatcher: linkedFolderWatcher, + queryHistoryManager: queryHistoryManager, + dateFormattingService: dateFormattingService, + copilotService: copilotService, + mcpServerManager: mcpServerManager, + syncTracker: syncTracker, + welcomeRouter: welcomeRouter, + telemetry: telemetry + ) + } +} diff --git a/apple/TableProTests/Theme/ThemeCatalogTests.swift b/apple/TableProTests/Theme/ThemeCatalogTests.swift index 30527c765b..2fbee6b303 100644 --- a/apple/TableProTests/Theme/ThemeCatalogTests.swift +++ b/apple/TableProTests/Theme/ThemeCatalogTests.swift @@ -133,8 +133,7 @@ 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. + // Writing the file 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 +169,7 @@ 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 stale save would write `-2.json` beside the broken file, leaving two files with one id once it 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() @@ -599,8 +597,7 @@ 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 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 ba10190bbc..494bcf56a4 100644 --- a/apple/TableProTests/Theme/ThemeContrastTests.swift +++ b/apple/TableProTests/Theme/ThemeContrastTests.swift @@ -59,23 +59,47 @@ struct ThemeContrastTests { 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 { + // Alternate rows are on by default, and a modified cell or an inserted row washes either row. + private static func rowSurfaces(of theme: ThemeDefinition) throws -> [ThemeColor] { + let background = try #require(theme.overrides[.gridBackground], "\(theme.id)") + let stripe = try #require(theme.value(for: .gridAlternateRow).themedColor, "\(theme.id)") + let rows = [background, stripe.composited(over: background)] + let modified = try #require(theme.overrides[.gridModified], "\(theme.id)") + let inserted = try #require(theme.overrides[.gridInserted], "\(theme.id)") + return rows + [modified, inserted].flatMap { wash in rows.map { wash.composited(over: $0) } } + } + + private static func lowestRatio(of color: ThemeColor, on surfaces: [ThemeColor]) -> Double { + surfaces.map { color.contrastRatio(with: $0) }.min() ?? 0 + } + + @Test("Every bundled value color reaches 4.5:1 on both rows and on the modified and inserted washes over them") + func bundledValueColorsOnEveryRowSurface() 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)") + let surfaces = try Self.rowSurfaces(of: theme) for slot in Self.valueSlots { let color = try #require(theme.overrides[slot], "\(theme.id) sets no \(slot.rawValue)") - let ratio = color.contrastRatio(with: background) + let ratio = Self.lowestRatio(of: color, on: surfaces) #expect(ratio >= ThemeContrastNote.minimumRatio, "\(theme.id) \(slot.rawValue) \(color.hex): \(ratio)") } #expect(ThemeContrastNote.message(for: .values, in: theme) == nil, "\(theme.id)") } } + @Test("No two value colors in a bundled theme are the same") + func bundledValueColorsAreDistinct() throws { + for theme in try Self.bundledThemes() { + let colors = Self.valueSlots.compactMap { theme.overrides[$0] } + let hexes = colors.map { $0.hex } + #expect(colors.count == Self.valueSlots.count, "\(theme.id)") + #expect(Set(colors).count == colors.count, "\(theme.id): \(hexes)") + } + } + private static let valuesTheme = ThemeDefinition( id: "user.aaaa0004", name: "Ledger", @@ -83,9 +107,12 @@ struct ThemeContrastTests { appearance: .dark, overrides: [ .gridBackground: ThemeColor(red: 0x28, green: 0x2A, blue: 0x36), + .gridAlternateRow: ThemeColor(red: 0x21, green: 0x22, blue: 0x2C), + .gridModified: ThemeColor(red: 0xFF, green: 0xB8, blue: 0x6C, alpha: 0x26), + .gridInserted: ThemeColor(red: 0x50, green: 0xFA, blue: 0x7B, alpha: 0x26), .gridText: ThemeColor(red: 0xF8, green: 0xF8, blue: 0xF2), .gridBoolTrue: ThemeColor(red: 0x50, green: 0xFA, blue: 0x7B), - .gridNumber: ThemeColor(red: 0xBD, green: 0x93, blue: 0xF9), + .gridNumber: ThemeColor(red: 0xD6, green: 0xBC, blue: 0xFF), ] ) @@ -96,9 +123,9 @@ struct ThemeContrastTests { 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) + let surfaces = try Self.rowSurfaces(of: theme) + let falseRatio = Self.lowestRatio(of: try #require(theme.overrides[.gridBoolFalse]), on: surfaces) + let jsonRatio = Self.lowestRatio(of: try #require(theme.overrides[.gridJSON]), on: surfaces) #expect(falseRatio < jsonRatio) #expect(jsonRatio < ThemeContrastNote.minimumRatio) @@ -113,6 +140,28 @@ struct ThemeContrastTests { ) } + @Test( + "The values note checks the alternate row and the modified and inserted washes over both rows", + arguments: [ + (ThemeSlot.gridAlternateRow, ThemeColor(red: 0x5A, green: 0x5D, blue: 0x70)), + (ThemeSlot.gridModified, ThemeColor(red: 0xFF, green: 0xB8, blue: 0x6C, alpha: 0x80)), + (ThemeSlot.gridInserted, ThemeColor(red: 0x50, green: 0xFA, blue: 0x7B, alpha: 0x80)), + ] + ) + func valuesNoteChecksEveryRowSurface(surface: ThemeSlot, color: ThemeColor) throws { + var theme = Self.valuesTheme + theme.overrides[surface] = color + let background = try #require(theme.overrides[.gridBackground]) + let number = try #require(theme.overrides[.gridNumber]) + let expected = Self.lowestRatio(of: number, on: try Self.rowSurfaces(of: theme)) + #expect(number.contrastRatio(with: background) >= ThemeContrastNote.minimumRatio) + + let lowest = try #require(ThemeContrastNote.lowest(for: .values, in: theme)) + #expect(lowest.slot == .gridNumber) + #expect(lowest.ratio == expected) + #expect(lowest.ratio < ThemeContrastNote.minimumRatio) + } + @Test("The values note skips NULL and the value colors that follow the grid text") func valuesNoteSkipsNullAndUnsetColors() { var theme = Self.valuesTheme diff --git a/apple/TableProTests/Utilities/RowSortComparatorTests.swift b/apple/TableProTests/Utilities/RowSortComparatorTests.swift index 3283bce86d..e73ab8c16e 100644 --- a/apple/TableProTests/Utilities/RowSortComparatorTests.swift +++ b/apple/TableProTests/Utilities/RowSortComparatorTests.swift @@ -57,6 +57,35 @@ struct RowSortComparatorTests { #expect(result == .orderedSame) } + @Test("A boolean column holding integers orders them as numbers") + func booleanColumnHoldingIntegers() { + let tinyint = ColumnTypeClassifier(family: .mysql).classify(rawTypeName: "TINYINT(1)") + #expect(tinyint.isBooleanType) + #expect(RowSortComparator.compare("-2", "-1", columnType: tinyint) == .orderedAscending) + #expect(RowSortComparator.compare("10", "9", columnType: tinyint) == .orderedDescending) + #expect(RowSortComparator.compare("-128", "127", columnType: tinyint) == .orderedAscending) + #expect(RowSortComparator.compare("0", "1", columnType: tinyint) == .orderedAscending) + #expect(RowSortComparator.compare("1", "1", columnType: tinyint) == .orderedSame) + } + + @Test("A boolean column holding true and false orders false first, whatever the spelling") + func booleanColumnHoldingTruthValues() { + let boolean = ColumnType.boolean(rawType: "BOOLEAN") + #expect(RowSortComparator.compare("false", "true", columnType: boolean) == .orderedAscending) + #expect(RowSortComparator.compare("TRUE", "false", columnType: boolean) == .orderedDescending) + #expect(RowSortComparator.compare("t", "f", columnType: boolean) == .orderedDescending) + #expect(RowSortComparator.compare("t", "true", columnType: boolean) == .orderedSame) + } + + @Test("A boolean column mixing integers and true or false orders them one way") + func booleanColumnMixingIntegersAndTruthValues() { + let boolean = ColumnType.boolean(rawType: "BOOLEAN") + #expect(RowSortComparator.compare("1", "2", columnType: boolean) == .orderedAscending) + #expect(RowSortComparator.compare("2", "true", columnType: boolean) == .orderedDescending) + #expect(RowSortComparator.compare("true", "1", columnType: boolean) == .orderedSame) + #expect(RowSortComparator.compare("false", "-1", columnType: boolean) == .orderedDescending) + } + @Test("text column uses numeric string comparison") func textColumn() { let result = RowSortComparator.compare("file2", "file10", columnType: .text(rawType: "VARCHAR")) diff --git a/apple/TableProTests/Views/Compare/CompareRowGridModelTests.swift b/apple/TableProTests/Views/Compare/CompareRowGridModelTests.swift index 4ac04fa01c..8ee9d8f4d4 100644 --- a/apple/TableProTests/Views/Compare/CompareRowGridModelTests.swift +++ b/apple/TableProTests/Views/Compare/CompareRowGridModelTests.swift @@ -725,6 +725,7 @@ struct CompareRowGridModelTests { placeholderText: .secondaryLabelColor, booleanTrueText: .labelColor, booleanFalseText: .labelColor, + valueColors: DataGridValueColors(all: .labelColor), rowNumberText: .secondaryLabelColor, deletedRowText: .systemRed, modifiedColumnTint: .systemYellow, diff --git a/apple/TableProTests/Views/DataGrid/DataGridCellAppearanceTests.swift b/apple/TableProTests/Views/DataGrid/DataGridCellAppearanceTests.swift index 77d4b55c62..7b8523d2e1 100644 --- a/apple/TableProTests/Views/DataGrid/DataGridCellAppearanceTests.swift +++ b/apple/TableProTests/Views/DataGrid/DataGridCellAppearanceTests.swift @@ -6,6 +6,7 @@ import AppKit @testable import TablePro import TableProPluginKit +import TableProTestSupport import TableProTheme import Testing @@ -19,6 +20,7 @@ struct DataGridCellAppearanceTests { placeholderText: .secondaryLabelColor, booleanTrueText: .labelColor, booleanFalseText: .labelColor, + valueColors: DataGridValueColors(all: .labelColor), rowNumberText: .secondaryLabelColor, deletedRowText: .systemRed, modifiedColumnTint: .systemYellow, @@ -35,6 +37,7 @@ struct DataGridCellAppearanceTests { placeholderText: .systemBrown, booleanTrueText: .systemGreen, booleanFalseText: .systemPink, + valueColors: DataGridValueColors(all: .systemPurple), rowNumberText: .systemTeal, deletedRowText: .systemRed, modifiedColumnTint: .systemYellow, @@ -498,13 +501,6 @@ struct DataGridCellAppearanceTests { #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)) @@ -555,9 +551,17 @@ struct DataGridCellAppearanceTests { colorsValuesByType: Bool, settingIsOn: Bool = true, overridesSetting: Bool = false, + isForeignKey: Bool = false, format: ValueDisplayFormat? ) throws { - owner = GridTestOwner(TableRows.from(queryRows: values.map { [$0] }, columns: ["value"], columnTypes: [type])) + owner = GridTestOwner(TableRows.from( + queryRows: values.map { [$0] }, + columns: ["value"], + columnTypes: [type], + columnForeignKeys: isForeignKey + ? ["value": ForeignKeyInfo(name: "fk_value", column: "value", referencedTable: "targets", referencedColumn: "id")] + : [:] + )) var configuration = DataGridConfiguration() configuration.colorsValuesByType = colorsValuesByType configuration.overridesValueColorSetting = overridesSetting @@ -659,27 +663,6 @@ struct DataGridCellAppearanceTests { #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")] @@ -695,6 +678,29 @@ struct DataGridCellAppearanceTests { ) == ["#111111"]) } + @Test("A foreign key cell keeps its link and takes its type's color") + func foreignKeyTakesItsTypeColor() throws { + let number = try TypedGrid( + values: [.text("7")], + type: .integer(rawType: "INT"), + colorsValuesByType: true, + isForeignKey: true, + format: nil + ) + let string = try TypedGrid( + values: [.text("ops")], + type: .text(rawType: "VARCHAR(8)"), + colorsValuesByType: true, + isForeignKey: true, + format: nil + ) + + #expect(number.presenter.presentation(forDataIndex: 0).kind == .foreignKey) + #expect(try withExtendedLifetime(number.owner) { try number.colors() } == ["#0044AA"]) + #expect(string.presenter.presentation(forDataIndex: 0).kind == .foreignKey) + #expect(try withExtendedLifetime(string.owner) { try string.colors() } == ["#AA1100"]) + } + @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"]) @@ -821,3 +827,60 @@ struct DataGridCellAppearanceTests { #expect(resolve(onEmphasizedSelection: true, palette: palette).textColor === NSColor.alternateSelectedControlTextColor) } } + +extension GlobalStateSuites { + @MainActor + struct DataGridValueColorSettingTests { + private func valueColor(in grid: HostedDataGrid) throws -> String? { + let aqua = try #require(NSAppearance(named: .aqua)) + let presenter = grid.controller.presenter + let pass = try #require(presenter.makePass(in: aqua)) + let row = try #require(grid.model.row(at: 0)) + let column = try #require(grid.model.columns.column(at: 0)) + let appearance = try #require(presenter.appearance( + of: row, + displayRow: 0, + column: column, + visualState: .empty, + pass: pass, + onEmphasizedSelection: false + )) + return ThemeTestFixture.hex(appearance.textColor) + } + + @Test("Turning Color values by type on or off reaches a mounted grid's cells, with no reload of its rows") + func settingChangeRecolorsWithoutReload() async throws { + let settings = AppSettingsManager.shared + let original = settings.valueColors + defer { settings.valueColors = original } + settings.valueColors = ValueColorSettings(colorsValuesByType: false) + var configuration = DataGridConfiguration() + configuration.colorsValuesByType = true + let scope = ThemeScope.fixed(try ThemeTestFixture.theme(.light, [.gridText: "#111111", .gridNumber: "#0044AA"])) + let grid = HostedDataGrid( + rows: TableRows.from(queryRows: [[.text("42")]], columns: ["value"], columnTypes: [.integer(rawType: "INT")]), + configuration: configuration, + prepare: { _, model in model.themeScope = scope } + ) + defer { grid.close() } + let rowView = try #require(grid.main.rowView(atRow: 0, makeIfNecessary: true)) + let rowID = try #require(grid.model.row(at: 0)).id + let off = try valueColor(in: grid) + let cached = try #require(grid.model.display.cache.box(forID: rowID)) + + settings.valueColors = ValueColorSettings(colorsValuesByType: true) + try await waitUntil { grid.controller.presenter.registry.colorsValuesByType } + let on = try valueColor(in: grid) + + settings.valueColors = ValueColorSettings(colorsValuesByType: false) + try await waitUntil { !grid.controller.presenter.registry.colorsValuesByType } + let offAgain = try valueColor(in: grid) + + #expect(off == "#111111") + #expect(on == "#0044AA") + #expect(offAgain == "#111111") + #expect(grid.model.display.cache.box(forID: rowID) === cached) + #expect(grid.main.rowView(atRow: 0, makeIfNecessary: false) === rowView) + } + } +} diff --git a/apple/TableProTests/Views/DataGrid/DataGridConfigurationTests.swift b/apple/TableProTests/Views/DataGrid/DataGridConfigurationTests.swift index e22ce7c07c..8f4b9d71d8 100644 --- a/apple/TableProTests/Views/DataGrid/DataGridConfigurationTests.swift +++ b/apple/TableProTests/Views/DataGrid/DataGridConfigurationTests.swift @@ -4,8 +4,10 @@ // import AppKit +import SwiftUI @testable import TablePro import TableProPluginKit +import TableProTestSupport import Testing @MainActor @@ -47,6 +49,92 @@ struct DataGridConfigurationTests { #expect(!configuration.overridesValueColorSetting) } + // MARK: - Value colors + + @Test("The data tab, routine debug results and both comparison grids color values by type") + func resultGridsColorValuesByType() async throws { + let fixture = CoordinatorFixture() + let tab = fixture.addTab(QueryTab(title: "Query 1", query: "SELECT 1", tabType: .query)) + let result = PluginQueryResult( + columns: ["total"], + columnTypeNames: ["integer"], + rows: [[.text("7")]], + rowsAffected: 0, + timing: PluginQueryTiming(total: 0) + ) + let (routine, _) = RoutineDebugFixtures.controller(session: FakeDebugSession(performAnswer: .finished(result))) + routine.start() + try await waitUntil { routine.state.pausedLocation != nil && !routine.variables.isEmpty } + routine.perform(.continueExecution) + try await waitUntil { routine.hasEnded } + let debugGrid = try #require(routine.resultGrid) + + #expect(fixture.coordinator.gridConfiguration(for: tab).colorsValuesByType) + #expect(debugGrid.model.configuration.colorsValuesByType) + #expect(CompareRowGridModel().model.configuration.colorsValuesByType) + #expect(ResultComparisonGridModel().model.configuration.colorsValuesByType) + } + + @Test("Table Listing draws its catalog rows in the grid text") + func tableListingStaysPlain() { + #expect(!TableListingTabView.gridConfiguration(databaseType: .mysql).colorsValuesByType) + } + + @Test("Structure lists, Structure sections and data files draw values in the grid text") + func definitionAndFileGridsStayPlain() throws { + let list = StructureListGridModel() + list.load( + columns: ["Name"], + rows: [StructureListRow(id: "a", values: ["alpha"])], + defaultWidths: [], + minimumWidths: [], + databaseType: .mysql, + sortState: nil + ) + let suite = try EphemeralDefaults() + let editor = StructureDefinitionEditor( + manager: StructureChangeManager(), + context: .existingTable( + connection: TestFixtures.makeConnection(type: .mysql), + databaseName: "db", + schemaName: nil, + tableName: "t", + objectKind: .table, + isSessionBound: false + ), + defaults: suite.defaults + ) + editor.refresh() + + #expect(!list.model.configuration.colorsValuesByType) + #expect(!editor.adapter(for: .columns).model.configuration.colorsValuesByType) + #expect(!DataFileContentView.gridConfiguration(for: DataFileController()).colorsValuesByType) + } + + @Test("An agent session's rows draw values in the grid text") + func agentRowsStayPlain() async throws { + let run = AgentQueryRun(id: "1", sql: "select total from orders", resultJSON: #"{"columns":["total"],"rows":[["7"]]}"#) + let window = ThemeTestFixture.host( + AgentResultRowsView(runs: [run], artifacts: AgentArtifactCache(), connection: nil), + size: NSSize(width: 600, height: 300) + ) + defer { window.contentView = nil } + + try await waitUntil { + window.contentView?.layoutSubtreeIfNeeded() + return Self.mainTable(in: window.contentView)?.controller?.model.rowCount == 1 + } + let model = try #require(Self.mainTable(in: window.contentView)?.controller?.model) + + #expect(!model.configuration.colorsValuesByType) + } + + private static func mainTable(in view: NSView?) -> DataGridTableView? { + guard let view else { return nil } + if let table = view as? DataGridTableView, table.paneKind == .main { return table } + return view.subviews.lazy.compactMap { mainTable(in: $0) }.first + } + // MARK: - Sort @Test("Every data column sorts by its column identity, first click ascending outside row grids") diff --git a/apple/TableProTests/Views/DataGrid/DataGridInvisibleCharacterTests.swift b/apple/TableProTests/Views/DataGrid/DataGridInvisibleCharacterTests.swift index d7407746f6..c57045fbde 100644 --- a/apple/TableProTests/Views/DataGrid/DataGridInvisibleCharacterTests.swift +++ b/apple/TableProTests/Views/DataGrid/DataGridInvisibleCharacterTests.swift @@ -18,6 +18,7 @@ struct DataGridInvisibleCharacterTests { placeholderText: .secondaryLabelColor, booleanTrueText: .black, booleanFalseText: .black, + valueColors: DataGridValueColors(all: .black), rowNumberText: .secondaryLabelColor, deletedRowText: .systemRed, modifiedColumnTint: .systemYellow, diff --git a/apple/TableProTests/Views/DataGrid/DataGridPendingChangeMarkTests.swift b/apple/TableProTests/Views/DataGrid/DataGridPendingChangeMarkTests.swift index 5f2c5c43ae..9ae1140a94 100644 --- a/apple/TableProTests/Views/DataGrid/DataGridPendingChangeMarkTests.swift +++ b/apple/TableProTests/Views/DataGrid/DataGridPendingChangeMarkTests.swift @@ -18,6 +18,7 @@ struct DataGridPendingChangeMarkTests { placeholderText: .secondaryLabelColor, booleanTrueText: .labelColor, booleanFalseText: .labelColor, + valueColors: DataGridValueColors(all: .labelColor), rowNumberText: .secondaryLabelColor, deletedRowText: .systemRed, modifiedColumnTint: .systemYellow, diff --git a/apple/TableProTests/Views/DataGrid/DataGridValueToneTests.swift b/apple/TableProTests/Views/DataGrid/DataGridValueToneTests.swift index aebac73a27..67485c798b 100644 --- a/apple/TableProTests/Views/DataGrid/DataGridValueToneTests.swift +++ b/apple/TableProTests/Views/DataGrid/DataGridValueToneTests.swift @@ -26,6 +26,14 @@ struct DataGridValueToneTests { (.text(rawType: "String"), .string), (.text(rawType: "Nullable(String)"), .string), (.text(rawType: "LowCardinality(Nullable(String))"), .string), + (.text(rawType: "keyword"), .string), + (.text(rawType: "wildcard"), .string), + (.text(rawType: "constant_keyword"), .string), + (.text(rawType: "ascii"), .string), + (.text(rawType: "long"), .string), + (.text(rawType: "sysname"), .string), + (.integer(rawType: "long"), .number), + (.blob(rawType: "long raw"), .binary), (.text(rawType: "UUID"), .uuid), (.text(rawType: "Nullable(UUID)"), .uuid), (.text(rawType: "UNIQUEIDENTIFIER"), .uuid), @@ -47,6 +55,14 @@ struct DataGridValueToneTests { #expect(DataGridValueTone.base(of: type, declaresEnumLabels: false) == expected) } + @Test("Character types only the tone knows leave a filter's LIKE operand cast", arguments: [ + "keyword", "wildcard", "constant_keyword", "ascii", "long", "sysname", + ]) + func toneOnlyCharacterTypesKeepTheirQuoting(rawType: String) { + #expect(DataGridValueTone.base(of: .text(rawType: rawType), declaresEnumLabels: false) == .string) + #expect(!ColumnTypeSQLQuoting.isCharacterType(.text(rawType: rawType))) + } + @Test("Declared labels make a text column an enum, never an array, vector or number") func declaredLabelsMarkTextOnly() { #expect(DataGridValueTone.base(of: .text(rawType: "mood"), declaresEnumLabels: true) == .enumeration) diff --git a/apple/TableProTests/Views/Structure/StructureListGridModelTests.swift b/apple/TableProTests/Views/Structure/StructureListGridModelTests.swift index bceac298d7..731c1126a8 100644 --- a/apple/TableProTests/Views/Structure/StructureListGridModelTests.swift +++ b/apple/TableProTests/Views/Structure/StructureListGridModelTests.swift @@ -222,6 +222,7 @@ struct StructureListGridToneTests { placeholderText: .systemBrown, booleanTrueText: .labelColor, booleanFalseText: .labelColor, + valueColors: DataGridValueColors(all: .systemPurple), rowNumberText: .secondaryLabelColor, deletedRowText: .systemPink, modifiedColumnTint: .systemYellow, diff --git a/apple/changelog.d/4205.changed.md b/apple/changelog.d/4205.changed.md index 2dc37713bb..9dfff3253f 100644 --- a/apple/changelog.d/4205.changed.md +++ b/apple/changelog.d/4205.changed.md @@ -1 +1 @@ -Built-in themes set a color for each value type, and their boolean colors now reach 4.5:1 contrast. +Bundled palette themes like Dracula and Nord color each value type, and booleans and values reach 4.5:1 on every row. diff --git a/apple/changelog.d/4207.changed.1.md b/apple/changelog.d/4207.changed.1.md new file mode 100644 index 0000000000..54c382b929 --- /dev/null +++ b/apple/changelog.d/4207.changed.1.md @@ -0,0 +1 @@ +Themes that set Values colors, copies of built-in themes included, sync only to Macs on this version or later. diff --git a/apple/changelog.d/4207.changed.md b/apple/changelog.d/4207.changed.md new file mode 100644 index 0000000000..143021a1a8 --- /dev/null +++ b/apple/changelog.d/4207.changed.md @@ -0,0 +1 @@ +Edited cells in Dracula and Nord use a lighter tint, so values stay readable on them. diff --git a/apple/changelog.d/4207.fixed.md b/apple/changelog.d/4207.fixed.md new file mode 100644 index 0000000000..bea94351bf --- /dev/null +++ b/apple/changelog.d/4207.fixed.md @@ -0,0 +1 @@ +Sorting and highlight rules on boolean columns holding numbers compared them as text.