botui/src/main/main.ts

93 lines
2.8 KiB
TypeScript
Raw Normal View History

2024-10-26 16:26:11 -03:00
require('dotenv').config();
2024-10-26 21:21:51 -03:00
require('electron-require');
import { app, BrowserWindow, desktopCapturer, ipcMain } from 'electron';
2024-10-26 13:05:56 -03:00
import * as path from 'path';
2024-10-26 21:21:51 -03:00
import { systemPreferences } from 'electron';
2024-10-26 13:05:56 -03:00
import { RecorderService } from '../services/recorder.service';
import { PlayerService } from '../services/player.service';
const recorder = new RecorderService();
const player = new PlayerService();
function createWindow() {
const mainWindow = new BrowserWindow({
width: 1200,
height: 800,
2024-10-26 21:21:51 -03:00
2024-10-26 13:05:56 -03:00
webPreferences: {
2024-10-26 21:21:51 -03:00
nodeIntegrationInWorker: true,
2024-10-26 13:05:56 -03:00
nodeIntegration: true,
2024-10-26 21:21:51 -03:00
nodeIntegrationInSubFrames: true,
2024-10-26 13:05:56 -03:00
contextIsolation: false,
preload: path.join(__dirname, '../preload/preload.js')
}
});
if (process.env.NODE_ENV === 'development') {
mainWindow.loadURL('http://localhost:8080');
mainWindow.webContents.openDevTools();
} else {
mainWindow.loadFile(path.join(__dirname, '../../src/renderer/index.html'));
}
}
app.whenReady().then(createWindow);
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
ipcMain.handle('mouse-event', recorder.mouseHandleEvent.bind(recorder));
ipcMain.handle('keyboard-event', recorder.keyboardHandleEvent.bind(recorder));
ipcMain.handle('screenshot-captured', recorder.screenshotHandleEvent.bind(recorder));
2024-10-26 21:21:51 -03:00
// Handler to capture the entire screen
ipcMain.handle('get-screenshot', async () => {
console.log('get-screenshot called');
const sources = await desktopCapturer.getSources({ types: ['screen'] });
const screenSource = sources[0]; // Get the first screen source
const { thumbnail } = screenSource; // Thumbnail is a native image
return thumbnail.toPNG(); // Return the screenshot as PNG buffer
});
2024-10-26 13:05:56 -03:00
ipcMain.handle('start-recording', async () => {
2024-10-26 21:21:51 -03:00
console.log('start-recording called');
2024-10-26 13:05:56 -03:00
await recorder.startRecording();
});
ipcMain.handle('stop-recording', async () => {
2024-10-26 21:21:51 -03:00
console.log('stop-recording called');
2024-10-26 13:05:56 -03:00
return await recorder.stopRecording();
});
ipcMain.handle('execute-basic-code', async (_, code: string) => {
2024-10-26 21:21:51 -03:00
console.log('execute-basic-code called with:', code);
2024-10-26 13:05:56 -03:00
await player.executeBasicCode(code);
});
2024-10-26 16:26:11 -03:00
ipcMain.handle('check-microphone-permission', async () => {
2024-10-26 21:21:51 -03:00
console.log('check-microphone-permission called');
2024-10-26 16:26:11 -03:00
if (process.platform === 'darwin') {
const status = await systemPreferences.getMediaAccessStatus('microphone');
if (status !== 'granted') {
const success = await systemPreferences.askForMediaAccess('microphone');
return success;
}
return true;
}
2024-10-26 21:21:51 -03:00
return true; // On Windows/Linux, permissions are handled by the OS
2024-10-26 16:26:11 -03:00
});
// Enable required permissions
2024-10-26 21:21:51 -03:00
app.commandLine.appendSwitch('enable-speech-dispatcher');