/
grid.ts
450 lines (405 loc) · 11.1 KB
/
grid.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { IThemeManager } from '@jupyterlab/apputils';
import { CommandRegistry } from '@lumino/commands';
import {
BasicKeyHandler,
BasicMouseHandler,
BasicSelectionModel,
DataGrid,
DataModel,
TextRenderer
} from '@lumino/datagrid';
import { ISignal, Signal } from '@lumino/signaling';
import { Panel } from '@lumino/widgets';
import { Debugger } from '../../';
import { IDebugger } from '../../tokens';
import { VariablesModel } from './model';
/**
* A data grid that displays variables in a debugger session.
*/
export class VariablesBodyGrid extends Panel {
/**
* Instantiate a new VariablesBodyGrid.
*
* @param options The instantiation options for a VariablesBodyGrid.
*/
constructor(options: VariablesBodyGrid.IOptions) {
super();
const { model, commands, themeManager, scopes } = options;
this._grid = new Grid({ commands, themeManager });
this._grid.addClass('jp-DebuggerVariables-grid');
this._model = model;
this._model.changed.connect((model: VariablesModel): void => {
this._update();
}, this);
this._grid.dataModel.setData(scopes ?? []);
this.addWidget(this._grid);
this.addClass('jp-DebuggerVariables-body');
}
/**
* Set the variable filter list.
*
* @param filter The variable filter to apply.
*/
set filter(filter: Set<string>) {
(this._grid.dataModel as GridModel).filter = filter;
this._update();
}
/**
* Set the current scope.
*
* @param scope The current scope for the variables.
*/
set scope(scope: string) {
(this._grid.dataModel as GridModel).scope = scope;
this._update();
}
/**
* Update the underlying data model
*/
private _update(): void {
this._grid.dataModel.setData(this._model.scopes ?? []);
}
private _grid: Grid;
private _model: IDebugger.Model.IVariables;
}
/**
* A namespace for `VariablesBodyGrid` statics.
*/
export namespace VariablesBodyGrid {
/**
* Instantiation options for `VariablesBodyGrid`.
*/
export interface IOptions {
/**
* The variables model.
*/
model: IDebugger.Model.IVariables;
/**
* The commands registry.
*/
commands: CommandRegistry;
/**
* The optional initial scopes data.
*/
scopes?: IDebugger.IScope[];
/**
* An optional application theme manager to detect theme changes.
*/
themeManager?: IThemeManager | null;
}
}
/**
* A class wrapping the underlying variables datagrid.
*/
class Grid extends Panel {
/**
* Instantiate a new VariablesGrid.
*
* @param options The instantiation options for a VariablesGrid.
*/
constructor(options: Grid.IOptions) {
super();
const { commands, themeManager } = options;
const dataModel = new GridModel();
const grid = new DataGrid();
const mouseHandler = new Private.MouseHandler();
mouseHandler.doubleClicked.connect((_, hit) =>
commands.execute(Debugger.CommandIDs.inspectVariable, {
variableReference: dataModel.getVariableReference(hit.row),
title: dataModel.getVariableName(hit.row)
})
);
grid.dataModel = dataModel;
grid.keyHandler = new BasicKeyHandler();
grid.mouseHandler = mouseHandler;
grid.selectionModel = new BasicSelectionModel({
dataModel
});
grid.stretchLastColumn = true;
grid.node.style.height = '100%';
this._grid = grid;
// Compute the grid's styles based on the current theme.
if (themeManager) {
themeManager.themeChanged.connect(this._updateStyles, this);
}
this.addWidget(grid);
}
/**
* Set the variable filter list.
*
* @param filter The variable filter to apply.
*/
set filter(filter: Set<string>) {
(this._grid.dataModel as GridModel).filter = filter;
this.update();
}
/**
* Set the scope for the variables data model.
*
* @param scope The scopes for the variables
*/
set scope(scope: string) {
(this._grid.dataModel as GridModel).scope = scope;
this.update();
}
/**
* Get the data model for the data grid.
*/
get dataModel(): GridModel {
return this._grid.dataModel as GridModel;
}
/**
* Handle `after-attach` messages.
*
* @param message - The `after-attach` message.
*/
protected onAfterAttach(message: any): void {
super.onAfterAttach(message);
this._updateStyles();
}
/**
* Update the computed style for the data grid on theme change.
*/
private _updateStyles(): void {
const { style, textRenderer } = Private.computeStyle();
this._grid.cellRenderers.update({}, textRenderer);
this._grid.style = style;
}
private _grid: DataGrid;
}
/**
* A namespace for VariablesGrid `statics`.
*/
namespace Grid {
/**
* Instantiation options for `VariablesGrid`.
*/
export interface IOptions {
/**
* The commands registry.
*/
commands: CommandRegistry;
/**
* An optional application theme manager to detect theme changes.
*/
themeManager?: IThemeManager | null;
}
}
/**
* A data grid model for variables.
*/
class GridModel extends DataModel {
/**
* Set the variable filter list.
*/
set filter(filter: Set<string>) {
this._filter = filter;
}
/**
* Get the current scope for the variables.
*/
get scope(): string {
return this._scope;
}
/**
* Set the variable scope
*/
set scope(scope: string) {
this._scope = scope;
}
/**
* Get the row count for a particular region in the data grid.
*
* @param region The datagrid region.
*/
rowCount(region: DataModel.RowRegion): number {
return region === 'body' ? this._data.name.length : 1;
}
/**
* Get the column count for a particular region in the data grid.
*
* @param region The datagrid region.
*/
columnCount(region: DataModel.ColumnRegion): number {
return region === 'body' ? 2 : 1;
}
/**
* Get the data count for a particular region, row and column in the data grid.
*
* @param region The datagrid region.
* @param row The datagrid row
* @param column The datagrid column
*/
data(region: DataModel.CellRegion, row: number, column: number): any {
if (region === 'row-header') {
return this._data.name[row];
}
if (region === 'column-header') {
return column === 1 ? 'Value' : 'Type';
}
if (region === 'corner-header') {
return 'Name';
}
return column === 1 ? this._data.value[row] : this._data.type[row];
}
/**
* Get the variable reference for a given row
*
* @param row The row in the datagrid.
*/
getVariableReference(row: number): number {
return this._data.variablesReference[row];
}
/**
* Get the variable name for a given row
*
* @param row The row in the datagrid.
*/
getVariableName(row: number): string {
return this._data.name[row];
}
/**
* Set the datagrid model data from the list of variables.
*
* @param variables The list of variables.
*/
setData(scopes: IDebugger.IScope[]): void {
this._clearData();
this.emitChanged({
type: 'model-reset',
region: 'body'
});
const scope = scopes.find(scope => scope.name === this._scope) ?? scopes[0];
const variables = scope?.variables ?? [];
const filtered = variables.filter(
variable => variable.name && !this._filter.has(variable.name)
);
filtered.forEach((variable, index) => {
this._data.name[index] = variable.name;
this._data.type[index] = variable.type ?? '';
this._data.value[index] = variable.value;
this._data.variablesReference[index] = variable.variablesReference;
});
this.emitChanged({
type: 'rows-inserted',
region: 'body',
index: 1,
span: filtered.length
});
}
/**
* Clear all the data in the datagrid.
*/
private _clearData(): void {
this._data = {
name: [],
type: [],
value: [],
variablesReference: []
};
}
private _filter = new Set<string>();
private _scope = '';
private _data: {
name: string[];
type: string[];
value: string[];
variablesReference: number[];
} = {
name: [],
type: [],
value: [],
variablesReference: []
};
}
/**
* A namespace for private data.
*/
namespace Private {
/**
* Create a color palette element.
*/
function createPalette(): HTMLDivElement {
const div = document.createElement('div');
div.className = 'jp-DebuggerVariables-colorPalette';
div.innerHTML = `
<div class="jp-mod-void"></div>
<div class="jp-mod-background"></div>
<div class="jp-mod-header-background"></div>
<div class="jp-mod-grid-line"></div>
<div class="jp-mod-header-grid-line"></div>
<div class="jp-mod-selection"></div>
<div class="jp-mod-text"></div>
`;
return div;
}
/**
* Compute the style and renderer for a data grid.
*/
export function computeStyle(): {
style: DataGrid.Style;
textRenderer: TextRenderer;
} {
const palette = createPalette();
document.body.appendChild(palette);
let node: HTMLDivElement | null;
node = palette.querySelector('.jp-mod-void');
const voidColor = getComputedStyle(node!).color;
node = palette.querySelector('.jp-mod-background');
const backgroundColor = getComputedStyle(node!).color;
node = palette.querySelector('.jp-mod-header-background');
const headerBackgroundColor = getComputedStyle(node!).color;
node = palette.querySelector('.jp-mod-grid-line');
const gridLineColor = getComputedStyle(node!).color;
node = palette.querySelector('.jp-mod-header-grid-line');
const headerGridLineColor = getComputedStyle(node!).color;
node = palette.querySelector('.jp-mod-selection');
const selectionFillColor = getComputedStyle(node!).color;
node = palette.querySelector('.jp-mod-text');
const textColor = getComputedStyle(node!).color;
document.body.removeChild(palette);
return {
style: {
voidColor,
backgroundColor,
headerBackgroundColor,
gridLineColor,
headerGridLineColor,
rowBackgroundColor: (i: number): string =>
i % 2 === 0 ? voidColor : backgroundColor,
selectionFillColor
},
textRenderer: new TextRenderer({
font: '12px sans-serif',
textColor,
backgroundColor: '',
verticalAlignment: 'center',
horizontalAlignment: 'left'
})
};
}
/**
* A custom click handler to handle clicks on the variables grid.
*/
export class MouseHandler extends BasicMouseHandler {
/**
* A signal emitted when the variables grid is double clicked.
*/
get doubleClicked(): ISignal<this, DataGrid.HitTestResult> {
return this._doubleClicked;
}
/**
* Handle a mouse double-click event.
*
* @param grid The datagrid clicked.
* @param event The mouse event.
*/
onMouseDoubleClick(grid: DataGrid, event: MouseEvent): void {
const hit = grid.hitTest(event.clientX, event.clientY);
this._doubleClicked.emit(hit);
}
private _doubleClicked = new Signal<this, DataGrid.HitTestResult>(this);
}
}