@@ -14,7 +14,7 @@ import type { ScreenCaptureOptions, ScreenshotFrameData } from './screenCapture.
1414import { safeHandle , getMainWindowId } from './ipcUtils.js' ;
1515import { logger } from './logger.js' ;
1616import { scoreAndFilterWindows } from './windowScorer.js' ;
17- import { getCaptureRateScale } from './performanceMode.js' ;
17+ import { getCaptureRateScale , onPerformanceModeChange } from './performanceMode.js' ;
1818
1919// ================================================================
2020// 模块级状态
@@ -23,6 +23,15 @@ import { getCaptureRateScale } from './performanceMode.js';
2323/** 当前活跃的截图采集实例 */
2424let activeCapture : ScreenCapture | null = null ;
2525
26+ /** 活跃采集的原始参数(未缩放 interval),供性能模式变更后按新频率重建实例 */
27+ let activeOptions : ScreenCaptureOptions | null = null ;
28+
29+ /** 活跃采集的帧推送目标窗口(重启实例后保持推送不中断) */
30+ let activeSenderWin : BrowserWindow | null = null ;
31+
32+ /** 性能模式变更订阅的取消函数 */
33+ let unsubscribeModeChange : ( ( ) => void ) | null = null ;
34+
2635/** screen_capture_start 防抖:500ms 内多次调用只响应最后一次 */
2736let startDebounceTimer : ReturnType < typeof setTimeout > | null = null ;
2837const START_DEBOUNCE_MS = 500 ;
@@ -45,10 +54,43 @@ let lastWindowIds: Set<string> = new Set();
4554
4655const WINDOW_WATCH_INTERVAL_MS = 3000 ;
4756
57+ /** 按性能模式缩放采集间隔(静谧档 scale=0.5 → 间隔翻倍、频率减半,降低开销) */
58+ function applyRateScale ( options : ScreenCaptureOptions ) : ScreenCaptureOptions {
59+ const scaled = { ...options } ;
60+ const rateScale = getCaptureRateScale ( ) ;
61+ if ( typeof scaled . interval === 'number' && rateScale !== 1 ) {
62+ scaled . interval = Math . min ( 60000 , Math . round ( scaled . interval / rateScale ) ) ;
63+ }
64+ return scaled ;
65+ }
66+
67+ /** 销毁旧实例并按其参数/推送目标重建采集(start 与性能模式重启共用) */
68+ function startCaptureWith ( senderWin : BrowserWindow | null , options : ScreenCaptureOptions ) : void {
69+ if ( activeCapture ) {
70+ activeCapture . dispose ( ) ;
71+ activeCapture = null ;
72+ }
73+ activeCapture = new ScreenCapture ( applyRateScale ( options ) , ( frame : ScreenshotFrameData ) => {
74+ if ( senderWin && ! senderWin . isDestroyed ( ) ) {
75+ senderWin . webContents . send ( 'screen_capture_frame' , frame ) ;
76+ }
77+ } ) ;
78+ activeCapture . start ( ) ;
79+ }
80+
4881/**
4982 * 注册屏幕截图与窗口监听相关的 IPC handler
5083 */
5184export function registerScreenCaptureHandlers ( ) : void {
85+ // 性能模式变更 → 活跃采集按新频率优雅重启(即时生效,替代"等下次启动")
86+ unsubscribeModeChange = onPerformanceModeChange ( ( ) => {
87+ if ( ! activeCapture || ! activeOptions ) return ;
88+ const senderWin = activeSenderWin ;
89+ const options = activeOptions ;
90+ logger . info ( '[IPC] 性能模式变更,采集按新频率重启' ) ;
91+ startCaptureWith ( senderWin , options ) ;
92+ } ) ;
93+
5294 safeHandle (
5395 'screen_capture_start' ,
5496 async ( event , options : ScreenCaptureOptions ) => {
@@ -93,19 +135,11 @@ export function registerScreenCaptureHandlers(): void {
93135 safeOptions . interval = 5000 ;
94136 }
95137
96- // 按性能模式缩放采集间隔(静谧档 scale=0.5 → 间隔翻倍、频率减半,降低开销)
97- const rateScale = getCaptureRateScale ( ) ;
98- if ( typeof safeOptions . interval === 'number' && rateScale !== 1 ) {
99- safeOptions . interval = Math . min ( 60000 , Math . round ( safeOptions . interval / rateScale ) ) ;
100- }
138+ // 记录原始参数与推送目标(供性能模式变更时重启采集)
139+ activeOptions = safeOptions ;
140+ activeSenderWin = senderWin ;
101141
102- activeCapture = new ScreenCapture ( safeOptions , ( frame : ScreenshotFrameData ) => {
103- if ( senderWin && ! senderWin . isDestroyed ( ) ) {
104- senderWin . webContents . send ( 'screen_capture_frame' , frame ) ;
105- }
106- } ) ;
107-
108- activeCapture . start ( ) ;
142+ startCaptureWith ( senderWin , safeOptions ) ;
109143 logger . info ( '[IPC] screen_capture_start 已启动(防抖后)' ) ;
110144 resolve ( { success : true } ) ;
111145 } , START_DEBOUNCE_MS ) ;
@@ -136,6 +170,8 @@ export function registerScreenCaptureHandlers(): void {
136170 activeCapture = null ;
137171 logger . info ( '[IPC] screen_capture_stop 已停止' ) ;
138172 }
173+ activeOptions = null ;
174+ activeSenderWin = null ;
139175 return { success : true } ;
140176 } ) ;
141177
@@ -226,6 +262,11 @@ export function registerScreenCaptureHandlers(): void {
226262 * 释放屏幕截图相关资源(含防抖定时器与轮询)
227263 */
228264export function disposeScreenCaptureHandlers ( ) : void {
265+ // 注销性能模式变更订阅
266+ if ( unsubscribeModeChange ) {
267+ unsubscribeModeChange ( ) ;
268+ unsubscribeModeChange = null ;
269+ }
229270 // 清理防抖定时器
230271 if ( startDebounceTimer ) {
231272 clearTimeout ( startDebounceTimer ) ;
@@ -245,4 +286,6 @@ export function disposeScreenCaptureHandlers(): void {
245286 activeCapture . dispose ( ) ;
246287 activeCapture = null ;
247288 }
289+ activeOptions = null ;
290+ activeSenderWin = null ;
248291}
0 commit comments