@jupyterlab/launcher 4.0.0-alpha.2 → 4.0.0-alpha.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/widget.tsx ADDED
@@ -0,0 +1,376 @@
1
+ // Copyright (c) Jupyter Development Team.
2
+ // Distributed under the terms of the Modified BSD License.
3
+
4
+ import { showErrorMessage } from '@jupyterlab/apputils';
5
+ import {
6
+ ITranslator,
7
+ nullTranslator,
8
+ TranslationBundle
9
+ } from '@jupyterlab/translation';
10
+ import {
11
+ classes,
12
+ LabIcon,
13
+ VDomModel,
14
+ VDomRenderer
15
+ } from '@jupyterlab/ui-components';
16
+ import { ArrayExt, map } from '@lumino/algorithm';
17
+ import { CommandRegistry } from '@lumino/commands';
18
+ import { DisposableDelegate, IDisposable } from '@lumino/disposable';
19
+ import { AttachedProperty } from '@lumino/properties';
20
+ import { Widget } from '@lumino/widgets';
21
+ import * as React from 'react';
22
+ import { ILauncher } from './tokens';
23
+
24
+ /**
25
+ * The class name added to Launcher instances.
26
+ */
27
+ const LAUNCHER_CLASS = 'jp-Launcher';
28
+
29
+ /**
30
+ * LauncherModel keeps track of the path to working directory and has a list of
31
+ * LauncherItems, which the Launcher will render.
32
+ */
33
+ export class LauncherModel extends VDomModel implements ILauncher.IModel {
34
+ /**
35
+ * Add a command item to the launcher, and trigger re-render event for parent
36
+ * widget.
37
+ *
38
+ * @param options - The specification options for a launcher item.
39
+ *
40
+ * @returns A disposable that will remove the item from Launcher, and trigger
41
+ * re-render event for parent widget.
42
+ *
43
+ */
44
+ add(options: ILauncher.IItemOptions): IDisposable {
45
+ // Create a copy of the options to circumvent mutations to the original.
46
+ const item = Private.createItem(options);
47
+
48
+ this.itemsList.push(item);
49
+ this.stateChanged.emit(void 0);
50
+
51
+ return new DisposableDelegate(() => {
52
+ ArrayExt.removeFirstOf(this.itemsList, item);
53
+ this.stateChanged.emit(void 0);
54
+ });
55
+ }
56
+
57
+ /**
58
+ * Return an iterator of launcher items.
59
+ */
60
+ items(): IterableIterator<ILauncher.IItemOptions> {
61
+ return this.itemsList[Symbol.iterator]();
62
+ }
63
+
64
+ protected itemsList: ILauncher.IItemOptions[] = [];
65
+ }
66
+
67
+ /**
68
+ * A virtual-DOM-based widget for the Launcher.
69
+ */
70
+ export class Launcher extends VDomRenderer<ILauncher.IModel> {
71
+ /**
72
+ * Construct a new launcher widget.
73
+ */
74
+ constructor(options: ILauncher.IOptions) {
75
+ super(options.model);
76
+ this._cwd = options.cwd;
77
+ this.translator = options.translator || nullTranslator;
78
+ this._trans = this.translator.load('jupyterlab');
79
+ this._callback = options.callback;
80
+ this._commands = options.commands;
81
+ this.addClass(LAUNCHER_CLASS);
82
+ }
83
+
84
+ /**
85
+ * The cwd of the launcher.
86
+ */
87
+ get cwd(): string {
88
+ return this._cwd;
89
+ }
90
+ set cwd(value: string) {
91
+ this._cwd = value;
92
+ this.update();
93
+ }
94
+
95
+ /**
96
+ * Whether there is a pending item being launched.
97
+ */
98
+ get pending(): boolean {
99
+ return this._pending;
100
+ }
101
+ set pending(value: boolean) {
102
+ this._pending = value;
103
+ }
104
+
105
+ /**
106
+ * Render the launcher to virtual DOM nodes.
107
+ */
108
+ protected render(): React.ReactElement<any> | null {
109
+ // Bail if there is no model.
110
+ if (!this.model) {
111
+ return null;
112
+ }
113
+
114
+ const knownCategories = [
115
+ this._trans.__('Notebook'),
116
+ this._trans.__('Console'),
117
+ this._trans.__('Other')
118
+ ];
119
+ const kernelCategories = [
120
+ this._trans.__('Notebook'),
121
+ this._trans.__('Console')
122
+ ];
123
+
124
+ // First group-by categories
125
+ const categories = Object.create(null);
126
+ for (const item of this.model.items()) {
127
+ const cat = item.category || this._trans.__('Other');
128
+ if (!(cat in categories)) {
129
+ categories[cat] = [];
130
+ }
131
+ categories[cat].push(item);
132
+ }
133
+ // Within each category sort by rank
134
+ for (const cat in categories) {
135
+ categories[cat] = categories[cat].sort(
136
+ (a: ILauncher.IItemOptions, b: ILauncher.IItemOptions) => {
137
+ return Private.sortCmp(a, b, this._cwd, this._commands);
138
+ }
139
+ );
140
+ }
141
+
142
+ // Variable to help create sections
143
+ const sections: React.ReactElement<any>[] = [];
144
+ let section: React.ReactElement<any>;
145
+
146
+ // Assemble the final ordered list of categories, beginning with
147
+ // KNOWN_CATEGORIES.
148
+ const orderedCategories: string[] = [];
149
+ for (const cat of knownCategories) {
150
+ orderedCategories.push(cat);
151
+ }
152
+ for (const cat in categories) {
153
+ if (knownCategories.indexOf(cat) === -1) {
154
+ orderedCategories.push(cat);
155
+ }
156
+ }
157
+
158
+ // Now create the sections for each category
159
+ orderedCategories.forEach(cat => {
160
+ if (!categories[cat]) {
161
+ return;
162
+ }
163
+ const item = categories[cat][0] as ILauncher.IItemOptions;
164
+ const args = { ...item.args, cwd: this.cwd };
165
+ const kernel = kernelCategories.indexOf(cat) > -1;
166
+ const iconClass = this._commands.iconClass(item.command, args);
167
+ const icon = this._commands.icon(item.command, args);
168
+
169
+ if (cat in categories) {
170
+ section = (
171
+ <div className="jp-Launcher-section" key={cat}>
172
+ <div className="jp-Launcher-sectionHeader">
173
+ <LabIcon.resolveReact
174
+ icon={icon}
175
+ iconClass={classes(iconClass, 'jp-Icon-cover')}
176
+ stylesheet="launcherSection"
177
+ />
178
+ <h2 className="jp-Launcher-sectionTitle">{cat}</h2>
179
+ </div>
180
+ <div className="jp-Launcher-cardContainer">
181
+ {Array.from(
182
+ map(categories[cat], (item: ILauncher.IItemOptions) => {
183
+ return Card(
184
+ kernel,
185
+ item,
186
+ this,
187
+ this._commands,
188
+ this._trans,
189
+ this._callback
190
+ );
191
+ })
192
+ )}
193
+ </div>
194
+ </div>
195
+ );
196
+ sections.push(section);
197
+ }
198
+ });
199
+
200
+ // Wrap the sections in body and content divs.
201
+ return (
202
+ <div className="jp-Launcher-body">
203
+ <div className="jp-Launcher-content">
204
+ <div className="jp-Launcher-cwd">
205
+ <h3>{this.cwd}</h3>
206
+ </div>
207
+ {sections}
208
+ </div>
209
+ </div>
210
+ );
211
+ }
212
+
213
+ protected translator: ITranslator;
214
+ private _trans: TranslationBundle;
215
+ private _commands: CommandRegistry;
216
+ private _callback: (widget: Widget) => void;
217
+ private _pending = false;
218
+ private _cwd = '';
219
+ }
220
+ /**
221
+ * A pure tsx component for a launcher card.
222
+ *
223
+ * @param kernel - whether the item takes uses a kernel.
224
+ *
225
+ * @param item - the launcher item to render.
226
+ *
227
+ * @param launcher - the Launcher instance to which this is added.
228
+ *
229
+ * @param commands - the command registry holding the command of item.
230
+ *
231
+ * @param trans - the translation bundle.
232
+ *
233
+ * @returns a vdom `VirtualElement` for the launcher card.
234
+ */
235
+ function Card(
236
+ kernel: boolean,
237
+ item: ILauncher.IItemOptions,
238
+ launcher: Launcher,
239
+ commands: CommandRegistry,
240
+ trans: TranslationBundle,
241
+ launcherCallback: (widget: Widget) => void
242
+ ): React.ReactElement<any> {
243
+ // Get some properties of the command
244
+ const command = item.command;
245
+ const args = { ...item.args, cwd: launcher.cwd };
246
+ const caption = commands.caption(command, args);
247
+ const label = commands.label(command, args);
248
+ const title = kernel ? label : caption || label;
249
+
250
+ // Build the onclick handler.
251
+ const onclick = () => {
252
+ // If an item has already been launched,
253
+ // don't try to launch another.
254
+ if (launcher.pending === true) {
255
+ return;
256
+ }
257
+ launcher.pending = true;
258
+ void commands
259
+ .execute(command, {
260
+ ...item.args,
261
+ cwd: launcher.cwd
262
+ })
263
+ .then(value => {
264
+ launcher.pending = false;
265
+ if (value instanceof Widget) {
266
+ launcherCallback(value);
267
+ }
268
+ })
269
+ .catch(err => {
270
+ console.error(err);
271
+ launcher.pending = false;
272
+ void showErrorMessage(trans._p('Error', 'Launcher Error'), err);
273
+ });
274
+ };
275
+
276
+ // With tabindex working, you can now pick a kernel by tabbing around and
277
+ // pressing Enter.
278
+ const onkeypress = (event: React.KeyboardEvent) => {
279
+ if (event.key === 'Enter') {
280
+ onclick();
281
+ }
282
+ };
283
+
284
+ const iconClass = commands.iconClass(command, args);
285
+ const icon = commands.icon(command, args);
286
+
287
+ // Return the VDOM element.
288
+ return (
289
+ <div
290
+ className="jp-LauncherCard"
291
+ title={title}
292
+ onClick={onclick}
293
+ onKeyPress={onkeypress}
294
+ tabIndex={0}
295
+ data-category={item.category || trans.__('Other')}
296
+ key={Private.keyProperty.get(item)}
297
+ >
298
+ <div className="jp-LauncherCard-icon">
299
+ {kernel ? (
300
+ item.kernelIconUrl ? (
301
+ <img src={item.kernelIconUrl} className="jp-Launcher-kernelIcon" />
302
+ ) : (
303
+ <div className="jp-LauncherCard-noKernelIcon">
304
+ {label[0].toUpperCase()}
305
+ </div>
306
+ )
307
+ ) : (
308
+ <LabIcon.resolveReact
309
+ icon={icon}
310
+ iconClass={classes(iconClass, 'jp-Icon-cover')}
311
+ stylesheet="launcherCard"
312
+ />
313
+ )}
314
+ </div>
315
+ <div className="jp-LauncherCard-label" title={title}>
316
+ <p>{label}</p>
317
+ </div>
318
+ </div>
319
+ );
320
+ }
321
+
322
+ /**
323
+ * The namespace for module private data.
324
+ */
325
+ namespace Private {
326
+ /**
327
+ * An incrementing counter for keys.
328
+ */
329
+ let id = 0;
330
+
331
+ /**
332
+ * An attached property for an item's key.
333
+ */
334
+ export const keyProperty = new AttachedProperty<
335
+ ILauncher.IItemOptions,
336
+ number
337
+ >({
338
+ name: 'key',
339
+ create: () => id++
340
+ });
341
+
342
+ /**
343
+ * Create a fully specified item given item options.
344
+ */
345
+ export function createItem(
346
+ options: ILauncher.IItemOptions
347
+ ): ILauncher.IItemOptions {
348
+ return {
349
+ ...options,
350
+ category: options.category || '',
351
+ rank: options.rank !== undefined ? options.rank : Infinity
352
+ };
353
+ }
354
+
355
+ /**
356
+ * A sort comparison function for a launcher item.
357
+ */
358
+ export function sortCmp(
359
+ a: ILauncher.IItemOptions,
360
+ b: ILauncher.IItemOptions,
361
+ cwd: string,
362
+ commands: CommandRegistry
363
+ ): number {
364
+ // First, compare by rank.
365
+ const r1 = a.rank;
366
+ const r2 = b.rank;
367
+ if (r1 !== r2 && r1 !== undefined && r2 !== undefined) {
368
+ return r1 < r2 ? -1 : 1; // Infinity safe
369
+ }
370
+
371
+ // Finally, compare by display name.
372
+ const aLabel = commands.label(a.command, { ...a.args, cwd });
373
+ const bLabel = commands.label(b.command, { ...b.args, cwd });
374
+ return aLabel.localeCompare(bLabel);
375
+ }
376
+ }
package/style/base.css CHANGED
@@ -6,13 +6,13 @@
6
6
  /* Private CSS variables */
