mirror of
https://github.com/HiMeditator/auto-caption.git
synced 2026-02-14 11:34:43 +08:00
feat: 新增配置保存和读取、新增文档
- 添加配置数据文件保存和载入 - 添加字幕样式恢复默认的选项 - 添加用户说明文档 - 添加字幕引擎说明文档
This commit is contained in:
@@ -8,10 +8,12 @@ import {
|
||||
captionLog,
|
||||
controls,
|
||||
setStyles,
|
||||
resetStyles,
|
||||
sendStyles,
|
||||
sendCaptionLog,
|
||||
setControls,
|
||||
sendControls
|
||||
sendControls,
|
||||
readConfig
|
||||
} from './utils/config'
|
||||
|
||||
class ControlWindow {
|
||||
@@ -36,6 +38,7 @@ class ControlWindow {
|
||||
|
||||
setTimeout(() => {
|
||||
if (this.window) {
|
||||
readConfig()
|
||||
sendStyles(this.window) // 配置初始样式
|
||||
sendCaptionLog(this.window, 'set') // 配置当前字幕记录
|
||||
sendControls(this.window) // 配置字幕引擎配置
|
||||
@@ -71,6 +74,15 @@ class ControlWindow {
|
||||
sendStyles(captionWindow.window)
|
||||
}
|
||||
})
|
||||
ipcMain.on('control.style.reset', () => {
|
||||
resetStyles()
|
||||
if(captionWindow.window){
|
||||
sendStyles(captionWindow.window)
|
||||
}
|
||||
if(this.window){
|
||||
sendStyles(this.window)
|
||||
}
|
||||
})
|
||||
// 控制窗口请求创建字幕窗口
|
||||
ipcMain.on('control.captionWindow.activate', () => {
|
||||
if(!captionWindow.window){
|
||||
|
||||
@@ -2,7 +2,7 @@ import { app, BrowserWindow } from 'electron'
|
||||
import { electronApp, optimizer } from '@electron-toolkit/utils'
|
||||
import { controlWindow } from './control'
|
||||
import { captionWindow } from './caption'
|
||||
import { captionEngine } from './utils/config'
|
||||
import { captionEngine, writeConfig } from './utils/config'
|
||||
|
||||
app.whenReady().then(() => {
|
||||
electronApp.setAppUserModelId('com.himeditator.autocaption')
|
||||
@@ -25,6 +25,7 @@ app.whenReady().then(() => {
|
||||
|
||||
app.on('will-quit', async () => {
|
||||
captionEngine.stop()
|
||||
writeConfig()
|
||||
});
|
||||
|
||||
app.on('window-all-closed', () => {
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import { Styles, CaptionItem, Controls } from '../types'
|
||||
import { BrowserWindow } from 'electron'
|
||||
import { app, BrowserWindow } from 'electron'
|
||||
import { CaptionEngine } from './engine'
|
||||
import * as path from 'path'
|
||||
import * as fs from 'fs'
|
||||
|
||||
export const captionEngine = new CaptionEngine()
|
||||
|
||||
@@ -30,8 +32,6 @@ export const controls: Controls = {
|
||||
customizedCommand: ''
|
||||
}
|
||||
|
||||
export let engineRunning: boolean = false
|
||||
|
||||
export function setStyles(args: any) {
|
||||
styles.fontFamily = args.fontFamily
|
||||
styles.fontSize = args.fontSize
|
||||
@@ -45,6 +45,20 @@ export function setStyles(args: any) {
|
||||
console.log('[INFO] Set Styles:', styles)
|
||||
}
|
||||
|
||||
export function resetStyles() {
|
||||
setStyles({
|
||||
fontFamily: 'sans-serif',
|
||||
fontSize: 24,
|
||||
fontColor: '#000000',
|
||||
background: '#dbe2ef',
|
||||
opacity: 80,
|
||||
transDisplay: true,
|
||||
transFontFamily: 'sans-serif',
|
||||
transFontSize: 24,
|
||||
transFontColor: '#000000'
|
||||
})
|
||||
}
|
||||
|
||||
export function sendStyles(window: BrowserWindow) {
|
||||
window.webContents.send('caption.style.set', styles)
|
||||
console.log(`[INFO] Send Styles to #${window.id}:`, styles)
|
||||
@@ -86,4 +100,24 @@ export function setControls(args: any) {
|
||||
export function sendControls(window: BrowserWindow) {
|
||||
window.webContents.send('control.control.set', controls)
|
||||
console.log(`[INFO] Send Controls to #${window.id}:`, controls)
|
||||
}
|
||||
|
||||
export function readConfig() {
|
||||
const configPath = path.join(app.getPath('userData'), 'config.json')
|
||||
if(fs.existsSync(configPath)){
|
||||
const config = JSON.parse(fs.readFileSync(configPath, 'utf-8'))
|
||||
setStyles(config.styles)
|
||||
setControls(config.controls)
|
||||
console.log('[INFO] Read Config from:', configPath)
|
||||
}
|
||||
}
|
||||
|
||||
export function writeConfig() {
|
||||
const config = {
|
||||
controls: controls,
|
||||
styles: styles
|
||||
}
|
||||
const configPath = path.join(app.getPath('userData'), 'config.json')
|
||||
fs.writeFileSync(configPath, JSON.stringify(config, null, 2))
|
||||
console.log('[INFO] Write Config to:', configPath)
|
||||
}
|
||||
3
src/main/utils/configSave.ts
Normal file
3
src/main/utils/configSave.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
class configSave {
|
||||
|
||||
}
|
||||
@@ -96,6 +96,8 @@ export class CaptionEngine {
|
||||
this.process.on('close', (code: any) => {
|
||||
console.log(`[INFO] Subprocess exited with code ${code}`);
|
||||
this.process = undefined;
|
||||
controls.engineEnabled = false
|
||||
sendControls(window)
|
||||
});
|
||||
}
|
||||
|
||||
@@ -111,9 +113,10 @@ export class CaptionEngine {
|
||||
} else {
|
||||
this.process.kill('SIGKILL');
|
||||
}
|
||||
this.process = undefined;
|
||||
controls.engineEnabled = false;
|
||||
console.log('[INFO] Caption engine process stopped');
|
||||
}
|
||||
this.process = undefined;
|
||||
controls.engineEnabled = false;
|
||||
console.log('[INFO] Caption engine process stopped');
|
||||
if(controlWindow.window) sendControls(controlWindow.window);
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,8 @@
|
||||
<a-card size="small" title="字幕样式设置">
|
||||
<template #extra>
|
||||
<a @click="applyStyle">应用样式</a> |
|
||||
<a @click="resetStyle">取消更改</a>
|
||||
<a @click="backStyle">取消更改</a> |
|
||||
<a @click="resetStyle">恢复默认</a>
|
||||
</template>
|
||||
<div class="style-item">
|
||||
<span class="style-label">字体族</span>
|
||||
@@ -168,7 +169,7 @@ function applyStyle(){
|
||||
captionStyle.sendStyleChange();
|
||||
}
|
||||
|
||||
function resetStyle(){
|
||||
function backStyle(){
|
||||
currentFontFamily.value = captionStyle.fontFamily;
|
||||
currentFontSize.value = captionStyle.fontSize;
|
||||
currentFontColor.value = captionStyle.fontColor;
|
||||
@@ -181,9 +182,13 @@ function resetStyle(){
|
||||
currentTransFontColor.value = captionStyle.transFontColor;
|
||||
}
|
||||
|
||||
function resetStyle() {
|
||||
captionStyle.sendStyleReset();
|
||||
}
|
||||
|
||||
watch(changeSignal, (val) => {
|
||||
if(val == true) {
|
||||
resetStyle();
|
||||
backStyle();
|
||||
captionStyle.changeSignal = false;
|
||||
}
|
||||
})
|
||||
|
||||
@@ -76,6 +76,7 @@ export const useCaptionControlStore = defineStore('captionControl', () => {
|
||||
targetLang.value = controls.targetLang
|
||||
engine.value = controls.engine
|
||||
audio.value = controls.audio
|
||||
engineEnabled.value = controls.engineEnabled
|
||||
translation.value = controls.translation
|
||||
customized.value = controls.customized
|
||||
customizedApp.value = controls.customizedApp
|
||||
@@ -84,7 +85,6 @@ export const useCaptionControlStore = defineStore('captionControl', () => {
|
||||
})
|
||||
|
||||
window.electron.ipcRenderer.on('control.engine.already', () => {
|
||||
engineEnabled.value = true
|
||||
notification.open({
|
||||
message: '字幕引擎已经启动',
|
||||
description: '字幕引擎已经启动,请勿重复启动'
|
||||
@@ -92,7 +92,6 @@ export const useCaptionControlStore = defineStore('captionControl', () => {
|
||||
})
|
||||
|
||||
window.electron.ipcRenderer.on('control.engine.started', () => {
|
||||
engineEnabled.value = true
|
||||
const str0 =
|
||||
`原语言:${sourceLang.value},是否翻译:${translation.value?'是':'否'},` +
|
||||
`字幕引擎:${engine.value},音频类型:${audio.value ? '输入音频' : '输出音频'}` +
|
||||
@@ -105,7 +104,6 @@ export const useCaptionControlStore = defineStore('captionControl', () => {
|
||||
})
|
||||
|
||||
window.electron.ipcRenderer.on('control.engine.stopped', () => {
|
||||
engineEnabled.value = false
|
||||
notification.open({
|
||||
message: '字幕引擎停止',
|
||||
description: '可点击“启动字幕引擎”按钮重新启动'
|
||||
|
||||
@@ -40,6 +40,10 @@ export const useCaptionStyleStore = defineStore('captionStyle', () => {
|
||||
window.electron.ipcRenderer.send('control.style.change', styles)
|
||||
}
|
||||
|
||||
function sendStyleReset() {
|
||||
window.electron.ipcRenderer.send('control.style.reset')
|
||||
}
|
||||
|
||||
window.electron.ipcRenderer.on('caption.style.set', (_, args) => {
|
||||
fontFamily.value = args.fontFamily
|
||||
fontSize.value = args.fontSize
|
||||
@@ -65,6 +69,7 @@ export const useCaptionStyleStore = defineStore('captionStyle', () => {
|
||||
transFontColor, // 翻译字体颜色
|
||||
backgroundRGBA, // 带透明度的背景颜色
|
||||
sendStyleChange, // 发送样式改变
|
||||
sendStyleReset, // 恢复默认样式
|
||||
changeSignal // 样式改变信号
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user