Add basic tray icon

This commit is contained in:
TheThomaas 2024-12-08 21:24:01 +01:00
parent ba521c61e3
commit c0d0d8563b

View file

@ -1,11 +1,14 @@
import { app, shell, BrowserWindow, ipcMain } from 'electron'
import { app, shell, BrowserWindow, ipcMain, Menu, Tray, nativeImage } from 'electron'
import { join } from 'path'
import { electronApp, optimizer, is } from '@electron-toolkit/utils'
import icon from '../../resources/icon.png?asset'
let mainWindow: BrowserWindow;
let tray: Tray;
function createWindow(): void {
// Create the browser window.
const mainWindow = new BrowserWindow({
mainWindow = new BrowserWindow({
width: 900,
height: 670,
show: false,
@ -17,6 +20,7 @@ function createWindow(): void {
}
})
// TODO show window according to the app settings
mainWindow.on('ready-to-show', () => {
mainWindow.show()
})
@ -59,6 +63,44 @@ app.whenReady().then(() => {
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
// TODO close/hide window according to the app settings
// Prevent window from closing and quitting app
// Instead make close simply hide main window
// Clicking on tray icon will bring back main window
mainWindow.on('close', event => {
event.preventDefault()
mainWindow.hide()
})
// Creating the tray icon
const icon = nativeImage.createFromPath('./resources/icon.png')
tray = new Tray(icon.resize({ width: 16, height: 16 }))
tray.setIgnoreDoubleClickEvents(true)
const contextMenu = Menu.buildFromTemplate([
{
label: 'Hide',
click: _ => {
mainWindow.hide()
}
},
{
label: 'Quit',
click: _ => {
console.log('Menu/Quit was clicked')
app.exit()
}
},
])
tray.setToolTip('Umbra')
tray.setContextMenu(contextMenu)
// Prevent menu from being shown on left click
// Instead make main window visible (if it had been invisible)
tray.on('click', _ => {
mainWindow.show()
})
})
// Quit when all windows are closed, except on macOS. There, it's common