UNPKG

5.63 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 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 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 * A namespace for RunningSessionsComponents statics.
56 */
57namespace RunningSessionsComponent {
58 /**
59 * The props for rendering the RunningSessionsComponent.
60 */
61 export interface IProps {
62 /**
63 * A click handler for the component. By default this is used
64 * to activate the running sessions side panel.
65 */
66 handleClick: () => void;
67
68 /**
69 * The number of running kernel sessions.
70 */
71 sessions: number;
72
73 /**
74 * The number of active terminal sessions.
75 */
76 terminals: number;
77 }
78}
79
80/**
81 * A VDomRenderer for a RunningSessions status item.
82 */
83export class RunningSessions extends VDomRenderer<RunningSessions.Model> {
84 /**
85 * Create a new RunningSessions widget.
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 * Render the running sessions widget.
108 */
109 render(): JSX.Element | null {
110 if (!this.model) {
111 return null;
112 }
113 // TODO-TRANS: Should probably be handled differently.
114 // This is more localizable friendly: "Terminals: %1 | Kernels: %2"
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 * Dispose of the status item.
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 * Set the number of kernel sessions when the list changes.
147 */
148 private _onSessionsRunningChanged(
149 manager: SessionManager,
150 sessions: Session.IModel[]
151 ): void {
152 this.model!.sessions = sessions.length;
153 }
154
155 /**
156 * Set the number of terminal sessions when the list changes.
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 * A namespace for RunningSessions statics.
173 */
174export namespace RunningSessions {
175 /**
176 * A VDomModel for the RunningSessions status item.
177 */
178 export class Model extends VDomModel {
179 /**
180 * The number of active kernel sessions.
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 * The number of active terminal sessions.
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 * Options for creating a RunningSessions item.
215 */
216 export interface IOptions {
217 /**
218 * The application service manager.
219 */
220 serviceManager: ServiceManager.IManager;
221
222 /**
223 * A click handler for the item. By default this is used
224 * to activate the running sessions side panel.
225 */
226 onClick: () => void;
227
228 /**
229 * The application language translator.
230 */
231 translator?: ITranslator;
232 }
233}