Handle case where there are no windows and thus no sensible bounding box

Resolves Sentry issue WINAMP2-JS-PROD-GC
This commit is contained in:
Jordan Eldredge 2019-10-22 08:28:51 -07:00
parent e57a958613
commit 328e9f8074
2 changed files with 26 additions and 14 deletions

View file

@ -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?

View file

@ -336,20 +336,23 @@ export function objectFilter<V>(
}, {});
}
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<T>(arr: T[], cb: (val: T) => boolean) {
for (let i = arr.length - 1; i >= 0; i--) {