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.

✨ feat(desktop): show a page thumbnail when hovering a tab (#19284)

Innei83ced941be03
diff --git a/apps/desktop/src/main/controllers/TabPreviewCtr.ts b/apps/desktop/src/main/controllers/TabPreviewCtr.ts
new file mode 100644
index 00000000000..65b83790bf4
--- /dev/null
+++ b/apps/desktop/src/main/controllers/TabPreviewCtr.ts
@@ -0,0 +1,61 @@
+import type { TabPreviewCaptureParams } from '@lobechat/electron-client-ipc';
+
+import { getIpcContext } from '@/utils/ipc';
+import { createLogger } from '@/utils/logger';
+
+import { ControllerModule, IpcMethod } from './index';
+
+const logger = createLogger('controllers:TabPreviewCtr');
+
+const PREVIEW_WIDTH = 320;
+const PREVIEW_QUALITY = 60;
+const MAX_ENTRIES = 24;
+
+export default class TabPreviewCtr extends ControllerModule {
+ static override readonly groupName = 'tabPreview';
+
+ private previews = new Map<string, string>();
+
+ @IpcMethod()
+ async capture({ rect, tabId }: TabPreviewCaptureParams): Promise<void> {
+ const sender = getIpcContext()?.sender;
+ if (!sender || sender.isDestroyed()) return;
+
+ // `capturePage` takes device-independent pixels of the window; the renderer
+ // measures CSS pixels of the page, and the two diverge once the user zooms.
+ const scale = sender.getZoomFactor();
+ const width = Math.floor(rect.width * scale);
+ const height = Math.floor(rect.height * scale);
+ if (width < 8 || height < 8) return;
+
+ const image = await sender.capturePage({
+ height,
+ width,
+ x: Math.floor(rect.x * scale),
+ y: Math.floor(rect.y * scale),
+ });
+ if (image.isEmpty()) return;
+
+ const thumbnail =
+ image.getSize().width > PREVIEW_WIDTH ? image.resize({ width: PREVIEW_WIDTH }) : image;
+
+ this.previews.delete(tabId);
+ this.previews.set(
+ tabId,
+ `data:image/jpeg;base64,${thumbnail.toJPEG(PREVIEW_QUALITY).toString('base64')}`,
+ );
+
+ while (this.previews.size > MAX_ENTRIES) {
+ const oldest = this.previews.keys().next().value;
+ if (oldest === undefined) break;
+ this.previews.delete(oldest);
+ }
+
+ logger.debug(`captured preview for ${tabId} (${width}x${height} @${scale})`);
+ }
+
+ @IpcMethod()
+ async get(tabId: string): Promise<string | undefined> {
+ return this.previews.get(tabId);
+ }
+}
diff --git a/apps/desktop/src/main/controllers/registry.ts b/apps/desktop/src/main/controllers/registry.ts
index 441ba17f60f..8596b857ef9 100644
--- a/apps/desktop/src/main/controllers/registry.ts
+++ b/apps/desktop/src/main/controllers/registry.ts
@@ -28,6 +28,7 @@ import ScreenCaptureCtr from './ScreenCaptureCtr';
import ShellCommandCtr from './ShellCommandCtr';
import ShortcutController from './ShortcutCtr';
import SystemController from './SystemCtr';
+import TabPreviewCtr from './TabPreviewCtr';
import TerminalCtr from './TerminalCtr';
import TrayMenuCtr from './TrayMenuCtr';
import UpdaterCtr from './UpdaterCtr';
@@ -61,6 +62,7 @@ export const controllerIpcConstructors = [
ShellCommandCtr,
ShortcutController,
SystemController,
+ TabPreviewCtr,
TerminalCtr,
BinaryCtr,
TrayMenuCtr,
diff --git a/packages/electron-client-ipc/src/types/index.ts b/packages/electron-client-ipc/src/types/index.ts
index 311b39749fa..d38510997e1 100644
--- a/packages/electron-client-ipc/src/types/index.ts
+++ b/packages/electron-client-ipc/src/types/index.ts
@@ -18,6 +18,7 @@ export * from './route';
export * from './screenCapture';
export * from './shortcut';
export * from './system';
+export * from './tabPreview';
export * from './terminal';
export * from './topicPopup';
export * from './tray';
diff --git a/packages/electron-client-ipc/src/types/tabPreview.ts b/packages/electron-client-ipc/src/types/tabPreview.ts
new file mode 100644
index 00000000000..b459899773d
--- /dev/null
+++ b/packages/electron-client-ipc/src/types/tabPreview.ts
@@ -0,0 +1,12 @@
+export interface TabPreviewRect {
+ height: number;
+ width: number;
+ x: number;
+ y: number;
+}
+
+export interface TabPreviewCaptureParams {
+ /** Content-area bounds in CSS pixels, as reported by `getBoundingClientRect()`. */
+ rect: TabPreviewRect;
+ tabId: string;
+}
diff --git a/src/features/Electron/TabHost/TabHost.tsx b/src/features/Electron/TabHost/TabHost.tsx
index 915cf02fc71..488f96fc4b4 100644
--- a/src/features/Electron/TabHost/TabHost.tsx
+++ b/src/features/Electron/TabHost/TabHost.tsx
@@ -6,8 +6,10 @@ import {
type CSSProperties,
type KeyboardEvent,
type PointerEvent,
+ type ReactNode,
useEffect,
useMemo,
+ useRef,
} from 'react';
import { useTranslation } from 'react-i18next';
import { UNSAFE_LocationContext } from 'react-router';
@@ -27,6 +29,7 @@ import {
syncTabRouters,
type TabRouter,
} from './tabRouterManager';
+import { useTabPreviewCapture } from './useTabPreviewCapture';
interface TabHostProps {
createRouter?: (url: string) => TabRouter;
@@ -87,6 +90,44 @@ const styles = createStaticStyles(({ css, cssVar }) => ({
`,
}));
+interface TabPaneProps {
+ children: ReactNode;
+ isActive: boolean;
+ isVisible: boolean;
+ onFocusPane: () => void;
+ pane: 'primary' | 'secondary' | 'single';
+ style: CSSProperties;
+ tabId: string;
+}
+
+const TabPane = ({
+ children,
+ isActive,
+ isVisible,
+ onFocusPane,
+ pane,
+ style,
+ tabId,
+}: TabPaneProps) => {
+ const ref = useRef<HTMLDivElement>(null);
+
+ useTabPreviewCapture(tabId, isVisible, ref);
+
+ return (
+ <div
+ className={styles.pane}
+ data-focused={isActive ? 'true' : undefined}
+ data-pane={pane}
+ ref={ref}
+ style={style}
+ onFocusCapture={onFocusPane}
+ onPointerDownCapture={onFocusPane}
+ >
+ {children}
+ </div>
+ );
+};
+
const TabHost = ({ createRouter = createTabRouter }: TabHostProps) => {
const { t } = useTranslation('electron');
const tabs = useElectronStore((s) => s.tabs);
@@ -168,13 +209,13 @@ const TabHost = ({ createRouter = createTabRouter }: TabHostProps) => {
<Activity key={tab.id} mode={isVisible ? 'visible' : 'hidden'} name={`Tab:${tab.id}`}>
{/* Activity preserves state but doesn't visually hide the DOM in this React
version, so force-hide the inactive slot (mirrors home/_layout). */}
- <div
- className={styles.pane}
- data-focused={tab.id === activeTabId ? 'true' : undefined}
- data-pane={effectiveSplitView ? (isPrimary ? 'primary' : 'secondary') : 'single'}
+ <TabPane
+ isActive={tab.id === activeTabId}
+ isVisible={isVisible}
+ pane={effectiveSplitView ? (isPrimary ? 'primary' : 'secondary') : 'single'}
style={isVisible ? paneStyle : hiddenSlotStyle}
- onFocusCapture={() => focusTabPane(tab.id)}
- onPointerDownCapture={() => focusTabPane(tab.id)}
+ tabId={tab.id}
+ onFocusPane={() => focusTabPane(tab.id)}
>
<TabIdContext value={tab.id}>
{/* react-router forbids a data <RouterProvider> inside another Router
@@ -185,7 +226,7 @@ const TabHost = ({ createRouter = createTabRouter }: TabHostProps) => {
<RouterProvider router={getOrCreateTabRouter(tab.id, tab.url, createRouter)} />
</UNSAFE_LocationContext>
</TabIdContext>
- </div>
+ </TabPane>
</Activity>
);
})}
diff --git a/src/features/Electron/TabHost/index.ts b/src/features/Electron/TabHost/index.ts
index 4c7dca4a9b0..daf1ee32b20 100644
--- a/src/features/Electron/TabHost/index.ts
+++ b/src/features/Electron/TabHost/index.ts
@@ -9,3 +9,4 @@ export {
subscribeTabHistory,
} from './tabRouterManager';
export { useSeedTabsOnBoot } from './useSeedTabsOnBoot';
+export { captureVisibleTabPreviews } from './useTabPreviewCapture';
diff --git a/src/features/Electron/TabHost/useTabPreviewCapture.ts b/src/features/Electron/TabHost/useTabPreviewCapture.ts
new file mode 100644
index 00000000000..9c8190a7362
--- /dev/null
+++ b/src/features/Electron/TabHost/useTabPreviewCapture.ts
@@ -0,0 +1,45 @@
+import { type RefObject, useEffect } from 'react';
+
+import { isDesktop } from '@/const/version';
+import { ensureElectronIpc } from '@/utils/electron/ipc';
+
+/**
+ * Capturing at switch-away time races the tab swap: `capturePage` resolves against
+ * whichever frame the compositor services next, which is already the incoming tab.
+ * So visible panes register here and the shot is taken when the pointer enters the
+ * tab bar, which precedes any click that would swap tabs. Keyboard switches skip the
+ * bar and simply reuse whatever was cached last.
+ */
+const visibleCaptures = new Map<string, () => void>();
+
+export const captureVisibleTabPreviews = () => {
+ for (const capture of visibleCaptures.values()) capture();
+};
+
+export const useTabPreviewCapture = (
+ tabId: string,
+ isVisible: boolean,
+ ref: RefObject<HTMLDivElement | null>,
+) => {
+ useEffect(() => {
+ const element = ref.current;
+ if (!isDesktop || !isVisible || !element) return;
+
+ visibleCaptures.set(tabId, () => {
+ if (document.hidden) return;
+ const rect = element.getBoundingClientRect();
+ if (rect.width < 8 || rect.height < 8) return;
+
+ ensureElectronIpc()
+ .tabPreview.capture({
+ rect: { height: rect.height, width: rect.width, x: rect.x, y: rect.y },
+ tabId,
+ })
+ .catch(() => {});
+ });
+
+ return () => {
+ visibleCaptures.delete(tabId);
+ };
+ }, [isVisible, tabId, ref]);
+};
diff --git a/src/features/Electron/titlebar/TabBar/TabItem.tsx b/src/features/Electron/titlebar/TabBar/TabItem.tsx
index 65c7ec96434..f686b1d41c7 100644
--- a/src/features/Electron/titlebar/TabBar/TabItem.tsx
+++ b/src/features/Electron/titlebar/TabBar/TabItem.tsx
@@ -7,13 +7,14 @@ import { cx } from 'antd-style';
import { X } from 'lucide-react';
import { useMotionValue, useSpring, useTransform } from 'motion/react';
import * as m from 'motion/react-m';
-import { memo, useCallback, useEffect, useRef } from 'react';
+import { memo, useCallback, useEffect, useRef, useState } from 'react';
import { useTranslation } from 'react-i18next';
import Avatar from '@/components/Avatar';
import { electronStylish } from '@/styles/electron';
import { type ResolvedTab } from './hooks/useResolvedTabs';
+import { useTabPreview } from './hooks/useTabPreview';
import { useTabRunning } from './hooks/useTabRunning';
import { useTabUnread } from './hooks/useTabUnread';
import { TAB_SPRING } from './motion';
@@ -199,6 +200,9 @@ const TabItem = memo<TabItemProps>(
],
);
+ const [hovered, setHovered] = useState(false);
+ const preview = useTabPreview(id, hovered);
+
const indicator = (
<span className={styles.avatarWrapper}>
{meta.avatar ? (
@@ -243,6 +247,8 @@ const TabItem = memo<TabItemProps>(
}}
onAuxClick={handleAuxClick}
onClick={handleClick}
+ onPointerEnter={() => setHovered(true)}
+ onPointerLeave={() => setHovered(false)}
{...attributes}
{...listeners}
>
@@ -272,7 +278,19 @@ const TabItem = memo<TabItemProps>(
// pop a blank bubble on hover.
return (
<ContextMenuTrigger items={contextMenuItems}>
- <Tooltip disabled={tier === 'full'} title={meta.title}>
+ <Tooltip
+ disabled={tier === 'full' && !preview}
+ title={
+ preview ? (
+ <span className={styles.previewCard}>
+ <img alt={meta.title} className={styles.previewImage} src={preview} />
+ <span className={styles.previewTitle}>{meta.title}</span>
+ </span>
+ ) : (
+ meta.title
+ )
+ }
+ >
{face}
</Tooltip>
</ContextMenuTrigger>
diff --git a/src/features/Electron/titlebar/TabBar/hooks/useTabPreview.ts b/src/features/Electron/titlebar/TabBar/hooks/useTabPreview.ts
new file mode 100644
index 00000000000..d47f4c70a87
--- /dev/null
+++ b/src/features/Electron/titlebar/TabBar/hooks/useTabPreview.ts
@@ -0,0 +1,26 @@
+import { useEffect, useState } from 'react';
+
+import { isDesktop } from '@/const/version';
+import { ensureElectronIpc } from '@/utils/electron/ipc';
+
+export const useTabPreview = (tabId: string, enabled: boolean) => {
+ const [preview, setPreview] = useState<string>();
+
+ useEffect(() => {
+ if (!isDesktop || !enabled) return;
+
+ let cancelled = false;
+ ensureElectronIpc()
+ .tabPreview.get(tabId)
+ .then((dataUrl: string | undefined) => {
+ if (!cancelled) setPreview(dataUrl);
+ })
+ .catch(() => {});
+
+ return () => {
+ cancelled = true;
+ };
+ }, [enabled, tabId]);
+
+ return preview;
+};
diff --git a/src/features/Electron/titlebar/TabBar/index.tsx b/src/features/Electron/titlebar/TabBar/index.tsx
index a02a64ddadd..c4ea8f81fba 100644
--- a/src/features/Electron/titlebar/TabBar/index.tsx
+++ b/src/features/Electron/titlebar/TabBar/index.tsx
@@ -21,6 +21,7 @@ import * as m from 'motion/react-m';
import { useCallback, useEffect, useMemo, useRef } from 'react';
import { useTranslation } from 'react-i18next';
+import { captureVisibleTabPreviews } from '@/features/Electron/TabHost';
import { buildWorkspaceAwarePath } from '@/features/Workspace/workspaceAwarePath';
import { useActiveLocation } from '@/hooks/useActiveLocation';
import { useRegisterDesktopTabHotkeys } from '@/hooks/useHotkeys/desktopTabScope';
@@ -230,7 +231,14 @@ const TabBar = () => {
if (tabs.length === 0) return null;
return (
- <Flexbox horizontal align={'center'} className={styles.container} gap={TAB_GAP} ref={stripRef}>
+ <Flexbox
+ horizontal
+ align={'center'}
+ className={styles.container}
+ gap={TAB_GAP}
+ ref={stripRef}
+ onPointerEnter={captureVisibleTabPreviews}
+ >
<DndContext
collisionDetection={closestCenter}
modifiers={[restrictToHorizontalAxis]}
diff --git a/src/features/Electron/titlebar/TabBar/styles.ts b/src/features/Electron/titlebar/TabBar/styles.ts
index 333b5e7a478..2f269b5e54c 100644
--- a/src/features/Electron/titlebar/TabBar/styles.ts
+++ b/src/features/Electron/titlebar/TabBar/styles.ts
@@ -238,6 +238,29 @@ export const useStyles = createStaticStyles(({ css, cssVar }) => ({
}
}
`,
+ previewCard: css`
+ display: flex;
+ flex-direction: column;
+ gap: 6px;
+ width: 240px;
+ `,
+ previewImage: css`
+ display: block;
+
+ aspect-ratio: 16 / 10;
+ width: 100%;
+ border: 1px solid ${cssVar.colorBorderSecondary};
+ border-radius: 6px;
+
+ object-fit: cover;
+ object-position: top center;
+ `,
+ previewTitle: css`
+ overflow: hidden;
+ font-size: 12px;
+ text-overflow: ellipsis;
+ white-space: nowrap;
+ `,
tabIcon: css`
flex-shrink: 0;
color: ${cssVar.colorTextSecondary};