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 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 spacing={HALF_SPACING} onClick={props.handleClick}>
|
42 | <GroupItem spacing={HALF_SPACING}>
|
43 | <TextItem source={props.terminals} />
|
44 | <terminalIcon.react left={'1px'} top={'3px'} stylesheet={'statusBar'} />
|
45 | </GroupItem>
|
46 | <GroupItem spacing={HALF_SPACING}>
|
47 | <TextItem source={props.sessions} />
|
48 | <kernelIcon.react top={'2px'} stylesheet={'statusBar'} />
|
49 | </GroupItem>
|
50 | </GroupItem>
|
51 | );
|
52 | }
|
53 |
|
54 |
|
55 |
|
56 |
|
57 | namespace RunningSessionsComponent {
|
58 | |
59 |
|
60 |
|
61 | export interface IProps {
|
62 | |
63 |
|
64 |
|
65 |
|
66 | handleClick: () => void;
|
67 |
|
68 | |
69 |
|
70 |
|
71 | sessions: number;
|
72 |
|
73 | |
74 |
|
75 |
|
76 | terminals: number;
|
77 | }
|
78 | }
|
79 |
|
80 |
|
81 |
|
82 |
|
83 | export class RunningSessions extends VDomRenderer<RunningSessions.Model> {
|
84 | |
85 |
|
86 |
|
87 | constructor(opts: RunningSessions.IOptions) {
|
88 | super(new RunningSessions.Model());
|
89 | this._serviceManager = opts.serviceManager;
|
90 | this._handleClick = opts.onClick;
|
91 | this.translator = opts.translator || nullTranslator;
|
92 | this._trans = this.translator.load('jupyterlab');
|
93 |
|
94 | this._serviceManager.sessions.runningChanged.connect(
|
95 | this._onSessionsRunningChanged,
|
96 | this
|
97 | );
|
98 | this._serviceManager.terminals.runningChanged.connect(
|
99 | this._onTerminalsRunningChanged,
|
100 | this
|
101 | );
|
102 |
|
103 | this.addClass('jp-mod-highlighted');
|
104 | }
|
105 |
|
106 | |
107 |
|
108 |
|
109 | render(): JSX.Element | null {
|
110 | if (!this.model) {
|
111 | return null;
|
112 | }
|
113 |
|
114 |
|
115 | this.title.caption = this._trans.__(
|
116 | '%1 Terminals, %2 Kernel sessions',
|
117 | this.model.terminals,
|
118 | this.model!.sessions
|
119 | );
|
120 | return (
|
121 | <RunningSessionsComponent
|
122 | sessions={this.model.sessions}
|
123 | terminals={this.model.terminals}
|
124 | handleClick={this._handleClick}
|
125 | />
|
126 | );
|
127 | }
|
128 |
|
129 | |
130 |
|
131 |
|
132 | dispose(): void {
|
133 | super.dispose();
|
134 |
|
135 | this._serviceManager.sessions.runningChanged.disconnect(
|
136 | this._onSessionsRunningChanged,
|
137 | this
|
138 | );
|
139 | this._serviceManager.terminals.runningChanged.disconnect(
|
140 | this._onTerminalsRunningChanged,
|
141 | this
|
142 | );
|
143 | }
|
144 |
|
145 | |
146 |
|
147 |
|
148 | private _onSessionsRunningChanged(
|
149 | manager: SessionManager,
|
150 | sessions: Session.IModel[]
|
151 | ): void {
|
152 | this.model!.sessions = sessions.length;
|
153 | }
|
154 |
|
155 | |
156 |
|
157 |
|
158 | private _onTerminalsRunningChanged(
|
159 | manager: TerminalManager,
|
160 | terminals: Terminal.IModel[]
|
161 | ): void {
|
162 | this.model!.terminals = terminals.length;
|
163 | }
|
164 |
|
165 | protected translator: ITranslator;
|
166 | private _trans: TranslationBundle;
|
167 | private _handleClick: () => void;
|
168 | private _serviceManager: ServiceManager.IManager;
|
169 | }
|
170 |
|
171 |
|
172 |
|
173 |
|
174 | export namespace RunningSessions {
|
175 | |
176 |
|
177 |
|
178 | export class Model extends VDomModel {
|
179 | |
180 |
|
181 |
|
182 | get sessions(): number {
|
183 | return this._sessions;
|
184 | }
|
185 | set sessions(sessions: number) {
|
186 | const oldSessions = this._sessions;
|
187 | this._sessions = sessions;
|
188 |
|
189 | if (oldSessions !== this._sessions) {
|
190 | this.stateChanged.emit(void 0);
|
191 | }
|
192 | }
|
193 |
|
194 | |
195 |
|
196 |
|
197 | get terminals(): number {
|
198 | return this._terminals;
|
199 | }
|
200 | set terminals(terminals: number) {
|
201 | const oldTerminals = this._terminals;
|
202 | this._terminals = terminals;
|
203 |
|
204 | if (oldTerminals !== this._terminals) {
|
205 | this.stateChanged.emit(void 0);
|
206 | }
|
207 | }
|
208 |
|
209 | private _terminals: number = 0;
|
210 | private _sessions: number = 0;
|
211 | }
|
212 |
|
213 | |
214 |
|
215 |
|
216 | export interface IOptions {
|
217 | |
218 |
|
219 |
|
220 | serviceManager: ServiceManager.IManager;
|
221 |
|
222 | |
223 |
|
224 |
|
225 |
|
226 | onClick: () => void;
|
227 |
|
228 | |
229 |
|
230 |
|
231 | translator?: ITranslator;
|
232 | }
|
233 | }
|