-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
170 lines (143 loc) · 4.39 KB
/
index.js
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
164
165
166
167
168
169
170
'use-strict';
const util = require('electron-util');
const path = require('path');
const {BrowserWindow, screen, ipcMain, dialog, shell, app} = require('electron');
const execa = require('execa');
const binary = path.join(util.fixPathForAsarUnpack(__dirname), 'video-devices');
const permissionsBinary = path.join(util.fixPathForAsarUnpack(__dirname), 'permissions');
const contentPath = path.join(util.fixPathForAsarUnpack(__dirname), 'index.html');
const PADDING = 20;
const devices = ['Default'];
try {
devices.push(...execa.sync(binary).stdout.trim().split('\n'));
} catch {}
const config = {
deviceName: {
title: 'Device',
description: 'Which device to display.',
enum: devices,
required: true,
default: 'Default'
},
borderRadius: {
title: 'Border Radius',
description: 'Any valid `border-radius` value, like `10px` or `50%`.',
type: 'string',
required: true,
default: '50%'
},
hoverOpacity: {
title: 'Hover Opacity',
description: 'Opacity of the window on when moused over.',
type: 'number',
minimum: 0,
maximum: 1,
default: 0.6,
required: true
},
width: {
title: 'Width',
description: 'Width of the window.',
type: 'number',
minimum: 0,
default: 128,
required: true
},
height: {
title: 'Height',
description: 'Height of the window.',
type: 'number',
minimum: 0,
default: 128,
required: true
}
};
const getBounds = (cropArea, screenBounds, {width, height}) => {
return {
x: cropArea.x + screenBounds.x + cropArea.width - width - PADDING,
y: screenBounds.height - (cropArea.y + cropArea.height) + screenBounds.y + cropArea.height - height - PADDING,
width,
height
};
}
const willStartRecording = async ({state, config, apertureOptions: {screenId, cropArea}}) => {
const hasPermissions = await ensureCameraPermission();
if (!hasPermissions) {
return;
}
const screens = screen.getAllDisplays();
const {bounds} = screens.find(s => s.id === screenId) || {};
const position = getBounds(cropArea, bounds, {
width: config.get('width'),
height: config.get('height')
});
state.window = new BrowserWindow({
...position,
closable: false,
minimizable: false,
alwaysOnTop: true,
frame: false,
transparent: true,
titleBarStyle: 'customButtonsOnHover',
webPreferences: {nodeIntegration: true}
});
state.window.loadFile(contentPath);
// state.window.openDevTools({mode: 'detach'});
state.window.webContents.on('did-finish-load', () => {
state.window.webContents.send('data', {
videoDeviceName: config.get('deviceName'),
hoverOpacity: config.get('hoverOpacity'),
borderRadius: config.get('borderRadius')
});
});
return new Promise(resolve => {
ipcMain.on('kap-camera-mount', resolve);
// Resolve after 5 seconds to not block recording if for some reason there's no event
setTimeout(resolve, 5000);
});
};
const didStopRecording = ({state}) => {
if (state.window) {
state.window.destroy();
}
};
const configDescription =
`Create a window showing the selected camera on the bottom-left corner of the recording.
The window is click-through and its hover opacity and size can be adjusted.
To move the window, hold Command before you hover over it, then click and drag it anywhere on the screen.
`;
const openSystemPreferences = path => shell.openExternal(`x-apple.systempreferences:com.apple.preference.security?${path}`);
const hasCameraPermission = async () => {
try {
return (await execa(permissionsBinary)).stdout === 'true';
} catch {
return false;
}
}
const ensureCameraPermission = async () => {
const hasPermission = await hasCameraPermission();
if (hasPermission) {
return true;
}
const {response} = await dialog.showMessageBox({
type: 'warning',
buttons: ['Open System Preferences', 'Cancel'],
defaultId: 0,
message: 'kap-camera cannot access the camera.',
detail: 'kap-camera requires camera access to be able to show the contents of the webcam. You can grant this in the System Preferences. Afterwards, launch Kap for the changes to take effect.',
cancelId: 1
});
if (response === 0) {
await openSystemPreferences('Privacy_Camera');
app.quit();
}
return false;
}
exports.recordServices = [{
title: 'Show Camera',
config,
configDescription,
willStartRecording,
didStopRecording,
willEnable: ensureCameraPermission
}];