mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2025-12-23 10:00:08 +08:00
* Added 'electron-store' to the Vite configuration for Electron. * Imported configuration from '@main/config' in the main index file. * Changed default return value for getTrayOnClose method in ConfigManager to false
140 lines
4.2 KiB
TypeScript
140 lines
4.2 KiB
TypeScript
import '@main/config'
|
|
|
|
import { electronApp, optimizer } from '@electron-toolkit/utils'
|
|
import { replaceDevtoolsFont } from '@main/utils/windowUtil'
|
|
import { IpcChannel } from '@shared/IpcChannel'
|
|
import { app, BrowserWindow, ipcMain } from 'electron'
|
|
import installExtension, { REACT_DEVELOPER_TOOLS, REDUX_DEVTOOLS } from 'electron-devtools-installer'
|
|
import Logger from 'electron-log'
|
|
|
|
import { isDev, isMac, isWin } from './constant'
|
|
import { registerIpc } from './ipc'
|
|
import { configManager } from './services/ConfigManager'
|
|
import mcpService from './services/MCPService'
|
|
import {
|
|
CHERRY_STUDIO_PROTOCOL,
|
|
handleProtocolUrl,
|
|
registerProtocolClient,
|
|
setupAppImageDeepLink
|
|
} from './services/ProtocolClient'
|
|
import { registerShortcuts } from './services/ShortcutService'
|
|
import { TrayService } from './services/TrayService'
|
|
import { windowService } from './services/WindowService'
|
|
import { setUserDataDir } from './utils/file'
|
|
|
|
Logger.initialize()
|
|
|
|
// in production mode, handle uncaught exception and unhandled rejection globally
|
|
if (!isDev) {
|
|
// handle uncaught exception
|
|
process.on('uncaughtException', (error) => {
|
|
Logger.error('Uncaught Exception:', error)
|
|
})
|
|
|
|
// handle unhandled rejection
|
|
process.on('unhandledRejection', (reason, promise) => {
|
|
Logger.error('Unhandled Rejection at:', promise, 'reason:', reason)
|
|
})
|
|
}
|
|
|
|
// Check for single instance lock
|
|
if (!app.requestSingleInstanceLock()) {
|
|
app.quit()
|
|
process.exit(0)
|
|
} else {
|
|
// Portable dir must be setup before app ready
|
|
setUserDataDir()
|
|
|
|
// 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(async () => {
|
|
// Set app user model id for windows
|
|
electronApp.setAppUserModelId(import.meta.env.VITE_MAIN_BUNDLE_ID || 'com.kangfenmao.CherryStudio')
|
|
|
|
// Mac: Hide dock icon before window creation when launch to tray is set
|
|
const isLaunchToTray = configManager.getLaunchToTray()
|
|
if (isLaunchToTray) {
|
|
app.dock?.hide()
|
|
}
|
|
|
|
const mainWindow = windowService.createMainWindow()
|
|
new TrayService()
|
|
|
|
app.on('activate', function () {
|
|
const mainWindow = windowService.getMainWindow()
|
|
if (!mainWindow || mainWindow.isDestroyed()) {
|
|
windowService.createMainWindow()
|
|
} else {
|
|
windowService.showMainWindow()
|
|
}
|
|
})
|
|
|
|
registerShortcuts(mainWindow)
|
|
|
|
registerIpc(mainWindow, app)
|
|
|
|
replaceDevtoolsFont(mainWindow)
|
|
|
|
// Setup deep link for AppImage on Linux
|
|
await setupAppImageDeepLink()
|
|
|
|
if (isDev) {
|
|
installExtension([REDUX_DEVTOOLS, REACT_DEVELOPER_TOOLS])
|
|
.then((name) => console.log(`Added Extension: ${name}`))
|
|
.catch((err) => console.log('An error occurred: ', err))
|
|
}
|
|
ipcMain.handle(IpcChannel.System_GetDeviceType, () => {
|
|
return isMac ? 'mac' : isWin ? 'windows' : 'linux'
|
|
})
|
|
|
|
ipcMain.handle(IpcChannel.System_GetHostname, () => {
|
|
return require('os').hostname()
|
|
})
|
|
|
|
ipcMain.handle(IpcChannel.System_ToggleDevTools, (e) => {
|
|
const win = BrowserWindow.fromWebContents(e.sender)
|
|
win && win.webContents.toggleDevTools()
|
|
})
|
|
})
|
|
|
|
registerProtocolClient(app)
|
|
|
|
// macOS specific: handle protocol when app is already running
|
|
app.on('open-url', (event, url) => {
|
|
event.preventDefault()
|
|
handleProtocolUrl(url)
|
|
})
|
|
|
|
// Listen for second instance
|
|
app.on('second-instance', (_event, argv) => {
|
|
windowService.showMainWindow()
|
|
|
|
// Protocol handler for Windows/Linux
|
|
// The commandLine is an array of strings where the last item might be the URL
|
|
const url = argv.find((arg) => arg.startsWith(CHERRY_STUDIO_PROTOCOL + '://'))
|
|
if (url) handleProtocolUrl(url)
|
|
})
|
|
|
|
app.on('browser-window-created', (_, window) => {
|
|
optimizer.watchWindowShortcuts(window)
|
|
})
|
|
|
|
app.on('before-quit', () => {
|
|
app.isQuitting = true
|
|
})
|
|
|
|
app.on('will-quit', async () => {
|
|
// event.preventDefault()
|
|
try {
|
|
await mcpService.cleanup()
|
|
} catch (error) {
|
|
Logger.error('Error cleaning up MCP service:', error)
|
|
}
|
|
})
|
|
|
|
// 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.
|
|
}
|