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.
This commit is contained in:
Jordan Eldredge 2018-09-19 21:43:55 -07:00
parent 87ca43ba45
commit 777d482e73
2 changed files with 19 additions and 12 deletions

View file

@ -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 }
}),
{}
);

View file

@ -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<T>(arr: T[], cb: (val: T) => boolean) {
for (let i = arr.length - 1; i >= 0; i--) {