UNPKG

6.29 kBTypeScriptView Raw
1// Copyright (c) Jupyter Development Team.
2// Distributed under the terms of the Modified BSD License.
3
4import {
5 ServiceManager,
6 Session,
7 SessionManager,
8 Terminal,
9 TerminalManager
10} from '@jupyterlab/services';
11import {
12 ITranslator,
13 nullTranslator,
14 TranslationBundle
15} from '@jupyterlab/translation';
16import {
17 kernelIcon,
18 terminalIcon,
19 VDomModel,
20 VDomRenderer
21} from '@jupyterlab/ui-components';
22import React, { KeyboardEvent } from 'react';
23import { GroupItem, TextItem } from '@jupyterlab/statusbar';
24
25/**
26 * Half spacing between subitems in a status item.
27 */
28const HALF_SPACING = 4;
29
30/**
31 * A pure functional component for rendering kernel and terminal sessions.
32 *
33 * @param props the props for the component.
34 *
35 * @returns a tsx component for the running sessions.
36 */
37function RunningSessionsComponent(
38 props: RunningSessionsComponent.IProps
39): React.ReactElement<RunningSessionsComponent.IProps> {
40 return (
41 <GroupItem
42 tabIndex={0}
43 spacing={HALF_SPACING}
44 onClick={props.handleClick}
45 onKeyDown={props.handleKeyDown}
46 >
47 <GroupItem spacing={HALF_SPACING}>
48 <TextItem source={props.terminals} />
49 <terminalIcon.react left={'1px'} top={'3px'} stylesheet={'statusBar'} />
50 </GroupItem>
51 <GroupItem spacing={HALF_SPACING}>
52 <TextItem source={props.sessions} />
53 <kernelIcon.react top={'2px'} stylesheet={'statusBar'} />
54 </GroupItem>
55 </GroupItem>
56 );
57}
58
59/**
60 * A namespace for RunningSessionsComponents statics.
61 */
62namespace RunningSessionsComponent {
63 /**
64 * The props for rendering the RunningSessionsComponent.
65 */
66 export interface IProps {
67 /**
68 * A key down handler for the component. By default this is used
69 * to activate the running sessions side panel.
70 */
71 handleKeyDown: (event: KeyboardEvent<HTMLImageElement>) => void;
72 /**
73 * A click handler for the component. By default this is used
74 * to activate the running sessions side panel.
75 */
76 handleClick: () => void;
77
78 /**
79 * The number of running kernel sessions.
80 */
81 sessions: number;
82
83 /**
84 * The number of active terminal sessions.
85 */
86 terminals: number;
87 }
88}
89
90/**
91 * A VDomRenderer for a RunningSessions status item.
92 */
93export class RunningSessions extends VDomRenderer<RunningSessions.Model> {
94 /**
95 * Create a new RunningSessions widget.
96 */
97 constructor(opts: RunningSessions.IOptions) {
98 super(new RunningSessions.Model());
99 this._serviceManager = opts.serviceManager;
100 this._handleClick = opts.onClick;
101 this._handleKeyDown = opts.onKeyDown;
102 this.translator = opts.translator || nullTranslator;
103 this._trans = this.translator.load('jupyterlab');
104
105 this._serviceManager.sessions.runningChanged.connect(
106 this._onSessionsRunningChanged,
107 this
108 );
109 this._serviceManager.terminals.runningChanged.connect(
110 this._onTerminalsRunningChanged,
111 this
112 );
113
114 this.addClass('jp-mod-highlighted');
115 }
116
117 /**
118 * Render the running sessions widget.
119 */
120 render(): JSX.Element | null {
121 if (!this.model) {
122 return null;
123 }
124 // TODO-TRANS: Should probably be handled differently.
125 // This is more localizable friendly: "Terminals: %1 | Kernels: %2"
126 this.title.caption = this._trans.__(
127 '%1 Terminals, %2 Kernel sessions',
128 this.model.terminals,
129 this.model!.sessions
130 );
131 return (
132 <RunningSessionsComponent
133 sessions={this.model.sessions}
134 terminals={this.model.terminals}
135 handleClick={this._handleClick}
136 handleKeyDown={this._handleKeyDown}
137 />
138 );
139 }
140
141 /**
142 * Dispose of the status item.
143 */
144 dispose(): void {
145 super.dispose();
146
147 this._serviceManager.sessions.runningChanged.disconnect(
148 this._onSessionsRunningChanged,
149 this
150 );
151 this._serviceManager.terminals.runningChanged.disconnect(
152 this._onTerminalsRunningChanged,
153 this
154 );
155 }
156
157 /**
158 * Set the number of kernel sessions when the list changes.
159 */
160 private _onSessionsRunningChanged(
161 manager: SessionManager,
162 sessions: Session.IModel[]
163 ): void {
164 this.model!.sessions = sessions.length;
165 }
166
167 /**
168 * Set the number of terminal sessions when the list changes.
169 */
170 private _onTerminalsRunningChanged(
171 manager: TerminalManager,
172 terminals: Terminal.IModel[]
173 ): void {
174 this.model!.terminals = terminals.length;
175 }
176
177 protected translator: ITranslator;
178 private _trans: TranslationBundle;
179 private _handleClick: () => void;
180 private _handleKeyDown: (event: KeyboardEvent<HTMLImageElement>) => void;
181 private _serviceManager: ServiceManager.IManager;
182}
183
184/**
185 * A namespace for RunningSessions statics.
186 */
187export namespace RunningSessions {
188 /**
189 * A VDomModel for the RunningSessions status item.
190 */
191 export class Model extends VDomModel {
192 /**
193 * The number of active kernel sessions.
194 */
195 get sessions(): number {
196 return this._sessions;
197 }
198 set sessions(sessions: number) {
199 const oldSessions = this._sessions;
200 this._sessions = sessions;
201
202 if (oldSessions !== this._sessions) {
203 this.stateChanged.emit(void 0);
204 }
205 }
206
207 /**
208 * The number of active terminal sessions.
209 */
210 get terminals(): number {
211 return this._terminals;
212 }
213 set terminals(terminals: number) {
214 const oldTerminals = this._terminals;
215 this._terminals = terminals;
216
217 if (oldTerminals !== this._terminals) {
218 this.stateChanged.emit(void 0);
219 }
220 }
221
222 private _terminals: number = 0;
223 private _sessions: number = 0;
224 }
225
226 /**
227 * Options for creating a RunningSessions item.
228 */
229 export interface IOptions {
230 /**
231 * The application service manager.
232 */
233 serviceManager: ServiceManager.IManager;
234
235 /**
236 * A click handler for the item. By default this is used
237 * to activate the running sessions side panel.
238 */
239 onClick: () => void;
240
241 /**
242 * A key down handler for the item. By default this is used
243 * to activate the running sessions side panel.
244 */
245 onKeyDown: (event: KeyboardEvent<HTMLImageElement>) => void;
246
247 /**
248 * The application language translator.
249 */
250 translator?: ITranslator;
251 }
252}