From 777d482e73ef04c2d4fce85c4a6ca76b09bf284a Mon Sep 17 00:00:00 2001 From: Jordan Eldredge Date: Wed, 19 Sep 2018 21:43:55 -0700 Subject: [PATCH] Center windows correctly, even if the windows don't start at 0,0 This is particularly noticeable when state has been rehydrated from a serialized state. --- js/components/WindowManager.js | 9 ++++++--- js/utils.ts | 22 +++++++++++++--------- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/js/components/WindowManager.js b/js/components/WindowManager.js index a31e5816..b5a91cad 100644 --- a/js/components/WindowManager.js +++ b/js/components/WindowManager.js @@ -64,12 +64,15 @@ class WindowManager extends React.Component { const boxWidth = bounding.right - bounding.left; const move = { - x: Math.ceil(offsetLeft + (width - boxWidth) / 2), - y: Math.ceil(offsetTop + (height - boxHeight) / 2) + x: Math.ceil(offsetLeft - bounding.left + (width - boxWidth) / 2), + y: Math.ceil(offsetTop - bounding.top + (height - boxHeight) / 2) }; const newPositions = this.props.windowsInfo.reduce( - (pos, w) => ({ ...pos, [w.key]: { x: move.x + w.x, y: move.y + w.y } }), + (pos, w) => ({ + ...pos, + [w.key]: { x: move.x + w.x, y: move.y + w.y } + }), {} ); diff --git a/js/utils.ts b/js/utils.ts index bebf80b5..5c2a4ef5 100644 --- a/js/utils.ts +++ b/js/utils.ts @@ -328,15 +328,19 @@ interface Window { } export const calculateBoundingBox = (windows: Window[]) => - windows.reduce( - (b, w) => ({ - left: Math.min(b.left, w.x), - top: Math.min(b.top, w.y), - bottom: Math.max(b.bottom, w.y + w.height), - right: Math.max(b.right, w.x + w.width) - }), - { top: 0, bottom: 0, left: 0, right: 0 } - ); + windows + .map(w => ({ + left: w.x, + top: w.y, + bottom: w.y + w.height, + right: w.x + w.width + })) + .reduce((b, w) => ({ + left: Math.min(b.left, w.left), + top: Math.min(b.top, w.top), + bottom: Math.max(b.bottom, w.bottom), + right: Math.max(b.right, w.right) + })); export function findLastIndex(arr: T[], cb: (val: T) => boolean) { for (let i = arr.length - 1; i >= 0; i--) {