Skip to content

Commit

Permalink
tray icon
Browse files Browse the repository at this point in the history
  • Loading branch information
aza547 committed Aug 13, 2022
1 parent bc4709a commit fe87d45
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
55 changes: 45 additions & 10 deletions src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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?
Expand Down Expand Up @@ -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.
*/
Expand All @@ -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,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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") {
Expand Down

0 comments on commit fe87d45

Please sign in to comment.