@jupyterlab/application 4.6.3 → 4.6.5

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/dockpanel.ts CHANGED
@@ -1,8 +1,20 @@
1
1
  import { DockPanelSvg } from '@jupyterlab/ui-components';
2
+ import type { Message } from '@lumino/messaging';
2
3
  import type { Widget } from '@lumino/widgets';
3
4
 
4
- const DEFAULT_NODE_THRESHOLD = 1000;
5
- const DEFAULT_TEXT_LENGTH_THRESHOLD = 25000;
5
+ export const DEFAULT_NODE_THRESHOLD = 1000;
6
+ export const DEFAULT_TEXT_LENGTH_THRESHOLD = 25000;
7
+
8
+ /**
9
+ * Milliseconds after the last pointer move before the frozen sizes are
10
+ * re-measured.
11
+ */
12
+ export const REFRESH_DEBOUNCE = 300;
13
+
14
+ /**
15
+ * Milliseconds between re-measures while a drag is held still.
16
+ */
17
+ export const REFRESH_INTERVAL = 3000;
6
18
 
7
19
  /**
8
20
  * A dock panel that freezes heavy panel dimensions during handle drags to
@@ -20,18 +32,26 @@ export class OptimizedDockPanelSvg extends DockPanelSvg {
20
32
  * not be called directly by user code.
21
33
  */
