Skip to content

Commit bac7e4c

Browse files
committed
fix column width rendering race condition, initialize with column_widths, update column widths when base sizes change
1 parent 6358975 commit bac7e4c

File tree

4 files changed

+66
-53
lines changed

4 files changed

+66
-53
lines changed

src/core/datagrid.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1081,7 +1081,7 @@ class DataGrid extends Widget {
10811081
*
10821082
* @returns The column count for the specified region.
10831083
*/
1084-
columnCount(region: DataModel.RowRegion): number {
1084+
columnCount(region: DataModel.ColumnRegion): number {
10851085
let count: number;
10861086
if (region === 'body') {
10871087
count = this._columnSections.count;

src/core/viewbasedjsonmodel.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ export class ViewBasedJSONModel extends MutableDataModel {
237237
return index;
238238
}
239239

240-
public columnIndexToName(index: number, region: DataModel.CellRegion): string {
240+
public columnIndexToName(index: number, region: DataModel.ColumnRegion): string {
241241
let schema = this.dataset.schema;
242242
if (region == 'row-header') {
243243
return schema.primaryKey[index];

src/datagrid.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ function unpack_data(
265265
}
266266

267267
export
268-
class DataGridView extends DOMWidgetView {
268+
class DataGridView extends DOMWidgetView {
269269
_createElement(tagName: string) {
270270
this.pWidget = new JupyterPhosphorPanelWidget({ view: this });
271271
// initialize to light theme unless set earlier
@@ -284,18 +284,23 @@ export
284284
render() {
285285
this.el.classList.add('datagrid-container');
286286

287-
this.grid = new FeatherGrid();
288-
this.grid.baseRowSize = this.model.get('base_row_size');
289-
this.grid.baseColumnSize = this.model.get('base_column_size');
290-
this.grid.baseRowHeaderSize = this.model.get('base_row_header_size');
291-
this.grid.baseColumnHeaderSize = this.model.get('base_column_header_size');
292-
this.grid.headerVisibility = this.model.get('header_visibility');
293-
this.grid.dataModel = this.model.data_model;
294-
this.grid.selectionModel = this.model.selectionModel;
287+
this.grid = new FeatherGrid({
288+
defaultSizes: {
289+
rowHeight: this.model.get('base_row_size'),
290+
columnWidth: this.model.get('base_column_size'),
291+
rowHeaderWidth: this.model.get('base_row_header_size'),
292+
columnHeaderHeight: this.model.get('base_column_header_size')
293+
},
294+
headerVisibility: this.model.get('header_visibility')
295+
});
296+
297+
this.grid.columnWidths = this.model.get('column_widths');
295298
this.grid.editable = this.model.get('editable');
296299
// this.default_renderer must be created after setting grid.isLightTheme
297300
// for proper color variable initialization
298301
this.grid.isLightTheme = this._isLightTheme;
302+
this.grid.dataModel = this.model.data_model;
303+
this.grid.selectionModel = this.model.selectionModel;
299304

300305
this.grid.cellClicked.connect((sender: FeatherGrid, event: FeatherGrid.ICellClickedEvent) => {
301306
if (this.model.comm) {

src/feathergrid.ts

Lines changed: 50 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,19 @@ class FeatherGridMouseHandler extends BasicMouseHandler {
151151

152152
export
153153
class FeatherGrid extends Widget {
154-
constructor() {
154+
constructor(options: DataGrid.IOptions = {}) {
155155
super();
156156
this.addClass('ipydatagrid-widget');
157157

158-
this._createGrid();
158+
if (options.defaultSizes) {
159+
this._baseRowSize = options.defaultSizes.rowHeight || this._baseRowSize;
160+
this._baseColumnSize = options.defaultSizes.columnWidth || this._baseColumnSize;
161+
this._baseRowHeaderSize = options.defaultSizes.rowHeaderWidth || this._baseRowHeaderSize;
162+
this._baseColumnHeaderSize = options.defaultSizes.columnHeaderHeight || this._baseColumnHeaderSize;
163+
}
164+
this._headerVisibility = options.headerVisibility || this._headerVisibility;
165+
166+
this._createGrid(options);
159167

160168
this._defaultRenderer = new TextRenderer({
161169
font: '12px sans-serif',
@@ -235,6 +243,7 @@ class FeatherGrid extends Widget {
235243
}
236244

237245
this.grid.defaultSizes = { ...this.grid.defaultSizes, columnWidth: size };
246+
this._updateColumnWidths();
238247
}
239248

240249
get baseColumnSize(): number {
@@ -263,6 +272,7 @@ class FeatherGrid extends Widget {
263272
}
264273

265274
this.grid.defaultSizes = { ...this.grid.defaultSizes, rowHeaderWidth: size };
275+
this._updateColumnWidths();
266276
}
267277

268278
get baseRowHeaderSize(): number {
@@ -354,15 +364,17 @@ class FeatherGrid extends Widget {
354364
return this.grid.cellRenderers;
355365
}
356366

357-
private _createGrid() {
367+
private _createGrid(options: DataGrid.IOptions = {}) {
358368
this.grid = new DataGrid({
359-
defaultSizes: {
360-
rowHeight: this._baseRowSize,
361-
columnWidth: this._baseColumnSize,
362-
rowHeaderWidth: this._baseRowHeaderSize,
363-
columnHeaderHeight: this._baseColumnHeaderSize
364-
},
365-
headerVisibility: this._headerVisibility
369+
...options, ...{
370+
defaultSizes: {
371+
rowHeight: this._baseRowSize,
372+
columnWidth: this._baseColumnSize,
373+
rowHeaderWidth: this._baseRowHeaderSize,
374+
columnHeaderHeight: this._baseColumnHeaderSize
375+
},
376+
headerVisibility: this._headerVisibility
377+
}
366378
});
367379

368380
MessageLoop.installMessageHook(this.grid.viewport, this);
@@ -688,44 +700,40 @@ class FeatherGrid extends Widget {
688700
private _updateGridRenderers() {
689701
this.grid.cellRenderers.update({ 'body': this._rendererResolver.bind(this) });
690702
}
691-
692-
/**
693-
* This function resets the column sizes to the base column size and functions
694-
* identically to phosphor's resetColumns() but does not call _repaintOverlay
695-
* or _repaintContent in the process.
696-
*/
697-
private _resetAllColumnWidths() {
698-
let column_base_size = this._baseColumnSize;
699-
700-
// Resizing columns from body region
701-
for (let i = 0; i < this.grid.columnCount('body'); i++) {
702-
this.grid.resizeColumn('body', i, column_base_size)
703-
}
704-
705-
// Resizing columns from row header region
706-
for (let i = 0; i < this.grid.columnCount('column-header'); i++) {
707-
this.grid.resizeColumn('row-header', i, column_base_size)
708-
}
709-
}
710-
703+
711704
private _updateColumnWidths() {
712-
//@ts-ignore added so we don't have to add basicmousehandler.ts fork
705+
const columnWidths = this._columnWidths;
706+
// @ts-ignore added so we don't have to add basicmousehandler.ts fork
713707
let mouseHandler = this.grid.mouseHandler as FeatherGridMouseHandler;
714-
708+
715709
// Do not want this callback to be executed when user resizes using the mouse
716-
if (!mouseHandler.mouseIsDown) {
717-
let column_widths_dict = this._columnWidths;
718-
719-
this._resetAllColumnWidths();
720-
721-
for (let key in column_widths_dict) {
722-
let index = this.dataModel.columnNameToIndex(key);
723-
let region = this.dataModel.columnNameToRegion(key);
724-
this.grid.resizeColumn(region, index, column_widths_dict[key]);
710+
if (mouseHandler.mouseIsDown) {
711+
return;
712+
}
713+
714+
// Resizing columns from row header region
715+
if (this._headerVisibility === 'row' || this._headerVisibility === 'all') {
716+
const baseRowHeaderSize = this._baseRowHeaderSize;
717+
const rowHeaderColCount = this.grid.columnCount('row-header');
718+
for (let i = 0; i < rowHeaderColCount; i++) {
719+
const colName = this.dataModel.columnIndexToName(i, 'row-header');
720+
const colSize = columnWidths.hasOwnProperty(colName) ? columnWidths[colName] : baseRowHeaderSize;
721+
722+
this.grid.resizeColumn('row-header', i, colSize);
725723
}
726724
}
725+
726+
// Resizing columns from body region
727+
const baseColumnSize = this._baseColumnSize;
728+
const bodyColCount = this.grid.columnCount('body');
729+
for (let i = 0; i < bodyColCount; i++) {
730+
const colName = this.dataModel.columnIndexToName(i, 'body');
731+
const colSize = columnWidths.hasOwnProperty(colName) ? columnWidths[colName] : baseColumnSize;
732+
733+
this.grid.resizeColumn('body', i, colSize);
734+
}
727735
}
728-
736+
729737
private _updateHeaderRenderer() {
730738
const headerRenderer = new HeaderRenderer({
731739
textOptions: {

0 commit comments

Comments
 (0)