@@ -92,7 +92,8 @@ const evaluate = async (debuggee: Electron.Debugger, expression: string) => {
9292 return reply . result ?. value ;
9393} ;
9494
95- let focusSequence = 0 ;
95+ /** Host focus shared by every guest press in flight in one window, so overlapping presses agree on it. */
96+ const HOST_FOCUS = "globalThis.__t3PreviewFocus" ;
9697
9798/** A CDP pointer press focuses the guest's native view even with focus emulation enabled. */
9899const sendPointerPress = async (
@@ -106,53 +107,52 @@ const sendPointerPress = async (
106107 if ( ! host || host . isDestroyed ( ) || webContents . focusedFrame ) {
107108 return debuggee . sendCommand ( method , params , sessionId ) ;
108109 }
109- const key = JSON . stringify ( `__t3PreviewFocus${ ++ focusSequence } ` ) ;
110110 await host . executeJavaScript ( `(() => {
111+ const state = (${ HOST_FOCUS } ??= { saved: null, presses: [] });
111112 const active = document.activeElement;
112- if (!active || active === document.body || active.tagName === "WEBVIEW") return;
113- globalThis[${ key } ] = () => {
114- delete globalThis[${ key } ];
115- const current = document.activeElement;
116- if (active.isConnected && current?.tagName === "WEBVIEW" &&
117- current.getWebContentsId() === ${ webContents . id } ) active.focus({ preventScroll: true });
118- };
113+ if (active && active !== document.body && active.tagName !== "WEBVIEW") state.saved = active;
114+ state.presses.push(${ webContents . id } );
119115 })()` ) ;
120116 try {
121117 return await debuggee . sendCommand ( method , params , sessionId ) ;
122118 } finally {
123119 if ( ! host . isDestroyed ( ) ) {
124- await host . executeJavaScript ( `globalThis[${ key } ]?.()` ) ;
120+ await host . executeJavaScript ( `(() => {
121+ const state = ${ HOST_FOCUS } ;
122+ if (!state) return;
123+ const current = document.activeElement;
124+ if (state.saved?.isConnected && current?.tagName === "WEBVIEW" &&
125+ state.presses.includes(current.getWebContentsId())) state.saved.focus({ preventScroll: true });
126+ state.presses.splice(state.presses.indexOf(${ webContents . id } ), 1);
127+ if (state.presses.length === 0) delete ${ HOST_FOCUS } ;
128+ })()` ) ;
125129 }
126130 }
127131} ;
128132
129133const pointerCommands = new WeakMap < Electron . WebContents , Promise < unknown > > ( ) ;
130134
131- /**
132- * Focus capture crosses processes; later pointer events must not overtake the press.
133- * Queued per window: a press in a sibling tab would capture that tab's webview as host focus.
134- */
135+ /** Focus capture crosses processes; later pointer events must not overtake the press. */
135136const sendPointerCommand = async (
136137 webContents : Electron . WebContents ,
137138 debuggee : Electron . Debugger ,
138139 method : string ,
139140 params : Record < string , unknown > ,
140141 sessionId : string | undefined ,
141142) => {
142- const queue = webContents . hostWebContents ?? webContents ;
143- const previous = pointerCommands . get ( queue ) ?? Promise . resolve ( ) ;
143+ const previous = pointerCommands . get ( webContents ) ?? Promise . resolve ( ) ;
144144 const current = previous
145145 . catch ( ( ) => { } )
146146 . then ( ( ) =>
147147 params [ "type" ] === "mousePressed" || params [ "type" ] === "touchStart"
148148 ? sendPointerPress ( webContents , debuggee , method , params , sessionId )
149149 : debuggee . sendCommand ( method , params , sessionId ) ,
150150 ) ;
151- pointerCommands . set ( queue , current ) ;
151+ pointerCommands . set ( webContents , current ) ;
152152 try {
153153 return await current ;
154154 } finally {
155- if ( pointerCommands . get ( queue ) === current ) pointerCommands . delete ( queue ) ;
155+ if ( pointerCommands . get ( webContents ) === current ) pointerCommands . delete ( webContents ) ;
156156 }
157157} ;
158158
0 commit comments