lbry-desktop/ui/component/fileRenderFloating/view.jsx

305 lines
9.2 KiB
React
Raw Normal View History

2018-03-26 23:32:43 +02:00
// @flow
2019-08-13 07:35:13 +02:00
import * as ICONS from 'constants/icons';
import * as RENDER_MODES from 'constants/file_render_modes';
import React, { useEffect, useState } from 'react';
2019-08-13 07:35:13 +02:00
import Button from 'component/button';
2018-03-26 23:32:43 +02:00
import classnames from 'classnames';
import LoadingScreen from 'component/common/loading-screen';
2019-08-02 08:28:14 +02:00
import FileRender from 'component/fileRender';
2019-08-13 07:35:13 +02:00
import UriIndicator from 'component/uriIndicator';
2019-09-27 20:56:15 +02:00
import usePersistedState from 'effects/use-persisted-state';
import { PRIMARY_PLAYER_WRAPPER_CLASS } from 'page/file/view';
2019-08-13 07:35:13 +02:00
import Draggable from 'react-draggable';
2019-10-14 00:28:12 +02:00
import { onFullscreenChange } from 'util/full-screen';
2020-08-10 22:47:39 +02:00
import { useIsMobile } from 'effects/use-screensize';
import debounce from 'util/debounce';
import { useHistory } from 'react-router';
const IS_DESKTOP_MAC = typeof process === 'object' ? process.platform === 'darwin' : false;
const DEBOUNCE_WINDOW_RESIZE_HANDLER_MS = 60;
export const INLINE_PLAYER_WRAPPER_CLASS = 'inline-player__wrapper';
2018-05-16 21:34:38 +02:00
2018-03-26 23:32:43 +02:00
type Props = {
2020-04-14 01:48:11 +02:00
isFloating: boolean,
2019-08-06 05:25:33 +02:00
fileInfo: FileListItem,
2018-03-26 23:32:43 +02:00
uri: string,
2019-08-02 08:28:14 +02:00
streamingUrl?: string,
2019-08-13 07:35:13 +02:00
title: ?string,
floatingPlayerEnabled: boolean,
closeFloatingPlayer: () => void,
renderMode: string,
playingUri: ?PlayingUri,
primaryUri: ?string,
2021-01-08 16:21:27 +01:00
videoTheaterMode: boolean,
2018-03-26 23:32:43 +02:00
};
2017-04-23 11:56:50 +02:00
2020-04-14 01:48:11 +02:00
export default function FileRenderFloating(props: Props) {
const {
fileInfo,
uri,
streamingUrl,
title,
isFloating,
closeFloatingPlayer,
floatingPlayerEnabled,
renderMode,
playingUri,
primaryUri,
2021-01-08 16:21:27 +01:00
videoTheaterMode,
} = props;
const {
location: { pathname },
} = useHistory();
2019-12-18 06:27:08 +01:00
const isMobile = useIsMobile();
const mainFilePlaying = playingUri && playingUri.uri === primaryUri;
const [fileViewerRect, setFileViewerRect] = useState();
const [desktopPlayStartTime, setDesktopPlayStartTime] = useState();
const [wasDragging, setWasDragging] = useState(false);
2019-08-13 07:35:13 +02:00
const [position, setPosition] = usePersistedState('floating-file-viewer:position', {
x: -25,
y: window.innerHeight - 400,
});
const [relativePos, setRelativePos] = useState({
x: 0,
y: 0,
});
2020-04-14 01:48:11 +02:00
const playingUriSource = playingUri && playingUri.source;
const isPlayable = RENDER_MODES.FLOATING_MODES.includes(renderMode);
const isReadyToPlay = isPlayable && (streamingUrl || (fileInfo && fileInfo.completed));
2019-08-02 08:28:14 +02:00
const loadingMessage =
fileInfo && fileInfo.blobs_completed >= 1 && (!fileInfo.download_path || !fileInfo.written_bytes)
2019-08-02 08:28:14 +02:00
? __("It looks like you deleted or moved this file. We're rebuilding it now. It will only take a few seconds.")
: __('Loading');
2019-08-14 05:04:08 +02:00
function getScreenWidth() {
if (document && document.documentElement) {
return document.documentElement.clientWidth;
} else {
return window.innerWidth;
}
}
function getScreenHeight() {
if (document && document.documentElement) {
return document.documentElement.clientHeight;
} else {
return window.innerHeight;
}
}
function clampToScreen(pos) {
const GAP_PX = 10;
const ESTIMATED_SCROLL_BAR_PX = 50;
const FLOATING_PLAYER_CLASS = 'content__viewer--floating';
const fpPlayerElem = document.querySelector(`.${FLOATING_PLAYER_CLASS}`);
if (fpPlayerElem) {
if (pos.x + fpPlayerElem.getBoundingClientRect().width > getScreenWidth() - ESTIMATED_SCROLL_BAR_PX) {
pos.x = getScreenWidth() - fpPlayerElem.getBoundingClientRect().width - ESTIMATED_SCROLL_BAR_PX - GAP_PX;
}
if (pos.y + fpPlayerElem.getBoundingClientRect().height > getScreenHeight()) {
pos.y = getScreenHeight() - fpPlayerElem.getBoundingClientRect().height - GAP_PX * 2;
}
}
}
// Updated 'relativePos' based on persisted 'position':
2020-12-01 18:56:59 +01:00
const stringifiedPosition = JSON.stringify(position);
useEffect(() => {
2020-12-01 18:56:59 +01:00
const jsonPosition = JSON.parse(stringifiedPosition);
setRelativePos({
2020-12-01 18:56:59 +01:00
x: jsonPosition.x / getScreenWidth(),
y: jsonPosition.y / getScreenHeight(),
});
2020-12-01 18:56:59 +01:00
}, [stringifiedPosition]);
// Ensure player is within screen when 'isFloating' changes.
useEffect(() => {
const jsonPosition = JSON.parse(stringifiedPosition);
if (isFloating) {
let pos = { x: jsonPosition.x, y: jsonPosition.y };
clampToScreen(pos);
if (pos.x !== position.x || pos.y !== position.y) {
setPosition({ x: pos.x, y: pos.y });
}
}
}, [isFloating, stringifiedPosition]);
// Listen to main-window resizing and adjust the fp position accordingly:
useEffect(() => {
const handleMainWindowResize = debounce(e => {
let newPos = {
x: Math.round(relativePos.x * getScreenWidth()),
y: Math.round(relativePos.y * getScreenHeight()),
};
clampToScreen(newPos);
setPosition({ x: newPos.x, y: newPos.y });
}, DEBOUNCE_WINDOW_RESIZE_HANDLER_MS);
window.addEventListener('resize', handleMainWindowResize);
return () => window.removeEventListener('resize', handleMainWindowResize);
// 'relativePos' is needed in the dependency list to avoid stale closure.
// Otherwise, this could just be changed to a one-time effect.
}, [relativePos]);
function handleResize() {
const element = mainFilePlaying
? document.querySelector(`.${PRIMARY_PLAYER_WRAPPER_CLASS}`)
: document.querySelector(`.${INLINE_PLAYER_WRAPPER_CLASS}`);
if (!element) {
return;
}
const rect = element.getBoundingClientRect();
// getBoundingCLientRect returns a DomRect, not an object
const objectRect = {
top: rect.top,
right: rect.right,
bottom: rect.bottom,
left: rect.left,
width: rect.width,
height: rect.height,
2020-11-09 19:36:35 +01:00
// $FlowFixMe
x: rect.x,
};
// $FlowFixMe
setFileViewerRect({ ...objectRect, windowOffset: window.pageYOffset });
}
2019-08-13 07:35:13 +02:00
useEffect(() => {
if (streamingUrl) {
handleResize();
2019-08-13 07:35:13 +02:00
}
}, [streamingUrl, pathname, playingUriSource, isFloating, mainFilePlaying]);
2019-08-13 07:35:13 +02:00
useEffect(() => {
handleResize();
window.addEventListener('resize', handleResize);
onFullscreenChange(window, 'add', handleResize);
2019-12-19 21:43:49 +01:00
return () => {
window.removeEventListener('resize', handleResize);
onFullscreenChange(window, 'remove', handleResize);
};
2021-01-08 16:21:27 +01:00
}, [setFileViewerRect, isFloating, playingUriSource, mainFilePlaying, videoTheaterMode]);
useEffect(() => {
// @if TARGET='app'
setDesktopPlayStartTime(Date.now());
// @endif
return () => {
// @if TARGET='app'
setDesktopPlayStartTime(undefined);
// @endif
};
}, [uri]);
2020-04-16 23:43:09 +02:00
if (!isPlayable || !uri || (isFloating && (isMobile || !floatingPlayerEnabled))) {
return null;
}
function handleDragStart(e, ui) {
// Not really necessary, but reset just in case 'handleStop' didn't fire.
setWasDragging(false);
}
function handleDragMove(e, ui) {
setWasDragging(true);
2019-08-14 05:04:08 +02:00
const { x, y } = position;
const newX = x + ui.deltaX;
const newY = y + ui.deltaY;
setPosition({
x: newX,
y: newY,
});
}
function handleDragStop(e, ui) {
if (wasDragging) {
e.stopPropagation();
setWasDragging(false);
setRelativePos({
x: position.x / getScreenWidth(),
y: position.y / getScreenHeight(),
});
}
}
2019-08-02 08:28:14 +02:00
return (
2019-08-13 07:35:13 +02:00
<Draggable
onDrag={handleDragMove}
onStart={handleDragStart}
onStop={handleDragStop}
2019-08-13 07:35:13 +02:00
defaultPosition={position}
2020-04-14 01:48:11 +02:00
position={isFloating ? position : { x: 0, y: 0 }}
2019-08-13 07:35:13 +02:00
bounds="parent"
2020-04-14 01:48:11 +02:00
disabled={!isFloating}
handle=".draggable"
2019-08-13 07:35:13 +02:00
cancel=".button"
2019-08-02 08:28:14 +02:00
>
2019-08-13 07:35:13 +02:00
<div
className={classnames('content__viewer', {
2020-04-14 01:48:11 +02:00
'content__viewer--floating': isFloating,
'content__viewer--inline': !isFloating,
2021-01-08 16:21:27 +01:00
'content__viewer--theater-mode': !isFloating && videoTheaterMode,
2019-08-13 07:35:13 +02:00
})}
style={
2020-04-14 01:48:11 +02:00
!isFloating && fileViewerRect
? {
width: fileViewerRect.width,
height: fileViewerRect.height,
left: fileViewerRect.x,
// 80px is header height in scss/init/vars.scss
top: fileViewerRect.windowOffset + fileViewerRect.top - 80 - (IS_DESKTOP_MAC ? 24 : 0),
}
2019-08-13 07:35:13 +02:00
: {}
}
>
<div
className={classnames('content__wrapper', {
2020-04-14 01:48:11 +02:00
'content__wrapper--floating': isFloating,
2019-08-02 08:28:14 +02:00
})}
2019-08-13 07:35:13 +02:00
>
2020-04-14 01:48:11 +02:00
{isFloating && (
<Button
title={__('Close')}
onClick={closeFloatingPlayer}
icon={ICONS.REMOVE}
button="primary"
className="content__floating-close"
/>
2019-08-13 07:35:13 +02:00
)}
{isReadyToPlay ? (
<FileRender
2020-07-22 22:56:58 +02:00
className="draggable"
uri={uri}
// @if TARGET='app'
desktopPlayStartTime={desktopPlayStartTime}
// @endif
/>
) : (
<LoadingScreen status={loadingMessage} />
)}
2020-04-14 01:48:11 +02:00
{isFloating && (
<div className="draggable content__info">
2020-01-30 23:25:15 +01:00
<div className="claim-preview__title" title={title || uri}>
2020-07-22 22:56:58 +02:00
<Button label={title || uri} navigate={uri} button="link" className="content__floating-link" />
2019-08-13 07:35:13 +02:00
</div>
<UriIndicator link uri={uri} />
2019-08-13 07:35:13 +02:00
</div>
)}
</div>
</div>
</Draggable>
2019-08-02 08:28:14 +02:00
);
2017-04-23 11:56:50 +02:00
}