Skip to content

Commit

Permalink
Introduce AsyncCellRenderer class
Browse files Browse the repository at this point in the history
  • Loading branch information
martinRenou committed Sep 6, 2023
1 parent 0c07cf6 commit 865eb72
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 6 deletions.
52 changes: 52 additions & 0 deletions packages/datagrid/src/asynccellrenderer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
/*-----------------------------------------------------------------------------
| Copyright (c) 2014-2023, Lumino Contributors
|
| Distributed under the terms of the BSD 3-Clause License.
|
| The full license is in the file LICENSE, distributed with this software.
|----------------------------------------------------------------------------*/

import { CellRenderer } from "./cellrenderer";
import { GraphicsContext } from "./graphicscontext";

/**
* An object which renders the cells of a data grid asynchronously.
*
* #### Notes
* For performance reason, the datagrid only paints cells synchronously,
* though if your cell renderer inherits from AsyncCellRenderer, you will
* be able to do some asynchronous work prior to painting the cell.
* See `ImageRenderer` for an example of an asynchronous renderer.
*/
export abstract class AsyncCellRenderer extends CellRenderer {

/**
* Whether the renderer is ready or not for that specific config.
* If it's not ready, the datagrid will paint the placeholder using `paintPlaceholder`.
* If it's ready, the datagrid will paint the cell synchronously using `paint`.
*
* @param config - The configuration data for the cell.
*
* @returns Whether the renderer is ready for this config or not.
*/
abstract isReady(config: CellRenderer.CellConfig): boolean;

/**
* Do any asynchronous work prior to painting this cell config.
*
* @param config - The configuration data for the cell.
*/
abstract load(config: CellRenderer.CellConfig): Promise<void>;

/**
* Paint the placeholder for a cell, waiting for the renderer to be ready.
*
* @param gc - The graphics context to use for drawing.
*
* @param config - The configuration data for the cell.
*/
abstract paintPlaceholder(gc: GraphicsContext, config: CellRenderer.CellConfig): void;

}
5 changes: 3 additions & 2 deletions packages/datagrid/src/datagrid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import {

import { GridLayout, ScrollBar, Widget } from '@lumino/widgets';

import { AsyncCellRenderer } from './asynccellrenderer';

import { CellRenderer } from './cellrenderer';

import { DataModel, MutableDataModel } from './datamodel';
Expand All @@ -40,7 +42,6 @@ import {
} from './celleditorcontroller';

import { TextRenderer } from './textrenderer';
import { ImageRenderer } from './imagerenderer';

/**
* A widget which implements a high-performance tabular data grid.
Expand Down Expand Up @@ -5062,7 +5063,7 @@ export class DataGrid extends Widget {

// Paint the cell into the off-screen buffer.
try {
if (renderer instanceof ImageRenderer) {
if (renderer instanceof AsyncCellRenderer) {
if (renderer.isReady(config)) {
renderer.paint(gc, config);
} else {
Expand Down
7 changes: 3 additions & 4 deletions packages/datagrid/src/imagerenderer.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
/*-----------------------------------------------------------------------------
| Copyright (c) 2014-2019, PhosphorJS Contributors
| Copyright (c) 2014-2023, Lumino Contributors
|
| Distributed under the terms of the BSD 3-Clause License.
|
| The full license is in the file LICENSE, distributed with this software.
|----------------------------------------------------------------------------*/
import { PromiseDelegate } from '@lumino/coreutils';
import { AsyncCellRenderer } from './asynccellrenderer';
import { CellRenderer } from './cellrenderer';

import { GraphicsContext } from './graphicscontext';


export type SizingMode = 'fit' | 'fill' | 'original';

// TODO Inherit from AsyncCellRenderer?

/**
* A cell renderer which renders data values as images.
*/
export class ImageRenderer extends CellRenderer {
export class ImageRenderer extends AsyncCellRenderer {
/**
* Construct a new text renderer.
*
Expand Down
1 change: 1 addition & 0 deletions packages/datagrid/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
export * from './basickeyhandler';
export * from './basicmousehandler';
export * from './basicselectionmodel';
export * from './asynccellrenderer';
export * from './cellrenderer';
export * from './celleditor';
export * from './celleditorcontroller';
Expand Down

0 comments on commit 865eb72

Please sign in to comment.