add tray future #2
|
|
@ -1,16 +1,18 @@
|
||||||
/* eslint-disable prettier/prettier */
|
/* eslint-disable prettier/prettier */
|
||||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||||
import { app, shell, BrowserWindow, ipcMain, screen, globalShortcut } from 'electron'
|
import { electronApp, is, optimizer } from '@electron-toolkit/utils'
|
||||||
import path, { join } from 'path'
|
import { app, BrowserWindow, globalShortcut, ipcMain, Menu, screen, shell, Tray } from 'electron'
|
||||||
import { electronApp, optimizer, is } from '@electron-toolkit/utils'
|
|
||||||
import icon from '../../resources/icon.png?asset'
|
|
||||||
import fs from 'fs'
|
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 {
|
function createWindow(): void {
|
||||||
// Get Screen width, height
|
// Get Screen width, height
|
||||||
const { width, height } = screen.getPrimaryDisplay().workAreaSize
|
const { width, height } = screen.getPrimaryDisplay().workAreaSize
|
||||||
|
|
||||||
const mainWindow = new BrowserWindow({
|
mainWindow = new BrowserWindow({
|
||||||
width: width / 2,
|
width: width / 2,
|
||||||
height: height / 2,
|
height: height / 2,
|
||||||
show: false,
|
show: false,
|
||||||
|
|
@ -26,7 +28,7 @@ function createWindow(): void {
|
||||||
mainWindow.setPosition(width / 2, height / 2)
|
mainWindow.setPosition(width / 2, height / 2)
|
||||||
|
|
||||||
mainWindow.on('ready-to-show', () => {
|
mainWindow.on('ready-to-show', () => {
|
||||||
mainWindow.show()
|
mainWindow?.show()
|
||||||
})
|
})
|
||||||
|
|
||||||
mainWindow.webContents.setWindowOpenHandler((details) => {
|
mainWindow.webContents.setWindowOpenHandler((details) => {
|
||||||
|
|
@ -46,19 +48,19 @@ function createWindow(): void {
|
||||||
mainWindow.webContents.once('did-finish-load', () => {
|
mainWindow.webContents.once('did-finish-load', () => {
|
||||||
const shortcut = 'Control+Shift+C'
|
const shortcut = 'Control+Shift+C'
|
||||||
|
|
||||||
mainWindow.on('focus', () => {
|
mainWindow?.on('focus', () => {
|
||||||
globalShortcut.register(shortcut, () => {
|
globalShortcut.register(shortcut, () => {
|
||||||
const pos = screen.getCursorScreenPoint()
|
const pos = screen.getCursorScreenPoint()
|
||||||
mainWindow.webContents.inspectElement(pos.x, pos.y)
|
mainWindow?.webContents.inspectElement(pos.x, pos.y)
|
||||||
mainWindow.webContents.focus() // optional: refocus back
|
mainWindow?.webContents.focus() // optional: refocus back
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
mainWindow.on('blur', () => {
|
mainWindow?.on('blur', () => {
|
||||||
globalShortcut.unregister(shortcut)
|
globalShortcut.unregister(shortcut)
|
||||||
})
|
})
|
||||||
|
|
||||||
mainWindow.on('closed', () => {
|
mainWindow?.on('closed', () => {
|
||||||
globalShortcut.unregister(shortcut)
|
globalShortcut.unregister(shortcut)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
@ -70,6 +72,41 @@ function createWindow(): void {
|
||||||
} else {
|
} else {
|
||||||
mainWindow.loadFile(join(__dirname, '../renderer/index.html'))
|
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
|
// This method will be called when Electron has finished
|
||||||
|
|
@ -89,8 +126,12 @@ app.whenReady().then(() => {
|
||||||
// IPC test
|
// IPC test
|
||||||
ipcMain.on('ping', () => console.log('pong'))
|
ipcMain.on('ping', () => console.log('pong'))
|
||||||
|
|
||||||
|
// Create main window
|
||||||
createWindow()
|
createWindow()
|
||||||
|
|
||||||
|
// Create tray icon
|
||||||
|
createTray()
|
||||||
|
|
||||||
app.on('activate', function () {
|
app.on('activate', function () {
|
||||||
// On macOS it's common to re-create a window in the app when the
|
// 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.
|
// dock icon is clicked and there are no other windows open.
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue