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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

170 lines
5.5 KiB
TypeScript
Raw Normal View History

2020-12-26 08:32:51 +01:00
/*
2024-09-09 14:57:16 +01:00
Copyright 2024 New Vector Ltd.
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
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
2024-09-09 14:57:16 +01:00
Please see LICENSE files in the repository root for full details.
2020-12-26 08:32:51 +01:00
*/
import React from "react";
2021-10-22 17:23:32 -05:00
import classNames from "classnames";
import { _t, _td, TranslationKey } from "../../../languageHandler";
2021-06-29 13:11:58 +01:00
import BaseDialog from "..//dialogs/BaseDialog";
import DialogButtons from "./DialogButtons";
2020-12-26 16:56:25 +01:00
import AccessibleButton from "./AccessibleButton";
2021-07-16 20:50:29 +02:00
import TabbedView, { Tab, TabLocation } from "../../structures/TabbedView";
import PlatformPeg from "../../../PlatformPeg";
import { NonEmptyArray } from "../../../@types/common";
2020-12-26 08:32:51 +01:00
export function getDesktopCapturerSources(): Promise<Array<DesktopCapturerSource>> {
const options: GetSourcesOptions = {
thumbnailSize: {
height: 176,
width: 312,
},
types: ["screen", "window"],
};
const plaf = PlatformPeg.get();
return plaf ? plaf?.getDesktopCapturerSources(options) : Promise.resolve<DesktopCapturerSource[]>([]);
}
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> {
2021-07-16 20:50:29 +02:00
private onClick = (): void => {
2020-12-26 08:32:51 +01:00
this.props.onSelect(this.props.source);
2021-06-29 13:11:58 +01:00
};
2020-12-26 08:32:51 +01:00
public render(): React.ReactNode {
2021-07-16 21:16:07 +02:00
const thumbnailClasses = classNames({
mx_desktopCapturerSourcePicker_source_thumbnail: true,
mx_desktopCapturerSourcePicker_source_thumbnail_selected: this.props.selected,
2021-06-12 13:43:42 +02:00
});
2020-12-26 08:32:51 +01:00
return (
2020-12-26 16:56:25 +01:00
<AccessibleButton
2021-07-16 21:16:07 +02:00
className="mx_desktopCapturerSourcePicker_source"
2020-12-26 08:32:51 +01:00
title={this.props.source.name}
2021-07-16 20:50:29 +02:00
onClick={this.onClick}
>
<img alt={this.props.source.name} className={thumbnailClasses} src={this.props.source.thumbnailURL} />
<span className="mx_desktopCapturerSourcePicker_source_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>;
selectedSource?: DesktopCapturerSource;
2020-12-26 08:32:51 +01:00
}
2021-06-12 12:44:12 +02:00
export interface PickerIProps {
onFinished(source?: DesktopCapturerSource): void;
2020-12-26 08:32:51 +01:00
}
2020-12-26 16:56:25 +01:00
export default class DesktopCapturerSourcePicker extends React.Component<PickerIProps, PickerIState> {
public interval?: number;
2021-01-14 12:44:48 +01:00
public constructor(props: PickerIProps) {
2020-12-26 08:32:51 +01:00
super(props);
2020-12-26 16:56:25 +01:00
this.state = {
selectedTab: Tabs.Screens,
2021-01-14 12:44:48 +01:00
sources: [],
};
}
public async componentDidMount(): Promise<void> {
2022-11-30 11:32:56 +00:00
// window.setInterval() first waits and then executes, therefore
2021-02-09 13:07:23 +01:00
// 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
this.interval = window.setInterval(async (): Promise<void> => {
2021-01-14 12:44:48 +01:00
this.setState({
sources: await getDesktopCapturerSources(),
});
}, 500);
}
public componentWillUnmount(): void {
2021-01-14 12:44:48 +01:00
clearInterval(this.interval);
2020-12-26 08:32:51 +01:00
}
2021-07-16 20:50:29 +02:00
private onSelect = (source: DesktopCapturerSource): void => {
2021-06-12 13:43:42 +02:00
this.setState({ selectedSource: source });
2021-06-29 13:11:58 +01:00
};
2020-12-26 08:32:51 +01:00
2021-07-16 20:50:29 +02:00
private onShare = (): void => {
this.props.onFinished(this.state.selectedSource);
2021-06-29 13:11:58 +01:00
};
2020-12-26 08:32:51 +01:00
private onTabChange = (tab: Tabs): void => {
this.setState({ selectedSource: undefined, selectedTab: tab });
2021-06-29 13:11:58 +01:00
};
2020-12-26 16:56:25 +01:00
2021-07-16 20:50:29 +02:00
private onCloseClick = (): void => {
this.props.onFinished();
2021-06-29 13:11:58 +01:00
};
2020-12-26 16:56:25 +01:00
private getTab(type: Tabs, label: TranslationKey): Tab<Tabs> {
2021-07-16 20:50:29 +02:00
const sources = this.state.sources
.filter((source) => source.id.startsWith(type))
.map((source) => {
2021-06-12 13:43:42 +02:00
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-07-16 21:16:07 +02:00
return new Tab(type, label, null, <div className="mx_desktopCapturerSourcePicker_tab">{sources}</div>);
2021-07-16 20:50:29 +02:00
}
public render(): React.ReactNode {
const tabs: NonEmptyArray<Tab<Tabs>> = [
this.getTab(Tabs.Screens, _td("voip|screenshare_monitor")),
this.getTab(Tabs.Windows, _td("voip|screenshare_window")),
2021-07-16 20:50:29 +02:00
];
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}
title={_t("voip|screenshare_title")}
2020-12-26 16:56:25 +01:00
>
<TabbedView
tabs={tabs}
tabLocation={TabLocation.TOP}
activeTabId={this.state.selectedTab}
onChange={this.onTabChange}
/>
2021-06-12 13:43:42 +02:00
<DialogButtons
primaryButton={_t("action|share")}
2021-06-12 13:43:42 +02:00
hasCancel={true}
onCancel={this.onCloseClick}
onPrimaryButtonClick={this.onShare}
primaryDisabled={!this.state.selectedSource}
/>
2020-12-26 08:32:51 +01:00
</BaseDialog>
);
}
}