|
1 | | -import { app, BrowserWindow, ipcMain } from 'electron' |
| 1 | +import { app, BrowserWindow, ipcMain, dialog } from 'electron' |
2 | 2 | import path from 'path' |
3 | 3 | import { fileURLToPath } from 'url' |
| 4 | +import fs from 'fs/promises' |
4 | 5 |
|
5 | 6 | const __dirname = path.dirname(fileURLToPath(import.meta.url)) |
6 | 7 |
|
7 | | -let mainWindow: BrowserWindow | null |
| 8 | +let mainWindow: BrowserWindow | null = null |
8 | 9 |
|
9 | 10 | function createWindow() { |
10 | 11 | mainWindow = new BrowserWindow({ |
11 | | - width: 1024, |
12 | | - height: 768, |
13 | | - frame: false, // MAGIC HAPPENS HERE: Removes default OS window frame |
14 | | - titleBarStyle: 'hidden', // Extra clean up for macOS |
| 12 | + width: 1100, |
| 13 | + height: 750, |
| 14 | + minWidth: 800, |
| 15 | + minHeight: 600, |
| 16 | + frame: false, |
| 17 | + backgroundColor: '#0a0a0b', |
15 | 18 | webPreferences: { |
16 | | - preload: path.join(__dirname, 'preload.js'), // Security bridge (compiled from preload.ts) |
| 19 | + preload: path.join(__dirname, 'preload.js'), |
17 | 20 | contextIsolation: true, |
18 | 21 | nodeIntegration: false, |
| 22 | + sandbox: false, |
19 | 23 | }, |
20 | 24 | }) |
21 | 25 |
|
22 | | - // Load Vite Dev Server in development, else load built index.html |
| 26 | + // Forward maximize/unmaximize state changes to renderer |
| 27 | + mainWindow.on('maximize', () => { |
| 28 | + mainWindow?.webContents.send('window-maximized-changed', true) |
| 29 | + }) |
| 30 | + mainWindow.on('unmaximize', () => { |
| 31 | + mainWindow?.webContents.send('window-maximized-changed', false) |
| 32 | + }) |
| 33 | + |
| 34 | + // Clean up reference when window is closed |
| 35 | + mainWindow.on('closed', () => { |
| 36 | + mainWindow = null |
| 37 | + }) |
| 38 | + |
23 | 39 | if (process.env.VITE_DEV_SERVER_URL) { |
24 | 40 | mainWindow.loadURL(process.env.VITE_DEV_SERVER_URL) |
| 41 | + mainWindow.webContents.openDevTools({ mode: 'detach' }) |
25 | 42 | } else { |
26 | 43 | mainWindow.loadFile(path.join(__dirname, '../dist/index.html')) |
27 | 44 | } |
28 | 45 | } |
29 | 46 |
|
30 | 47 | app.whenReady().then(createWindow) |
31 | 48 |
|
32 | | -// Handle IPC messages from React UI |
| 49 | +// Quit when all windows are closed (except on macOS) |
| 50 | +app.on('window-all-closed', () => { |
| 51 | + if (process.platform !== 'darwin') { |
| 52 | + app.quit() |
| 53 | + } |
| 54 | +}) |
| 55 | + |
| 56 | +// Re-create window on macOS when dock icon is clicked |
| 57 | +app.on('activate', () => { |
| 58 | + if (BrowserWindow.getAllWindows().length === 0) { |
| 59 | + createWindow() |
| 60 | + } |
| 61 | +}) |
| 62 | + |
| 63 | +// Window controls |
33 | 64 | ipcMain.on('window-minimize', () => mainWindow?.minimize()) |
34 | 65 | ipcMain.on('window-maximize', () => { |
35 | 66 | if (mainWindow?.isMaximized()) { |
36 | | - mainWindow?.unmaximize() |
| 67 | + mainWindow.unmaximize() |
37 | 68 | } else { |
38 | 69 | mainWindow?.maximize() |
39 | 70 | } |
40 | 71 | }) |
41 | 72 | ipcMain.on('window-close', () => mainWindow?.close()) |
| 73 | + |
| 74 | +// File dialog |
| 75 | +ipcMain.handle('open-file-dialog', async () => { |
| 76 | + if (!mainWindow) return [] |
| 77 | + |
| 78 | + const result = await dialog.showOpenDialog(mainWindow, { |
| 79 | + properties: ['openFile', 'multiSelections'], |
| 80 | + filters: [ |
| 81 | + { |
| 82 | + name: 'Supported Files', |
| 83 | + extensions: [ |
| 84 | + 'png', 'jpg', 'jpeg', 'gif', 'webp', 'bmp', 'avif', 'tiff', 'tif', 'svg', 'ico', 'jxl', |
| 85 | + 'pdf', 'epub', 'xps', 'cbz', 'mobi', 'fb2', 'docx', 'txt', 'rtf', 'odt', |
| 86 | + 'mp4', 'mkv', 'avi', 'mov', 'webm', '3gp', 'flv', 'wmv', |
| 87 | + 'mp3', 'wav', 'aac', 'ogg', 'flac', 'wma', 'm4a', |
| 88 | + ], |
| 89 | + }, |
| 90 | + { name: 'Images', extensions: ['png', 'jpg', 'jpeg', 'gif', 'webp', 'bmp', 'avif', 'tiff', 'tif', 'svg', 'ico', 'jxl'] }, |
| 91 | + { name: 'Documents', extensions: ['pdf', 'epub', 'xps', 'cbz', 'mobi', 'fb2', 'docx', 'txt', 'rtf', 'odt'] }, |
| 92 | + { name: 'Video', extensions: ['mp4', 'mkv', 'avi', 'mov', 'webm', '3gp', 'flv', 'wmv'] }, |
| 93 | + { name: 'Audio', extensions: ['mp3', 'wav', 'aac', 'ogg', 'flac', 'wma', 'm4a'] }, |
| 94 | + { name: 'All Files', extensions: ['*'] }, |
| 95 | + ], |
| 96 | + }) |
| 97 | + |
| 98 | + if (result.canceled) return [] |
| 99 | + |
| 100 | + const fileInfos = await Promise.all( |
| 101 | + result.filePaths.map(async (filePath) => { |
| 102 | + const stat = await fs.stat(filePath) |
| 103 | + return { |
| 104 | + path: filePath, |
| 105 | + name: path.basename(filePath), |
| 106 | + ext: path.extname(filePath).slice(1).toLowerCase(), |
| 107 | + size: stat.size, |
| 108 | + } |
| 109 | + }) |
| 110 | + ) |
| 111 | + return fileInfos |
| 112 | +}) |
| 113 | + |
| 114 | +// Save dialog for output directory |
| 115 | +ipcMain.handle('select-output-dir', async () => { |
| 116 | + if (!mainWindow) return null |
| 117 | + |
| 118 | + const result = await dialog.showOpenDialog(mainWindow, { |
| 119 | + properties: ['openDirectory', 'createDirectory'], |
| 120 | + }) |
| 121 | + if (result.canceled) return null |
| 122 | + return result.filePaths[0] |
| 123 | +}) |
| 124 | + |
| 125 | +// Get file info (for drag & drop) |
| 126 | +ipcMain.handle('get-file-info', async (_event, filePath: string) => { |
| 127 | + try { |
| 128 | + const stat = await fs.stat(filePath) |
| 129 | + return { |
| 130 | + path: filePath, |
| 131 | + name: path.basename(filePath), |
| 132 | + ext: path.extname(filePath).slice(1).toLowerCase(), |
| 133 | + size: stat.size, |
| 134 | + } |
| 135 | + } catch { |
| 136 | + return null |
| 137 | + } |
| 138 | +}) |
0 commit comments