@jupyterlab/application
Version:
JupyterLab - Application
369 lines • 13.8 kB
JavaScript
import { DockPanelSvg } from '@jupyterlab/ui-components';
export const DEFAULT_NODE_THRESHOLD = 1000;
export const DEFAULT_TEXT_LENGTH_THRESHOLD = 25000;
/**
* Milliseconds after the last pointer move before the frozen sizes are
* re-measured.
*/
export const REFRESH_DEBOUNCE = 300;
/**
* Milliseconds between re-measures while a drag is held still.
*/
export const REFRESH_INTERVAL = 3000;
/**
* A dock panel that freezes heavy panel dimensions during handle drags to
* reduce layout reflow and improve resize performance.
*/
export class OptimizedDockPanelSvg extends DockPanelSvg {
constructor() {
super(...arguments);
this._optimizeResize = true;
this._isResizeDragActive = false;
this._dragPointerId = -1;
this._cancelListener = null;
this._frozenGroups = [];
this._refreshTimerId = 0;
this._refreshRAFId = 0;
this._refreshGeneration = 0;
this._intervalId = 0;
}
/**
* Handle the DOM events for the dock panel.
*
* @param event - The DOM event sent to the panel.
*
* #### Notes
* This method implements the DOM `EventListener` interface and is
* called in response to events registered for the node. It should
* not be called directly by user code.
*/
handleEvent(event) {
// A second pointer going down while a drag is in progress must not be
// mistaken for a new drag which never started, or the flag is cleared and
// the panels stay frozen for good.
if (event.type === 'pointerdown' && !this._isResizeDragActive) {
this._isResizeDragActive = this._isHandlePointerDown(event);
if (this._isResizeDragActive) {
this._dragPointerId = event.pointerId;
// Installed here rather than below, so that the listener and the flag
// are always set together, whether or not the freeze is enabled.
this._listenForCancel();
}
}
// Unfreeze before super processes the drag-release events so Lumino
// measures natural sizes when it finalises the split position.
if (this._isResizeDragActive &&
Private.endsDrag(event, this._dragPointerId)) {
this._endDrag();
}
super.handleEvent(event);
if (!this._optimizeResize) {
return;
}
if (event.type === 'pointerdown' && this._isResizeDragActive) {
this._freezeHeavyLeaves();
}
else if (event.type === 'pointermove' && this._isResizeDragActive) {
this._scheduleRefresh();
}
}
/**
* Dispose of the resources held by the panel.
*/
dispose() {
this._endDrag();
super.dispose();
}
/**
* A message handler invoked on an `'after-detach'` message.
*
* #### Notes
* Lumino ends a handle drag on detach without an event reaching
* `handleEvent`, so the drag has to be ended here as well.
*/
onAfterDetach(msg) {
this._endDrag();
super.onAfterDetach(msg);
}
/**
* Stop the drag and put back everything it froze.
*/
_endDrag() {
this._isResizeDragActive = false;
this._dragPointerId = -1;
if (this._cancelListener) {
this._cancelListener.removeEventListener('pointercancel', this, true);
this._cancelListener = null;
}
this._unfreezeElements();
}
/**
* Listen for the drag being cancelled, which Lumino does not listen for.
*
* #### Notes
* A pen or touch drag taken over by a system gesture ends with
* `pointercancel` and no `pointerup`, so without this the panels would stay
* frozen.
*/
_listenForCancel() {
if (this._cancelListener) {
return;
}
this._cancelListener = this.node.ownerDocument;
this._cancelListener.addEventListener('pointercancel', this, true);
}
_isHandlePointerDown(event) {
const pointerEvent = event;
if (pointerEvent.button !== 0) {
return false;
}
const target = pointerEvent.target;
if (!(target instanceof HTMLElement)) {
return false;
}
for (const handle of this.handles()) {
if (handle.contains(target)) {
return true;
}
}
return false;
}
/**
* Whether resize optimizations are enabled.
*
* When `true` (the default), panels with heavy DOM content have their
* dimensions frozen during a handle drag, avoiding repeated reflows.
* Setting this to `false` immediately unfreezes any frozen panels.
*/
get optimizeResize() {
return this._optimizeResize;
}
set optimizeResize(enabled) {
this._optimizeResize = enabled;
if (!enabled) {
this._unfreezeElements();
}
}
_freezeHeavyLeaves() {
if (this._frozenGroups.length > 0) {
return;
}
const targets = [];
for (const child of this.widgets()) {
this._collectHeavyWidgets(child, targets);
}
for (const target of targets) {
const head = target.node;
const candidates = [head];
for (let i = 0; i < head.children.length; i++) {
candidates.push(head.children[i]);
}
// Leave the elements whose geometry Lumino owns untouched; freezing
// those corrupts the layout, see `Private.isLayoutManaged`.
const elements = candidates.filter(el => !Private.isLayoutManaged(el));
if (elements.length === 0) {
continue;
}
// Read all rects before writing to avoid layout thrashing.
const rects = elements.map(el => el.getBoundingClientRect());
const frozenGroup = elements.map(el => ({
element: el,
prevWidth: el.style.width,
prevMaxWidth: el.style.maxWidth,
prevHeight: el.style.height,
prevMaxHeight: el.style.maxHeight
}));
for (let i = 0; i < frozenGroup.length; i++) {
Private.pinToRect(frozenGroup[i].element, rects[i]);
}
this._frozenGroups.push(frozenGroup);
}
if (this._frozenGroups.length > 0 && this._intervalId === 0) {
this._intervalId = window.setInterval(() => {
this._refreshFrozenElements();
}, REFRESH_INTERVAL);
}
}
_scheduleRefresh() {
if (this._frozenGroups.length === 0) {
return;
}
if (this._refreshTimerId !== 0) {
clearTimeout(this._refreshTimerId);
}
this._refreshTimerId = window.setTimeout(() => {
this._refreshTimerId = 0;
this._refreshFrozenElements();
}, REFRESH_DEBOUNCE);
}
_refreshFrozenElements() {
// A refresh spans several frames, and both the interval and the pointer
// move debounce can start one. Stamp each pass so that a pass started
// while another is in flight replaces it instead of running alongside it;
// two passes sharing `_refreshRAFId` would leave one of them impossible
// to cancel, and it would then re-apply sizes to elements that are no
// longer frozen.
this._cancelRefresh();
const generation = this._refreshGeneration;
let g = 0;
const step = () => {
if (generation !== this._refreshGeneration) {
return;
}
if (g >= this._frozenGroups.length) {
this._refreshRAFId = 0;
return;
}
let group = this._frozenGroups[g];
for (let entry of group) {
let el = entry.element;
el.style.width = '';
el.style.maxWidth = '';
el.style.height = '';
el.style.maxHeight = '';
}
this._refreshRAFId = requestAnimationFrame(() => {
if (generation !== this._refreshGeneration) {
return;
}
let rects = group.map(entry => entry.element.getBoundingClientRect());
for (let i = 0; i < group.length; i++) {
Private.pinToRect(group[i].element, rects[i]);
}
g++;
this._refreshRAFId = requestAnimationFrame(step);
});
};
this._refreshRAFId = requestAnimationFrame(step);
}
/**
* Stop any refresh pass which is in flight.
*/
_cancelRefresh() {
this._refreshGeneration++;
if (this._refreshRAFId !== 0) {
cancelAnimationFrame(this._refreshRAFId);
this._refreshRAFId = 0;
}
}
_unfreezeElements() {
if (this._refreshTimerId !== 0) {
clearTimeout(this._refreshTimerId);
this._refreshTimerId = 0;
}
this._cancelRefresh();
if (this._intervalId !== 0) {
clearInterval(this._intervalId);
this._intervalId = 0;
}
for (let group of this._frozenGroups) {
for (let entry of group) {
entry.element.style.width = entry.prevWidth;
entry.element.style.maxWidth = entry.prevMaxWidth;
entry.element.style.height = entry.prevHeight;
entry.element.style.maxHeight = entry.prevMaxHeight;
}
}
this._frozenGroups = [];
}
_collectHeavyWidgets(widget, result) {
if (!this._isDOMHeavy(widget.node)) {
return;
}
let layout = widget.layout;
if (!layout) {
result.push(widget);
return;
}
let anyChildHeavy = false;
for (let child of layout) {
if (this._isDOMHeavy(child.node)) {
anyChildHeavy = true;
break;
}
}
if (anyChildHeavy) {
for (let child of layout) {
this._collectHeavyWidgets(child, result);
}
}
else {
result.push(widget);
}
}
_isDOMHeavy(el) {
var _a, _b;
if (el.querySelectorAll('*').length >= DEFAULT_NODE_THRESHOLD) {
return true;
}
if (((_b = (_a = el.textContent) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) >= DEFAULT_TEXT_LENGTH_THRESHOLD) {
return true;
}
return false;
}
}
/** Namespace for OptimizedDockPanelSvg statics */
var Private;
(function (Private) {
/**
* Whether a Lumino layout owns the geometry of the given element.
*
* #### Notes
* Lumino positions widget nodes through `LayoutItem`, which remembers the
* geometry it last wrote and skips writing `width` or `height` again when
* the value did not change. Anything else writing those properties leaves
* the DOM out of step with that record for as long as the layout keeps
* computing the same size, which pins the widget at a stale size.
* `LayoutItem.fit()` also reads `max-width` and `max-height` back off the
* node into the limits it clamps against, so an injected maximum can
* survive well past the drag which set it. Split handles are written the
* same way, without going through `LayoutItem`.
*
* Both are set up with an inline `position: absolute`, which is what this
* tests for. Testing for the `lm-Widget` class instead would also skip
* widget nodes of layouts which never write geometry, such as the outputs
* of an output area, and leave nothing to freeze. Should Lumino stop
* setting the position inline, this errs towards freezing less rather than
* towards corrupting a layout.
*/
function isLayoutManaged(el) {
return el.style.position === 'absolute';
}
Private.isLayoutManaged = isLayoutManaged;
/**
* Whether the event ends a handle drag, matching the rules Lumino releases
* the drag on, plus the cancellation Lumino does not listen for.
*/
function endsDrag(event, dragPointerId) {
switch (event.type) {
case 'pointerup':
// Deliberately not matched against `dragPointerId`. Lumino releases
// on any button 0 release and then drops its document listeners, so
// matching here would leave the panels frozen with no drag running
// and no further event able to unfreeze them.
return event.button === 0;
case 'pointercancel':
// Matched, because Lumino has no cancel case to keep in step with. An
// unrelated pointer being cancelled, a palm resting on a touch screen
// for instance, must not end a drag it was never part of.
return event.pointerId === dragPointerId;
case 'keydown':
return event.keyCode === 27;
default:
return false;
}
}
Private.endsDrag = endsDrag;
/**
* Hold an element at the size it has now, so that resizing an ancestor does
* not make the browser lay out its contents again.
*/
function pinToRect(el, rect) {
el.style.width = `${rect.width}px`;
el.style.maxWidth = `${rect.width}px`;
el.style.height = `${rect.height}px`;
el.style.maxHeight = `${rect.height}px`;
}
Private.pinToRect = pinToRect;
})(Private || (Private = {}));
//# sourceMappingURL=dockpanel.js.map