UNPKG

6.61 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 class KernelStatus extends Widget {
112 /**
113 * Construct a new kernel status widget.
114 */
115 constructor(sessionContext, translator) {
116 super();
117 this.translator = translator || nullTranslator;
118 this._trans = this.translator.load('jupyterlab');
119 this.addClass(TOOLBAR_KERNEL_STATUS_CLASS);
120 this._statusNames = translateKernelStatuses(this.translator);
121 this._onStatusChanged(sessionContext);
122 sessionContext.statusChanged.connect(this._onStatusChanged, this);
123 sessionContext.connectionStatusChanged.connect(this._onStatusChanged, this);
124 }
125 /**
126 * Handle a status on a kernel.
127 */
128 _onStatusChanged(sessionContext) {
129 if (this.isDisposed) {
130 return;
131 }
132 const status = sessionContext.kernelDisplayStatus;
133 const circleIconProps = {
134 container: this.node,
135 title: this._trans.__('Kernel %1', this._statusNames[status] || status),
136 stylesheet: 'toolbarButton',
137 alignSelf: 'normal',
138 height: '24px'
139 };
140 // set the icon
141 LabIcon.remove(this.node);
142 if (status === 'busy' ||
143 status === 'starting' ||
144 status === 'terminating' ||
145 status === 'restarting' ||
146 status === 'initializing') {
147 circleIcon.element(circleIconProps);
148 }
149 else if (status === 'connecting' ||
150 status === 'disconnected' ||
151 status === 'unknown') {
152 offlineBoltIcon.element(circleIconProps);
153 }
154 else {
155 circleEmptyIcon.element(circleIconProps);
156 }
157 }
158 }
159 Private.KernelStatus = KernelStatus;
160})(Private || (Private = {}));
161//# sourceMappingURL=widget.js.map
\No newline at end of file