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
|
|
|
|
2025-01-06 11:18:54 +00: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";
|
|
|
|
|
|
2023-08-22 16:32:05 +01:00
|
|
|
import { _t, _td, TranslationKey } from "../../../languageHandler";
|
2021-06-29 13:11:58 +01:00
|
|
|
import BaseDialog from "..//dialogs/BaseDialog";
|
2021-07-07 10:53:22 +02:00
|
|
|
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";
|
2022-02-11 11:41:15 +01:00
|
|
|
import PlatformPeg from "../../../PlatformPeg";
|
2023-03-10 14:55:06 +00:00
|
|
|
import { NonEmptyArray } from "../../../@types/common";
|
2020-12-26 08:32:51 +01:00
|
|
|
|
2021-09-01 18:17:52 +02:00
|
|
|
export function getDesktopCapturerSources(): Promise<Array<DesktopCapturerSource>> {
|
|
|
|
|
const options: GetSourcesOptions = {
|
|
|
|
|
thumbnailSize: {
|
|
|
|
|
height: 176,
|
|
|
|
|
width: 312,
|
|
|
|
|
},
|
|
|
|
|
types: ["screen", "window"],
|
|
|
|
|
};
|
2023-03-13 15:07:20 +00:00
|
|
|
const plaf = PlatformPeg.get();
|
|
|
|
|
return plaf ? plaf?.getDesktopCapturerSources(options) : Promise.resolve<DesktopCapturerSource[]>([]);
|
2021-09-01 18:17:52 +02:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
2023-02-13 17:01:43 +00: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}
|
|
|
|
|
>
|
2023-03-15 10:25:11 +13:00
|
|
|
<img alt={this.props.source.name} className={thumbnailClasses} src={this.props.source.thumbnailURL} />
|
2021-07-20 13:24:05 +02:00
|
|
|
<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>;
|
2023-07-14 22:51:24 +02:00
|
|
|
selectedSource?: DesktopCapturerSource;
|
2020-12-26 08:32:51 +01:00
|
|
|
}
|
2021-06-12 12:44:12 +02:00
|
|
|
export interface PickerIProps {
|
2023-07-14 22:51:24 +02:00
|
|
|
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> {
|
2023-05-10 08:41:55 +01:00
|
|
|
public interval?: number;
|
2021-01-14 12:44:48 +01:00
|
|
|
|
2022-12-16 12:29:59 +00: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: [],
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
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
|
2023-01-12 13:25:14 +00:00
|
|
|
this.interval = window.setInterval(async (): Promise<void> => {
|
2021-01-14 12:44:48 +01:00
|
|
|
this.setState({
|
|
|
|
|
sources: await getDesktopCapturerSources(),
|
|
|
|
|
});
|
|
|
|
|
}, 500);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
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 => {
|
2023-07-14 22:51:24 +02:00
|
|
|
this.props.onFinished(this.state.selectedSource);
|
2021-06-29 13:11:58 +01:00
|
|
|
};
|
2020-12-26 08:32:51 +01:00
|
|
|
|
2024-05-03 16:01:01 +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 => {
|
2023-03-10 14:55:06 +00:00
|
|
|
this.props.onFinished();
|
2021-06-29 13:11:58 +01:00
|
|
|
};
|
2020-12-26 16:56:25 +01:00
|
|
|
|
2024-05-03 16:01:01 +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
|
|
|
}
|
|
|
|
|
|
2023-02-13 17:01:43 +00:00
|
|
|
public render(): React.ReactNode {
|
2024-05-03 16:01:01 +01:00
|
|
|
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}
|
2023-09-25 18:12:41 +01:00
|
|
|
title={_t("voip|screenshare_title")}
|
2020-12-26 16:56:25 +01:00
|
|
|
>
|
2024-05-03 16:01:01 +01:00
|
|
|
<TabbedView
|
|
|
|
|
tabs={tabs}
|
|
|
|
|
tabLocation={TabLocation.TOP}
|
|
|
|
|
activeTabId={this.state.selectedTab}
|
|
|
|
|
onChange={this.onTabChange}
|
|
|
|
|
/>
|
2021-06-12 13:43:42 +02:00
|
|
|
<DialogButtons
|
2023-08-23 11:57:22 +01:00
|
|
|
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>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|