-
Notifications
You must be signed in to change notification settings - Fork 704
/
Copy pathDisplay.tsx
101 lines (87 loc) · 2.7 KB
/
Display.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import React, { useEffect, useRef } from 'react';
import { useVuex } from '../hooks';
import { Services } from '../service-provider';
import { Display as OBSDisplay } from '../../services/video';
import { TDisplayType } from 'services/settings-v2/video';
import uuid from 'uuid/v4';
import { useRealmObject } from 'components-react/hooks/realm';
interface DisplayProps {
id?: string;
sourceId?: string;
paddingSize?: number;
drawUI?: boolean;
renderingMode?: number;
onOutputResize?: (region: IRectangle) => void;
clickHandler?: (event: React.MouseEvent) => void;
style?: React.CSSProperties;
type?: TDisplayType;
}
export default function Display(props: DisplayProps) {
const { CustomizationService, VideoSettingsService } = Services;
const p = {
paddingSize: 0,
drawUI: false,
clickHandler: () => {},
onOutputResize: () => {},
type: props?.type ?? 'horizontal',
...props,
};
const v = useVuex(() => {
const videoSettings = VideoSettingsService.baseResolutions[p.type];
return {
baseResolution: `${videoSettings?.baseWidth}x${videoSettings?.baseHeight}`,
};
}, false);
const paddingColor = useRealmObject(CustomizationService.state).displayBackground;
const obsDisplay = useRef<OBSDisplay | null>(null);
const displayEl = useRef<HTMLDivElement>(null);
useEffect(updateDisplay, [p.sourceId, paddingColor]);
useEffect(refreshOutputRegion, [v.baseResolution]);
function refreshOutputRegion() {
if (!obsDisplay.current) return;
const [width, height] = v.baseResolution.split('x');
obsDisplay.current.resize(Number(width), Number(height));
}
function onClickHandler(event: React.MouseEvent) {
p.clickHandler(event);
}
async function createDisplay() {
const displayId = uuid();
obsDisplay.current = new OBSDisplay(displayId, {
sourceId: p.sourceId,
paddingSize: p.paddingSize,
paddingColor,
renderingMode: p.renderingMode,
type: p.type,
});
await refreshOutputRegion();
obsDisplay.current.setShoulddrawUI(p.drawUI);
obsDisplay.current.onOutputResize(region => p.onOutputResize(region));
if (displayEl.current) obsDisplay.current.trackElement(displayEl.current);
}
function destroyDisplay() {
if (obsDisplay.current) obsDisplay.current.destroy();
obsDisplay.current = null;
}
function updateDisplay() {
destroyDisplay();
createDisplay();
return function cleanup() {
destroyDisplay();
};
}
return (
<div
id={p?.id}
className="display"
ref={displayEl}
style={{
height: '100%',
backgroundColor: 'var(--section)',
flexGrow: 1,
...p.style,
}}
onClick={onClickHandler}
/>
);
}