diff --git a/js/actionCreators/windows.ts b/js/actionCreators/windows.ts index c57a7a3d..e26f5fdd 100644 --- a/js/actionCreators/windows.ts +++ b/js/actionCreators/windows.ts @@ -159,6 +159,11 @@ export function centerWindows(box: { windowsInfo.filter(w => getOpen(w.key)) ); + if (bounding == null) { + // There are no windows to center + return; + } + const boxHeight = bounding.bottom - bounding.top; const boxWidth = bounding.right - bounding.left; @@ -211,6 +216,10 @@ export function ensureWindowsAreOnScreen(): Thunk { const bounding = Utils.calculateBoundingBox( windowsInfo.filter(w => getOpen(w.key)) ); + if (bounding == null) { + // There are no windows visible, so there's no work to do. + return; + } const positions = Selectors.getWindowPositions(state); // Are we good? diff --git a/js/utils.ts b/js/utils.ts index 7802ed63..438b0dd2 100644 --- a/js/utils.ts +++ b/js/utils.ts @@ -336,20 +336,23 @@ export function objectFilter( }, {}); } -export const calculateBoundingBox = (windows: WindowInfo[]) => - 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 const calculateBoundingBox = (windows: WindowInfo[]) => { + if (windows.length === 0) { + return null; + } + const windowSizes = windows.map(w => ({ + left: w.x, + top: w.y, + bottom: w.y + w.height, + right: w.x + w.width, + })); + return windowSizes.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--) {