fix(core): right sidebar switching not work after switching workspace (#13179)

Due to missing the correct unsubscription, switching workspaces triggers
multiple events. As a result, the sidebar cannot be closed on every
second trigger.
This commit is contained in:
Cats Juice
2025-07-11 23:15:16 +08:00
committed by GitHub
parent 41f338bce0
commit 0d414d914a

View File

@@ -25,74 +25,82 @@ export class DesktopStateSynchronizer extends Service {
const workbench = this.workbenchService.workbench; const workbench = this.workbenchService.workbench;
const appInfo = this.electronApi.appInfo; const appInfo = this.electronApi.appInfo;
this.electronApi.events.ui.onTabAction(event => { this.disposables.push(
if ( this.electronApi.events.ui.onTabAction(event => {
event.type === 'open-in-split-view' && if (
event.payload.tabId === appInfo?.viewId event.type === 'open-in-split-view' &&
) { event.payload.tabId === appInfo?.viewId
workbench.openAll({ ) {
at: 'beside', workbench.openAll({
show: false, at: 'beside',
}); show: false,
} });
if (
event.type === 'separate-view' &&
event.payload.tabId === appInfo?.viewId
) {
const view = workbench.viewAt(event.payload.viewIndex);
if (view) {
workbench.close(view);
} }
}
if ( if (
event.type === 'activate-view' && event.type === 'separate-view' &&
event.payload.tabId === appInfo?.viewId event.payload.tabId === appInfo?.viewId
) { ) {
workbench.active(event.payload.viewIndex); const view = workbench.viewAt(event.payload.viewIndex);
} if (view) {
}); workbench.close(view);
this.electronApi.events.ui.onCloseView(() => {
(async () => {
if (await this.electronApi.handler.ui.isActiveTab()) {
// close current view. stop if any one is successful
// 1. peek view
// 2. split view
// 3. tab
// 4. otherwise, hide the window
if (this.peekViewService.peekView.show$.value?.value) {
this.peekViewService.peekView.close();
} else if (workbench.views$.value.length > 1) {
workbench.close(workbench.activeView$.value);
} else {
const tabs = await this.electronApi.handler.ui.getTabsStatus();
if (tabs.length > 1) {
await this.electronApi.handler.ui.closeTab();
} else {
await this.electronApi.handler.ui.handleHideApp();
}
} }
} }
})().catch(console.error);
});
this.electronApi.events.ui.onToggleRightSidebar(tabId => { if (
if (tabId === appInfo?.viewId) { event.type === 'activate-view' &&
workbench.setSidebarOpen(!workbench.sidebarOpen$.value); event.payload.tabId === appInfo?.viewId
} ) {
}); workbench.active(event.payload.viewIndex);
}
})
);
this.electronApi.events.ui.onTabGoToRequest(opts => { this.disposables.push(
if (opts.tabId === appInfo?.viewId) { this.electronApi.events.ui.onCloseView(() => {
this.workbenchService.workbench.open(opts.to); (async () => {
} if (await this.electronApi.handler.ui.isActiveTab()) {
}); // close current view. stop if any one is successful
// 1. peek view
// 2. split view
// 3. tab
// 4. otherwise, hide the window
if (this.peekViewService.peekView.show$.value?.value) {
this.peekViewService.peekView.close();
} else if (workbench.views$.value.length > 1) {
workbench.close(workbench.activeView$.value);
} else {
const tabs = await this.electronApi.handler.ui.getTabsStatus();
if (tabs.length > 1) {
await this.electronApi.handler.ui.closeTab();
} else {
await this.electronApi.handler.ui.handleHideApp();
}
}
}
})().catch(console.error);
})
);
this.disposables.push(
this.electronApi.events.ui.onToggleRightSidebar(tabId => {
if (tabId === appInfo?.viewId) {
workbench.setSidebarOpen(!workbench.sidebarOpen$.value);
}
})
);
this.disposables.push(
this.electronApi.events.ui.onTabGoToRequest(opts => {
if (opts.tabId === appInfo?.viewId) {
this.workbenchService.workbench.open(opts.to);
}
})
);
// sync workbench state with main process // sync workbench state with main process
// also fill tab view meta with title & moduleName // also fill tab view meta with title & moduleName
LiveData.computed(get => { const viewsSub = LiveData.computed(get => {
return get(workbench.views$).map(view => { return get(workbench.views$).map(view => {
const location = get(view.location$); const location = get(view.location$);
return { return {
@@ -118,19 +126,21 @@ export class DesktopStateSynchronizer extends Service {
.catch(console.error); .catch(console.error);
}); });
workbench.activeViewIndex$.subscribe(activeViewIndex => { const activeViewIndexSub = workbench.activeViewIndex$.subscribe(
if (!appInfo?.viewId) { activeViewIndex => {
return; if (!appInfo?.viewId) {
return;
}
this.electronApi.handler.ui
.updateWorkbenchMeta(appInfo.viewId, {
activeViewIndex: activeViewIndex,
})
.catch(console.error);
} }
);
this.electronApi.handler.ui const basenameSub = workbench.basename$.subscribe(basename => {
.updateWorkbenchMeta(appInfo.viewId, {
activeViewIndex: activeViewIndex,
})
.catch(console.error);
});
workbench.basename$.subscribe(basename => {
if (!appInfo?.viewId) { if (!appInfo?.viewId) {
return; return;
} }
@@ -141,5 +151,11 @@ export class DesktopStateSynchronizer extends Service {
}) })
.catch(console.error); .catch(console.error);
}); });
this.disposables.push(
() => viewsSub.unsubscribe(),
() => activeViewIndexSub.unsubscribe(),
() => basenameSub.unsubscribe()
);
}; };
} }