lbry-desktop/ui/component/fileRenderFloating/helper-functions.js
saltrafael 17e3fcc27c
[Fix] revert fileRenderFloating vs Mobile change (#937)
* Bump react-draggable

Old version was giving out console errors for outdated react functions

* Refactor fileRenderFloating

* Merge fileRenderMobile into fileRenderFloating

* Fixes from review

* Attempt fix failed to view live
2022-02-23 16:13:22 -05:00

54 lines
1.2 KiB
JavaScript

// @flow
import { FLOATING_PLAYER_CLASS } from './view';
function getRootEl() {
return document && document.documentElement;
}
export function getScreenWidth() {
const mainEl = getRootEl();
return mainEl ? mainEl.clientWidth : window.innerWidth;
}
export function getScreenHeight() {
const mainEl = getRootEl();
return mainEl ? mainEl.clientHeight : window.innerHeight;
}
export function getFloatingPlayerRect() {
const elem = document.querySelector(`.${FLOATING_PLAYER_CLASS}`);
return elem ? elem.getBoundingClientRect() : null;
}
export function clampFloatingPlayerToScreen(x: number, y: number) {
const playerRect = getFloatingPlayerRect();
let newX = x;
let newY = y;
if (playerRect) {
const screenW = getScreenWidth();
const screenH = getScreenHeight();
if (x + playerRect.width > screenW) {
newX = screenW - playerRect.width;
} else if (x < 0) {
newX = 0;
}
if (y + playerRect.height > screenH) {
newY = screenH - playerRect.height;
} else if (y < 0) {
newY = 0;
}
}
return { x: newX, y: newY };
}
export function calculateRelativePos(x: number, y: number) {
return {
x: x / getScreenWidth(),
y: y / getScreenHeight(),
};
}