-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrolsRenderer.js
More file actions
251 lines (217 loc) · 7.92 KB
/
Copy pathcontrolsRenderer.js
File metadata and controls
251 lines (217 loc) · 7.92 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
const { ipcRenderer } = require('electron');
// UI Elements
const closeBtn = document.getElementById('close-btn');
const windowSection = document.getElementById('window-section');
const windowHeader = document.getElementById('window-header');
const windowLabel = document.getElementById('window-label');
const cameraSection = document.getElementById('camera-section');
const cameraList = document.getElementById('camera-list');
const cameraStatus = document.getElementById('camera-status');
const micSection = document.getElementById('mic-section');
const micList = document.getElementById('mic-list');
const micStatus = document.getElementById('mic-status');
const startBtn = document.getElementById('start-recording');
const stopBtn = document.getElementById('stop-recording');
// State
let selectedWindow = null;
let selectedCamera = null;
let selectedMic = null;
let availableWindows = [];
let availableCameras = [];
let availableMics = [];
// Event Handlers
closeBtn.onclick = () => {
window.close();
};
windowHeader.onclick = () => {
// Show the window selector popup instead of dropdown
ipcRenderer.send('show-window-selector');
};
cameraSection.onclick = () => {
if (availableCameras.length > 0) {
cameraList.classList.toggle('hidden');
micList.classList.add('hidden');
}
};
micSection.onclick = () => {
if (availableMics.length > 0) {
micList.classList.toggle('hidden');
cameraList.classList.add('hidden');
}
};
let isRecording = false;
let isPaused = false;
startBtn.onclick = () => {
if (!isRecording) {
// Start recording
if (selectedWindow && selectedCamera && selectedMic) {
ipcRenderer.send('control-event', {
type: 'start',
windowId: selectedWindow,
cameraId: selectedCamera,
micId: selectedMic
});
}
} else if (isPaused) {
// Resume recording
ipcRenderer.send('control-event', { type: 'pause' });
isPaused = false;
updateRecordingUI();
} else {
// Pause recording
ipcRenderer.send('control-event', { type: 'pause' });
isPaused = true;
updateRecordingUI();
}
};
stopBtn.onclick = () => {
ipcRenderer.send('control-event', { type: 'stop' });
};
// Initialize the interface
async function initialize() {
try {
// Get available windows/screens (not needed for inline list anymore)
const sources = await ipcRenderer.invoke('get-sources');
availableWindows = sources;
// Get available cameras and microphones
const devices = await navigator.mediaDevices.enumerateDevices();
availableCameras = devices.filter(d => d.kind === 'videoinput');
availableMics = devices.filter(d => d.kind === 'audioinput');
// Auto-select default camera and microphone
if (availableCameras.length > 0) {
selectedCamera = availableCameras[0].deviceId;
}
if (availableMics.length > 0) {
selectedMic = availableMics[0].deviceId;
}
populateCameraList();
populateMicList();
updateUI();
} catch (error) {
console.error('Error initializing:', error);
}
}
function populateCameraList() {
cameraList.innerHTML = '';
availableCameras.forEach(device => {
const item = document.createElement('div');
item.className = 'option-item';
item.innerHTML = `
<span class="icon">📹</span>
<span class="label">${device.label || `Camera ${availableCameras.indexOf(device) + 1}`}</span>
`;
item.onclick = () => {
// Remove previous selection
cameraList.querySelectorAll('.option-item').forEach(el => el.classList.remove('selected'));
// Select this item
item.classList.add('selected');
selectedCamera = device.deviceId;
cameraList.classList.add('hidden');
updateUI();
};
// Pre-select if this is the default camera
if (device.deviceId === selectedCamera) {
item.classList.add('selected');
}
cameraList.appendChild(item);
});
}
function populateMicList() {
micList.innerHTML = '';
availableMics.forEach(device => {
const item = document.createElement('div');
item.className = 'option-item';
item.innerHTML = `
<span class="icon">🎤</span>
<span class="label">${device.label || `Microphone ${availableMics.indexOf(device) + 1}`}</span>
`;
item.onclick = () => {
// Remove previous selection
micList.querySelectorAll('.option-item').forEach(el => el.classList.remove('selected'));
// Select this item
item.classList.add('selected');
selectedMic = device.deviceId;
micList.classList.add('hidden');
updateUI();
};
// Pre-select if this is the default microphone
if (device.deviceId === selectedMic) {
item.classList.add('selected');
}
micList.appendChild(item);
});
}
function updateUI() {
// Update camera status and label
const cameraLabel = document.querySelector('#camera-section .label');
if (selectedCamera) {
const selectedCameraDevice = availableCameras.find(d => d.deviceId === selectedCamera);
const cameraName = selectedCameraDevice ? (selectedCameraDevice.label || 'Camera') : 'Camera';
cameraLabel.textContent = cameraName;
cameraStatus.textContent = 'On';
cameraStatus.classList.remove('off');
} else {
cameraLabel.textContent = 'Camera';
cameraStatus.textContent = 'Off';
cameraStatus.classList.add('off');
}
// Update mic status and label
const micLabel = document.querySelector('#mic-section .label');
if (selectedMic) {
const selectedMicDevice = availableMics.find(d => d.deviceId === selectedMic);
const micName = selectedMicDevice ? (selectedMicDevice.label || 'Microphone') : 'Microphone';
micLabel.textContent = micName;
micStatus.textContent = 'On';
micStatus.classList.remove('off');
} else {
micLabel.textContent = 'Microphone';
micStatus.textContent = 'Off';
micStatus.classList.add('off');
}
// Update start button
startBtn.disabled = !(selectedWindow && selectedCamera && selectedMic);
}
function updateRecordingUI() {
if (isRecording) {
startBtn.textContent = isPaused ? 'Resume' : 'Pause';
startBtn.classList.remove('hidden');
stopBtn.classList.remove('hidden');
startBtn.disabled = false;
} else {
startBtn.textContent = 'Start recording';
startBtn.classList.remove('hidden');
stopBtn.classList.add('hidden');
isPaused = false;
updateUI(); // Refresh start button state
}
}
// Hide dropdown lists when clicking outside
document.addEventListener('click', (e) => {
if (!cameraSection.contains(e.target)) {
cameraList.classList.add('hidden');
}
if (!micSection.contains(e.target)) {
micList.classList.add('hidden');
}
});
// Listen for window selection from the popup
ipcRenderer.on('window-selected', (event, windowData) => {
selectedWindow = windowData.id;
windowLabel.textContent = windowData.name;
updateUI();
});
// Listen for recording state changes
ipcRenderer.on('recording-state-changed', (event, data) => {
isRecording = data.isRecording;
updateRecordingUI();
});
// Handle window closing while recording
window.addEventListener('beforeunload', (e) => {
if (isRecording) {
console.log('Controls window closing while recording - stopping recording');
// Stop recording before closing
ipcRenderer.send('control-event', { type: 'stop' });
}
});
// Initialize when the page loads
window.onload = initialize;