Files
CeruMusic/src/main/index.ts

242 lines
6.6 KiB
TypeScript
Raw Normal View History

2025-08-16 11:34:37 +08:00
import { app, shell, BrowserWindow, ipcMain, screen, Tray, Menu } from 'electron'
2025-08-13 13:33:43 +08:00
import { join } from 'path'
import { electronApp, optimizer, is } from '@electron-toolkit/utils'
2025-08-13 22:39:11 +08:00
import icon from '../../resources/logo.png?asset'
import path from 'node:path'
2025-08-16 15:37:53 +08:00
import { NetEaseService } from './services/netease-service'
2025-08-13 13:33:43 +08:00
2025-08-16 11:34:37 +08:00
let tray: Tray | null = null
let mainWindow: BrowserWindow | null = null
let isQuitting = false
function createTray(): void {
// 创建系统托盘
const trayIconPath = path.join(__dirname, '../../resources/logo.png')
tray = new Tray(trayIconPath)
// 创建托盘菜单
const contextMenu = Menu.buildFromTemplate([
{
label: '显示窗口',
click: () => {
if (mainWindow) {
mainWindow.show()
mainWindow.focus()
}
}
},
{
label: '播放/暂停',
click: () => {
// 这里可以添加播放控制逻辑
console.log('播放/暂停')
}
},
{ type: 'separator' },
{
label: '退出',
click: () => {
isQuitting = true
app.quit()
}
}
])
tray.setContextMenu(contextMenu)
tray.setToolTip('Ceru Music')
// 双击托盘图标显示窗口
tray.on('click', () => {
if (mainWindow) {
if (mainWindow.isVisible()) {
mainWindow.hide()
} else {
mainWindow.show()
mainWindow.focus()
}
}
})
}
2025-08-13 13:33:43 +08:00
function createWindow(): void {
// Create the browser window.
2025-08-16 11:34:37 +08:00
mainWindow = new BrowserWindow({
width: 1100,
height: 750,
minWidth: 970,
minHeight: 670,
2025-08-13 13:33:43 +08:00
show: false,
2025-08-16 11:34:37 +08:00
center: true,
2025-08-13 13:33:43 +08:00
autoHideMenuBar: true,
2025-08-16 11:34:37 +08:00
// alwaysOnTop: true,
maxWidth: screen.getPrimaryDisplay()?.workAreaSize.width,
maxHeight: screen.getPrimaryDisplay()?.workAreaSize.height,
titleBarStyle: 'hidden',
2025-08-13 13:33:43 +08:00
...(process.platform === 'linux' ? { icon } : {}),
2025-08-16 11:34:37 +08:00
// ...(process.platform !== 'darwin' ? { titleBarOverlay: true } : {}),
2025-08-13 22:39:11 +08:00
icon: path.join(__dirname, '../../resources/logo.png'),
2025-08-13 13:33:43 +08:00
webPreferences: {
preload: join(__dirname, '../preload/index.js'),
sandbox: false,
webSecurity: false,
nodeIntegration: true,
contextIsolation: false
2025-08-13 13:33:43 +08:00
}
})
2025-08-16 11:34:37 +08:00
if (process.platform == 'darwin') mainWindow.setWindowButtonVisibility(false)
2025-08-13 13:33:43 +08:00
mainWindow.on('ready-to-show', () => {
2025-08-16 11:34:37 +08:00
mainWindow?.show()
})
// 阻止窗口关闭,改为隐藏到系统托盘
mainWindow.on('close', (event) => {
if (!isQuitting) {
event.preventDefault()
mainWindow?.hide()
// 显示托盘通知
if (tray) {
tray.displayBalloon({
iconType: 'info',
title: 'Ceru Music',
content: '已最小化到系统托盘啦,点击托盘图标可重新打开~'
})
}
}
2025-08-13 13:33:43 +08:00
})
mainWindow.webContents.setWindowOpenHandler((details) => {
shell.openExternal(details.url)
return { action: 'deny' }
})
// HMR for renderer base on electron-vite cli.
// Load the remote URL for development or the local html file for production.
if (is.dev && process.env['ELECTRON_RENDERER_URL']) {
mainWindow.loadURL(process.env['ELECTRON_RENDERER_URL'])
} else {
mainWindow.loadFile(join(__dirname, '../renderer/index.html'))
}
}
2025-08-16 15:37:53 +08:00
// 创建网易云音乐服务实例
const netEaseService = new NetEaseService()
// 注册网易云音乐服务的IPC处理器
ipcMain.handle('netease-search', async (_, args) => {
2025-08-16 16:13:47 +08:00
return await netEaseService.search(args)
2025-08-16 15:37:53 +08:00
})
ipcMain.handle('netease-getSongDetail', async (_, args) => {
2025-08-16 16:13:47 +08:00
return await netEaseService.getSongDetail(args)
2025-08-16 15:37:53 +08:00
})
ipcMain.handle('netease-getSongUrl', async (_, args) => {
2025-08-16 16:13:47 +08:00
return await netEaseService.getSongUrl(args)
2025-08-16 15:37:53 +08:00
})
ipcMain.handle('netease-getLyric', async (_, args) => {
2025-08-16 16:13:47 +08:00
return await netEaseService.getLyric(args)
2025-08-16 15:37:53 +08:00
})
ipcMain.handle('netease-getToplist', async (_, args) => {
2025-08-16 16:13:47 +08:00
return await netEaseService.getToplist(args)
2025-08-16 15:37:53 +08:00
})
ipcMain.handle('netease-getToplistDetail', async (_, args) => {
2025-08-16 16:13:47 +08:00
return await netEaseService.getToplistDetail(args)
2025-08-16 15:37:53 +08:00
})
ipcMain.handle('netease-getListSongs', async (_, args) => {
2025-08-16 16:13:47 +08:00
return await netEaseService.getListSongs(args)
2025-08-16 15:37:53 +08:00
})
2025-08-13 13:33:43 +08:00
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
// Set app user model id for windows
2025-08-16 11:34:37 +08:00
electronApp.setAppUserModelId('com.cerulean.music')
2025-08-13 13:33:43 +08:00
// Default open or close DevTools by F12 in development
// and ignore CommandOrControl + R in production.
// see https://github.com/alex8088/electron-toolkit/tree/master/packages/utils
app.on('browser-window-created', (_, window) => {
optimizer.watchWindowShortcuts(window)
})
// IPC test
ipcMain.on('ping', () => console.log('pong'))
2025-08-16 11:34:37 +08:00
// 窗口控制 IPC 处理
ipcMain.on('window-minimize', () => {
const window = BrowserWindow.getFocusedWindow()
if (window) {
window.minimize()
}
})
ipcMain.on('window-maximize', () => {
const window = BrowserWindow.getFocusedWindow()
if (window) {
if (window.isMaximized()) {
window.unmaximize()
} else {
window.maximize()
}
}
})
ipcMain.on('window-close', () => {
const window = BrowserWindow.getFocusedWindow()
if (window) {
window.close()
}
})
// Mini 模式 IPC 处理 - 最小化到系统托盘
ipcMain.on('window-mini-mode', (_, isMini) => {
if (mainWindow) {
if (isMini) {
// 进入 Mini 模式:隐藏窗口到系统托盘
mainWindow.hide()
// 显示托盘通知(可选)
if (tray) {
tray.displayBalloon({
title: '澜音 Music',
content: '已最小化到系统托盘啦,点击托盘图标可重新打开~'
})
}
} else {
// 退出 Mini 模式:显示窗口
mainWindow.show()
mainWindow.focus()
}
}
})
2025-08-13 13:33:43 +08:00
createWindow()
2025-08-16 11:34:37 +08:00
createTray()
2025-08-13 13:33:43 +08:00
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.
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})
2025-08-16 11:34:37 +08:00
// 当所有窗口关闭时不退出应用,因为我们有系统托盘
2025-08-13 13:33:43 +08:00
app.on('window-all-closed', () => {
2025-08-16 11:34:37 +08:00
// 在 macOS 上,应用通常会保持活跃状态
// 在其他平台上,我们也保持应用运行,因为有系统托盘
})
// 应用退出前的清理
app.on('before-quit', () => {
isQuitting = true
2025-08-13 13:33:43 +08:00
})
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.