22
34
  override handleEvent(event: Event): void {
23
- if (event.type === 'pointerdown') {
35
+ // A second pointer going down while a drag is in progress must not be
36
+ // mistaken for a new drag which never started, or the flag is cleared and
37
+ // the panels stay frozen for good.
38
+ if (event.type === 'pointerdown' && !this._isResizeDragActive) {
24
39
  this._isResizeDragActive = this._isHandlePointerDown(event);
40
+ if (this._isResizeDragActive) {
41
+ this._dragPointerId = (event as PointerEvent).pointerId;
42
+ // Installed here rather than below, so that the listener and the flag
43
+ // are always set together, whether or not the freeze is enabled.
44
+ this._listenForCancel();
45
+ }
25
46
  }
26
47
 
27
48
  // Unfreeze before super processes the drag-release events so Lumino
28
49
  // measures natural sizes when it finalises the split position.
29
- if (this._frozenGroups.length > 0 && this._isResizeDragActive) {
30
- const t = event.type;
31
- if (t === 'pointerup' || t === 'pointercancel' || t === 'keydown') {
32
- this._isResizeDragActive = false;
33
- this._unfreezeElements();
34
- }
50
+ if (
51
+ this._isResizeDragActive &&
52
+ Private.endsDrag(event, this._dragPointerId)
53
+ ) {
54
+ this._endDrag();
35
55
  }
36
56
 
37
57
  super.handleEvent(event);
@@ -47,6 +67,55 @@ export class OptimizedDockPanelSvg extends DockPanelSvg {
47
67
  }
48
68
  }
49
69
 
70
+ /**
71
+ * Dispose of the resources held by the panel.
72
+ */
73
+ override dispose(): void {
74
+ this._endDrag();
75
+ super.dispose();
76
+ }
77
+
78
+ /**
79
+ * A message handler invoked on an `'after-detach'` message.
80
+ *
81
+ * #### Notes
82
+ * Lumino ends a handle drag on detach without an event reaching
83
+ * `handleEvent`, so the drag has to be ended here as well.
84
+ */
85
+ protected override onAfterDetach(msg: Message): void {
86
+ this._endDrag();
87
+ super.onAfterDetach(msg);
88
+ }
89
+
90
+ /**
91
+ * Stop the drag and put back everything it froze.
92
+ */
93
+ private _endDrag(): void {
94
+ this._isResizeDragActive = false;
95
+ this._dragPointerId = -1;
96
+ if (this._cancelListener) {
97
+ this._cancelListener.removeEventListener('pointercancel', this, true);
98
+ this._cancelListener = null;
99
+ }
100
+ this._unfreezeElements();
101
+ }
102
+
103
+ /**
104
+ * Listen for the drag being cancelled, which Lumino does not listen for.
105
+ *
106
+ * #### Notes
107
+ * A pen or touch drag taken over by a system gesture ends with
108
+ * `pointercancel` and no `pointerup`, so without this the panels would stay
109
+ * frozen.
110
+ */
111
+ private _listenForCancel(): void {
112
+ if (this._cancelListener) {
113
+ return;
114
+ }
115
+ this._cancelListener = this.node.ownerDocument;
116
+ this._cancelListener.addEventListener('pointercancel', this, true);
117
+ }
118
+
50
119
  private _isHandlePointerDown(event: Event): boolean {
51
120
  const pointerEvent = event as PointerEvent;
52
121
  if (pointerEvent.button !== 0) {
@@ -94,17 +163,23 @@ export class OptimizedDockPanelSvg extends DockPanelSvg {
94
163
 
95
164
  for (const target of targets) {
96
165
  const head = target.node;
97
- const elements: HTMLElement[] = [head];
166
+ const candidates: HTMLElement[] = [head];
98
167
  for (let i = 0; i < head.children.length; i++) {
99
- elements.push(head.children[i] as HTMLElement);
168
+ candidates.push(head.children[i] as HTMLElement);
169
+ }
170
+
171
+ // Leave the elements whose geometry Lumino owns untouched; freezing
172
+ // those corrupts the layout, see `Private.isLayoutManaged`.
173
+ const elements = candidates.filter(el => !Private.isLayoutManaged(el));
174
+ if (elements.length === 0) {
175
+ continue;
100
176
  }
101
177
 
102
178
  // Read all rects before writing to avoid layout thrashing.
103
179
  const rects = elements.map(el => el.getBoundingClientRect());
104
180
 
105
- const frozenGroup: Private.IFrozenElement[] = elements.map((el, i) => ({
181
+ const frozenGroup: Private.IFrozenElement[] = elements.map(el => ({
106
182
  element: el,
107
- isHead: i === 0,
108
183
  prevWidth: el.style.width,
109
184
  prevMaxWidth: el.style.maxWidth,
110
185
  prevHeight: el.style.height,
@@ -112,14 +187,7 @@ export class OptimizedDockPanelSvg extends DockPanelSvg {
112
187
  }));
113
188
 
114
189
  for (let i = 0; i < frozenGroup.length; i++) {
115
- const el = frozenGroup[i].element;
116
- const rect = rects[i];
117
- el.style.width = `${rect.width}px`;
118
- el.style.maxWidth = `${rect.width}px`;
119
- el.style.maxHeight = `${rect.height}px`;
120
- if (frozenGroup[i].isHead) {
121
- el.style.height = `${rect.height}px`;
122
- }
190
+ Private.pinToRect(frozenGroup[i].element, rects[i]);
123
191
  }
124
192
  this._frozenGroups.push(frozenGroup);
125
193
  }
@@ -127,7 +195,7 @@ export class OptimizedDockPanelSvg extends DockPanelSvg {
127
195
  if (this._frozenGroups.length > 0 && this._intervalId === 0) {
128
196
  this._intervalId = window.setInterval(() => {
129
197
  this._refreshFrozenElements();
130
- }, 3000);
198
+ }, REFRESH_INTERVAL);
131
199
  }
132
200
  }
133
201
 
@@ -141,12 +209,24 @@ export class OptimizedDockPanelSvg extends DockPanelSvg {
141
209
  this._refreshTimerId = window.setTimeout(() => {
142
210
  this._refreshTimerId = 0;
143
211
  this._refreshFrozenElements();
144
- }, 300);
212
+ }, REFRESH_DEBOUNCE);
145
213
  }
146
214
 
147
215
  private _refreshFrozenElements(): void {
216
+ // A refresh spans several frames, and both the interval and the pointer
217
+ // move debounce can start one. Stamp each pass so that a pass started
218
+ // while another is in flight replaces it instead of running alongside it;
219
+ // two passes sharing `_refreshRAFId` would leave one of them impossible
220
+ // to cancel, and it would then re-apply sizes to elements that are no
221
+ // longer frozen.
222
+ this._cancelRefresh();
223
+ const generation = this._refreshGeneration;
224
+
148
225
  let g = 0;
149
226
  const step = () => {
227
+ if (generation !== this._refreshGeneration) {
228
+ return;
229
+ }
150
230
  if (g >= this._frozenGroups.length) {
151
231
  this._refreshRAFId = 0;
152
232
  return;
@@ -162,17 +242,12 @@ export class OptimizedDockPanelSvg extends DockPanelSvg {
162
242
  }
163
243
 
164
244
  this._refreshRAFId = requestAnimationFrame(() => {
245
+ if (generation !== this._refreshGeneration) {
246
+ return;
247
+ }
165
248
  let rects = group.map(entry => entry.element.getBoundingClientRect());
166
249
  for (let i = 0; i < group.length; i++) {
167
- let entry = group[i];
168
- let rect = rects[i];
169
- let el = entry.element;
170
- el.style.width = `${rect.width}px`;
171
- el.style.maxWidth = `${rect.width}px`;
172
- el.style.maxHeight = `${rect.height}px`;
173
- if (entry.isHead) {
174
- el.style.height = `${rect.height}px`;
175
- }
250
+ Private.pinToRect(group[i].element, rects[i]);
176
251
  }
177
252
  g++;
178
253
  this._refreshRAFId = requestAnimationFrame(step);
@@ -182,15 +257,23 @@ export class OptimizedDockPanelSvg extends DockPanelSvg {
182
257
  this._refreshRAFId = requestAnimationFrame(step);
183
258
  }
184
259
 
260
+ /**
261
+ * Stop any refresh pass which is in flight.
262
+ */
263
+ private _cancelRefresh(): void {
264
+ this._refreshGeneration++;
265
+ if (this._refreshRAFId !== 0) {
266
+ cancelAnimationFrame(this._refreshRAFId);
267
+ this._refreshRAFId = 0;
268
+ }
269
+ }
270
+
185
271
  private _unfreezeElements(): void {
186
272
  if (this._refreshTimerId !== 0) {
187
273
  clearTimeout(this._refreshTimerId);
188
274
  this._refreshTimerId = 0;
189
275
  }
190
- if (this._refreshRAFId !== 0) {
191
- cancelAnimationFrame(this._refreshRAFId);
192
- this._refreshRAFId = 0;
193
- }
276
+ this._cancelRefresh();
194
277
  if (this._intervalId !== 0) {
195
278
  clearInterval(this._intervalId);
196
279
  this._intervalId = 0;
@@ -248,17 +331,79 @@ export class OptimizedDockPanelSvg extends DockPanelSvg {
248
331
 
249
332
  private _optimizeResize = true;
250
333
  private _isResizeDragActive = false;
334
+ private _dragPointerId = -1;
335
+ private _cancelListener: Document | null = null;
251
336
  private _frozenGroups: Private.IFrozenElement[][] = [];
252
337
  private _refreshTimerId = 0;
253
338
  private _refreshRAFId = 0;
339
+ private _refreshGeneration = 0;
254
340
  private _intervalId = 0;
255
341
  }
256
342
 
257
343
  /** Namespace for OptimizedDockPanelSvg statics */
258
344
  namespace Private {
345
+ /**
346
+ * Whether a Lumino layout owns the geometry of the given element.
347
+ *
348
+ * #### Notes
349
+ * Lumino positions widget nodes through `LayoutItem`, which remembers the
350
+ * geometry it last wrote and skips writing `width` or `height` again when
351
+ * the value did not change. Anything else writing those properties leaves
352
+ * the DOM out of step with that record for as long as the layout keeps
353
+ * computing the same size, which pins the widget at a stale size.
354
+ * `LayoutItem.fit()` also reads `max-width` and `max-height` back off the
355
+ * node into the limits it clamps against, so an injected maximum can
356
+ * survive well past the drag which set it. Split handles are written the
357
+ * same way, without going through `LayoutItem`.
358
+ *
359
+ * Both are set up with an inline `position: absolute`, which is what this
360
+ * tests for. Testing for the `lm-Widget` class instead would also skip
361
+ * widget nodes of layouts which never write geometry, such as the outputs
362
+ * of an output area, and leave nothing to freeze. Should Lumino stop
363
+ * setting the position inline, this errs towards freezing less rather than
364
+ * towards corrupting a layout.
365
+ */
366
+ export function isLayoutManaged(el: HTMLElement): boolean {
367
+ return el.style.position === 'absolute';
368
+ }
369
+
370
+ /**
371
+ * Whether the event ends a handle drag, matching the rules Lumino releases
372
+ * the drag on, plus the cancellation Lumino does not listen for.
373
+ */
374
+ export function endsDrag(event: Event, dragPointerId: number): boolean {
375
+ switch (event.type) {
376
+ case 'pointerup':
377
+ // Deliberately not matched against `dragPointerId`. Lumino releases
378
+ // on any button 0 release and then drops its document listeners, so
379
+ // matching here would leave the panels frozen with no drag running
380
+ // and no further event able to unfreeze them.
381
+ return (event as PointerEvent).button === 0;
382
+ case 'pointercancel':
383
+ // Matched, because Lumino has no cancel case to keep in step with. An
384
+ // unrelated pointer being cancelled, a palm resting on a touch screen
385
+ // for instance, must not end a drag it was never part of.
386
+ return (event as PointerEvent).pointerId === dragPointerId;
387
+ case 'keydown':
388
+ return (event as KeyboardEvent).keyCode === 27;
389
+ default:
390
+ return false;
391
+ }
392
+ }
393
+
394
+ /**
395
+ * Hold an element at the size it has now, so that resizing an ancestor does
396
+ * not make the browser lay out its contents again.
397
+ */
398
+ export function pinToRect(el: HTMLElement, rect: DOMRect): void {
399
+ el.style.width = `${rect.width}px`;
400
+ el.style.maxWidth = `${rect.width}px`;
401
+ el.style.height = `${rect.height}px`;
402
+ el.style.maxHeight = `${rect.height}px`;
403
+ }
404
+
259
405
  export interface IFrozenElement {
260
406
  element: HTMLElement;
261
- isHead: boolean;
262
407
  prevWidth: string;
263
408
  prevMaxWidth: string;
264
409
  prevHeight: string;
@@ -15,7 +15,7 @@ import type {
15
15
  } from '@lumino/coreutils';
16
16
  import { JSONExt, PromiseDelegate, Token } from '@lumino/coreutils';
17
17
  import { AttachedProperty } from '@lumino/properties';
18
- import type { DockPanel, Widget } from '@lumino/widgets';
18
+ import type { DockLayout, DockPanel, Widget } from '@lumino/widgets';
19
19
  import type { ILabShell } from './shell';
20
20
 
21
21
  /**
@@ -776,6 +776,33 @@ namespace Private {
776
776
  create: owner => ''
777
777
  });
778
778
 
779
+ /**
780
+ * The name of the widget at `index`, or, when that widget has no name, the
781
+ * name of the nearest widget with a name before it, else after it.
782
+ *
783
+ * #### Notes
784
+ * A widget without a name, such as one that a plugin adds on each start,
785
+ * is not restored, so the tab next to it takes its place.
786
+ */
787
+ function nearestName(
788
+ widgets: ReadonlyArray<Widget>,
789
+ index: number
790
+ ): string | null {
791
+ for (let i = Math.min(index, widgets.length - 1); i >= 0; i--) {
792
+ const name = nameProperty.get(widgets[i]);
793
+ if (name) {
794
+ return name;
795
+ }
796
+ }
797
+ for (let i = index + 1; i < widgets.length; i++) {
798
+ const name = nameProperty.get(widgets[i]);
799
+ if (name) {
800
+ return name;
801
+ }
802
+ }
803
+ return null;
804
+ }
805
+
779
806
  /**
780
807
  * Serialize individual areas within the main area.
781
808
  */
@@ -787,12 +814,16 @@ namespace Private {
787
814
  }
788
815
 
789
816
  if (area.type === 'tab-area') {
817
+ const widgets = area.widgets
818
+ .map(widget => nameProperty.get(widget))
819
+ .filter(name => !!name);
820
+ // Only the widgets with a name are saved: count the current index
821
+ // among them, or it points at another tab after a restore.
822
+ const current = nearestName(area.widgets, area.currentIndex);
790
823
  return {
791
824
  type: 'tab-area',
792
- currentIndex: area.currentIndex,
793
- widgets: area.widgets
794
- .map(widget => nameProperty.get(widget))
795
- .filter(name => !!name)
825
+ currentIndex: current ? widgets.indexOf(current) : 0,
826
+ widgets
796
827
  };
797
828
  }
798
829
 
@@ -815,7 +846,9 @@ namespace Private {
815
846
  };
816
847
  if (area) {
817
848
  if (area.currentWidget) {
818
- const current = Private.nameProperty.get(area.currentWidget);
849
+ const current =
850
+ Private.nameProperty.get(area.currentWidget) ||
851
+ nearestInArea(area.dock?.main ?? null, area.currentWidget);
819
852
  if (current) {
820
853
  dehydrated.current = current;
821
854
  }
@@ -824,6 +857,57 @@ namespace Private {
824
857
  return dehydrated;
825
858
  }
826
859
 
860
+ /**
861
+ * The name of the widget nearest to `widget` in the tab area that holds it,
862
+ * for a current widget that has no name and is not restored.
863
+ */
864
+ function nearestInArea(
865
+ area: ILabShell.AreaConfig | null,
866
+ widget: Widget
867
+ ): string | null {
868
+ if (!area) {
869
+ return null;
870
+ }
871
+ if (area.type === 'tab-area') {
872
+ const index = area.widgets.indexOf(widget);
873
+ return index === -1 ? null : nearestName(area.widgets, index);
874
+ }
875
+ for (const child of area.children) {
876
+ const name = nearestInArea(child, widget);
877
+ if (name) {
878
+ return name;
879
+ }
880
+ }
881
+ return null;
882
+ }
883
+
884
+ /**
885
+ * Restore a saved tab area from the names of its widgets.
886
+ *
887
+ * #### Notes
888
+ * A saved widget that was not restored drops out: the current index follows
889
+ * the widget saved at `index`, or the nearest restored one before it.
890
+ */
891
+ function restoreTabArea(
892
+ widgets: ReadonlyArray<string>,
893
+ index: number,
894
+ names: Map<string, Widget>
895
+ ): DockLayout.ITabAreaConfig {
896
+ const restored: Widget[] = [];
897
+ let current = 0;
898
+ widgets.forEach((name, i) => {
899
+ const widget = names.get(name);
900
+ if (!widget) {
901
+ return;
902
+ }
903
+ if (i <= index) {
904
+ current = restored.length;
905
+ }
906
+ restored.push(widget);
907
+ });
908
+ return { type: 'tab-area', currentIndex: current, widgets: restored };
909
+ }
910
+
827
911
  /**
828
912
  * Deserialize individual areas within the main area.
829
913
  *
@@ -852,23 +936,7 @@ namespace Private {
852
936
 
853
937
  if (type === 'tab-area') {
854
938
  const { currentIndex, widgets } = area as ITabArea;
855
- const hydrated: ILabShell.AreaConfig = {
856
- type: 'tab-area',
857
- currentIndex: currentIndex || 0,
858
- widgets:
859
- (widgets &&
860
- (widgets
861
- .map(widget => names.get(widget))
862
- .filter(widget => !!widget) as Widget[])) ||
863
- []
864
- };
865
-
866
- // Make sure the current index is within bounds.
867
- if (hydrated.currentIndex > hydrated.widgets.length - 1) {
868
- hydrated.currentIndex = 0;
869
- }
870
-
871
- return hydrated;
939
+ return restoreTabArea(widgets || [], currentIndex || 0, names);
872
940
  }
873
941
 
874
942
  const { orientation, sizes, children } = area as ISplitArea;
@@ -904,12 +972,68 @@ namespace Private {
904
972
  return null;
905
973
  }
906
974
 
907
- const name = (area as any).current || null;
975
+ const name: string | null = (area as any).current || null;
908
976
  const dock = (area as any).dock || null;
977
+ const main = dock ? deserializeArea(dock, names) : null;
978
+
979
+ // The shell activates only the current widget. When the saved one did not
980
+ // come back, or had no name, use the tab that is shown in its place.
981
+ const currentWidget =
982
+ (name && (names.get(name) || nearestRestored(dock, name, names))) ||
983
+ selectedWidget(main);
909
984
 
910
985
  return {
911
- currentWidget: (name && names.has(name) && names.get(name)) || null,
912
- dock: dock ? { main: deserializeArea(dock, names) } : null
986
+ currentWidget,
987
+ dock: dock ? { main } : null
913
988
  };
914
989
  }
990
+
991
+ /**
992
+ * The widget that a restored tab area shows in place of the widget saved as
993
+ * `name`, when that widget did not come back.
994
+ */
995
+ function nearestRestored(
996
+ area: ITabArea | ISplitArea | null,
997
+ name: string,
998
+ names: Map<string, Widget>
999
+ ): Widget | null {
1000
+ if (!area) {
1001
+ return null;
1002
+ }
1003
+ if (area.type === 'tab-area') {
1004
+ const widgets = area.widgets || [];
1005
+ const index = widgets.indexOf(name);
1006
+ if (index === -1) {
1007
+ return null;
1008
+ }
1009
+ const restored = restoreTabArea(widgets, index, names);
1010
+ return restored.widgets[restored.currentIndex] ?? null;
1011
+ }
1012
+ for (const child of area.children || []) {
1013
+ const widget = nearestRestored(child, name, names);
1014
+ if (widget) {
1015
+ return widget;
1016
+ }
1017
+ }
1018
+ return null;
1019
+ }
1020
+
1021
+ /**
1022
+ * The widget that the first tab area with widgets shows.
1023
+ */
1024
+ function selectedWidget(area: ILabShell.AreaConfig | null): Widget | null {
1025
+ if (!area) {
1026
+ return null;
1027
+ }
1028
+ if (area.type === 'tab-area') {
1029
+ return area.widgets[area.currentIndex] ?? null;
1030
+ }
1031
+ for (const child of area.children) {
1032
+ const widget = selectedWidget(child);
1033
+ if (widget) {
1034
+ return widget;
1035
+ }
1036
+ }
1037
+ return null;
1038
+ }
915
1039
  }
package/src/shell.ts CHANGED
@@ -2443,7 +2443,7 @@ namespace Private {
2443
2443
  if (w.id && w.content instanceof SplitPanel) {
2444
2444
  widgetStates[w.id] = {
2445
2445
  sizes: w.content.relativeSizes() as number[],
2446
- expansionStates: w.content.widgets.map(wi => wi.isVisible)
2446
+ expansionStates: w.content.widgets.map(wi => !wi.isHidden)
2447
2447
  };
2448
2448
  }
2449
2449
  });
package/src/utils.ts CHANGED
@@ -169,26 +169,21 @@ export function createSemanticCommand(
169
169
  caption: concatenateTexts('caption'),
170
170
  isEnabled: () => {
171
171
  const isEnabled = reduceAttribute('isEnabled');
172
- return (
173
- (isEnabled.length > 0 &&
174
- !isEnabled.some(enabled => enabled === false)) ||
175
- (defaultValues.isEnabled ?? false)
176
- );
172
+ return isEnabled.length > 0
173
+ ? !isEnabled.some(enabled => enabled === false)
174
+ : (defaultValues.isEnabled ?? false);
177
175
  },
178
176
  isToggled: () => {
179
177
  const isToggled = reduceAttribute('isToggled');
180
- return (
181
- isToggled.some(enabled => enabled === true) ||
182
- (defaultValues.isToggled ?? false)
183
- );
178
+ return isToggled.length > 0
179
+ ? isToggled.some(enabled => enabled === true)
180
+ : (defaultValues.isToggled ?? false);
184
181
  },
185
182
  isVisible: () => {
186
183
  const isVisible = reduceAttribute('isVisible');
187
- return (
188
- (isVisible.length > 0 &&
189
- !isVisible.some(visible => visible === false)) ||
190
- (defaultValues.isVisible ?? true)
191
- );
184
+ return isVisible.length > 0
185
+ ? !isVisible.some(visible => visible === false)
186
+ : (defaultValues.isVisible ?? true);
192
187
  },
193
188
  execute: async () => {
194
189
  const widget = shell.currentWidget;