@jupyterlab/application 4.7.0-beta.0 → 4.7.0-beta.2

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.
@@ -484,6 +484,7 @@ export class LayoutRestorer implements ILayoutRestorer {
484
484
  [id: string]: {
485
485
  sizes: number[] | null;
486
486
  expansionStates: boolean[] | null;
487
+ widgetIds?: string[];
487
488
  };
488
489
  };
489
490
  }
@@ -531,6 +532,7 @@ export class LayoutRestorer implements ILayoutRestorer {
531
532
  [id: string]: {
532
533
  sizes: number[] | null;
533
534
  expansionStates: boolean[] | null;
535
+ widgetIds?: string[];
534
536
  };
535
537
  };
536
538
  return {
package/src/shell.ts CHANGED
@@ -8,6 +8,7 @@ import type { ITranslator } from '@jupyterlab/translation';
8
8
  import { nullTranslator } from '@jupyterlab/translation';
9
9
  import type { SidePanel } from '@jupyterlab/ui-components';
10
10
  import {
11
+ AccordionPanelReorderer,
11
12
  classes,
12
13
  LabIcon,
13
14
  TabBarSvg,
@@ -340,6 +341,12 @@ export namespace ILabShell {
340
341
  * Expansion states of the widgets.
341
342
  */
342
343
  readonly expansionStates: Array<boolean> | null;
344
+
345
+ /**
346
+ * Section IDs in display order, when available. A section whose ID
347
+ * Lumino generated is saved as `label:` followed by its title label.
348
+ */
349
+ readonly widgetIds?: string[];
343
350
  };
344
351
  };
345
352
  }
@@ -913,6 +920,26 @@ export class LabShell extends Widget implements JupyterFrontEnd.IShell {
913
920
  throw new Error(`Invalid area: ${area}`);
914
921
  }
915
922
  }
923
+
924
+ /**
925
+ * Activate a tab in the active tab bar.
926
+ *
927
+ * @param index - The zero-based tab index. A negative index counts from
928
+ * the end, so -1 is the last tab.
929
+ */
930
+ activateTab(index: number): void {
931
+ const current = this._currentTabBar();
932
+ if (!current) {
933
+ return;
934
+ }
935
+
936
+ const { titles } = current;
937
+ const title = titles[index < 0 ? titles.length + index : index];
938
+ if (title) {
939
+ this._dockPanel.activateWidget(title.owner);
940
+ }
941
+ }
942
+
916
943
  /**
917
944
  * Activate the next Tab in the active TabBar.
918
945
  */
@@ -2428,6 +2455,12 @@ namespace Private {
2428
2455
  widget.content?.expansionToggled?.connect(this._onExpansionToggle, this);
2429
2456
  // @ts-expect-error sometimes widget is a SidePanel
2430
2457
  widget.content?.handleMoved?.connect(this._onHandleMoved, this);
2458
+ if ('content' in widget && widget.content instanceof AccordionPanel) {
2459
+ AccordionPanelReorderer.find(widget.content.node)?.reordered.connect(
2460
+ this._onHandleMoved,
2461
+ this
2462
+ );
2463
+ }
2431
2464
  this._refreshVisibility();
2432
2465
  }
2433
2466
 
