Files
auto-caption/src/main/ControlWindow.ts
himeditator mac 65da30f83d build: 进行 macOS 适配,更新图标资源并升级项目版本
- 移除旧的图标资源,更新为新的图标
- 更新项目版本号至 0.2.1
- 修改 README 中的环境搭建说明,增加 macOS 支持
2025-07-08 13:27:44 +08:00

136 lines
3.5 KiB
TypeScript

import { shell, BrowserWindow, ipcMain, nativeTheme } from 'electron'
import path from 'path'
import { is } from '@electron-toolkit/utils'
import icon from '../../build/icon.png?asset'
import { captionWindow } from './CaptionWindow'
import { allConfig } from './utils/AllConfig'
import { captionEngine } from './utils/CaptionEngine'
class ControlWindow {
window: BrowserWindow | undefined;
public createWindow(): void {
this.window = new BrowserWindow({
icon: icon,
width: 1200,
height: 800,
minWidth: 750,
minHeight: 500,
show: false,
center: true,
autoHideMenuBar: true,
webPreferences: {
preload: path.join(__dirname, '../preload/index.js'),
sandbox: false
}
})
allConfig.readConfig()
this.window.on('ready-to-show', () => {
this.window?.show()
})
this.window.on('closed', () => {
this.window = undefined
allConfig.writeConfig()
})
this.window.webContents.setWindowOpenHandler((details) => {
shell.openExternal(details.url)
return { action: 'deny' }
})
if (is.dev && process.env['ELECTRON_RENDERER_URL']) {
this.window.loadURL(process.env['ELECTRON_RENDERER_URL'])
} else {
this.window.loadFile(path.join(__dirname, '../renderer/index.html'))
}
}
public handleMessage() {
nativeTheme.on('updated', () => {
if(allConfig.uiTheme === 'system'){
if(nativeTheme.shouldUseDarkColors && this.window){
this.window.webContents.send('control.nativeTheme.change', 'dark')
}
else if(!nativeTheme.shouldUseDarkColors && this.window){
this.window.webContents.send('control.nativeTheme.change', 'light')
}
}
})
ipcMain.handle('both.window.mounted', () => {
return allConfig.getFullConfig()
})
ipcMain.handle('control.nativeTheme.get', () => {
if(nativeTheme.shouldUseDarkColors) return 'dark'
return 'light'
})
ipcMain.on('control.uiLanguage.change', (_, args) => {
allConfig.uiLanguage = args
if(captionWindow.window){
captionWindow.window.webContents.send('control.uiLanguage.set', args)
}
})
ipcMain.on('control.uiTheme.change', (_, args) => {
allConfig.uiTheme = args
})
ipcMain.on('control.leftBarWidth.change', (_, args) => {
allConfig.leftBarWidth = args
})
ipcMain.on('control.styles.change', (_, args) => {
allConfig.setStyles(args)
if(captionWindow.window){
allConfig.sendStyles(captionWindow.window)
}
})
ipcMain.on('control.styles.reset', () => {
allConfig.resetStyles()
if(this.window){
allConfig.sendStyles(this.window)
}
if(captionWindow.window){
allConfig.sendStyles(captionWindow.window)
}
})
ipcMain.on('control.captionWindow.activate', () => {
if(!captionWindow.window){
captionWindow.createWindow()
}
else {
captionWindow.window.show()
}
})
ipcMain.on('control.controls.change', (_, args) => {
allConfig.setControls(args)
})
ipcMain.on('control.engine.start', () => {
captionEngine.start()
})
ipcMain.on('control.engine.stop', () => {
captionEngine.stop()
})
ipcMain.on('control.captionLog.clear', () => {
allConfig.captionLog.splice(0)
})
}
public sendErrorMessage(message: string) {
this.window?.webContents.send('control.error.occurred', message)
}
}
export const controlWindow = new ControlWindow()