Files
cstb-next/src/store/tool.ts

121 lines
3.3 KiB
TypeScript
Raw Normal View History

import { store } from "@tauri-store/valtio"
2025-03-17 11:48:30 +08:00
import { useSnapshot } from "valtio"
import { DEFAULT_STORE_CONFIG } from "./config"
import { emit } from "@tauri-apps/api/event"
import { send } from "process"
2025-03-23 16:42:13 +08:00
interface LaunchOption {
option: string
name: string
}
export interface VideoSetting {
width: number; // 分辨率宽度
height: number; // 分辨率高度
fullscreen: string; // 全屏
vsync: string; // 垂直同步
enhanceCharacterContrast: string; // 增强角色对比度
cmaa2AntiAliasing: string; // CMAA2抗锯齿
msaaAntiAliasing: string; // 多重采样抗锯齿
globalShadowQuality: string; // 全局阴影效果
dynamicShadows: string; // 动态阴影
modelTextureDetail: string; // 模型/贴图细节
textureFilteringMode: string; // 贴图过滤模式
lightShadowDetail: string; // 光影细节
particleDetail: string; // 粒子细节
ambientOcclusion: string; // 环境光遮蔽
hdr: string; // 高动态范围
fidelityFxSuperResolution: string; // Fidelity FX 超级分辨率
}
2025-03-17 11:48:30 +08:00
const defaultValue = {
launchOptions: [
2025-03-23 16:42:13 +08:00
{ option: "-novid -high -freq 144 -fullscreen", name: "" },
{ option: "-novid -high -w 1920 -h 1080 -freq 144 -sw -noborder", name: "" },
{ option: "-novid -high -freq 144 -fullscreen -allow_third_party_software", name: "" },
] as LaunchOption[],
launchIndex: 0,
2025-03-12 22:20:06 +08:00
powerPlan: 0,
2025-03-23 16:42:13 +08:00
videoSetting: {
width: 1920,
height: 1080
} as VideoSetting,
}
2025-03-12 22:20:06 +08:00
export const toolStore = store(
"tool",
{ ...defaultValue },
DEFAULT_STORE_CONFIG,
)
2025-03-17 11:48:30 +08:00
export const useToolStore = () => {
void toolStore.start
const state = useSnapshot(toolStore.state)
if (typeof window !== 'undefined') {
setTimeout(() => {
sendCurrentLaunchOptionToTray(state.launchIndex)
sendPowerPlanToTray(state.powerPlan)
}, 500)
}
2025-03-27 00:59:03 +08:00
2025-03-17 11:48:30 +08:00
return {
state,
store: toolStore,
2025-03-17 11:48:30 +08:00
setLaunchOption,
setLaunchOptions,
setLaunchIndex,
setPowerPlan,
2025-03-23 16:42:13 +08:00
setVideoSetting,
2025-03-17 11:48:30 +08:00
addLaunchOption,
resetToolStore,
}
}
2025-03-23 16:42:13 +08:00
const setLaunchOption = (option: LaunchOption, index: number) => {
toolStore.state.launchOptions = [
...toolStore.state.launchOptions.slice(0, index),
option,
2025-03-12 22:20:06 +08:00
...toolStore.state.launchOptions.slice(index + 1),
]
}
2025-03-23 16:42:13 +08:00
const setLaunchOptions = (options: LaunchOption[]) => {
toolStore.state.launchOptions = options
}
2025-03-17 11:48:30 +08:00
const setLaunchIndex = (index: number) => {
toolStore.state.launchIndex = index
sendCurrentLaunchOptionToTray(index)
}
const sendCurrentLaunchOptionToTray = (index: number) => {
2025-03-27 00:59:03 +08:00
void emit("tray://get_current_launch_option", toolStore.state.launchOptions[index].name || index + 1)
}
2025-03-17 11:48:30 +08:00
const setPowerPlan = (plan: number) => {
toolStore.state.powerPlan = plan
sendPowerPlanToTray(plan)
}
const sendPowerPlanToTray = (plan: number) => {
2025-03-27 00:59:03 +08:00
void emit("tray://get_powerplan", plan)
}
2025-03-23 16:42:13 +08:00
const setVideoSetting = (setting: VideoSetting) => {
toolStore.state.videoSetting = setting
}
const addLaunchOption = (option: LaunchOption) => {
// 限制最高10个
if (toolStore.state.launchOptions.length >= 10) {
return
}
2025-03-14 19:13:32 +08:00
toolStore.state.launchOptions = [...toolStore.state.launchOptions, option]
}
2025-03-17 11:48:30 +08:00
const resetToolStore = () => {
setLaunchOptions(defaultValue.launchOptions)
setLaunchIndex(defaultValue.launchIndex)
setPowerPlan(defaultValue.powerPlan)
2025-03-23 16:42:13 +08:00
setVideoSetting(defaultValue.videoSetting)
}