|
| 1 | +/* |
| 2 | +Copyright 2021 Šimon Brandner <[email protected]> |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +import React from 'react'; |
| 18 | +import { _t } from '../../../languageHandler'; |
| 19 | +import BaseDialog from "..//dialogs/BaseDialog" |
| 20 | +import AccessibleButton from './AccessibleButton'; |
| 21 | +import {getDesktopCapturerSources} from "matrix-js-sdk/src/webrtc/call"; |
| 22 | + |
| 23 | +export interface DesktopCapturerSource { |
| 24 | + id: string; |
| 25 | + name: string; |
| 26 | + thumbnailURL; |
| 27 | +} |
| 28 | + |
| 29 | +export enum Tabs { |
| 30 | + Screens = "screens", |
| 31 | + Windows = "windows", |
| 32 | +} |
| 33 | + |
| 34 | +export interface DesktopCapturerSourceIProps { |
| 35 | + source: DesktopCapturerSource; |
| 36 | + onSelect(source: DesktopCapturerSource): void; |
| 37 | +} |
| 38 | + |
| 39 | +export class ExistingSource extends React.Component<DesktopCapturerSourceIProps> { |
| 40 | + constructor(props) { |
| 41 | + super(props); |
| 42 | + } |
| 43 | + |
| 44 | + onClick = (ev) => { |
| 45 | + this.props.onSelect(this.props.source); |
| 46 | + } |
| 47 | + |
| 48 | + render() { |
| 49 | + return ( |
| 50 | + <AccessibleButton |
| 51 | + className="mx_desktopCapturerSourcePicker_stream_button" |
| 52 | + title={this.props.source.name} |
| 53 | + onClick={this.onClick} > |
| 54 | + <img |
| 55 | + className="mx_desktopCapturerSourcePicker_stream_thumbnail" |
| 56 | + src={this.props.source.thumbnailURL} |
| 57 | + /> |
| 58 | + <span className="mx_desktopCapturerSourcePicker_stream_name">{this.props.source.name}</span> |
| 59 | + </AccessibleButton> |
| 60 | + ); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +export interface DesktopCapturerSourcePickerIState { |
| 65 | + selectedTab: Tabs; |
| 66 | + sources: Array<DesktopCapturerSource>; |
| 67 | +} |
| 68 | +export interface DesktopCapturerSourcePickerIProps { |
| 69 | + onFinished(source: DesktopCapturerSource): void; |
| 70 | +} |
| 71 | + |
| 72 | +export default class DesktopCapturerSourcePicker extends React.Component< |
| 73 | + DesktopCapturerSourcePickerIProps, |
| 74 | + DesktopCapturerSourcePickerIState |
| 75 | + > { |
| 76 | + interval; |
| 77 | + |
| 78 | + constructor(props) { |
| 79 | + super(props); |
| 80 | + |
| 81 | + this.state = { |
| 82 | + selectedTab: Tabs.Screens, |
| 83 | + sources: [], |
| 84 | + }; |
| 85 | + } |
| 86 | + |
| 87 | + componentDidMount() { |
| 88 | + // We update the sources every 500ms to get newer thumbnails |
| 89 | + this.interval = setInterval(async () => { |
| 90 | + this.setState({ |
| 91 | + sources: await getDesktopCapturerSources(), |
| 92 | + }); |
| 93 | + }, 500); |
| 94 | + } |
| 95 | + |
| 96 | + componentWillUnmount() { |
| 97 | + clearInterval(this.interval); |
| 98 | + } |
| 99 | + |
| 100 | + onSelect = (source) => { |
| 101 | + this.props.onFinished(source); |
| 102 | + } |
| 103 | + |
| 104 | + onScreensClick = (ev) => { |
| 105 | + this.setState({selectedTab: Tabs.Screens}); |
| 106 | + } |
| 107 | + |
| 108 | + onWindowsClick = (ev) => { |
| 109 | + this.setState({selectedTab: Tabs.Windows}); |
| 110 | + } |
| 111 | + |
| 112 | + onCloseClick = (ev) => { |
| 113 | + this.props.onFinished(null); |
| 114 | + } |
| 115 | + |
| 116 | + render() { |
| 117 | + let sources; |
| 118 | + if (this.state.selectedTab === Tabs.Screens) { |
| 119 | + sources = this.state.sources |
| 120 | + .filter((source) => { |
| 121 | + return source.id.startsWith("screen"); |
| 122 | + }) |
| 123 | + .map((source) => { |
| 124 | + return <ExistingSource source={source} onSelect={this.onSelect} key={source.id} />; |
| 125 | + }); |
| 126 | + } else { |
| 127 | + sources = this.state.sources |
| 128 | + .filter((source) => { |
| 129 | + return source.id.startsWith("window"); |
| 130 | + }) |
| 131 | + .map((source) => { |
| 132 | + return <ExistingSource source={source} onSelect={this.onSelect} key={source.id} />; |
| 133 | + }); |
| 134 | + } |
| 135 | + |
| 136 | + const buttonStyle = "mx_desktopCapturerSourcePicker_tabLabel"; |
| 137 | + const screensButtonStyle = buttonStyle + ((this.state.selectedTab === Tabs.Screens) ? "_selected" : ""); |
| 138 | + const windowsButtonStyle = buttonStyle + ((this.state.selectedTab === Tabs.Windows) ? "_selected" : ""); |
| 139 | + |
| 140 | + return ( |
| 141 | + <BaseDialog |
| 142 | + className="mx_desktopCapturerSourcePicker" |
| 143 | + onFinished={this.onCloseClick} |
| 144 | + title={_t("Share your screen")} |
| 145 | + > |
| 146 | + <div className="mx_desktopCapturerSourcePicker_tabLabels"> |
| 147 | + <AccessibleButton |
| 148 | + className={screensButtonStyle} |
| 149 | + onClick={this.onScreensClick} |
| 150 | + > |
| 151 | + {_t("Screens")} |
| 152 | + </AccessibleButton> |
| 153 | + <AccessibleButton |
| 154 | + className={windowsButtonStyle} |
| 155 | + onClick={this.onWindowsClick} |
| 156 | + > |
| 157 | + {_t("Windows")} |
| 158 | + </AccessibleButton> |
| 159 | + </div> |
| 160 | + <div className="mx_desktopCapturerSourcePicker_panel"> |
| 161 | + { sources } |
| 162 | + </div> |
| 163 | + </BaseDialog> |
| 164 | + ); |
| 165 | + } |
| 166 | +} |
0 commit comments