@@ -2441,17 +2474,17 @@ namespace Private {
2441
2474
  : this._isCollapsedByUser;
2442
2475
  const widgets = Array.from(this._stackedPanel.widgets);
2443
2476
  const currentWidget = widgets[this._sideBar.currentIndex];
2444
- const widgetStates: {
2445
- [id: string]: {
2446
- sizes: number[] | null;
2447
- expansionStates: boolean[] | null;
2448
- };
2449
- } = {};
2477
+ const widgetStates: ILabShell.ISideArea['widgetStates'] = {};
2450
2478
  this._stackedPanel.widgets.forEach((w: SidePanel) => {
2451
2479
  if (w.id && w.content instanceof SplitPanel) {
2480
+ const widgetIds = w.content.widgets.map(wi => Private.sectionKey(wi));
2452
2481
  widgetStates[w.id] = {
2453
2482
  sizes: w.content.relativeSizes() as number[],
2454
- expansionStates: w.content.widgets.map(wi => !wi.isHidden)
2483
+ expansionStates: w.content.widgets.map(wi => !wi.isHidden),
2484
+ ...(widgetIds.every(id => id.length > 0) &&
2485
+ new Set(widgetIds).size === widgetIds.length
2486
+ ? { widgetIds }
2487
+ : {})
2455
2488
  };
2456
2489
  }
2457
2490
  });
@@ -2515,8 +2548,49 @@ namespace Private {
2515
2548
  this._stackedPanel.widgets.forEach((w: SidePanel) => {
2516
2549
  if (w.id && w.content instanceof SplitPanel) {
2517
2550
  const state = data.widgetStates[w.id] ?? {};
2551
+ const widgets = new Map(
2552
+ w.content.widgets
2553
+ .filter(wi => wi.id)
2554
+ .map(wi => [Private.sectionKey(wi), wi])
2555
+ );
2556
+ // Older extensions may recreate sections with generated IDs.
2557
+ const widgetIds = state.widgetIds?.some(id => widgets.has(id))
2558
+ ? state.widgetIds
2559
+ : undefined;
2560
+ if (widgetIds && w.content instanceof AccordionPanel) {
2561
+ const ordered = widgetIds
2562
+ .map(id => widgets.get(id))
2563
+ .filter((wi): wi is Widget => wi !== undefined);
2564
+ const savedWidgets = new Set(ordered);
2565
+ let savedIndex = 0;
2566
+ const targetWidgets = w.content.widgets.map(wi =>
2567
+ savedWidgets.has(wi) ? ordered[savedIndex++] : wi
2568
+ );
2569
+ for (const [index, wi] of targetWidgets.entries()) {
2570
+ if (w.content.widgets[index] === wi) {
2571
+ continue;
2572
+ }
2573
+ w.content.insertWidget(index, wi);
2574
+ // Lumino only repositions the section, so move its title, body
2575
+ // and handle to keep the Tab order the same as the visual order.
2576
+ const next = w.content.titles[index + 1] ?? null;
2577
+ for (const node of [
2578
+ w.content.titles[index],
2579
+ wi.node,
2580
+ w.content.handles[index]
2581
+ ]) {
2582
+ w.content.node.insertBefore(node, next);
2583
+ }
2584
+ }
2585
+ }
2586
+ const sizes = w.content.relativeSizes();
2518
2587
  w.content.widgets.forEach((wi, widx) => {
2519
- const expansion = (state.expansionStates ?? [])[widx];
2588
+ const savedIndex = widgetIds
2589
+ ? wi.id
2590
+ ? widgetIds.indexOf(Private.sectionKey(wi))
2591
+ : -1
2592
+ : widx;
2593
+ const expansion = state.expansionStates?.[savedIndex];
2520
2594
  if (
2521
2595
  typeof expansion === 'boolean' &&
2522
2596
  w.content instanceof AccordionPanel
@@ -2527,9 +2601,13 @@ namespace Private {
2527
2601
  w.content.collapse(widx);
2528
2602
  }
2529
2603
  }
2604
+ const size = state.sizes?.[savedIndex];
2605
+ if (typeof size === 'number') {
2606
+ sizes[widx] = size;
2607
+ }
2530
2608
  });
2531
2609
  if (state.sizes) {
2532
- w.content.setRelativeSizes(state.sizes);
2610
+ w.content.setRelativeSizes(sizes);
2533
2611
  }
2534
2612
  }
2535
2613
  });
@@ -2868,6 +2946,19 @@ namespace Private {
2868
2946
  private _selected: boolean = false;
2869
2947
  }
2870
2948
 
2949
+ /**
2950
+ * Key of a sidebar section in the saved layout: its ID, or its title label
2951
+ * when Lumino generated the ID, because a generated ID changes in every session.
2952
+ */
2953
+ export function sectionKey(widget: Widget): string {
2954
+ return GENERATED_ID.test(widget.id)
2955
+ ? `label:${widget.title.label}`
2956
+ : widget.id;
2957
+ }
2958
+
2959
+ const GENERATED_ID =
2960
+ /^id-[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
2961
+
2871
2962
  export class RestorableSplitPanel extends SplitPanel {
2872
2963
  /**
2873
2964
  * Construct a new RestorableSplitPanel.
package/src/utils.ts CHANGED
@@ -3,10 +3,12 @@
3
3
  * Distributed under the terms of the Modified BSD License.
4
4
  */
5
5
 
6
- import { SemanticCommand } from '@jupyterlab/apputils';
6
+ import { SemanticCommand, ToolbarRegistry } from '@jupyterlab/apputils';
7
7
  import type { TranslationBundle } from '@jupyterlab/translation';
8
8
  import { nullTranslator } from '@jupyterlab/translation';
9
9
  import type { CommandRegistry } from '@lumino/commands';
10
+ import type { ReadonlyPartialJSONObject } from '@lumino/coreutils';
11
+ import type { Widget } from '@lumino/widgets';
10
12
  import type { JupyterFrontEnd } from './frontend';
11
13
 
12
14
  export interface ISemanticCommandDefault {
@@ -166,26 +168,38 @@ export function createSemanticCommand(
166
168
  return {
167
169
  label: concatenateTexts('label'),
168
170
  caption: concatenateTexts('caption'),
169
- isEnabled: () => {
170
- const isEnabled = reduceAttribute('isEnabled');
171
+ isEnabled: args => {
172
+ const isEnabled = reduceAttribute('isEnabled', args);
171
173
  return isEnabled.length > 0
172
174
  ? !isEnabled.some(enabled => enabled === false)
173
175
  : (defaultValues.isEnabled ?? false);
174
176
  },
175
- isToggled: () => {
176
- const isToggled = reduceAttribute('isToggled');
177
+ isToggled: args => {
178
+ const isToggled = reduceAttribute('isToggled', args);
177
179
  return isToggled.length > 0
178
180
  ? isToggled.some(enabled => enabled === true)
179
181
  : (defaultValues.isToggled ?? false);
180
182
  },
181
- isVisible: () => {
182
- const isVisible = reduceAttribute('isVisible');
183
+ isVisible: args => {
184
+ let isVisible: unknown[];
185
+ if (
186
+ args?.[SemanticCommand.WIDGET] ||
187
+ args?.[ToolbarRegistry.WIDGET_ID_ARG]
188
+ ) {
189
+ // When a specific widget is identified, bypass getActiveCommandId (which
190
+ // relies on global focus state) and let each command's own isVisible decide.
191
+ isVisible = commandList
192
+ .reduce<string[]>((acc, cmd) => acc.concat(cmd.ids), [])
193
+ .map(id => commands.isVisible(id, args as never));
194
+ } else {
195
+ isVisible = reduceAttribute('isVisible', args);
196
+ }
183
197
  return isVisible.length > 0
184
198
  ? !isVisible.some(visible => visible === false)
185
199
  : (defaultValues.isVisible ?? true);
186
200
  },
187
- execute: async () => {
188
- const widget = shell.currentWidget;
201
+ execute: async args => {
202
+ const widget = getWidget(args);
189
203
  const commandIds = commandList.map(cmd =>
190
204
  widget !== null ? cmd.getActiveCommandId(widget) : null
191
205
  );
@@ -196,8 +210,8 @@ export function createSemanticCommand(
196
210
  let result: unknown = null;
197
211
  if (toExecute.length > 0) {
198
212
  for (const commandId of toExecute) {
199
- const args = { [SemanticCommand.WIDGET]: widget!.id };
200
- result = await commands.execute(commandId!, args);
213
+ const widgetArgs = { [SemanticCommand.WIDGET]: widget!.id };
214
+ result = await commands.execute(commandId!, widgetArgs);
201
215
  if (typeof result === 'boolean' && result === false) {
202
216
  // If a command returns a boolean, assume it is the execution success status
203
217
  // So break if it is false.
@@ -211,24 +225,38 @@ export function createSemanticCommand(
211
225
  }
212
226
  };
213
227
 
228
+ function getWidget(args?: ReadonlyPartialJSONObject): Widget | null {
229
+ const widgetId =
230
+ (args?.[SemanticCommand.WIDGET] as string | undefined) ??
231
+ (args?.[ToolbarRegistry.WIDGET_ID_ARG] as string | undefined);
232
+ if (widgetId) {
233
+ for (const widget of shell.widgets()) {
234
+ if (widget.id === widgetId) {
235
+ return widget;
236
+ }
237
+ }
238
+ }
239
+ return shell.currentWidget;
240
+ }
241
+
214
242
  function reduceAttribute(
215
- attribute: keyof CommandRegistry.ICommandOptions
243
+ attribute: keyof CommandRegistry.ICommandOptions,
244
+ args?: ReadonlyPartialJSONObject
216
245
  ): unknown[] {
217
- const widget = shell.currentWidget;
246
+ const widget = getWidget(args);
218
247
  const commandIds = commandList.map(cmd =>
219
248
  widget !== null ? cmd.getActiveCommandId(widget) : null
220
249
  );
221
- const attributes = commandIds
250
+ return commandIds
222
251
  .filter(commandId => commandId !== null)
223
- .map(commandId => commands[attribute](commandId!));
224
- return attributes;
252
+ .map(commandId => commands[attribute](commandId!, args as never));
225
253
  }
226
254
 
227
255
  function concatenateTexts(
228
256
  attribute: 'label' | 'caption'
229
257
  ): string | CommandRegistry.CommandFunc<string> | undefined {
230
- return () => {
231
- const texts = (reduceAttribute(attribute) as string[]).map(
258
+ return args => {
259
+ const texts = (reduceAttribute(attribute, args) as string[]).map(
232
260
  (text, textIndex) =>
233
261
  attribute == 'caption' && textIndex > 0
234
262
  ? text.toLocaleLowerCase()
package/style/menus.css CHANGED
@@ -29,44 +29,6 @@
29
29
 
30
30
  .lm-MenuBar-menu.jp-ThemedContainer {
31
31
  top: calc(-2 * var(--jp-border-width));
32
- scrollbar-width: none;
33
- -ms-overflow-style: none;
34
- overflow: auto;
35
- background:
36
- linear-gradient(var(--jp-layout-color0) 30%, rgba(0, 0, 0, 0)) center top,
37
- linear-gradient(rgba(0, 0, 0, 0), var(--jp-layout-color0) 70%) center bottom,
38
- radial-gradient(
39
- farthest-side at 50% 0,
40
- color-mix(
41
- in hsl,
42
- var(--jp-layout-color0) 50%,
43
- var(--jp-inverse-layout-color0) 30%
44
- ),
45
- rgba(0, 0, 0, 0)
46
- )
47
- center top,
48
- radial-gradient(
49
- farthest-side at 50% 100%,
50
- color-mix(
51
- in hsl,
52
- var(--jp-layout-color0) 50%,
53
- var(--jp-inverse-layout-color0) 30%
54
- ),
55
- rgba(0, 0, 0, 0)
56
- )
57
- center bottom;
58
- background-color: var(--jp-layout-color0);
59
- background-repeat: no-repeat;
60
- background-size:
61
- 100% 40px,
62
- 100% 40px,
63
- 100% 14px,
64
- 100% 14px;
65
- background-attachment: local, local, scroll, scroll;
66
- }
67
-
68
- .lm-MenuBar-menu.jp-ThemedContainer::-webkit-scrollbar {
69
- display: none;
70
32
  }
71
33
 
72
34
  .lm-MenuBar-item {
@@ -154,6 +116,47 @@
154
116
  box-shadow: var(--jp-elevation-z6);
155
117
  }
156
118
 
119
+ .lm-Menu.jp-ThemedContainer {
120
+ scrollbar-width: none;
121
+ -ms-overflow-style: none;
122
+ overflow: auto;
123
+ background:
124
+ linear-gradient(var(--jp-layout-color0) 30%, rgba(0, 0, 0, 0)) center top,
125
+ linear-gradient(rgba(0, 0, 0, 0), var(--jp-layout-color0) 70%) center bottom,
126
+ radial-gradient(
127
+ farthest-side at 50% 0,
128
+ color-mix(
129
+ in hsl,
130
+ var(--jp-layout-color0) 50%,
131
+ var(--jp-inverse-layout-color0) 30%
132
+ ),
133
+ rgba(0, 0, 0, 0)
134
+ )
135
+ center top,
136
+ radial-gradient(
137
+ farthest-side at 50% 100%,
138
+ color-mix(
139
+ in hsl,
140
+ var(--jp-layout-color0) 50%,
141
+ var(--jp-inverse-layout-color0) 30%
142
+ ),
143
+ rgba(0, 0, 0, 0)
144
+ )
145
+ center bottom;
146
+ background-color: var(--jp-layout-color0);
147
+ background-repeat: no-repeat;
148
+ background-size:
149
+ 100% 40px,
150
+ 100% 40px,
151
+ 100% 14px,
152
+ 100% 14px;
153
+ background-attachment: local, local, scroll, scroll;
154
+ }
155
+
156
+ .lm-Menu.jp-ThemedContainer::-webkit-scrollbar {
157
+ display: none;
158
+ }
159
+
157
160
  .lm-Menu-item {
158
161
  min-height: var(--jp-private-menu-item-height);
159
162
  max-height: var(--jp-private-menu-item-height);