feat(control): 重构项目,增加字幕引擎配置

This commit is contained in:
himeditator
2025-06-19 18:59:13 +08:00
parent 5da9c3c038
commit 54c618aa3f
17 changed files with 152 additions and 180 deletions

View File

@@ -3,8 +3,7 @@ import path from 'path'
import { is } from '@electron-toolkit/utils'
import icon from '../../resources/icon.png?asset'
import { controlWindow } from './control'
import { sendStyles, sendCaptionLog } from './data'
import { send } from 'vite'
import { sendStyles, sendCaptionLog } from './utils/config'
class CaptionWindow {
window: BrowserWindow | undefined;
@@ -40,7 +39,6 @@ class CaptionWindow {
})
this.window.on('closed', () => {
console.log('INFO caption window closed')
this.window = undefined
})
@@ -63,7 +61,6 @@ class CaptionWindow {
ipcMain.on('caption.controlWindow.activate', () => {
if(!controlWindow.window){
controlWindow.createWindow()
console.log('GET caption.controlWindow.activate')
}
else {
controlWindow.window.show()
@@ -71,21 +68,18 @@ class CaptionWindow {
})
// 字幕窗口高度发生变化
ipcMain.on('caption.windowHeight.change', (_, height) => {
console.log('GET caption.window.height.change', height)
if(this.window){
this.window.setSize(this.window.getSize()[0], height)
}
})
// 关闭字幕窗口
ipcMain.on('caption.window.close', () => {
console.log('GET caption.window.close')
if(this.window){
this.window.close()
}
})
// 是否固定在最前面
ipcMain.on('caption.pin.set', (_, pinned) => {
console.log('GET caption.pin.set', pinned)
if(this.window){
this.window.setAlwaysOnTop(pinned)
}

View File

@@ -2,8 +2,13 @@ import { shell, BrowserWindow, ipcMain } from 'electron'
import path from 'path'
import { is } from '@electron-toolkit/utils'
import icon from '../../resources/icon.png?asset'
import { setStyles, sendStyles, sendCaptionLog } from './data'
import { captionWindow } from './caption'
import {
setStyles,
sendStyles,
sendCaptionLog,
setControls
} from './utils/config'
class ControlWindow {
window: BrowserWindow | undefined;
@@ -38,7 +43,6 @@ class ControlWindow {
})
this.window.on('closed', () => {
console.log('INFO control window closed')
this.window = undefined
})
@@ -57,7 +61,6 @@ class ControlWindow {
public handleMessage() {
// 控制窗口样式更新
ipcMain.on('control.style.change', (_, args) => {
console.log('GET control.style.change', args)
setStyles(args)
if(captionWindow.window){
sendStyles(captionWindow.window)
@@ -67,12 +70,15 @@ class ControlWindow {
ipcMain.on('control.captionWindow.activate', () => {
if(!captionWindow.window){
captionWindow.createWindow()
console.log('GET control.captionWindow.activate')
}
else {
captionWindow.window.show()
}
})
// 字幕引擎控制配置更新
ipcMain.on('control.control.change', (_, args) => {
setControls(args)
})
}
}

View File

@@ -3,7 +3,7 @@ import { electronApp, optimizer } from '@electron-toolkit/utils'
import { controlWindow } from './control'
import { captionWindow } from './caption'
import { PythonProcess } from './pythonProcess'
import { PythonProcess } from './utils/pythonProcess'
const pySubProcess = new PythonProcess()
app.whenReady().then(() => {
@@ -18,7 +18,7 @@ app.whenReady().then(() => {
controlWindow.createWindow()
pySubProcess.start()
// pySubProcess.start()
app.on('activate', function () {
if (BrowserWindow.getAllWindows().length === 0){

29
src/main/types/index.ts Normal file
View File

@@ -0,0 +1,29 @@
export interface Styles {
fontFamily: string,
fontSize: number,
fontColor: string,
background: string,
opacity: number,
transDisplay: boolean,
transFontFamily: string,
transFontSize: number,
transFontColor: string
}
export interface CaptionItem {
index: number,
time_s: string,
time_t: string,
text: string,
translation: string
}
export interface Controls {
sourceLang: string,
targetLang: string,
engine: string,
translation: boolean,
customized: boolean,
customizedApp: string,
customizedCommand: string
}

View File

@@ -1,26 +1,7 @@
import { Styles, CaptionItem, Controls } from '../types'
import { BrowserWindow } from 'electron'
export interface Styles {
fontFamily: string,
fontSize: number,
fontColor: string,
background: string,
opacity: number,
transDisplay: boolean,
transFontFamily: string,
transFontSize: number,
transFontColor: string
}
export interface CaptionItem {
index: number,
time_s: string,
time_t: string,
text: string,
translation: string
}
export let styles: Styles = {
export const styles: Styles = {
fontFamily: 'sans-serif',
fontSize: 24,
fontColor: '#000000',
@@ -32,6 +13,18 @@ export let styles: Styles = {
transFontColor: '#000000'
}
export const captionLog: CaptionItem[] = []
export const controls: Controls = {
sourceLang: 'en',
targetLang: 'zh',
engine: 'gummy',
translation: true,
customized: false,
customizedApp: '',
customizedCommand: ''
}
export function setStyles(args: any) {
styles.fontFamily = args.fontFamily
styles.fontSize = args.fontSize
@@ -42,18 +35,16 @@ export function setStyles(args: any) {
styles.transFontFamily = args.transFontFamily
styles.transFontSize = args.transFontSize
styles.transFontColor = args.transFontColor
console.log('[INFO] Set Styles:', styles)
}
export function sendStyles(window: BrowserWindow) {
window.webContents.send('caption.style.set', styles)
console.log('SNED caption.style.set')
console.log('[INFO] Send Styles:', styles)
}
export let captionLog: CaptionItem[] = []
export function sendCaptionLog(window: BrowserWindow) {
window.webContents.send('both.log.set', captionLog)
console.log('SEND both.log.set')
}
export function addCaptionLog(log: CaptionItem) {
@@ -64,8 +55,18 @@ export function addCaptionLog(log: CaptionItem) {
else {
captionLog.push(log)
}
console.log('ADD caption')
for(const window of BrowserWindow.getAllWindows()){
sendCaptionLog(window)
}
}
export function setControls(args: any) {
controls.sourceLang = args.sourceLang
controls.targetLang = args.targetLang
controls.engine = args.engine
controls.translation = args.translation
controls.customized = args.customized
controls.customizedApp = args.customizedApp
controls.customizedCommand = args.customizedCommand
console.log('[INFO] Set Controls:', controls)
}

View File

@@ -1,7 +1,7 @@
import { spawn } from 'child_process'
import { app } from 'electron'
import path from 'path'
import { addCaptionLog } from './data'
import { addCaptionLog } from './config'
export class PythonProcess {
public start() {