@@ -82,7 +82,7 @@ export default () => {
8282 } ;
8383
8484 const init = ( plugin , window : BrowserWindow ) => {
85- if ( view === null || view === undefined ) {
85+ if ( view === null || view === undefined || view . inDetach ) {
8686 createView ( plugin , window ) ;
8787 // if (viewInstance.getView(plugin.name) && !commonConst.dev()) {
8888 // view = viewInstance.getView(plugin.name).view;
@@ -176,14 +176,29 @@ export default () => {
176176 const removeView = ( window : BrowserWindow ) => {
177177 if ( ! view ) return ;
178178 executeHooks ( 'PluginOut' , null ) ;
179+ // 先记住这次要移除的视图,防止后面异步代码里全局引用被换掉
180+ const snapshotView = view ;
179181 setTimeout ( ( ) => {
180- window . removeBrowserView ( view ) ;
181- if ( ! view . inDetach ) {
182- window . setBrowserView ( null ) ;
183- view . webContents ?. destroy ( ) ;
182+ // 获取当前视图,判断是否已经换成了新视图
183+ const currentView = window . getBrowserView ?.( ) ;
184+ window . removeBrowserView ( snapshotView ) ;
185+
186+ // 主窗口的插件视图仍然挂着旧实例时,需要还原主窗口 UI
187+ if ( ! snapshotView . inDetach ) {
188+ // 如果窗口还挂着旧视图,说明还没换掉,需要把主窗口恢复到初始状态
189+ if ( currentView === snapshotView ) {
190+ window . setBrowserView ( null ) ;
191+ if ( view === snapshotView ) {
192+ window . webContents ?. executeJavaScript ( `window.initRubick()` ) ;
193+ view = undefined ;
194+ }
195+ }
196+ snapshotView . webContents ?. destroy ( ) ;
197+ }
198+ // 分离窗口只需释放全局引用,视图由分离窗口继续管理
199+ else if ( view === snapshotView ) {
200+ view = undefined ;
184201 }
185- window . webContents ?. executeJavaScript ( `window.initRubick()` ) ;
186- view = undefined ;
187202 } , 0 ) ;
188203 } ;
189204
0 commit comments