|
| 1 | +const { app, BrowserWindow, globalShortcut } = require('electron'); |
| 2 | +const path = require('path'); |
| 3 | +const fs = require('fs'); |
| 4 | + |
| 5 | +let mainWindow; |
| 6 | +let config = { autoLaunch: false, shortcut: 'CommandOrControl+G' }; |
| 7 | + |
| 8 | +function createWindow() { |
| 9 | + mainWindow = new BrowserWindow({ |
| 10 | + width: 400, |
| 11 | + height: 500, |
| 12 | + frame: false, |
| 13 | + transparent: true, |
| 14 | + alwaysOnTop: true, |
| 15 | + skipTaskbar: true, |
| 16 | + focusable: false, |
| 17 | + resizable: false, |
| 18 | + webPreferences: { |
| 19 | + preload: path.join(__dirname, 'preload.js'), |
| 20 | + nodeIntegration: true, |
| 21 | + contextIsolation: false |
| 22 | + } |
| 23 | + }); |
| 24 | + |
| 25 | + mainWindow.loadFile('index.html'); |
| 26 | + mainWindow.setIgnoreMouseEvents(true); // Make it non-interactive, just overlay |
| 27 | + mainWindow.hide(); // Start hidden |
| 28 | +} |
| 29 | + |
| 30 | +app.whenReady().then(() => { |
| 31 | + // Load config |
| 32 | + const configPath = path.join(__dirname, 'config.json'); |
| 33 | + if (fs.existsSync(configPath)) { |
| 34 | + config = JSON.parse(fs.readFileSync(configPath, 'utf8')); |
| 35 | + } |
| 36 | + |
| 37 | + createWindow(); |
| 38 | + |
| 39 | + // Register global shortcut |
| 40 | + const ret = globalShortcut.register(config.shortcut, () => { |
| 41 | + if (mainWindow.isVisible()) { |
| 42 | + mainWindow.hide(); |
| 43 | + } else { |
| 44 | + mainWindow.show(); |
| 45 | + } |
| 46 | + }); |
| 47 | + |
| 48 | + if (!ret) { |
| 49 | + console.log('Shortcut registration failed'); |
| 50 | + } |
| 51 | + |
| 52 | + // If autoLaunch is true, show immediately |
| 53 | + if (config.autoLaunch) { |
| 54 | + mainWindow.show(); |
| 55 | + } |
| 56 | +}); |
| 57 | + |
| 58 | +app.on('will-quit', () => { |
| 59 | + globalShortcut.unregisterAll(); |
| 60 | +}); |
| 61 | + |
| 62 | +app.on('window-all-closed', () => { |
| 63 | + if (process.platform !== 'darwin') { |
| 64 | + app.quit(); |
| 65 | + } |
| 66 | +}); |
| 67 | + |
| 68 | +app.on('activate', () => { |
| 69 | + if (BrowserWindow.getAllWindows().length === 0) { |
| 70 | + createWindow(); |
| 71 | + } |
| 72 | +}); |
0 commit comments