7
7
 
8
8
  :root {
9
- --jp-private-launcher-top-padding: 16px;
10
- --jp-private-launcher-side-padding: 32px;
11
- --jp-private-launcher-card-size: 100px;
12
- --jp-private-launcher-card-label-height: 32px;
13
- --jp-private-launcher-card-icon-height: 68px;
14
- --jp-private-launcher-large-icon-size: 52px;
15
- --jp-private-launcher-small-icon-size: 32px;
9
+ --jp-private-launcher-top-padding: 1.231em;
10
+ --jp-private-launcher-side-padding: 2.462em;
11
+ --jp-private-launcher-card-size: 7.692em;
12
+ --jp-private-launcher-card-label-height: 2.462em;
13
+ --jp-private-launcher-card-icon-height: 5.231em;
14
+ --jp-private-launcher-large-icon-size: 4em;
15
+ --jp-private-launcher-small-icon-size: 2.462em;
16
16
  }
17
17
 
18
18
  /* Launcher */
@@ -25,6 +25,7 @@
25
25
  box-sizing: border-box;
26
26
  min-width: 120px;
27
27
  min-height: 120px;
28
+
28
29
  /* This is needed so that all font sizing of children done in ems is
29
30
  * relative to this base size */
30
31
  font-size: var(--jp-ui-font-size1);
@@ -43,7 +44,7 @@
43
44
  font-size: var(--jp-ui-font-size2);
44
45
  font-weight: normal;
45
46
  color: var(--jp-ui-font-color1);
46
- margin: 1em 0px;
47
+ margin: 1em 0;
47
48
  }
48
49
 
49
50
  .jp-Launcher-content {
@@ -68,17 +69,18 @@
68
69
  flex-direction: row;
69
70
  align-items: center;
70
71
  box-sizing: border-box;
72
+
71
73
  /* This is custom tuned to get the section header to align with the cards */
72
74
  margin-left: 5px;
73
75
  border-bottom: 1px solid var(--jp-border-color2);
74
- padding-bottom: 0px;
76
+ padding-bottom: 0;
75
77
  margin-bottom: 8px;
76
78
  }
77
79
 
78
80
  .jp-Launcher-sectionTitle {
79
81
  font-size: var(--jp-ui-font-size2);
80
82
  font-weight: normal;
81
- color: var(--jp-ui-font-color1);
83
+ color: var(--jp-ui-font-color0);
82
84
  box-sizing: border-box;
83
85
  }
84
86
 
@@ -100,7 +102,7 @@
100
102
  width: var(--jp-private-launcher-card-size);
101
103
  height: var(--jp-private-launcher-card-size);
