Skip to content

Commit 1a0ff8d

Browse files
authored
fix: Preview did not draw correctly when dragging Grids (#1183)
- When we drag something, there is a frame where the width/height is 0 - We didn't update the metrics within the actual animation frame, so it was drawing the wrong thing - Fix so metrics are always updated in the animation frame, and also to only check the metrics with the previously checked metrics - Tested with ticking tables stuck to the bottom to ensure they stuck while ticking - Fixes #1112
1 parent bba2d01 commit 1a0ff8d

2 files changed

Lines changed: 50 additions & 32 deletions

File tree

packages/golden-layout/src/controls/DragProxy.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,12 @@ export default class DragProxy extends EventEmitter {
9696

9797
this._updateTree();
9898
this._layoutManager._$calculateItemAreas();
99-
this._setDimensions();
10099

101100
$(document.body).append(this.element);
102101

102+
// Need to set dimensions after adding the element, or `Component.setSize()` will not pass the `.is('visible')` test and won't update
103+
this._setDimensions();
104+
103105
// there's no content tab to use yet, use the proxy tab size for placeholder sizing, after it's created
104106
if (!this._contentItem.tab && this._proxyTab.length) {
105107
this._layoutManager.tabDropPlaceholder.width(

packages/grid/src/Grid.tsx

Lines changed: 47 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -536,31 +536,7 @@ class Grid extends PureComponent<GridProps, GridState> {
536536

537537
this.requestUpdateCanvas();
538538

539-
if (!this.metrics || !this.prevMetrics) {
540-
return;
541-
}
542-
543-
const { rowCount, columnCount, height, width } = this.metrics;
544-
const {
545-
rowCount: prevRowCount,
546-
columnCount: prevColumnCount,
547-
height: prevHeight,
548-
width: prevWidth,
549-
} = this.prevMetrics;
550-
551-
if (prevRowCount !== rowCount || height !== prevHeight) {
552-
const { isStuckToBottom } = this.state;
553-
if (isStuckToBottom) {
554-
this.scrollToBottom();
555-
}
556-
}
557-
558-
if (prevColumnCount !== columnCount || width !== prevWidth) {
559-
const { isStuckToRight } = this.state;
560-
if (isStuckToRight) {
561-
this.scrollToRight();
562-
}
563-
}
539+
this.checkStickyScroll();
564540

565541
if (this.validateSelection()) {
566542
this.checkSelectionChange(prevState);
@@ -799,17 +775,23 @@ class Grid extends PureComponent<GridProps, GridState> {
799775
this.animationFrame = requestAnimationFrame(() => {
800776
this.animationFrame = null;
801777

802-
if (!this.metrics) throw new Error('Metrics not set');
803-
804-
this.updateCanvas(this.metrics);
778+
this.updateCanvas();
805779
});
806780
}
807781

808-
updateCanvas(metrics = this.updateMetrics()): void {
782+
updateCanvas(): void {
809783
this.updateCanvasScale();
784+
785+
this.updateMetrics();
786+
810787
this.updateRenderState();
811-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
812-
this.renderer.configureContext(this.canvasContext!, this.renderState);
788+
789+
const { canvasContext, metrics, renderState } = this;
790+
assertNotNull(canvasContext);
791+
assertNotNull(metrics);
792+
assertNotNull(renderState);
793+
794+
this.renderer.configureContext(canvasContext, renderState);
813795

814796
const { onViewChanged } = this.props;
815797
onViewChanged(metrics);
@@ -850,6 +832,40 @@ class Grid extends PureComponent<GridProps, GridState> {
850832
}
851833
}
852834

835+
/**
836+
* Compares the current metrics with the previous metrics to see if we need to scroll when it is stuck to the bottom or the right
837+
*/
838+
checkStickyScroll() {
839+
if (!this.metrics) {
840+
return;
841+
}
842+
843+
if (this.prevMetrics) {
844+
const { rowCount, columnCount, height, width } = this.metrics;
845+
const {
846+
rowCount: prevRowCount,
847+
columnCount: prevColumnCount,
848+
height: prevHeight,
849+
width: prevWidth,
850+
} = this.prevMetrics;
851+
852+
if (prevRowCount !== rowCount || height !== prevHeight) {
853+
const { isStuckToBottom } = this.state;
854+
if (isStuckToBottom) {
855+
this.scrollToBottom();
856+
}
857+
}
858+
859+
if (prevColumnCount !== columnCount || width !== prevWidth) {
860+
const { isStuckToRight } = this.state;
861+
if (isStuckToRight) {
862+
this.scrollToRight();
863+
}
864+
}
865+
}
866+
this.prevMetrics = this.metrics;
867+
}
868+
853869
updateMetrics(state = this.state): GridMetrics {
854870
this.prevMetrics = this.metrics;
855871

0 commit comments

Comments
 (0)