Files
ThreadNet-Web/src/components/views/elements/DesktopCapturerSourcePicker.tsx
T

200 lines
6.1 KiB
TypeScript
Raw Normal View History

2020-12-26 08:32:51 +01:00
/*
2021-01-05 20:36:54 +01:00
Copyright 2021 Šimon Brandner <simon.bra.ag@gmail.com>
2020-12-26 08:32:51 +01:00
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
2021-01-05 20:41:33 +01:00
http://www.apache.org/licenses/LICENSE-2.0
2020-12-26 08:32:51 +01:00
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import React from 'react';
2021-01-05 20:27:11 +01:00
import { _t } from '../../../languageHandler';
2020-12-26 08:32:51 +01:00
import BaseDialog from "..//dialogs/BaseDialog"
2021-06-12 13:43:42 +02:00
import DialogButtons from "./DialogButtons"
2020-12-26 16:56:25 +01:00
import AccessibleButton from './AccessibleButton';
2021-06-12 13:43:42 +02:00
import { getDesktopCapturerSources } from "matrix-js-sdk/src/webrtc/call";
import { replaceableComponent } from "../../../utils/replaceableComponent";
import classNames from 'classnames';
2020-12-26 08:32:51 +01:00
2021-02-01 15:53:53 +01:00
export interface DesktopCapturerSource {
id: string;
name: string;
thumbnailURL;
}
2020-12-26 16:56:25 +01:00
export enum Tabs {
2021-06-12 13:43:42 +02:00
Screens = "screen",
Windows = "window",
2020-12-26 16:56:25 +01:00
}
2020-12-26 18:10:50 +01:00
2021-06-12 12:44:12 +02:00
export interface ExistingSourceIProps {
2020-12-28 08:15:54 +01:00
source: DesktopCapturerSource;
onSelect(source: DesktopCapturerSource): void;
2021-06-12 13:43:42 +02:00
selected: boolean;
2020-12-26 08:32:51 +01:00
}
2021-06-12 12:44:12 +02:00
export class ExistingSource extends React.Component<ExistingSourceIProps> {
2020-12-26 08:32:51 +01:00
constructor(props) {
super(props);
}
onClick = (ev) => {
this.props.onSelect(this.props.source);
}
render() {
2021-06-12 13:43:42 +02:00
const classes = classNames({
mx_desktopCapturerSourcePicker_stream_button: true,
mx_desktopCapturerSourcePicker_stream_button_selected: this.props.selected,
});
2020-12-26 08:32:51 +01:00
return (
2020-12-26 16:56:25 +01:00
<AccessibleButton
2021-06-12 13:43:42 +02:00
className={classes}
2020-12-26 08:32:51 +01:00
title={this.props.source.name}
onClick={this.onClick} >
<img
2021-01-05 19:49:09 +01:00
className="mx_desktopCapturerSourcePicker_stream_thumbnail"
2021-01-14 08:35:04 +01:00
src={this.props.source.thumbnailURL}
2020-12-26 08:32:51 +01:00
/>
2021-01-05 19:49:09 +01:00
<span className="mx_desktopCapturerSourcePicker_stream_name">{this.props.source.name}</span>
2020-12-26 16:56:25 +01:00
</AccessibleButton>
2020-12-26 08:32:51 +01:00
);
}
}
2021-06-12 12:44:12 +02:00
export interface PickerIState {
2020-12-26 16:56:25 +01:00
selectedTab: Tabs;
2021-01-14 12:44:48 +01:00
sources: Array<DesktopCapturerSource>;
2021-06-12 13:43:42 +02:00
selectedSource: DesktopCapturerSource | null;
2020-12-26 08:32:51 +01:00
}
2021-06-12 12:44:12 +02:00
export interface PickerIProps {
2020-12-28 08:15:54 +01:00
onFinished(source: DesktopCapturerSource): void;
2020-12-26 08:32:51 +01:00
}
@replaceableComponent("views.elements.DesktopCapturerSourcePicker")
2020-12-26 16:56:25 +01:00
export default class DesktopCapturerSourcePicker extends React.Component<
2021-06-12 12:44:12 +02:00
PickerIProps,
PickerIState
2020-12-26 16:56:25 +01:00
> {
2021-01-14 12:44:48 +01:00
interval;
2020-12-26 08:32:51 +01:00
constructor(props) {
super(props);
2020-12-26 16:56:25 +01:00
this.state = {
selectedTab: Tabs.Screens,
2021-01-14 12:44:48 +01:00
sources: [],
2021-06-12 13:43:42 +02:00
selectedSource: null,
2021-01-14 12:44:48 +01:00
};
}
2021-02-09 13:19:12 +01:00
async componentDidMount() {
2021-02-09 13:07:23 +01:00
// setInterval() first waits and then executes, therefore
// we call getDesktopCapturerSources() here without any delay.
// Otherwise the dialog would be left empty for some time.
2021-02-09 13:19:12 +01:00
this.setState({
sources: await getDesktopCapturerSources(),
2021-02-09 13:07:23 +01:00
});
2021-02-01 16:03:20 +01:00
// We update the sources every 500ms to get newer thumbnails
2021-01-14 12:44:48 +01:00
this.interval = setInterval(async () => {
this.setState({
sources: await getDesktopCapturerSources(),
});
}, 500);
}
componentWillUnmount() {
clearInterval(this.interval);
2020-12-26 08:32:51 +01:00
}
onSelect = (source) => {
2021-06-12 13:43:42 +02:00
this.setState({ selectedSource: source });
2020-12-26 08:32:51 +01:00
}
2021-06-12 13:43:42 +02:00
onShare = () => {
this.props.onFinished(this.state.selectedSource);
2020-12-26 16:56:25 +01:00
}
2020-12-26 08:32:51 +01:00
2021-06-12 13:43:42 +02:00
onScreensClick = () => {
if (this.state.selectedTab === Tabs.Screens) return;
this.setState({
selectedTab: Tabs.Screens,
selectedSource: null,
});
2020-12-26 16:56:25 +01:00
}
2021-06-12 13:43:42 +02:00
onWindowsClick = () => {
if (this.state.selectedTab === Tabs.Windows) return;
this.setState({
selectedTab: Tabs.Windows,
selectedSource: null,
});
}
onCloseClick = () => {
2020-12-26 16:56:25 +01:00
this.props.onFinished(null);
}
render() {
2021-06-12 13:43:42 +02:00
const sources = this.state.sources.filter((source) => {
return source.id.startsWith(this.state.selectedTab)
});
const sourceElements = sources.map((source) => {
return (
<ExistingSource
selected={this.state.selectedSource?.id === source.id}
source={source}
onSelect={this.onSelect}
key={source.id}
/>
);
});
2021-01-05 21:08:25 +01:00
2021-01-05 19:49:09 +01:00
const buttonStyle = "mx_desktopCapturerSourcePicker_tabLabel";
2020-12-26 16:56:25 +01:00
const screensButtonStyle = buttonStyle + ((this.state.selectedTab === Tabs.Screens) ? "_selected" : "");
const windowsButtonStyle = buttonStyle + ((this.state.selectedTab === Tabs.Windows) ? "_selected" : "");
2020-12-26 08:32:51 +01:00
return (
2020-12-26 16:56:25 +01:00
<BaseDialog
2021-01-05 19:49:09 +01:00
className="mx_desktopCapturerSourcePicker"
2020-12-26 16:56:25 +01:00
onFinished={this.onCloseClick}
2021-01-05 20:27:11 +01:00
title={_t("Share your screen")}
2020-12-26 16:56:25 +01:00
>
2021-01-05 19:49:09 +01:00
<div className="mx_desktopCapturerSourcePicker_tabLabels">
2020-12-26 16:56:25 +01:00
<AccessibleButton
className={screensButtonStyle}
onClick={this.onScreensClick}
>
2021-01-05 20:27:11 +01:00
{_t("Screens")}
2020-12-26 16:56:25 +01:00
</AccessibleButton>
<AccessibleButton
className={windowsButtonStyle}
onClick={this.onWindowsClick}
>
2021-01-05 20:27:11 +01:00
{_t("Windows")}
2020-12-26 16:56:25 +01:00
</AccessibleButton>
2020-12-26 08:32:51 +01:00
</div>
2021-01-05 19:49:09 +01:00
<div className="mx_desktopCapturerSourcePicker_panel">
2021-06-12 13:43:42 +02:00
{ sourceElements }
2020-12-26 08:32:51 +01:00
</div>
2021-06-12 13:43:42 +02:00
<DialogButtons
primaryButton={_t("Share")}
hasCancel={true}
onCancel={this.onCloseClick}
onPrimaryButtonClick={this.onShare}
primaryDisabled={!this.state.selectedSource}
/>
2020-12-26 08:32:51 +01:00
</BaseDialog>
);
}
}