system tray and package config

This commit is contained in:
michalcourson
2026-02-28 19:09:45 -05:00
parent 510b92f669
commit 39395fd846
19 changed files with 106 additions and 103 deletions

View File

@ -10,7 +10,7 @@
*/
import fs from 'fs';
import path from 'path';
import { app, BrowserWindow, shell, ipcMain } from 'electron';
import { app, BrowserWindow, shell, ipcMain, Tray, Menu } from 'electron';
import { autoUpdater } from 'electron-updater';
import log from 'electron-log';
import MenuBuilder from './menu';
@ -28,7 +28,8 @@ class AppUpdater {
}
}
let mainWindow: BrowserWindow | null = null;
let mainWindow: BrowserWindow;
let tray: Tray | null = null;
ipcMain.on('ipc-example', async (event, arg) => {
const msgTemplate = (pingPong: string) => `IPC test: ${pingPong}`;
@ -78,7 +79,7 @@ const createWindow = async () => {
show: false,
width: 1024,
height: 728,
icon: getAssetPath('icon.png'),
icon: getAssetPath('icon.png'), // Set app icon
webPreferences: {
preload: app.isPackaged
? path.join(__dirname, 'preload.js')
@ -99,12 +100,15 @@ const createWindow = async () => {
}
});
mainWindow.on('closed', () => {
mainWindow = null;
mainWindow.on('close', (event) => {
console.log('close event triggered');
event.preventDefault();
mainWindow.hide();
});
const menuBuilder = new MenuBuilder(mainWindow);
menuBuilder.buildMenu();
registerFileIpcHandlers();
// Open urls in the user's browser
mainWindow.webContents.setWindowOpenHandler((edata) => {
@ -112,11 +116,31 @@ const createWindow = async () => {
return { action: 'deny' };
});
registerFileIpcHandlers();
// Remove this if your app does not use auto updates
// eslint-disable-next-line
new AppUpdater();
console.log('asset path: ', getAssetPath('tray_icon.png'));
tray = new Tray(getAssetPath('tray_icon.png'));
const contextMenu = Menu.buildFromTemplate([
{
label: 'Show',
click: () => {
mainWindow?.show();
},
},
{
label: 'Quit',
click: () => {
pythonManager.stop();
app.quit();
},
},
]);
tray.setToolTip('ClipTrim');
tray.setContextMenu(contextMenu);
tray.on('double-click', () => {
mainWindow?.show();
});
};
/**
@ -126,16 +150,17 @@ const createWindow = async () => {
app.on('window-all-closed', () => {
// Respect the OSX convention of having the application in memory even
// after all windows have been closed
pythonManager.stop();
if (process.platform !== 'darwin') {
app.quit();
}
// pythonManager.stop();
// Do not quit app, keep tray active
// if (process.platform !== 'darwin') {
// app.quit();
// }
});
app
.whenReady()
.then(() => {
// pythonManager.start();
pythonManager.start();
createWindow();
app.on('activate', () => {
// On macOS it's common to re-create a window in the app when the