Skip to content

Commit 7a10db3

Browse files
committed
Preserve calculated column width by name
1 parent fe1b591 commit 7a10db3

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

packages/iris-grid/src/IrisGridMetricCalculator.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,25 @@ export class IrisGridMetricCalculator extends GridMetricCalculator {
3939
(columns: readonly dh.Column[]) => columns.map(col => col.name)
4040
);
4141

42+
private updateCalculatedColumnWidths(model: IrisGridModel): void {
43+
assertNotNull(this.cachedModelColumnNames);
44+
const calculatedColumnWidthsByName = new Map<ColumnName, number>();
45+
this.cachedModelColumnNames.forEach((name, index) => {
46+
const prevColumnWidth = this.calculatedColumnWidths.get(index);
47+
if (prevColumnWidth != null) {
48+
calculatedColumnWidthsByName.set(name, prevColumnWidth);
49+
}
50+
});
51+
this.resetCalculatedColumnWidths();
52+
calculatedColumnWidthsByName.forEach((width, name) => {
53+
const index = model.getColumnIndexByName(name);
54+
if (index != null) {
55+
this.calculatedColumnWidths.set(index, width);
56+
}
57+
});
58+
trimMap(this.calculatedColumnWidths);
59+
}
60+
4261
/**
4362
* Updates the user column widths based on the current model state
4463
* @param model The current IrisGridModel
@@ -61,14 +80,17 @@ export class IrisGridMetricCalculator extends GridMetricCalculator {
6180
// Comparing model.columns references wouldn't work here because
6281
// the reference can change in the model without the actual column definitions changing
6382
if (
83+
this.cachedModelColumnNames != null &&
6484
!deepEqual(
6585
this.getCachedCurrentModelColumnNames(model.columns),
6686
this.cachedModelColumnNames
6787
)
6888
) {
69-
this.resetCalculatedColumnWidths();
89+
// Preserve column widths when possible to minimize visual shifts in the grid layout
90+
this.updateCalculatedColumnWidths(model);
7091
this.updateUserColumnWidths(model);
7192
}
93+
this.cachedModelColumnNames = model.columns.map(col => col.name);
7294
}
7395

7496
getGridY(state: IrisGridMetricState): number {
@@ -108,7 +130,6 @@ export class IrisGridMetricCalculator extends GridMetricCalculator {
108130
// Update column widths if columns in the cached model don't match the current model passed in the state
109131
this.updateColumnWidthsIfNecessary(model);
110132

111-
this.cachedModelColumnNames = model.columns.map(col => col.name);
112133
return super.getMetrics(state);
113134
}
114135

0 commit comments

Comments
 (0)