Files
Teleprompter/src/main.ts
T

87 lines
2.3 KiB
TypeScript

import { app, BrowserWindow, ipcMain, dialog } from 'electron';
import * as path from 'path';
import * as fs from 'fs';
function createWindow() {
const win = new BrowserWindow({
width: 1000,
height: 800,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
nodeIntegration: false,
contextIsolation: true,
sandbox: true
},
// Modern dark theme background
backgroundColor: '#0f172a',
titleBarStyle: 'hiddenInset' // Looks cleaner on macOS, normal on others
});
win.loadFile(path.join(__dirname, '../index.html'));
// Note: index.html is in the root, dist/main.js is in dist/. So we need to go up one level.
}
app.whenReady().then(() => {
createWindow();
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
ipcMain.handle('open-file-dialog', async () => {
const { canceled, filePaths } = await dialog.showOpenDialog({
properties: ['openFile'],
filters: [
{ name: 'Markdown', extensions: ['md', 'markdown', 'txt'] },
{ name: 'All Files', extensions: ['*'] }
]
});
if (canceled) {
return null;
} else {
return filePaths[0];
}
});
ipcMain.handle('read-file', async (event, filePath: string) => {
try {
const content = fs.readFileSync(filePath, 'utf-8');
return content;
} catch (err) {
console.error("Error reading file", err);
throw err;
}
});
const configPath = path.join(app.getPath('userData'), 'config.json');
ipcMain.handle('load-settings', async () => {
try {
if (fs.existsSync(configPath)) {
const data = fs.readFileSync(configPath, 'utf-8');
return JSON.parse(data);
}
} catch (error) {
console.error('Failed to load settings:', error);
}
return {};
});
ipcMain.handle('save-settings', async (event, settings) => {
try {
fs.writeFileSync(configPath, JSON.stringify(settings, null, 2));
} catch (error) {
console.error('Failed to save settings:', error);
}
});