Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add auto-resize function for column widths #221

Merged
merged 8 commits into from
Aug 19, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add body column header helper function
Signed-off-by: Itay Dafna <[email protected]>
  • Loading branch information
ibdafna committed Aug 18, 2021
commit cbea8d63750edc8639f3f8452bd551347a009197
50 changes: 50 additions & 0 deletions packages/datagrid/src/datagrid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3722,6 +3722,56 @@ class DataGrid extends Widget {
this.drawCornerHeaderRegion(rx, ry, rw, rh);
}

/**
* Resizes body column headers so their text fits
* without clipping or wrapping.
* @param dataModel
*/
private _fitBodyColumnHeaders(dataModel: DataModel): void {
// Get the body column count
const bodyColumnCount = dataModel.columnCount('body');
for (let i = 0; i < bodyColumnCount; i++) {
/*
if we're working with nested column headers,
retrieve the nested levels and iterate on them.
*/
const numRows = dataModel.rowCount('column-header');

/*
Calculate the maximum text width vertically, across
all nested rows under a given column number.
*/
let maxWidth = 0;
for (let j = 0; j < numRows; j++) {
const cellValue = dataModel.data('column-header', j, i);

// Basic CellConfig object to get the renderer for that cell
let config = {
x: 0, y: 0, width: 0, height: 0,
region: 'column-header' as DataModel.CellRegion, row: 0, column: i,
value: (null as any), metadata: DataModel.emptyMetadata
};

// Get the renderer for the given cell
const renderer = this.cellRenderers.get(config) as TextRenderer;

// Use the canvas context to measure the cell's text width
const gc = this.canvasGC;
gc.font = CellRenderer.resolveOption(renderer.font, config);
const textWidth = gc.measureText(cellValue).width;

// Update the maximum width for that column.
maxWidth = Math.max(maxWidth, textWidth);
}

/*
Send a resize message with new width for the given column.
Using a padding of 15 pixels to leave some room.
*/
this.resizeColumn('body', i, maxWidth + 15);
}
}

/**
* Paint the overlay content for the entire grid.
*
Expand Down