Move maxSize to window functions

This commit is contained in:
Dustin Brett
2022-09-07 20:21:15 -07:00
parent 15ac9f0a50
commit 13a81946bb
4 changed files with 46 additions and 46 deletions

View File

@@ -1,10 +1,10 @@
import { maxSize } from "components/system/Window/functions";
import useDefaultSize from "components/system/Window/RndWindow/useDefaultSize";
import useMinMaxRef from "components/system/Window/RndWindow/useMinMaxRef";
import { useProcesses } from "contexts/process";
import { useSession } from "contexts/session";
import { useLayoutEffect, useState } from "react";
import type { Props } from "react-rnd";
import { maxSize } from "utils/functions";
export type Size = NonNullable<Props["size"]>;

View File

@@ -74,3 +74,47 @@ export const isWindowOutsideBounds = (
y + pxToNum(height) > bounds.y
);
};
export const maxSize = (size: Size, lockAspectRatio: boolean): Size => {
const desiredHeight = Number(size.height);
const desiredWidth = Number(size.width);
const [vh, vw] = [viewHeight(), viewWidth()];
const vhWithoutTaskbar = vh - TASKBAR_HEIGHT;
const height = Math.min(desiredHeight, vhWithoutTaskbar);
const width = Math.min(desiredWidth, vw);
if (!lockAspectRatio) return { height, width };
const isDesiredHeight = desiredHeight === height;
const isDesiredWidth = desiredWidth === width;
if (!isDesiredHeight && !isDesiredWidth) {
if (desiredHeight > desiredWidth) {
return {
height,
width: Math.round(width / (vhWithoutTaskbar / height)),
};
}
return {
height: Math.round(height / (vw / width)),
width,
};
}
if (!isDesiredHeight) {
return {
height,
width: Math.round(width / (desiredHeight / height)),
};
}
if (!isDesiredWidth) {
return {
height: Math.round(height / (desiredWidth / width)),
width,
};
}
return { height, width };
};

View File

@@ -1,8 +1,8 @@
import { maxSize } from "components/system/Window/functions";
import { useProcesses } from "contexts/process";
import { useSession } from "contexts/session";
import { useCallback } from "react";
import { useTheme } from "styled-components";
import { maxSize } from "utils/functions";
type WindowSize = {
updateWindowSize: (height: number, width: number) => void;

View File

@@ -425,50 +425,6 @@ export const isCanvasDrawn = (canvas?: HTMLCanvasElement | null): boolean =>
.data.some((channel) => channel !== 0)
);
export const maxSize = (size: Size, lockAspectRatio: boolean): Size => {
const desiredHeight = Number(size.height);
const desiredWidth = Number(size.width);
const [vh, vw] = [viewHeight(), viewWidth()];
const vhWithoutTaskbar = vh - TASKBAR_HEIGHT;
const height = Math.min(desiredHeight, vhWithoutTaskbar);
const width = Math.min(desiredWidth, vw);
if (!lockAspectRatio) return { height, width };
const isDesiredHeight = desiredHeight === height;
const isDesiredWidth = desiredWidth === width;
if (!isDesiredHeight && !isDesiredWidth) {
if (desiredHeight > desiredWidth) {
return {
height,
width: Math.round(width / (vhWithoutTaskbar / height)),
};
}
return {
height: Math.round(height / (vw / width)),
width,
};
}
if (!isDesiredHeight) {
return {
height,
width: Math.round(width / (desiredHeight / height)),
};
}
if (!isDesiredWidth) {
return {
height: Math.round(height / (desiredWidth / width)),
width,
};
}
return { height, width };
};
const bytesInKB = 1024;
const bytesInMB = 1022976; // 1024 * 999;
const bytesInGB = 1047527424; // 1024 * 1024 * 999;