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 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
66 changes: 66 additions & 0 deletions packages/datagrid/src/datagrid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2012,6 +2012,47 @@ 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 r1 = this._rowSections.indexOf(y1 - contentY + this.scrollY);
const c1 = this._columnSections.indexOf(x1 - contentX + this.scrollX);
const r2 = this._rowSections.indexOf(y2 - contentY + this.scrollY);
const c2 = this._columnSections.indexOf(x2 - contentX + this.scrollX);

return {
r1,
r2,
c1,
c2
};
}

/**
* A message handler invoked on an `'activate-request'` message.
*/
Expand Down Expand Up @@ -6721,6 +6762,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
*/
r1: number;
martinRenou marked this conversation as resolved.
Show resolved Hide resolved

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

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

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

/**
Expand Down
Loading