|
| 1 | +const { app, BrowserWindow, ipcMain, Menu, Tray, nativeImage } = require('electron'); |
| 2 | +const path = require('path'); |
| 3 | +const fs = require('fs'); |
| 4 | + |
| 5 | +app.commandLine.appendSwitch('--no-sandbox'); |
| 6 | +app.commandLine.appendSwitch('--disable-setuid-sandbox'); |
| 7 | + |
| 8 | +let win; |
| 9 | +let dragOffset = { x: 0, y: 0 }; |
| 10 | +const boundsFile = path.join(app.getPath('userData'), 'bounds.json'); |
| 11 | + |
| 12 | +function saveBounds() { |
| 13 | + if (win) { |
| 14 | + const bounds = win.getBounds(); |
| 15 | + fs.writeFileSync(boundsFile, JSON.stringify(bounds)); |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +function loadBounds() { |
| 20 | + try { |
| 21 | + return JSON.parse(fs.readFileSync(boundsFile, 'utf8')); |
| 22 | + } catch { |
| 23 | + return { width: 655, height: 150, x: 100, y: 100 }; |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +ipcMain.on('close-window', () => { |
| 28 | + if (win) win.close(); |
| 29 | +}); |
| 30 | + |
| 31 | +ipcMain.on('show-context-menu', (event) => { |
| 32 | + const menu = Menu.buildFromTemplate([ |
| 33 | + { |
| 34 | + label: 'Settings', |
| 35 | + click: () => event.sender.send('toggle-settings') |
| 36 | + }, |
| 37 | + { |
| 38 | + label: 'Close', |
| 39 | + click: () => win.close() |
| 40 | + } |
| 41 | + ]); |
| 42 | + menu.popup(); |
| 43 | +}); |
| 44 | + |
| 45 | +ipcMain.on('start-drag', (event, pos) => { |
| 46 | + const bounds = win.getBounds(); |
| 47 | + dragOffset.x = pos.x - bounds.x; |
| 48 | + dragOffset.y = pos.y - bounds.y; |
| 49 | +}); |
| 50 | + |
| 51 | +ipcMain.on('drag', (event, pos) => { |
| 52 | + const newX = pos.x - dragOffset.x; |
| 53 | + const newY = pos.y - dragOffset.y; |
| 54 | + win.setBounds({ x: newX, y: newY, width: 655, height: win.getBounds().height }); |
| 55 | +}); |
| 56 | + |
| 57 | +ipcMain.on('resize-window', (event, height) => { |
| 58 | + const bounds = win.getBounds(); |
| 59 | + win.setBounds({ ...bounds, height }); |
| 60 | +}); |
| 61 | + |
| 62 | +ipcMain.on('save-bounds', saveBounds); |
| 63 | + |
| 64 | +function createWindow() { |
| 65 | + const bounds = loadBounds(); |
| 66 | + win = new BrowserWindow({ |
| 67 | + ...bounds, |
| 68 | + webPreferences: { |
| 69 | + nodeIntegration: true, |
| 70 | + contextIsolation: false, |
| 71 | + enableRemoteModule: true |
| 72 | + }, |
| 73 | + resizable: false, |
| 74 | + frame: false, |
| 75 | + transparent: true, |
| 76 | + show: false, |
| 77 | + skipTaskbar: true, |
| 78 | + type: process.platform === 'linux' ? 'toolbar' : undefined, |
| 79 | + alwaysOnTop: false, |
| 80 | + icon:"./icon.png", |
| 81 | + title: 'GitHub Contributions', |
| 82 | + name: 'GitHub Contributions' |
| 83 | + }); |
| 84 | + |
| 85 | + win.loadFile('index.html'); |
| 86 | + win.on('close', saveBounds); |
| 87 | + |
| 88 | + const icon = nativeImage.createFromPath(path.join(__dirname, 'icon.png')); |
| 89 | + const tray = new Tray(icon); |
| 90 | + tray.setToolTip('GitHub Contributions'); |
| 91 | + |
| 92 | + const trayMenu = Menu.buildFromTemplate([ |
| 93 | + { |
| 94 | + label: 'Settings', |
| 95 | + click: () => win.webContents.send('toggle-settings') |
| 96 | + }, |
| 97 | + { |
| 98 | + label: 'Close', |
| 99 | + click: () => win.close() |
| 100 | + } |
| 101 | + ]); |
| 102 | + |
| 103 | + tray.on('click', () => { |
| 104 | + if (win.isVisible()) { |
| 105 | + win.hide(); |
| 106 | + } else { |
| 107 | + win.show(); |
| 108 | + setTimeout(() => { |
| 109 | + win.setSkipTaskbar(true); |
| 110 | + }, 100); |
| 111 | + } |
| 112 | + }); |
| 113 | + |
| 114 | + tray.setContextMenu(trayMenu); |
| 115 | + |
| 116 | + win.once('ready-to-show', () => { |
| 117 | + win.show(); |
| 118 | + |
| 119 | + // Force hide from taskbar on Linux |
| 120 | + if (process.platform === 'linux') { |
| 121 | + setTimeout(() => { |
| 122 | + win.setSkipTaskbar(true); |
| 123 | + // Additional Linux-specific method |
| 124 | + win.blur(); |
| 125 | + }, 100); |
| 126 | + |
| 127 | + // Try multiple times for Linux |
| 128 | + setTimeout(() => win.setSkipTaskbar(true), 500); |
| 129 | + setTimeout(() => win.setSkipTaskbar(true), 1000); |
| 130 | + } else { |
| 131 | + win.setSkipTaskbar(true); |
| 132 | + } |
| 133 | + }); |
| 134 | +} |
| 135 | + |
| 136 | +app.whenReady().then(createWindow); |
| 137 | + |
| 138 | +app.on('window-all-closed', () => { |
| 139 | + if (process.platform !== 'darwin') app.quit(); |
| 140 | +}); |
0 commit comments