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

Datagrid: Add a public API to get the current viewport #695

Merged
merged 8 commits into from
Apr 16, 2024
Merged
Changes from 2 commits
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
70 changes: 70 additions & 0 deletions packages/datagrid/src/datagrid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2012,6 +2012,51 @@ export class DataGrid extends Widget {
}
}

/**
* Get the current viewport.
*
* @returns The current viewport as row/column coordinates.
* Returns undefined if the grid is not visible.
*/
get currentViewport(): DataGrid.IBodyRegion | undefined {
let width = this.viewport.node.offsetWidth;
let height = this.viewport.node.offsetHeight;

width = Math.round(width);
height = Math.round(height);

if (width <= 0 || height <= 0) {
return;
}

const contentW = this._columnSections.length - this.scrollX;
const contentH = this._rowSections.length - this.scrollY;

const contentX = this.headerWidth;
const contentY = this.headerHeight;

const x1 = contentX;
const y1 = contentY;
const x2 = Math.min(width - 1, contentX + contentW - 1);
const y2 = Math.min(height - 1, contentY + contentH - 1);

const firstRow = this._rowSections.indexOf(y1 - contentY + this.scrollY);
const firstColumn = this._columnSections.indexOf(
x1 - contentX + this.scrollX
);
const lastRow = this._rowSections.indexOf(y2 - contentY + this.scrollY);
const lastColumn = this._columnSections.indexOf(
x2 - contentX + this.scrollX
);

return {
firstRow,
firstColumn,
lastRow,
lastColumn
};
}

/**
* A message handler invoked on an `'activate-request'` message.
*/
Expand Down Expand Up @@ -6721,6 +6766,31 @@ export namespace DataGrid {
headers: 'none',
warningThreshold: 1e6
};

/**
*
martinRenou marked this conversation as resolved.
Show resolved Hide resolved
*/
export interface IBodyRegion {
/**
* First row of the region
*/
firstRow: number;

/**
* Last row of the region
*/
lastRow: number;

/**
* First column of the region
*/
firstColumn: number;

/**
* Last column of the region
*/
lastColumn: number;
}
}

/**
Expand Down
Loading