Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix(desktop): make split view collapse cleanly on tab switch and close (#18088)

Arvin Xu8e73aee25572
diff --git a/src/features/Electron/TabHost/TabHost.tsx b/src/features/Electron/TabHost/TabHost.tsx
index 9b850cf9d56..915cf02fc71 100644
--- a/src/features/Electron/TabHost/TabHost.tsx
+++ b/src/features/Electron/TabHost/TabHost.tsx
@@ -33,7 +33,18 @@ interface TabHostProps {
}
const rootStyle: CSSProperties = { blockSize: '100%', inlineSize: '100%', position: 'relative' };
-const slotStyle: CSSProperties = { inset: 0, position: 'absolute' };
+// Longhands only — no `inset` shorthand. The split styles override `left`/`right`
+// per pane, and React's style diff never re-applies an unchanged shorthand, so a
+// pane leaving the split would keep `right` removed (not restored to 0) and
+// collapse to zero width.
+const slotStyle: CSSProperties = {
+ bottom: 0,
+ left: 0,
+ position: 'absolute',
+ right: 0,
+ top: 0,
+ width: 'auto',
+};
const hiddenSlotStyle: CSSProperties = { ...slotStyle, display: 'none' };
const styles = createStaticStyles(({ css, cssVar }) => ({
diff --git a/src/features/Electron/titlebar/TabBar/TabItem.tsx b/src/features/Electron/titlebar/TabBar/TabItem.tsx
index 959d5c58d8f..01d670ca358 100644
--- a/src/features/Electron/titlebar/TabBar/TabItem.tsx
+++ b/src/features/Electron/titlebar/TabBar/TabItem.tsx
@@ -144,10 +144,10 @@ const TabItem = memo<TabItemProps>(
}, [x, isSorting, targetX, springX]);
const handleClick = useCallback(() => {
- if (!isActive) {
+ if (!isActive || isSplitVisible) {
onActivate(id, tab.url);
}
- }, [isActive, onActivate, id, tab.url]);
+ }, [isActive, isSplitVisible, onActivate, id, tab.url]);
const handleClose = useCallback(
(e: React.MouseEvent) => {
diff --git a/src/features/Electron/titlebar/TabBar/index.tsx b/src/features/Electron/titlebar/TabBar/index.tsx
index a1ddcee1676..44ded723433 100644
--- a/src/features/Electron/titlebar/TabBar/index.tsx
+++ b/src/features/Electron/titlebar/TabBar/index.tsx
@@ -63,7 +63,7 @@ const TabBar = () => {
const { tabs, activeTabId } = useResolvedTabs();
const splitView = useElectronStore((s) => s.splitView);
const splitViewEnabled = useUserStore(labPreferSelectors.enableDesktopSplitView);
- const activateTab = useElectronStore((s) => s.activateTab);
+ const switchTab = useElectronStore((s) => s.switchTab);
const addNewTab = useElectronStore((s) => s.addNewTab);
const removeTab = useElectronStore((s) => s.removeTab);
const closeOtherTabs = useElectronStore((s) => s.closeOtherTabs);
@@ -148,9 +148,9 @@ const TabBar = () => {
const handleActivate = useCallback(
(id: string) => {
- activateTab(id);
+ switchTab(id);
},
- [activateTab],
+ [switchTab],
);
const handleClose = useCallback(
diff --git a/src/hooks/useHotkeys/desktopTabScope.ts b/src/hooks/useHotkeys/desktopTabScope.ts
index 922f89b7aa0..7adf4338b24 100644
--- a/src/hooks/useHotkeys/desktopTabScope.ts
+++ b/src/hooks/useHotkeys/desktopTabScope.ts
@@ -15,11 +15,11 @@ import { useElectronStore } from '@/store/electron';
*/
export const useRegisterDesktopTabHotkeys = () => {
const switchToTabByIndex = useCallback((index: number) => {
- const { tabs, activateTab } = useElectronStore.getState();
+ const { tabs, switchTab } = useElectronStore.getState();
if (index < 0 || index >= tabs.length) return;
const target = tabs[index];
- activateTab(target.id);
+ switchTab(target.id);
}, []);
// Mod+1 through Mod+9
@@ -43,14 +43,14 @@ export const useRegisterDesktopTabHotkeys = () => {
'ctrl+tab',
(e) => {
e.preventDefault();
- const { tabs, activeTabId, activateTab } = useElectronStore.getState();
+ const { tabs, activeTabId, switchTab } = useElectronStore.getState();
if (tabs.length === 0) return;
const currentIndex = tabs.findIndex((t) => t.id === activeTabId);
const nextIndex = (currentIndex + 1) % tabs.length;
const target = tabs[nextIndex];
- activateTab(target.id);
+ switchTab(target.id);
},
{
enableOnFormTags: true,
@@ -63,14 +63,14 @@ export const useRegisterDesktopTabHotkeys = () => {
'ctrl+shift+tab',
(e) => {
e.preventDefault();
- const { tabs, activeTabId, activateTab } = useElectronStore.getState();
+ const { tabs, activeTabId, switchTab } = useElectronStore.getState();
if (tabs.length === 0) return;
const currentIndex = tabs.findIndex((t) => t.id === activeTabId);
const prevIndex = (currentIndex - 1 + tabs.length) % tabs.length;
const target = tabs[prevIndex];
- activateTab(target.id);
+ switchTab(target.id);
},
{
enableOnFormTags: true,
diff --git a/src/store/electron/actions/__tests__/tabPages.test.ts b/src/store/electron/actions/__tests__/tabPages.test.ts
index 9e41bf98c7e..38185c82335 100644
--- a/src/store/electron/actions/__tests__/tabPages.test.ts
+++ b/src/store/electron/actions/__tests__/tabPages.test.ts
@@ -229,6 +229,16 @@ describe('tabPages actions', () => {
expect(result.current.activeTabId).toBe('/third');
});
+ it('collapses split view when switching from the titlebar tab strip', () => {
+ const result = seed();
+ act(() => result.current.openTabInSplitView('/right'));
+
+ act(() => result.current.switchTab('/left'));
+
+ expect(result.current.activeTabId).toBe('/left');
+ expect(result.current.splitView).toBeNull();
+ });
+
it('collapses to the remaining pane when one visible tab closes', () => {
const result = seed();
act(() => result.current.openTabInSplitView('/right'));
@@ -238,6 +248,133 @@ describe('tabPages actions', () => {
expect(result.current.activeTabId).toBe('/left');
});
+ it('removes the duplicated pane and refocuses the source when the split closes', () => {
+ const result = seed();
+ let duplicateId: string | null = null;
+ act(() => {
+ duplicateId = result.current.openTabInSplitView('/left');
+ });
+
+ act(() => result.current.closeSplitView());
+
+ expect(result.current.splitView).toBeNull();
+ expect(result.current.activeTabId).toBe('/left');
+ expect(result.current.tabs.some((t) => t.id === duplicateId)).toBe(false);
+ });
+
+ it('redirects a titlebar switch on the duplicate back to its source tab', () => {
+ const result = seed();
+ let duplicateId: string | null = null;
+ act(() => {
+ duplicateId = result.current.openTabInSplitView('/left');
+ });
+
+ act(() => result.current.switchTab(duplicateId!));
+
+ expect(result.current.splitView).toBeNull();
+ expect(result.current.activeTabId).toBe('/left');
+ expect(result.current.tabs).toHaveLength(3);
+ });
+
+ it('keeps the duplicate as the only remaining tab when its source closes', () => {
+ const result = seed();
+ let duplicateId: string | null = null;
+ act(() => {
+ duplicateId = result.current.openTabInSplitView('/left');
+ });
+
+ act(() => result.current.removeTab('/left'));
+
+ expect(result.current.splitView).toBeNull();
+ expect(result.current.activeTabId).toBe(duplicateId);
+ expect(result.current.tabs.some((t) => t.id === duplicateId)).toBe(true);
+ });
+
+ it('drops the duplicate when another tab takes its pane', () => {
+ const result = seed();
+ let duplicateId: string | null = null;
+ act(() => {
+ duplicateId = result.current.openTabInSplitView('/left');
+ });
+ // the duplicate pane holds focus, so activating a third tab replaces that pane
+ act(() => result.current.activateTab('/third'));
+
+ expect(result.current.splitView).toMatchObject({
+ primaryTabId: '/left',
+ secondaryTabId: '/third',
+ });
+ expect(result.current.tabs.some((t) => t.id === duplicateId)).toBe(false);
+ });
+
+ it('drops the duplicate when a newly created tab takes its pane', () => {
+ const result = seed();
+ let duplicateId: string | null = null;
+ act(() => {
+ duplicateId = result.current.openTabInSplitView('/left');
+ });
+ // the duplicate pane holds focus, so the new tab replaces that pane
+ let newTabId = '';
+ act(() => {
+ newTabId = result.current.addNewTab('/');
+ });
+
+ expect(result.current.splitView).toMatchObject({
+ primaryTabId: '/left',
+ secondaryTabId: newTabId,
+ });
+ expect(result.current.splitView?.duplicatedTabId).toBeUndefined();
+ expect(result.current.tabs.some((t) => t.id === duplicateId)).toBe(false);
+ expect(result.current.activeTabId).toBe(newTabId);
+ });
+
+ it('keeps the copy when its real source closed after being displaced from the split', () => {
+ const result = seed();
+ let duplicateId: string | null = null;
+ act(() => {
+ duplicateId = result.current.openTabInSplitView('/left');
+ });
+ // replace the source pane with a third tab, then close the real source tab
+ act(() => result.current.focusTabPane('/left'));
+ act(() => result.current.activateTab('/third'));
+ act(() => result.current.removeTab('/left'));
+
+ act(() => result.current.closeSplitView());
+
+ // the copy is the page's only remaining tab — it must survive the close
+ expect(result.current.splitView).toBeNull();
+ expect(result.current.tabs.some((t) => t.id === duplicateId)).toBe(true);
+ });
+
+ it('removes the copy and refocuses its real source when the replaced pane closes', () => {
+ const result = seed();
+ let duplicateId: string | null = null;
+ act(() => {
+ duplicateId = result.current.openTabInSplitView('/left');
+ });
+ act(() => result.current.focusTabPane('/left'));
+ act(() => result.current.activateTab('/third'));
+
+ act(() => result.current.removeTab('/third'));
+
+ expect(result.current.splitView).toBeNull();
+ expect(result.current.tabs.some((t) => t.id === duplicateId)).toBe(false);
+ expect(result.current.activeTabId).toBe('/left');
+ });
+
+ it('refreshes the promoted source tab recency when the split closes', () => {
+ const result = seed();
+ act(() => {
+ result.current.openTabInSplitView('/left');
+ });
+
+ act(() => result.current.closeSplitView());
+
+ // seed tabs start at lastVisited=1; the promoted source must be re-touched
+ // or the limited live-router set can evict the tab that just became active
+ const source = result.current.tabs.find((t) => t.id === '/left');
+ expect(source!.lastVisited).toBeGreaterThan(1);
+ });
+
it('clamps the divider ratio to usable pane widths', () => {
const result = seed();
act(() => result.current.openTabInSplitView('/right'));
diff --git a/src/store/electron/actions/tabPages.ts b/src/store/electron/actions/tabPages.ts
index 31dfc4d2ed0..a07e2063ed9 100644
--- a/src/store/electron/actions/tabPages.ts
+++ b/src/store/electron/actions/tabPages.ts
@@ -29,6 +29,18 @@ export interface TabPagesState {
}
export interface SplitViewState {
+ /**
+ * The tab `duplicatedTabId` was copied from. Recorded at split time because
+ * panes can be replaced afterwards — deriving the source from "the other
+ * pane" later would point at whatever tab happens to sit there.
+ */
+ duplicatedFromTabId?: string;
+ /**
+ * Tab created by copying the active tab when it was split against itself.
+ * It only exists to mirror its source side-by-side and is removed as soon as
+ * it leaves the split, so no stray duplicate tab survives the session.
+ */
+ duplicatedTabId?: string;
primaryTabId: string;
ratio: number;
secondaryTabId: string;
@@ -63,11 +75,18 @@ export class TabPagesActionImpl {
const { activeTabId, splitView, tabs } = this.#get();
if (!tabs.some((t) => t.id === id)) return;
+ const cleaned = this.#dropDisplacedDuplicate(
+ splitView,
+ this.#replaceFocusedPane(splitView, activeTabId, id),
+ tabs,
+ id,
+ );
+
this.#set(
{
- activeTabId: id,
- splitView: this.#replaceFocusedPane(splitView, activeTabId, id),
- tabs: this.#touch(tabs, id),
+ activeTabId: cleaned.activeTabId,
+ splitView: cleaned.splitView,
+ tabs: this.#touch(cleaned.tabs, cleaned.activeTabId),
},
false,
'activateTab',
@@ -75,6 +94,24 @@ export class TabPagesActionImpl {
this.#persist();
};
+ switchTab = (id: string): void => {
+ const { splitView, tabs } = this.#get();
+ if (!tabs.some((t) => t.id === id)) return;
+
+ const cleaned = this.#dropDisplacedDuplicate(splitView, null, tabs, id);
+
+ this.#set(
+ {
+ activeTabId: cleaned.activeTabId,
+ splitView: null,
+ tabs: this.#touch(cleaned.tabs, cleaned.activeTabId),
+ },
+ false,
+ 'switchTab',
+ );
+ this.#persist();
+ };
+
addTab = (url: string, cached?: DynamicRouteMeta, activate = true): string => {
this.#ensureScopeForUrl(url);
const { tabs } = this.#get();
@@ -102,8 +139,22 @@ export class TabPagesActionImpl {
};
closeSplitView = (): void => {
- if (!this.#get().splitView) return;
- this.#set({ splitView: null }, false, 'closeSplitView');
+ const { activeTabId, splitView, tabs } = this.#get();
+ if (!splitView) return;
+
+ const cleaned = this.#dropDisplacedDuplicate(splitView, null, tabs, activeTabId);
+
+ this.#set(
+ {
+ activeTabId: cleaned.activeTabId,
+ splitView: null,
+ // #touch keeps the promoted source tab's keep-alive recency fresh — see #touch.
+ tabs: this.#touch(cleaned.tabs, cleaned.activeTabId),
+ },
+ false,
+ 'closeSplitView',
+ );
+ this.#persist();
};
focusTabPane = (id: string): void => {
@@ -141,15 +192,25 @@ export class TabPagesActionImpl {
]
: this.#touch(tabs, id);
+ const nextSplitView: SplitViewState = {
+ duplicatedFromTabId: shouldDuplicate ? id : undefined,
+ duplicatedTabId: shouldDuplicate ? secondaryTabId : undefined,
+ primaryTabId,
+ ratio: splitView?.ratio ?? 0.5,
+ secondaryTabId,
+ };
+ const cleaned = this.#dropDisplacedDuplicate(
+ splitView,
+ nextSplitView,
+ nextTabs,
+ secondaryTabId,
+ );
+
this.#set(
{
- activeTabId: secondaryTabId,
- splitView: {
- primaryTabId,
- ratio: splitView?.ratio ?? 0.5,
- secondaryTabId,
- },
- tabs: nextTabs,
+ activeTabId: cleaned.activeTabId,
+ splitView: cleaned.splitView,
+ tabs: cleaned.tabs,
},
false,
'openTabInSplitView',
@@ -196,7 +257,7 @@ export class TabPagesActionImpl {
{
activeTabId: newActiveId,
splitView: reconciled.splitView,
- tabs: this.#touch(newTabs, newActiveId),
+ tabs: this.#touch(reconciled.tabs, newActiveId),
},
false,
'removeTab',
@@ -337,13 +398,14 @@ export class TabPagesActionImpl {
if (newTabs.length === tabs.length) return;
const preferredActiveId = newTabs.some((t) => t.id === activeTabId) ? activeTabId : targetId;
- const { activeTabId: newActiveId, splitView } = this.#reconcileSplitView(
- newTabs,
- preferredActiveId,
- );
+ const reconciled = this.#reconcileSplitView(newTabs, preferredActiveId);
this.#set(
- { activeTabId: newActiveId, splitView, tabs: this.#touch(newTabs, newActiveId) },
+ {
+ activeTabId: reconciled.activeTabId,
+ splitView: reconciled.splitView,
+ tabs: this.#touch(reconciled.tabs, reconciled.activeTabId),
+ },
false,
action,
);
@@ -400,24 +462,62 @@ export class TabPagesActionImpl {
: { ...splitView, primaryTabId: nextTabId };
};
+ // A duplicated pane that leaves the split (the split collapses or another tab takes
+ // its pane) must not survive as a stray second tab for the same page: closing or
+ // revisiting the leftover forces a hidden→visible remount of a pane the user never
+ // saw leave. Drop the copy and hand focus back to its source tab instead.
+ #dropDisplacedDuplicate = (
+ prevSplitView: SplitViewState | null,
+ nextSplitView: SplitViewState | null,
+ tabs: TabItem[],
+ activeTabId: string | null,
+ ): Pick<TabPagesState, 'activeTabId' | 'splitView' | 'tabs'> => {
+ const duplicatedTabId = prevSplitView?.duplicatedTabId;
+ const keep = { activeTabId, splitView: nextSplitView, tabs };
+ if (!prevSplitView || !duplicatedTabId) return keep;
+
+ const stillInPane =
+ nextSplitView &&
+ (nextSplitView.primaryTabId === duplicatedTabId ||
+ nextSplitView.secondaryTabId === duplicatedTabId);
+ if (stillInPane) return keep;
+
+ const sourceTabId = prevSplitView.duplicatedFromTabId;
+ // The source tab already closed, so the copy is the page's only remaining tab.
+ if (!sourceTabId || !tabs.some((tab) => tab.id === sourceTabId)) return keep;
+
+ return {
+ activeTabId: activeTabId === duplicatedTabId ? sourceTabId : activeTabId,
+ splitView: nextSplitView
+ ? { ...nextSplitView, duplicatedFromTabId: undefined, duplicatedTabId: undefined }
+ : null,
+ tabs: tabs.filter((tab) => tab.id !== duplicatedTabId),
+ };
+ };
+
#reconcileSplitView = (
tabs: TabItem[],
preferredActiveId: string | null,
- ): Pick<TabPagesState, 'activeTabId' | 'splitView'> => {
+ ): Pick<TabPagesState, 'activeTabId' | 'splitView' | 'tabs'> => {
const { splitView } = this.#get();
- if (!splitView) return { activeTabId: preferredActiveId, splitView: null };
+ if (!splitView) return { activeTabId: preferredActiveId, splitView: null, tabs };
const tabIds = new Set(tabs.map((tab) => tab.id));
const hasPrimary = tabIds.has(splitView.primaryTabId);
const hasSecondary = tabIds.has(splitView.secondaryTabId);
- if (hasPrimary && hasSecondary) return { activeTabId: preferredActiveId, splitView };
+ if (hasPrimary && hasSecondary) return { activeTabId: preferredActiveId, splitView, tabs };
const remainingPaneId = hasPrimary
? splitView.primaryTabId
: hasSecondary
? splitView.secondaryTabId
: null;
- return { activeTabId: remainingPaneId ?? preferredActiveId, splitView: null };
+ return this.#dropDisplacedDuplicate(
+ splitView,
+ null,
+ tabs,
+ remainingPaneId ?? preferredActiveId,
+ );
};
#createTab = (url: string, cached: DynamicRouteMeta | undefined, activate: boolean): string => {
@@ -430,11 +530,21 @@ export class TabPagesActionImpl {
url,
};
+ const withNew = [...tabs, newTab];
+ const cleaned = activate
+ ? this.#dropDisplacedDuplicate(
+ splitView,
+ this.#replaceFocusedPane(splitView, activeTabId, id),
+ withNew,
+ id,
+ )
+ : { activeTabId, splitView, tabs: withNew };
+
this.#set(
{
activeTabId: activate ? id : activeTabId,
- splitView: activate ? this.#replaceFocusedPane(splitView, activeTabId, id) : splitView,
- tabs: [...tabs, newTab],
+ splitView: cleaned.splitView,
+ tabs: cleaned.tabs,
},
false,
'addTab',