Debian Codegraph @

js/plugin.ts

raw
1 // Copyright (c) NumFOCUS.
2 // Distributed under the terms of the Modified BSD License.
3
4 import { Application, IPlugin } from '@lumino/application';
5
6 import { Widget } from '@lumino/widgets';
7
8 import { IJupyterWidgetRegistry, WidgetView } from '@jupyter-widgets/base';
9
10 import { IThemeManager } from '@jupyterlab/apputils';
11
12 import * as widgetExports from './datagrid';
13
14 import { MODULE_NAME, MODULE_VERSION } from './version';
15
16 const EXTENSION_ID = 'ipydatagrid:plugin';
17
18 /**
19 * The datagrid plugin.
20 */
21 const datagridPlugin: IPlugin<Application<Widget>, void> = {
22 id: EXTENSION_ID,
23 requires: [IJupyterWidgetRegistry],
24 // eslint-disable-next-line @typescript-eslint/no-explicit-any
25 optional: [IThemeManager as any],
26 activate: activateWidgetExtension,
27 autoStart: true,
28 };
29
30 export default datagridPlugin;
31
32 /**
33 * Activate the widget extension.
34 */
35 function activateWidgetExtension(
36 app: Application<Widget>,
37 registry: IJupyterWidgetRegistry,
38 themeManager: IThemeManager | null,
39 ): void {
40 // Exporting a patched DataGridView widget which handles dynamic theme changes
41 class DataGridView extends widgetExports.DataGridView {
42 initialize(parameters: WidgetView.IInitializeParameters) {
43 if (themeManager?.theme != null) {
44 this.isLightTheme = themeManager.isLight(themeManager.theme);
45 }
46 super.initialize(parameters);
47 }
48
49 render() {
50 return super.render().then(() => {
51 if (themeManager) {
52 themeManager.themeChanged.connect(this.onThemeChanged, this);
53 }
54 });
55 }
56
57 private onThemeChanged() {
58 if (themeManager?.theme != null) {
59 this.isLightTheme = themeManager.isLight(themeManager.theme);
60 }
61 this.updateGridStyle();
62 this.default_renderer.onThemeChanged();
63
64 for (const key in this.renderers) {
65 this.renderers[key].onThemeChanged();
66 }
67 }
68
69 remove() {
70 if (themeManager) {
71 themeManager.themeChanged.disconnect(this.onThemeChanged, this);
72 }
73 return super.remove();
74 }
75 }
76
77 registry.registerWidget({
78 name: MODULE_NAME,
79 version: MODULE_VERSION,
80 exports: {
81 ...widgetExports,
82 DataGridView,
83 },
84 });
85 }