diff --git a/src/renderer/src/components/right-sidebar/FileExplorer.tsx b/src/renderer/src/components/right-sidebar/FileExplorer.tsx index b70e7bed5..5e7f58c00 100644 --- a/src/renderer/src/components/right-sidebar/FileExplorer.tsx +++ b/src/renderer/src/components/right-sidebar/FileExplorer.tsx @@ -2,17 +2,17 @@ import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react' import { useVirtualizer } from '@tanstack/react-virtual' import { Loader2 } from 'lucide-react' import { useAppStore } from '@/store' -import { detectLanguage } from '@/lib/language-detect' import { dirname, normalizeRelativePath } from '@/lib/path' import { ScrollArea } from '@/components/ui/scroll-area' import { cn } from '@/lib/utils' import { FileDeleteDialog } from './FileDeleteDialog' import { FileExplorerBackgroundMenu } from './FileExplorerBackgroundMenu' import { FileExplorerRow, InlineInputRow } from './FileExplorerRow' -import type { TreeNode } from './file-explorer-types' import { splitPathSegments } from './path-tree' import { buildFolderStatusMap, buildStatusMap, STATUS_COLORS } from './status-display' import { useFileDeletion } from './useFileDeletion' +import { useFileExplorerAutoReveal } from './useFileExplorerAutoReveal' +import { useFileExplorerHandlers } from './useFileExplorerHandlers' import { useFileExplorerReveal } from './useFileExplorerReveal' import { useFileExplorerInlineInput } from './useFileExplorerInlineInput' import { useFileExplorerKeys } from './useFileExplorerKeys' @@ -190,6 +190,18 @@ export default function FileExplorer(): React.JSX.Element { virtualizer }) + useFileExplorerAutoReveal({ + activeFileId, + activeWorktreeId, + worktreePath, + pendingExplorerReveal, + openFiles, + rowsByPath, + flatRows, + setSelectedPath, + virtualizer + }) + useEffect(() => { if (inlineInputIndex >= 0) { virtualizer.scrollToIndex(inlineInputIndex, { align: 'auto' }) @@ -206,55 +218,17 @@ export default function FileExplorer(): React.JSX.Element { requestDelete }) - const handleClick = useCallback( - (node: TreeNode) => { - if (!activeWorktreeId) { - return - } - setSelectedPath(node.path) - if (node.isDirectory) { - toggleDir(activeWorktreeId, node.path) - return - } - openFile({ - filePath: node.path, - relativePath: node.relativePath, - worktreeId: activeWorktreeId, - language: detectLanguage(node.name), - mode: 'edit' - }) - }, - [activeWorktreeId, openFile, toggleDir] - ) - - const handleDoubleClick = useCallback( - (node: TreeNode) => { - if (!activeWorktreeId || node.isDirectory) { - return - } - pinFile(node.path) - }, - [activeWorktreeId, pinFile] - ) + const { handleClick, handleDoubleClick, handleWheelCapture } = useFileExplorerHandlers({ + activeWorktreeId, + openFile, + pinFile, + toggleDir, + setSelectedPath, + scrollRef + }) const handleDuplicate = useFileDuplicate({ worktreePath, refreshDir }) - const handleWheelCapture = useCallback((e: React.WheelEvent) => { - const container = scrollRef.current - if (!container || Math.abs(e.deltaY) <= Math.abs(e.deltaX)) { - return - } - const target = e.target - if (!(target instanceof Element) || !target.closest('[data-explorer-draggable="true"]')) { - return - } - if (container.scrollHeight <= container.clientHeight) { - return - } - e.preventDefault() - container.scrollTop += e.deltaY - }, []) - if (!worktreePath) { return (
diff --git a/src/renderer/src/components/right-sidebar/useFileExplorerAutoReveal.ts b/src/renderer/src/components/right-sidebar/useFileExplorerAutoReveal.ts new file mode 100644 index 000000000..a4b2ecc46 --- /dev/null +++ b/src/renderer/src/components/right-sidebar/useFileExplorerAutoReveal.ts @@ -0,0 +1,96 @@ +import { useEffect, useRef } from 'react' +import type { Dispatch, SetStateAction } from 'react' +import type { Virtualizer } from '@tanstack/react-virtual' +import { useAppStore } from '@/store' +import type { OpenFile } from '@/store/slices/editor' +import type { TreeNode } from './file-explorer-types' + +type UseFileExplorerAutoRevealParams = { + activeFileId: string | null + activeWorktreeId: string | null + worktreePath: string | null + pendingExplorerReveal: { worktreeId: string; filePath: string; requestId: number } | null + openFiles: OpenFile[] + rowsByPath: Map + flatRows: TreeNode[] + setSelectedPath: Dispatch> + virtualizer: Virtualizer +} + +/** + * Auto-reveal: when the active editor file changes, scroll the explorer to show it. + * This mirrors VS Code's explorer.autoReveal behavior. + * + * For files already visible in the tree, scrolls directly (no flash). + * For files whose ancestors are collapsed, triggers the reveal machinery + * to expand ancestors and scroll, but skips the flash animation. + */ +export function useFileExplorerAutoReveal({ + activeFileId, + activeWorktreeId, + worktreePath, + pendingExplorerReveal, + openFiles, + rowsByPath, + flatRows, + setSelectedPath, + virtualizer +}: UseFileExplorerAutoRevealParams): void { + const prevActiveFileIdRef = useRef(null) + + useEffect(() => { + if (activeFileId === prevActiveFileIdRef.current) { + return + } + prevActiveFileIdRef.current = activeFileId + + if (!activeFileId || !activeWorktreeId || !worktreePath) { + return + } + + // Don't override a pending manual reveal (e.g. from Source Control "Reveal in Explorer") + if (pendingExplorerReveal) { + return + } + + // Only auto-reveal regular edit-mode files, not diffs or conflict reviews + const activeFile = openFiles.find((f) => f.id === activeFileId) + if (!activeFile || activeFile.worktreeId !== activeWorktreeId || activeFile.mode !== 'edit') { + return + } + + const filePath = activeFile.filePath + + if (rowsByPath.has(filePath)) { + // File is already visible in the tree — just scroll to it and select + setSelectedPath(filePath) + const targetIndex = flatRows.findIndex((row) => row.path === filePath) + if (targetIndex !== -1) { + requestAnimationFrame(() => { + virtualizer.scrollToIndex(targetIndex, { align: 'auto' }) + }) + } + } else { + // File's ancestor folders aren't expanded — use the reveal machinery + // to expand them and scroll, but skip the flash animation. + useAppStore.setState({ + pendingExplorerReveal: { + worktreeId: activeWorktreeId, + filePath, + requestId: Date.now(), + flash: false + } + }) + } + }, [ + activeFileId, + activeWorktreeId, + worktreePath, + pendingExplorerReveal, + openFiles, + rowsByPath, + flatRows, + setSelectedPath, + virtualizer + ]) +} diff --git a/src/renderer/src/components/right-sidebar/useFileExplorerHandlers.ts b/src/renderer/src/components/right-sidebar/useFileExplorerHandlers.ts new file mode 100644 index 000000000..8ffbdb668 --- /dev/null +++ b/src/renderer/src/components/right-sidebar/useFileExplorerHandlers.ts @@ -0,0 +1,87 @@ +import { useCallback } from 'react' +import type React from 'react' +import type { RefObject } from 'react' +import { detectLanguage } from '@/lib/language-detect' +import type { TreeNode } from './file-explorer-types' + +type UseFileExplorerHandlersParams = { + activeWorktreeId: string | null + openFile: (params: { + filePath: string + relativePath: string + worktreeId: string + language: string + mode: 'edit' + }) => void + pinFile: (filePath: string) => void + toggleDir: (worktreeId: string, dirPath: string) => void + setSelectedPath: (path: string) => void + scrollRef: RefObject +} + +type UseFileExplorerHandlersReturn = { + handleClick: (node: TreeNode) => void + handleDoubleClick: (node: TreeNode) => void + handleWheelCapture: (e: React.WheelEvent) => void +} + +export function useFileExplorerHandlers({ + activeWorktreeId, + openFile, + pinFile, + toggleDir, + setSelectedPath, + scrollRef +}: UseFileExplorerHandlersParams): UseFileExplorerHandlersReturn { + const handleClick = useCallback( + (node: TreeNode) => { + if (!activeWorktreeId) { + return + } + setSelectedPath(node.path) + if (node.isDirectory) { + toggleDir(activeWorktreeId, node.path) + return + } + openFile({ + filePath: node.path, + relativePath: node.relativePath, + worktreeId: activeWorktreeId, + language: detectLanguage(node.name), + mode: 'edit' + }) + }, + [activeWorktreeId, openFile, toggleDir, setSelectedPath] + ) + + const handleDoubleClick = useCallback( + (node: TreeNode) => { + if (!activeWorktreeId || node.isDirectory) { + return + } + pinFile(node.path) + }, + [activeWorktreeId, pinFile] + ) + + const handleWheelCapture = useCallback( + (e: React.WheelEvent) => { + const container = scrollRef.current + if (!container || Math.abs(e.deltaY) <= Math.abs(e.deltaX)) { + return + } + const target = e.target + if (!(target instanceof Element) || !target.closest('[data-explorer-draggable="true"]')) { + return + } + if (container.scrollHeight <= container.clientHeight) { + return + } + e.preventDefault() + container.scrollTop += e.deltaY + }, + [scrollRef] + ) + + return { handleClick, handleDoubleClick, handleWheelCapture } +} diff --git a/src/renderer/src/components/right-sidebar/useFileExplorerReveal.ts b/src/renderer/src/components/right-sidebar/useFileExplorerReveal.ts index e0be75dd4..79fcf030f 100644 --- a/src/renderer/src/components/right-sidebar/useFileExplorerReveal.ts +++ b/src/renderer/src/components/right-sidebar/useFileExplorerReveal.ts @@ -8,7 +8,12 @@ import type { DirCache, TreeNode } from './file-explorer-types' type UseFileExplorerRevealParams = { activeWorktreeId: string | null worktreePath: string | null - pendingExplorerReveal: { worktreeId: string; filePath: string; requestId: number } | null + pendingExplorerReveal: { + worktreeId: string + filePath: string + requestId: number + flash?: boolean + } | null clearPendingExplorerReveal: () => void expanded: Set dirCache: Record @@ -145,14 +150,19 @@ export function useFileExplorerReveal({ clearPendingExplorerReveal() setSelectedPath(revealPath) - setFlashingPath(revealPath) - if (flashTimeoutRef.current !== null) { - window.clearTimeout(flashTimeoutRef.current) + + // Only flash when explicitly requested (e.g. "Reveal in Explorer" from Source Control). + // Auto-reveals on tab switch skip the flash to avoid visual noise. + if (pendingExplorerReveal.flash !== false) { + setFlashingPath(revealPath) + if (flashTimeoutRef.current !== null) { + window.clearTimeout(flashTimeoutRef.current) + } + flashTimeoutRef.current = window.setTimeout(() => { + setFlashingPath((current) => (current === revealPath ? null : current)) + flashTimeoutRef.current = null + }, 2000) } - flashTimeoutRef.current = window.setTimeout(() => { - setFlashingPath((current) => (current === revealPath ? null : current)) - flashTimeoutRef.current = null - }, 2000) requestAnimationFrame(() => { window.setTimeout(() => { diff --git a/src/renderer/src/store/slices/editor.ts b/src/renderer/src/store/slices/editor.ts index 5e68eea48..f82c4c0ed 100644 --- a/src/renderer/src/store/slices/editor.ts +++ b/src/renderer/src/store/slices/editor.ts @@ -132,6 +132,7 @@ export type EditorSlice = { worktreeId: string filePath: string requestId: number + flash?: boolean } | null revealInExplorer: (worktreeId: string, filePath: string) => void clearPendingExplorerReveal: () => void