diff --git a/CHANGELOG.md b/CHANGELOG.md index fbc1b3b4..86da5ea1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Resources directory and better test scripts, although they still suck. - [10](https://github.com/aza547/wow-recorder/issues/10) - Add logging infrastructure. +- Add tray icon and menu. Make minimizing now hide in system tray. ### Changed - Record at 60 FPS instead of 30. diff --git a/src/main/main.ts b/src/main/main.ts index 5d53b3e5..7bbc707f 100644 --- a/src/main/main.ts +++ b/src/main/main.ts @@ -4,7 +4,7 @@ * Application entrypoint point. */ import path from 'path'; -import { app, BrowserWindow, shell, ipcMain, dialog } from 'electron'; +import { app, BrowserWindow, shell, ipcMain, dialog, Tray, Menu } from 'electron'; import { resolveHtmlPath, getVideoState, writeMetadataFile, runSizeMonitor, isConfigReady, deleteVideo, openSystemExplorer, toggleVideoProtected, fixPathWhenPackaged} from './util'; import { watchLogs, Metadata, getLatestLog } from './logutils'; import Store from 'electron-store'; @@ -44,6 +44,7 @@ ipcMain.on('cfg-set', async (_event, key, val) => { */ let mainWindow: BrowserWindow | null = null; let settingsWindow: BrowserWindow | null = null; +let tray = null; /** * Are we currently recording, and what category? @@ -76,6 +77,44 @@ const installExtensions = async () => { .catch(console.log); }; +const RESOURCES_PATH = app.isPackaged +? path.join(process.resourcesPath, 'assets') +: path.join(__dirname, '../../assets'); + +const getAssetPath = (...paths: string[]): string => { + return path.join(RESOURCES_PATH, ...paths); +}; + +/** + * Setup tray icon, menu and even listeners. + */ +const setupTray = () => { + tray = new Tray(getAssetPath("./icon/small-icon.png")); + + const contextMenu = Menu.buildFromTemplate([ + { + label: 'Open', click() { + console.log("User clicked open on tray icon"); + if (mainWindow) mainWindow.show(); + } + }, + { + label: 'Quit', click() { + console.log("User clicked close on tray icon"); + if (mainWindow) mainWindow.close(); + } + }, + ]) + + tray.setToolTip('Warcraft Recorder') + tray.setContextMenu(contextMenu) + + tray.on("double-click", () => { + console.log("User double clicked tray icon"); + if (mainWindow) mainWindow.show(); + }) +} + /** * Creates the main window. */ @@ -84,14 +123,6 @@ const createWindow = async () => { await installExtensions(); } - const RESOURCES_PATH = app.isPackaged - ? path.join(process.resourcesPath, 'assets') - : path.join(__dirname, '../../assets'); - - const getAssetPath = (...paths: string[]): string => { - return path.join(RESOURCES_PATH, ...paths); - }; - mainWindow = new BrowserWindow({ show: false, width: 1024, @@ -132,6 +163,8 @@ const createWindow = async () => { mainWindow = null; }); + setupTray(); + // Open urls in the user's browser mainWindow.webContents.setWindowOpenHandler((edata) => { shell.openExternal(edata.url); @@ -246,9 +279,11 @@ const openPathDialog = (event: any, args: any) => { */ ipcMain.on('mainWindow', (_event, args) => { if (mainWindow === null) return; + if (args[0] === "minimize") { console.log("User clicked minimize"); - mainWindow.minimize(); + //mainWindow.minimize(); + mainWindow.hide(); } if (args[0] === "resize") {