Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { copyFileToOsClipboard, downloadRemoteFile } from './file-explorer-row-file-transfer'
import {
copyFileToOsClipboard,
downloadRemoteFile,
openFileInDefaultApp
} from './file-explorer-row-file-transfer'
import {
shouldShowCollapseFolderAction,
shouldShowFindInFolderAction,
shouldShowCopyFileAction,
shouldShowOpenInDefaultAppAction,
shouldShowOpenInTerminalAction,
shouldShowRemoteDownloadAction,
shouldShowViewFileAction
Expand Down Expand Up @@ -40,6 +45,15 @@ function setDownloadPlatform(platform: NodeJS.Platform): void {
).api.platform = { get: () => ({ platform }) }
}

function replaceGlobalWindowForTest(windowValue: unknown): () => void {
const target = globalThis as { window?: unknown }
const original = target.window
target.window = windowValue
return () => {
target.window = original
}
}

beforeEach(() => {
toastErrorMock.mockReset()
toastSuccessMock.mockReset()
Expand Down Expand Up @@ -87,6 +101,27 @@ describe('FileExplorerRow collapse folder action', () => {
expect(shouldShowViewFileAction(directoryNode)).toBe(false)
})

it('shows default app open only for local desktop files', () => {
const runtimeContext = {
settings: { activeRuntimeEnvironmentId: 'runtime-1' },
worktreeId: 'wt-1',
worktreePath: '/repo'
}
const previous = (globalThis as { __ORCA_WEB_CLIENT__?: boolean }).__ORCA_WEB_CLIENT__
try {
expect(shouldShowOpenInDefaultAppAction(fileNode)).toBe(true)
expect(shouldShowOpenInDefaultAppAction(directoryNode)).toBe(false)
expect(shouldShowOpenInDefaultAppAction(fileNode, 'ssh-1')).toBe(false)
expect(shouldShowOpenInDefaultAppAction(fileNode, null, runtimeContext)).toBe(false)

;(globalThis as { __ORCA_WEB_CLIENT__?: boolean }).__ORCA_WEB_CLIENT__ = true

expect(shouldShowOpenInDefaultAppAction(fileNode)).toBe(false)
} finally {
;(globalThis as { __ORCA_WEB_CLIENT__?: boolean }).__ORCA_WEB_CLIENT__ = previous
}
})

it('shows remote download only for desktop SSH rows and file-like Remote Host rows', () => {
const runtimeContext = {
settings: { activeRuntimeEnvironmentId: 'runtime-1' },
Expand Down Expand Up @@ -192,6 +227,33 @@ describe('FileExplorerRow collapse folder action', () => {
expect(toastErrorMock).toHaveBeenCalledWith('Remote connection dropped')
})

it('opens a local file with the default app', async () => {
const openFilePath = vi.fn().mockResolvedValue(true)
const restoreWindow = replaceGlobalWindowForTest({ api: { shell: { openFilePath } } })

try {
await openFileInDefaultApp(fileNode)

expect(openFilePath).toHaveBeenCalledWith('/repo/src/index.ts')
expect(toastErrorMock).not.toHaveBeenCalled()
} finally {
restoreWindow()
}
})

it('shows a failure toast when the default app launch fails', async () => {
const openFilePath = vi.fn().mockResolvedValue(false)
const restoreWindow = replaceGlobalWindowForTest({ api: { shell: { openFilePath } } })

try {
await openFileInDefaultApp(fileNode)

expect(toastErrorMock).toHaveBeenCalledWith("Could not open 'index.ts' in the default app.")
} finally {
restoreWindow()
}
})

it('calls the preload download API and shows success only when not canceled', async () => {
const downloadFile = vi
.fn()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ export function shouldShowViewFileAction(node: TreeNode): boolean {
return !node.isDirectory
}

export function shouldShowOpenInDefaultAppAction(
node: TreeNode,
connectionId?: string | null,
runtimeDownloadContext?: RuntimeFileOperationArgs | null
): boolean {
return (
!node.isDirectory &&
!connectionId &&
!runtimeDownloadContext &&
(globalThis as { __ORCA_WEB_CLIENT__?: boolean }).__ORCA_WEB_CLIENT__ !== true
)
}

export function shouldShowRemoteDownloadAction(
node: TreeNode,
connectionId?: string | null,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { useCallback } from 'react'
import {
AppWindow,
Copy,
Download,
ExternalLink,
Expand Down Expand Up @@ -33,11 +34,16 @@ import {
shouldShowCollapseFolderAction,
shouldShowCopyFileAction,
shouldShowFindInFolderAction,
shouldShowOpenInDefaultAppAction,
shouldShowOpenInTerminalAction,
shouldShowRemoteDownloadAction,
shouldShowViewFileAction
} from './file-explorer-row-action-visibility'
import { copyFileToOsClipboard, downloadRemoteFile } from './file-explorer-row-file-transfer'
import {
copyFileToOsClipboard,
downloadRemoteFile,
openFileInDefaultApp
} from './file-explorer-row-file-transfer'

const isMac = navigator.userAgent.includes('Mac')
const isLinux = navigator.userAgent.includes('Linux')
Expand Down Expand Up @@ -113,6 +119,7 @@ export function FileExplorerRowContextMenu({
const activeWorktreeId = useAppStore((s) => s.activeWorktreeId)
const copyPathShortcutLabel = useShortcutLabel('fileExplorer.copyPath')
const copyRelativePathShortcutLabel = useShortcutLabel('fileExplorer.copyRelativePath')
const openInDefaultAppShortcutLabel = useShortcutLabel('fileExplorer.openInDefaultApp')
const findInFolderShortcutLabel = useShortcutLabel('sidebar.search.toggle')
const showRemoteDownloadAction = shouldShowRemoteDownloadAction(
node,
Expand All @@ -121,6 +128,11 @@ export function FileExplorerRowContextMenu({
supportsFolderDownload
)
const showCopyFileAction = shouldShowCopyFileAction(node, connectionId, selectionSize)
const showOpenInDefaultAppAction = shouldShowOpenInDefaultAppAction(
node,
connectionId,
runtimeDownloadContext
)
const handleOpenInOrcaBrowser = useCallback(() => {
if (!activeWorktreeId) {
return
Expand All @@ -140,6 +152,24 @@ export function FileExplorerRowContextMenu({
const handleCopyFile = useCallback(() => {
void copyFileToOsClipboard(node, connectionId)
}, [connectionId, node])
const handleOpenInDefaultApp = useCallback(() => {
const state = useAppStore.getState()
const activeWorktree = Object.values(state.worktreesByRepo)
.flat()
.find((worktree) => worktree.id === activeWorktreeId)
const activeRepo = activeWorktree
? state.repos.find((repo) => repo.id === activeWorktree.repoId)
: null
if (
isLocalPathOpenBlocked(state.settings, {
connectionId: activeRepo?.connectionId ?? null
})
) {
showLocalPathOpenBlockedToast()
return
}
void openFileInDefaultApp(node)
}, [activeWorktreeId, node])

return (
<ContextMenuContent
Expand Down Expand Up @@ -216,6 +246,18 @@ export function FileExplorerRowContextMenu({
{translate('auto.components.right.sidebar.FileExplorerRow.1d8e182c32', 'View File')}
</ContextMenuItem>
)}
{showOpenInDefaultAppAction && (
<ContextMenuItem onSelect={handleOpenInDefaultApp}>
<AppWindow />
{translate(
'auto.components.right.sidebar.FileExplorerRow.288a8e8081',
'Open in Default App'
)}
{openInDefaultAppShortcutLabel !== 'Unassigned' ? (
<ContextMenuShortcut>{openInDefaultAppShortcutLabel}</ContextMenuShortcut>
) : null}
</ContextMenuItem>
)}
{!node.isDirectory && activeWorktreeId && canOpenInOrcaBrowser && (
<ContextMenuItem onSelect={handleOpenInOrcaBrowser}>
<Globe />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,19 @@ export async function copyFileToOsClipboard(
toast.error(extractIpcErrorMessage(error, failureMessage))
}
}

export async function openFileInDefaultApp(node: TreeNode): Promise<void> {
const failureMessage = translate(
'auto.components.right.sidebar.FileExplorerRow.d40c3303f4',
"Could not open '{{value0}}' in the default app.",
{ value0: node.name }
)
try {
const opened = await window.api.shell.openFilePath(node.path)
if (!opened) {
toast.error(failureMessage)
}
} catch (error) {
toast.error(extractIpcErrorMessage(error, failureMessage))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import type { RefObject } from 'react'
import { useCallback, useMemo } from 'react'
import { useAppStore } from '@/store'
import { createNewTerminalTab } from '@/components/terminal/terminal-tab-create'
import { isLocalPathOpenBlocked, showLocalPathOpenBlockedToast } from '@/lib/local-path-open-guard'
import type { RuntimeFileOperationArgs } from '@/runtime/runtime-file-client'
import type { Repo } from '../../../../shared/repo-types'
import {
buildAddProjectFromFolderModalData,
Expand All @@ -20,11 +22,14 @@ import type { InlineInput } from './file-explorer-inline-input-row'
import type { TreeNode } from './file-explorer-types'
import { useFileDuplicate } from './useFileDuplicate'
import { useFileExplorerKeys } from './useFileExplorerKeys'
import { shouldShowOpenInDefaultAppAction } from './file-explorer-row-action-visibility'
import { openFileInDefaultApp } from './file-explorer-row-file-transfer'

type UseFileExplorerNodeCommandsParams = {
activeWorktreeId: string | null
worktreePath: string | null
activeRepo: Repo | null
runtimeDownloadContext: RuntimeFileOperationArgs | null
containerRef: RefObject<HTMLDivElement | null>
rowProjection: FileExplorerRowProjection
rowExpandedPaths: Set<string>
Expand Down Expand Up @@ -63,6 +68,7 @@ export function useFileExplorerNodeCommands({
activeWorktreeId,
worktreePath,
activeRepo,
runtimeDownloadContext,
containerRef,
rowProjection,
rowExpandedPaths,
Expand Down Expand Up @@ -107,6 +113,30 @@ export function useFileExplorerNodeCommands({
},
[cancelPendingDirToggle, startRename]
)
const handleOpenInDefaultApp = useCallback(
(node: TreeNode): boolean => {
if (
!shouldShowOpenInDefaultAppAction(
node,
activeRepo?.connectionId ?? null,
runtimeDownloadContext
)
) {
return false
}
if (
isLocalPathOpenBlocked(useAppStore.getState().settings, {
connectionId: activeRepo?.connectionId ?? null
})
) {
showLocalPathOpenBlockedToast()
return false
}
void openFileInDefaultApp(node)
return true
},
[activeRepo?.connectionId, runtimeDownloadContext]
)

useFileExplorerKeys({
containerRef,
Expand All @@ -120,6 +150,7 @@ export function useFileExplorerNodeCommands({
moveSelection,
toggleDir,
startRename: handleStartRename,
openInDefaultApp: handleOpenInDefaultApp,
requestDelete,
requestDeleteAll,
scrollToIndex,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ export function useFileExplorerTreePaneState({
activeWorktreeId,
worktreePath,
activeRepo,
runtimeDownloadContext,
containerRef: rowScrolling.explorerShellRef,
rowProjection,
rowExpandedPaths,
Expand Down
20 changes: 19 additions & 1 deletion src/renderer/src/components/right-sidebar/useFileExplorerKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export function useFileExplorerKeys(opts: {
moveSelection: (targetPath: string, mode: SelectionMode) => void
toggleDir: (worktreeId: string, dirPath: string) => void
startRename: (node: TreeNode) => void
openInDefaultApp: (node: TreeNode) => boolean
requestDelete: (node: TreeNode) => void
requestDeleteAll: (nodes: TreeNode[]) => void
scrollToIndex: (index: number) => void
Expand All @@ -71,6 +72,8 @@ export function useFileExplorerKeys(opts: {
selectedNodeRef.current = opts.selectedNode
const startRenameRef = useRef(opts.startRename)
startRenameRef.current = opts.startRename
const openInDefaultAppRef = useRef(opts.openInDefaultApp)
openInDefaultAppRef.current = opts.openInDefaultApp
const requestDeleteRef = useRef(opts.requestDelete)
requestDeleteRef.current = opts.requestDelete
const requestDeleteAllRef = useRef(opts.requestDeleteAll)
Expand Down Expand Up @@ -258,14 +261,29 @@ export function useFileExplorerKeys(opts: {
platform,
keybindings
)
if (!wantsCopyRelativePath && !wantsCopyPath) {
const wantsOpenInDefaultApp = keybindingMatchesAction(
'fileExplorer.openInDefaultApp',
e,
platform,
keybindings
)
if (!wantsCopyRelativePath && !wantsCopyPath && !wantsOpenInDefaultApp) {
return
}

const focused = findFocusedIndex()
const node =
(focused !== null ? rowProjectionRef.current.getRowAtIndex(focused) : null) ??
selectedNodeRef.current
if (wantsOpenInDefaultApp) {
if (node && !node.isDirectory) {
if (openInDefaultAppRef.current(node)) {
e.preventDefault()
}
}
return
}

const selectedNodes = rowProjectionRef.current.getRowsByPaths(selectedPathsRef.current)
const fallbackNodes = selectedNodes.length > 0 ? selectedNodes : node ? [node] : []
if (fallbackNodes.length === 0) {
Expand Down
2 changes: 2 additions & 0 deletions src/renderer/src/i18n/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -11645,6 +11645,8 @@
"3161c4e425": "folder",
"e887fa4b2e": "Open in Terminal",
"1d8e182c32": "View File",
"d40c3303f4": "Could not open '{{value0}}' in the default app.",
"288a8e8081": "Open in Default App",
"clipboardStagingUnavailable": "Could not copy the file because Orca's temporary storage is unavailable"
},
"FileExplorerToolbar": {
Expand Down
4 changes: 3 additions & 1 deletion src/renderer/src/i18n/locales/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -10412,7 +10412,9 @@
"f9d7ca753d": "Copiar rutas",
"3161c4e425": "carpeta",
"e887fa4b2e": "Abrir en terminal",
"1d8e182c32": "Ver archivo"
"1d8e182c32": "Ver archivo",
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
"d40c3303f4": "No se pudo abrir '{{value0}}' en la aplicación predeterminada.",
"288a8e8081": "Abrir en la aplicación predeterminada"
},
"FileExplorerToolbar": {
"d238264654": "Mostrar archivos ignorados por Git",
Expand Down
4 changes: 3 additions & 1 deletion src/renderer/src/i18n/locales/ja.json
Original file line number Diff line number Diff line change
Expand Up @@ -10412,7 +10412,9 @@
"f9d7ca753d": "パスのコピー",
"3161c4e425": "フォルダー",
"e887fa4b2e": "ターミナルで開く",
"1d8e182c32": "ファイルを表示"
"1d8e182c32": "ファイルを表示",
"d40c3303f4": "'{{value0}}' をデフォルトアプリで開けませんでした。",
"288a8e8081": "デフォルトアプリで開く"
},
"FileExplorerToolbar": {
"d238264654": "Git で無視されたファイルを表示",
Expand Down
4 changes: 3 additions & 1 deletion src/renderer/src/i18n/locales/ko.json
Original file line number Diff line number Diff line change
Expand Up @@ -10420,7 +10420,9 @@
"f9d7ca753d": "경로 복사",
"3161c4e425": "폴더",
"e887fa4b2e": "터미널에서 열기",
"1d8e182c32": "파일 보기"
"1d8e182c32": "파일 보기",
"d40c3303f4": "기본 앱에서 '{{value0}}'을(를) 열 수 없습니다.",
"288a8e8081": "기본 앱에서 열기"
},
"FileExplorerToolbar": {
"d238264654": "Git이 무시한 파일 표시",
Expand Down
Loading