Skip to content

Commit 347022f

Browse files
committed
fix: address review feedback for data grid font setting
1 parent 0c0accc commit 347022f

File tree

4 files changed

+34
-28
lines changed

4 files changed

+34
-28
lines changed

TablePro/Theme/DataGridFontCache.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@
88

99
import AppKit
1010

11+
/// Tags stored on NSTextField.tag to identify which font variant a cell uses.
12+
/// Used by `updateVisibleCellFonts` to re-apply the correct variant after a font change.
13+
enum DataGridFontVariant {
14+
static let regular = 0
15+
static let italic = 1
16+
static let medium = 2
17+
static let rowNumber = 3
18+
}
19+
20+
@MainActor
1121
struct DataGridFontCache {
1222
private(set) static var regular = NSFont.monospacedSystemFont(ofSize: 13, weight: .regular)
1323
private(set) static var italic = regular.withTraits(.italic)

TablePro/Views/Results/DataGridCellFactory.swift

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,15 @@ final class DataGridCellFactory {
9696
let textField = reused.textField {
9797
cellView = reused
9898
cell = textField
99+
cell.font = DataGridFontCache.rowNumber
99100
} else {
100101
cellView = NSTableCellView()
101102
cellView.identifier = cellViewId
102103

103104
cell = NSTextField(labelWithString: "")
104105
cell.alignment = .right
105106
cell.font = DataGridFontCache.rowNumber
107+
cell.tag = DataGridFontVariant.rowNumber
106108
cell.textColor = .secondaryLabelColor
107109
cell.translatesAutoresizingMaskIntoConstraints = false
108110

@@ -312,35 +314,28 @@ final class DataGridCellFactory {
312314

313315
if value == nil {
314316
cell.stringValue = ""
317+
cell.font = DataGridFontCache.italic
318+
cell.tag = DataGridFontVariant.italic
315319
if !isLargeDataset {
316320
cell.placeholderString = nullDisplayString
317-
cell.textColor = .secondaryLabelColor
318-
if cell.font !== DataGridFontCache.italic {
319-
cell.font = DataGridFontCache.italic
320-
}
321-
} else {
322-
cell.textColor = .secondaryLabelColor
323321
}
322+
cell.textColor = .secondaryLabelColor
324323
} else if value == "__DEFAULT__" {
325324
cell.stringValue = ""
325+
cell.font = DataGridFontCache.medium
326+
cell.tag = DataGridFontVariant.medium
326327
if !isLargeDataset {
327328
cell.placeholderString = "DEFAULT"
328-
cell.textColor = .systemBlue
329-
cell.font = DataGridFontCache.medium
330-
} else {
331-
cell.textColor = .systemBlue
332329
}
330+
cell.textColor = .systemBlue
333331
} else if value == "" {
334332
cell.stringValue = ""
333+
cell.font = DataGridFontCache.italic
334+
cell.tag = DataGridFontVariant.italic
335335
if !isLargeDataset {
336336
cell.placeholderString = "Empty"
337-
cell.textColor = .secondaryLabelColor
338-
if cell.font !== DataGridFontCache.italic {
339-
cell.font = DataGridFontCache.italic
340-
}
341-
} else {
342-
cell.textColor = .secondaryLabelColor
343337
}
338+
cell.textColor = .secondaryLabelColor
344339
} else {
345340
var displayValue = value ?? ""
346341

@@ -360,9 +355,8 @@ final class DataGridCellFactory {
360355
cell.stringValue = displayValue
361356
(cell as? CellTextField)?.originalValue = value
362357
cell.textColor = .labelColor
363-
if cell.font !== DataGridFontCache.regular {
364-
cell.font = DataGridFontCache.regular
365-
}
358+
cell.font = DataGridFontCache.regular
359+
cell.tag = DataGridFontVariant.regular
366360
}
367361
}
368362

TablePro/Views/Results/DataGridView.swift

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ final class TableViewCoordinator: NSObject, NSTableViewDelegate, NSTableViewData
667667
// Settings observer for real-time updates
668668
fileprivate var settingsObserver: NSObjectProtocol?
669669
/// Snapshot of last-seen data grid settings for change detection
670-
private var lastDataGridSettings = AppSettingsManager.shared.dataGrid
670+
private var lastDataGridSettings: DataGridSettings
671671

672672
@Binding var selectedRowIndices: Set<Int>
673673

@@ -718,6 +718,7 @@ final class TableViewCoordinator: NSObject, NSTableViewDelegate, NSTableViewData
718718
self.onPasteRows = onPasteRows
719719
self.onUndo = onUndo
720720
self.onRedo = onRedo
721+
self.lastDataGridSettings = AppSettingsManager.shared.dataGrid
721722
super.init()
722723
updateCache()
723724

@@ -780,7 +781,8 @@ final class TableViewCoordinator: NSObject, NSTableViewDelegate, NSTableViewData
780781
// MARK: - Font Updates
781782

782783
/// Update fonts on existing visible cell views in-place.
783-
/// Avoids reloadData which recycles cells through the reuse pool.
784+
/// Uses `DataGridFontVariant` tags set during cell configuration
785+
/// to apply the correct font variant without inspecting cell content.
784786
@MainActor
785787
static func updateVisibleCellFonts(tableView: NSTableView) {
786788
let visibleRect = tableView.visibleRect
@@ -793,14 +795,14 @@ final class TableViewCoordinator: NSObject, NSTableViewDelegate, NSTableViewData
793795
guard let cellView = tableView.view(atColumn: col, row: row, makeIfNecessary: false) as? NSTableCellView,
794796
let textField = cellView.textField else { continue }
795797

796-
let columnId = tableView.tableColumns[col].identifier.rawValue
797-
if columnId == "__rowNumber__" {
798+
switch textField.tag {
799+
case DataGridFontVariant.rowNumber:
798800
textField.font = DataGridFontCache.rowNumber
799-
} else if textField.stringValue.isEmpty && textField.placeholderString == "DEFAULT" {
800-
textField.font = DataGridFontCache.medium
801-
} else if textField.stringValue.isEmpty && textField.placeholderString != nil {
801+
case DataGridFontVariant.italic:
802802
textField.font = DataGridFontCache.italic
803-
} else {
803+
case DataGridFontVariant.medium:
804+
textField.font = DataGridFontCache.medium
805+
default:
804806
textField.font = DataGridFontCache.regular
805807
}
806808
}

TablePro/Views/Settings/DataGridSettingsView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ struct DataGridSettingsView: View {
2727

2828
GroupBox("Preview") {
2929
Text("1 John Doe john@example.com NULL")
30-
.font(.custom(settings.fontFamily.displayName, size: CGFloat(settings.clampedFontSize)))
30+
.font(Font(settings.fontFamily.font(size: CGFloat(settings.clampedFontSize))))
3131
.frame(maxWidth: .infinity, alignment: .leading)
3232
.padding(8)
3333
}

0 commit comments

Comments
 (0)