102
104
  margin: 8px;
103
- padding: 0px;
105
+ padding: 0;
104
106
  border: 1px solid var(--jp-border-color2);
105
107
  background: var(--jp-layout-color0);
106
108
  box-shadow: var(--jp-elevation-z2);
@@ -111,6 +113,8 @@
111
113
  .jp-LauncherCard:focus:not(:active) {
112
114
  border: 1px solid var(--jp-brand-color1);
113
115
  box-shadow: var(--jp-elevation-z6);
116
+ outline: unset; /* if outline is not unset, then depending on which browser you use, the
117
+ border change is either hidden behind the outline or visually combined with it */
114
118
  }
115
119
 
116
120
  .jp-LauncherCard:hover {
@@ -126,8 +130,8 @@
126
130
  width: 100%;
127
131
  height: var(--jp-private-launcher-card-icon-height);
128
132
  box-sizing: border-box;
129
- margin: 0px;
130
- padding: 0px;
133
+ margin: 0;
134
+ padding: 0;
131
135
  display: flex;
132
136
  justify-content: center;
133
137
  align-items: center;
@@ -140,7 +144,7 @@
140
144
 
141
145
  .jp-LauncherCard[data-category='Notebook'] .jp-LauncherCard-noKernelIcon {
142
146
  /* This color is copied from the notebook icon. */
143
- color: #ef6c00;
147
+ color: var(--jp-jupyter-icon-color);
144
148
  }
145
149
 
146
150
  .jp-LauncherCard[data-category='Console'] .jp-LauncherCard-noKernelIcon {
@@ -151,19 +155,20 @@
151
155
  .jp-LauncherCard-label {
152
156
  width: 100%;
153
157
  height: var(--jp-private-launcher-card-label-height);
154
- padding: 0 4px 4px 4px;
158
+ padding: 0 4px 4px;
155
159
  box-sizing: border-box;
156
160
  overflow: hidden;
157
161
  }
158
162
 
159
163
  .jp-LauncherCard-label p {
160
- height: 28px;
164
+ height: 2.154em;
161
165
  word-break: break-word;
162
166
  text-align: center;
163
167
  color: var(--jp-ui-font-color1);
164
- line-height: 14px;
165
- font-size: 12px;
168
+ line-height: 1.077em;
169
+ font-size: calc(var(--jp-ui-font-size1) * 0.923);
166
170
  overflow: hidden;
171
+ margin: auto;
167
172
  }
168
173
 
169
174
  /* kernel icons */
@@ -171,6 +176,9 @@
171
176
  .jp-Launcher-kernelIcon {
172
177
  width: var(--jp-private-launcher-large-icon-size);
173
178
  height: var(--jp-private-launcher-large-icon-size);
174
- margin: 0px;
175
- padding: 0px;
179
+ margin: 0;
180
+ padding: 0;
181
+
182
+ /* Preserve image aspect ratio */
183
+ object-fit: contain;
176
184
  }