Merge pull request 'add tray future' (#2) from zelda.add-tray-icon into main

Reviewed-on: #2
This commit is contained in:
zelda 2025-05-09 18:49:04 +10:00
commit a64ae2aa7a
1 changed files with 52 additions and 11 deletions

View File

@ -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.