From 4ef15416e4c7135a00a132f30f7771c85f79a805 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ng=C3=B4=20Qu=E1=BB=91c=20=C4=90=E1=BA=A1t?= Date: Tue, 31 Mar 2026 14:07:11 +0700 Subject: [PATCH] perf: skip column width calculation when saved layout exists --- TablePro/Views/Results/DataGridView.swift | 28 +++++++++++++++-------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/TablePro/Views/Results/DataGridView.swift b/TablePro/Views/Results/DataGridView.swift index 52ee072f..0888fbad 100644 --- a/TablePro/Views/Results/DataGridView.swift +++ b/TablePro/Views/Results/DataGridView.swift @@ -373,6 +373,7 @@ struct DataGridView: NSViewRepresentable { tableView.removeTableColumn(column) } + let willRestoreWidths = !columnLayout.columnWidths.isEmpty for (index, columnName) in rowProvider.columns.enumerated() { let column = NSTableColumn(identifier: NSUserInterfaceItemIdentifier("col_\(index)")) column.title = columnName @@ -384,11 +385,15 @@ struct DataGridView: NSViewRepresentable { column.headerCell.setAccessibilityLabel( String(localized: "Column: \(columnName)") ) - column.width = coordinator.cellFactory.calculateOptimalColumnWidth( - for: columnName, - columnIndex: index, - rowProvider: rowProvider - ) + if willRestoreWidths { + column.width = columnLayout.columnWidths[columnName] ?? 100 + } else { + column.width = coordinator.cellFactory.calculateOptimalColumnWidth( + for: columnName, + columnIndex: index, + rowProvider: rowProvider + ) + } column.minWidth = 30 column.resizingMask = .userResizingMask column.isEditable = isEditable @@ -397,6 +402,7 @@ struct DataGridView: NSViewRepresentable { } } else { // Same column count — lightweight in-place update (avoids remove/add overhead) + let hasSavedWidths = !columnLayout.columnWidths.isEmpty for column in tableView.tableColumns where column.identifier.rawValue != "__rowNumber__" { guard let colIndex = Self.columnIndex(from: column.identifier), colIndex < rowProvider.columns.count else { continue } @@ -407,11 +413,13 @@ struct DataGridView: NSViewRepresentable { ?? rowProvider.columnTypes[colIndex].displayName column.headerToolTip = "\(columnName) (\(typeName))" } - column.width = coordinator.cellFactory.calculateOptimalColumnWidth( - for: columnName, - columnIndex: colIndex, - rowProvider: rowProvider - ) + if !hasSavedWidths { + column.width = coordinator.cellFactory.calculateOptimalColumnWidth( + for: columnName, + columnIndex: colIndex, + rowProvider: rowProvider + ) + } column.isEditable = isEditable } }