From a9222c173f6481b8845e3dcb0d027d09bcb93c50 Mon Sep 17 00:00:00 2001 From: nkhangg Date: Fri, 9 May 2025 15:47:40 +0700 Subject: [PATCH] add tray future --- src/main/index.ts | 63 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 52 insertions(+), 11 deletions(-) diff --git a/src/main/index.ts b/src/main/index.ts index 42f40c8..67039dc 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -1,16 +1,18 @@ /* eslint-disable prettier/prettier */ /* eslint-disable @typescript-eslint/no-explicit-any */ -import { app, shell, BrowserWindow, ipcMain, screen, globalShortcut } from 'electron' -import path, { join } from 'path' -import { electronApp, optimizer, is } from '@electron-toolkit/utils' -import icon from '../../resources/icon.png?asset' +import { electronApp, is, optimizer } from '@electron-toolkit/utils' +import { app, BrowserWindow, globalShortcut, ipcMain, Menu, screen, shell, Tray } from 'electron' import fs from 'fs' +import path, { join } from 'path' +import icon from '../../resources/icon.png?asset' +let mainWindow: null | BrowserWindow = null +let isQuiting = false function createWindow(): void { // Get Screen width, height const { width, height } = screen.getPrimaryDisplay().workAreaSize - const mainWindow = new BrowserWindow({ + mainWindow = new BrowserWindow({ width: width / 2, height: height / 2, show: false, @@ -26,7 +28,7 @@ function createWindow(): void { mainWindow.setPosition(width / 2, height / 2) mainWindow.on('ready-to-show', () => { - mainWindow.show() + mainWindow?.show() }) mainWindow.webContents.setWindowOpenHandler((details) => { @@ -46,19 +48,19 @@ function createWindow(): void { mainWindow.webContents.once('did-finish-load', () => { const shortcut = 'Control+Shift+C' - mainWindow.on('focus', () => { + mainWindow?.on('focus', () => { globalShortcut.register(shortcut, () => { const pos = screen.getCursorScreenPoint() - mainWindow.webContents.inspectElement(pos.x, pos.y) - mainWindow.webContents.focus() // optional: refocus back + mainWindow?.webContents.inspectElement(pos.x, pos.y) + mainWindow?.webContents.focus() // optional: refocus back }) }) - mainWindow.on('blur', () => { + mainWindow?.on('blur', () => { globalShortcut.unregister(shortcut) }) - mainWindow.on('closed', () => { + mainWindow?.on('closed', () => { globalShortcut.unregister(shortcut) }) }) @@ -70,6 +72,41 @@ function createWindow(): void { } else { mainWindow.loadFile(join(__dirname, '../renderer/index.html')) } + + // Khi bấm dấu X + mainWindow.on('close', (event) => { + if (!isQuiting) { + event.preventDefault() + mainWindow?.hide() + } + }) +} + +function createTray() { + const tray = new Tray(icon) + + const contextMenu = Menu.buildFromTemplate([ + { + label: 'Show', + click: () => { + mainWindow?.show() + } + }, + { + label: 'Quit', + click: () => { + isQuiting = true + app.quit() + } + } + ]) + + tray.setToolTip('Zulip notes') + tray.setContextMenu(contextMenu) + + tray.on('double-click', () => { + mainWindow?.show() + }) } // This method will be called when Electron has finished @@ -89,8 +126,12 @@ app.whenReady().then(() => { // IPC test ipcMain.on('ping', () => console.log('pong')) + // Create main window createWindow() + // Create tray icon + createTray() + app.on('activate', function () { // On macOS it's common to re-create a window in the app when the // dock icon is clicked and there are no other windows open. -- 2.39.2