UNPKG

6.7 kBJavaScriptView Raw
1// Copyright (c) Jupyter Development Team.
2// Distributed under the terms of the Modified BSD License.
3import { nullTranslator } from '@jupyterlab/translation';
4import { circleEmptyIcon, circleIcon, LabIcon, offlineBoltIcon, ReactWidget, refreshIcon, stopIcon, ToolbarButton, ToolbarButtonComponent, UseSignal } from '@jupyterlab/ui-components';
5import { Widget } from '@lumino/widgets';
6import * as React from 'react';
7import { SessionContextDialogs } from '../sessioncontext';
8import { translateKernelStatuses } from '../kernelstatuses';
9/**
10 * The class name added to toolbar kernel name text.
11 */
12const TOOLBAR_KERNEL_NAME_CLASS = 'jp-Toolbar-kernelName';
13/**
14 * The class name added to toolbar kernel status icon.
15 */
16const TOOLBAR_KERNEL_STATUS_CLASS = 'jp-Toolbar-kernelStatus';
17/**
18 * The namespace for Toolbar class statics.
19 */
20export var Toolbar;
21(function (Toolbar) {
22 /**
23 * Create an interrupt toolbar item.
24 *
25 * @deprecated since version v3.2
26 * This is dead code now.
27 */
28 function createInterruptButton(sessionContext, translator) {
29 translator = translator || nullTranslator;
30 const trans = translator.load('jupyterlab');
31 return new ToolbarButton({
32 icon: stopIcon,
33 onClick: () => {
34 var _a, _b;
35 void ((_b = (_a = sessionContext.session) === null || _a === void 0 ? void 0 : _a.kernel) === null || _b === void 0 ? void 0 : _b.interrupt());
36 },
37 tooltip: trans.__('Interrupt the kernel')
38 });
39 }
40 Toolbar.createInterruptButton = createInterruptButton;
41 /**
42 * Create a restart toolbar item.
43 *
44 * @deprecated since v3.2
45 * This is dead code now.
46 */
47 function createRestartButton(sessionContext, dialogs, translator) {
48 translator = translator !== null && translator !== void 0 ? translator : nullTranslator;
49 const trans = translator.load('jupyterlab');
50 return new ToolbarButton({
51 icon: refreshIcon,
52 onClick: () => {
53 void (dialogs !== null && dialogs !== void 0 ? dialogs : new SessionContextDialogs({ translator })).restart(sessionContext);
54 },
55 tooltip: trans.__('Restart the kernel')
56 });
57 }
58 Toolbar.createRestartButton = createRestartButton;
59 /**
60 * Create a kernel name indicator item.
61 *
62 * #### Notes
63 * It will display the `'display_name`' of the session context. It can
64 * handle a change in context or kernel.
65 */
66 function createKernelNameItem(sessionContext, dialogs, translator) {
67 const el = ReactWidget.create(React.createElement(Private.KernelNameComponent, { sessionContext: sessionContext, dialogs: dialogs !== null && dialogs !== void 0 ? dialogs : new SessionContextDialogs({ translator }), translator: translator }));
68 el.addClass('jp-KernelName');
69 return el;
70 }
71 Toolbar.createKernelNameItem = createKernelNameItem;
72 /**
73 * Create a kernel status indicator item.
74 *
75 * @deprecated since v3.5
76 * The kernel status indicator is now replaced by the execution status indicator.
77 *
78 * #### Notes
79 * It will show a busy status if the kernel status is busy.
80 * It will show the current status in the node title.
81 * It can handle a change to the context or the kernel.
82 */
83 function createKernelStatusItem(sessionContext, translator) {
84 return new Private.KernelStatus(sessionContext, translator);
85 }
86 Toolbar.createKernelStatusItem = createKernelStatusItem;
87})(Toolbar || (Toolbar = {}));
88/**
89 * A namespace for private data.
90 */
91var Private;
92(function (Private) {
93 /**
94 * React component for a kernel name button.
95 *
96 * This wraps the ToolbarButtonComponent and watches the kernel
97 * session for changes.
98 */
99 function KernelNameComponent(props) {
100 const translator = props.translator || nullTranslator;
101 const trans = translator.load('jupyterlab');
102 const callback = () => {
103 void props.dialogs.selectKernel(props.sessionContext);
104 };
105 return (React.createElement(UseSignal, { signal: props.sessionContext.kernelChanged, initialSender: props.sessionContext }, sessionContext => (React.createElement(ToolbarButtonComponent, { className: TOOLBAR_KERNEL_NAME_CLASS, onClick: callback, tooltip: trans.__('Switch kernel'), label: sessionContext === null || sessionContext === void 0 ? void 0 : sessionContext.kernelDisplayName }))));
106 }
107 Private.KernelNameComponent = KernelNameComponent;
108 /**
109 * A toolbar item that displays kernel status.
110 *
111 * @deprecated This code is not use any longer and will be removed in JupyterLab 5
112 */
113 class KernelStatus extends Widget {
114 /**
115 * Construct a new kernel status widget.
116 */
117 constructor(sessionContext, translator) {
118 super();
119 this.translator = translator || nullTranslator;
120 this._trans = this.translator.load('jupyterlab');
121 this.addClass(TOOLBAR_KERNEL_STATUS_CLASS);
122 this._statusNames = translateKernelStatuses(this.translator);
123 this._onStatusChanged(sessionContext);
124 sessionContext.statusChanged.connect(this._onStatusChanged, this);
125 sessionContext.connectionStatusChanged.connect(this._onStatusChanged, this);
126 }
127 /**
128 * Handle a status on a kernel.
129 */
130 _onStatusChanged(sessionContext) {
131 if (this.isDisposed) {
132 return;
133 }
134 const status = sessionContext.kernelDisplayStatus;
135 const circleIconProps = {
136 container: this.node,
137 title: this._trans.__('Kernel %1', this._statusNames[status] || status),
138 stylesheet: 'toolbarButton',
139 alignSelf: 'normal',
140 height: '24px'
141 };
142 // set the icon
143 LabIcon.remove(this.node);
144 if (status === 'busy' ||
145 status === 'starting' ||
146 status === 'terminating' ||
147 status === 'restarting' ||
148 status === 'initializing') {
149 circleIcon.element(circleIconProps);
150 }
151 else if (status === 'connecting' ||
152 status === 'disconnected' ||
153 status === 'unknown') {
154 offlineBoltIcon.element(circleIconProps);
155 }
156 else {
157 circleEmptyIcon.element(circleIconProps);
158 }
159 }
160 }
161 Private.KernelStatus = KernelStatus;
162})(Private || (Private = {}));
163//# sourceMappingURL=widget.js.map
\No newline at end of file