/
ScreenCaptureButtons.tsx
163 lines (141 loc) · 4.09 KB
/
ScreenCaptureButtons.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
import {Button, ButtonGroup, writeBufferToFile} from 'flipper';
import React, {Component} from 'react';
import {connect} from 'react-redux';
import expandTilde from 'expand-tilde';
import {remote} from 'electron';
import path from 'path';
import {reportPlatformFailures} from '../utils/metrics';
import config from '../utils/processConfig';
import BaseDevice from '../devices/BaseDevice';
import {State as Store} from '../reducers';
import open from 'open';
const CAPTURE_LOCATION = expandTilde(
config().screenCapturePath || remote.app.getPath('desktop'),
);
type OwnProps = {};
type StateFromProps = {
selectedDevice: BaseDevice | null | undefined;
};
type DispatchFromProps = {};
type State = {
recording: boolean;
recordingEnabled: boolean;
capturingScreenshot: boolean;
};
export async function openFile(path: string | null) {
if (!path) {
return;
}
try {
await open(path);
} catch (e) {
console.error(`Opening ${path} failed with error ${e}.`);
}
}
function getFileName(extension: 'png' | 'mp4'): string {
// Windows does not like `:` in its filenames. Yes, I know ...
return `screencap-${new Date().toISOString().replace(/:/g, '')}.${extension}`;
}
type Props = OwnProps & StateFromProps & DispatchFromProps;
class ScreenCaptureButtons extends Component<Props, State> {
videoPath: string | null | undefined;
state = {
recording: false,
recordingEnabled: false,
capturingScreenshot: false,
};
componentDidMount() {
this.checkIfRecordingIsAvailable();
}
UNSAFE_componentWillReceiveProps(nextProps: Props) {
if (nextProps.selectedDevice !== this.props.selectedDevice) {
this.checkIfRecordingIsAvailable(nextProps);
}
}
checkIfRecordingIsAvailable = async (props: Props = this.props) => {
const {selectedDevice} = props;
const recordingEnabled = selectedDevice
? await selectedDevice.screenCaptureAvailable()
: false;
this.setState({recordingEnabled});
};
captureScreenshot: Promise<void> | any = () => {
const {selectedDevice} = this.props;
const pngPath = path.join(CAPTURE_LOCATION, getFileName('png'));
if (selectedDevice != null) {
reportPlatformFailures(
selectedDevice
.screenshot()
.then(buffer => writeBufferToFile(pngPath, buffer))
.then(path => openFile(path)),
'captureScreenshot',
);
}
};
startRecording = async () => {
const {selectedDevice} = this.props;
if (!selectedDevice) {
return;
}
const videoPath = path.join(CAPTURE_LOCATION, getFileName('mp4'));
await selectedDevice.startScreenCapture(videoPath);
this.setState({
recording: true,
});
};
stopRecording = async () => {
const {selectedDevice} = this.props;
if (!selectedDevice) {
return;
}
const path = await selectedDevice.stopScreenCapture();
this.setState({
recording: false,
});
openFile(path);
};
onRecordingClicked = () => {
if (this.state.recording) {
this.stopRecording();
} else {
this.startRecording();
}
};
render() {
const {recordingEnabled} = this.state;
const {selectedDevice} = this.props;
return (
<ButtonGroup>
<Button
compact={true}
onClick={this.captureScreenshot}
icon="camera"
title="Take Screenshot"
disabled={!selectedDevice}
/>
<Button
compact={true}
onClick={this.onRecordingClicked}
icon={this.state.recording ? 'stop-playback' : 'camcorder'}
pulse={this.state.recording}
selected={this.state.recording}
title="Make Screen Recording"
disabled={!selectedDevice || !recordingEnabled}
/>
</ButtonGroup>
);
}
}
export default connect<StateFromProps, DispatchFromProps, OwnProps, Store>(
({connections: {selectedDevice}}) => ({
selectedDevice,
}),
)(ScreenCaptureButtons);