1 |
|
2 |
|
3 |
|
4 | import {
|
5 | ServiceManager,
|
6 | Session,
|
7 | SessionManager,
|
8 | Terminal,
|
9 | TerminalManager
|
10 | } from '@jupyterlab/services';
|
11 | import {
|
12 | ITranslator,
|
13 | nullTranslator,
|
14 | TranslationBundle
|
15 | } from '@jupyterlab/translation';
|
16 | import {
|
17 | kernelIcon,
|
18 | terminalIcon,
|
19 | VDomModel,
|
20 | VDomRenderer
|
21 | } from '@jupyterlab/ui-components';
|
22 | import React, { KeyboardEvent } from 'react';
|
23 | import { GroupItem, TextItem } from '@jupyterlab/statusbar';
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | const HALF_SPACING = 4;
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | function 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 |
|
61 |
|
62 | namespace RunningSessionsComponent {
|
63 | |
64 |
|
65 |
|
66 | export interface IProps {
|
67 | |
68 |
|
69 |
|
70 |
|
71 | handleKeyDown: (event: KeyboardEvent<HTMLImageElement>) => void;
|
72 | |
73 |
|
74 |
|
75 |
|
76 | handleClick: () => void;
|
77 |
|
78 | |
79 |
|
80 |
|
81 | sessions: number;
|
82 |
|
83 | |
84 |
|
85 |
|
86 | terminals: number;
|
87 | }
|
88 | }
|
89 |
|
90 |
|
91 |
|
92 |
|
93 | export class RunningSessions extends VDomRenderer<RunningSessions.Model> {
|
94 | |
95 |
|
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 |
|
119 |
|
120 | render(): JSX.Element | null {
|
121 | if (!this.model) {
|
122 | return null;
|
123 | }
|
124 |
|
125 |
|
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 |
|
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 |
|
159 |
|
160 | private _onSessionsRunningChanged(
|
161 | manager: SessionManager,
|
162 | sessions: Session.IModel[]
|
163 | ): void {
|
164 | this.model!.sessions = sessions.length;
|
165 | }
|
166 |
|
167 | |
168 |
|
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 |
|
186 |
|
187 | export namespace RunningSessions {
|
188 | |
189 |
|
190 |
|
191 | export class Model extends VDomModel {
|
192 | |
193 |
|
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 |
|
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 |
|
228 |
|
229 | export interface IOptions {
|
230 | |
231 |
|
232 |
|
233 | serviceManager: ServiceManager.IManager;
|
234 |
|
235 | |
236 |
|
237 |
|
238 |
|
239 | onClick: () => void;
|
240 |
|
241 | |
242 |
|
243 |
|
244 |
|
245 | onKeyDown: (event: KeyboardEvent<HTMLImageElement>) => void;
|
246 |
|
247 | |
248 |
|
249 |
|
250 | translator?: ITranslator;
|
251 | }
|
252 | }
|