From 8af101985078a02070e5637bc74e229c56ddc687 Mon Sep 17 00:00:00 2001 From: ameer2468 <33054370+ameer2468@users.noreply.github.com> Date: Tue, 3 Oct 2023 19:35:45 +0300 Subject: [PATCH] [ENG-872, ENG-1177] - Windows enter key opens & Open context menu correction (#1424) * Enter key opens on windows, cmd and o on mac. Open in context menu now opens in SD and not Finder. * Remove access time func as doubleClick already does it --- .../Explorer/ContextMenu/FilePath/Items.tsx | 31 ++--------- .../app/$libraryId/Explorer/View/index.tsx | 51 +++++-------------- .../settings/client/keybindings.tsx | 2 +- 3 files changed, 20 insertions(+), 64 deletions(-) diff --git a/interface/app/$libraryId/Explorer/ContextMenu/FilePath/Items.tsx b/interface/app/$libraryId/Explorer/ContextMenu/FilePath/Items.tsx index 361b36e67..926777b08 100644 --- a/interface/app/$libraryId/Explorer/ContextMenu/FilePath/Items.tsx +++ b/interface/app/$libraryId/Explorer/ContextMenu/FilePath/Items.tsx @@ -1,5 +1,5 @@ import { Image, Package, Trash, TrashSimple } from '@phosphor-icons/react'; -import { libraryClient, useLibraryContext, useLibraryMutation } from '@sd/client'; +import { libraryClient, useLibraryMutation } from '@sd/client'; import { ContextMenu, dialogManager, ModifierKeys, toast } from '@sd/ui'; import { Menu } from '~/components/Menu'; import { useKeybindFactory } from '~/hooks/useKeybindFactory'; @@ -10,6 +10,7 @@ import { useExplorerContext } from '../../Context'; import { CopyAsPathBase } from '../../CopyAsPath'; import DeleteDialog from '../../FilePath/DeleteDialog'; import EraseDialog from '../../FilePath/EraseDialog'; +import { useViewItemDoubleClick } from '../../View/ViewItem'; import { Conditional, ConditionalItem } from '../ConditionalItem'; import { useContextMenuContext } from '../context'; import OpenWith from './OpenWith'; @@ -237,12 +238,10 @@ export const OpenOrDownload = new ConditionalItem({ return { openFilePaths, selectedFilePaths }; }, - Component: ({ openFilePaths, selectedFilePaths }) => { + Component: () => { const keybind = useKeybindFactory(); const { platform } = usePlatform(); - const updateAccessTime = useLibraryMutation('files.updateAccessTime'); - - const { library } = useLibraryContext(); + const { doubleClick } = useViewItemDoubleClick(); if (platform === 'web') return ; else @@ -251,27 +250,7 @@ export const OpenOrDownload = new ConditionalItem({ { - if (selectedFilePaths.length < 1) return; - - updateAccessTime - .mutateAsync( - selectedFilePaths.map((p) => p.object_id!).filter(Boolean) - ) - .catch(console.error); - - try { - await openFilePaths( - library.uuid, - selectedFilePaths.map((p) => p.id) - ); - } catch (error) { - toast.error({ - title: `Failed to open file`, - body: `Error: ${error}.` - }); - } - }} + onClick={() => doubleClick()} /> diff --git a/interface/app/$libraryId/Explorer/View/index.tsx b/interface/app/$libraryId/Explorer/View/index.tsx index 6d9230586..cb187bcbc 100644 --- a/interface/app/$libraryId/Explorer/View/index.tsx +++ b/interface/app/$libraryId/Explorer/View/index.tsx @@ -10,7 +10,7 @@ import { type ReactNode } from 'react'; import { createPortal } from 'react-dom'; -import { useKeys } from 'rooks'; +import { useKey, useKeys } from 'rooks'; import { getItemObject, useLibraryContext, type Object } from '@sd/client'; import { dialogManager, ModifierKeys, toast } from '@sd/ui'; import { Loader } from '~/components'; @@ -62,6 +62,7 @@ export default memo( const explorer = useExplorerContext(); const quickPreview = useQuickPreviewContext(); const quickPreviewStore = useQuickPreviewStore(); + const os = useOperatingSystem(); const { doubleClick } = useViewItemDoubleClick(); @@ -95,8 +96,16 @@ export default memo( } else setShowLoading(false); }, [explorer.isFetchingNextPage]); - useKeys([metaCtrlKey, 'ArrowUp'], (e) => { + useKey(['Enter'], (e) => { e.stopPropagation(); + if (os === 'windows') { + doubleClick(); + } + }); + + useKeys([metaCtrlKey, 'KeyO'], (e) => { + e.stopPropagation(); + if (os === 'windows') return; doubleClick(); }); @@ -170,7 +179,7 @@ export const EmptyNotice = (props: { if (props.loading) return null; return ( -
+
{props.icon ? isValidElement(props.icon) ? props.icon @@ -213,38 +222,6 @@ const useKeyDownHandlers = ({ disabled }: { disabled: boolean }) => { [os, explorer.selectedItems] ); - const handleOpenShortcut = useCallback( - async (event: KeyboardEvent) => { - if ( - event.key.toUpperCase() !== 'O' || - !event.getModifierState( - os === 'macOS' ? ModifierKeys.Meta : ModifierKeys.Control - ) || - !openFilePaths - ) - return; - - const paths: number[] = []; - - for (const item of explorer.selectedItems) - for (const path of item.type === 'Path' - ? [item.item] - : item.type === 'Object' - ? item.item.file_paths - : []) - paths.push(path.id); - - if (!isNonEmpty(paths)) return; - - try { - await openFilePaths(library.uuid, paths); - } catch (error) { - toast.error({ title: 'Failed to open file', body: `Error: ${error}.` }); - } - }, - [os, library.uuid, openFilePaths, explorer.selectedItems] - ); - const handleExplorerShortcut = useCallback( (event: KeyboardEvent) => { if ( @@ -259,12 +236,12 @@ const useKeyDownHandlers = ({ disabled }: { disabled: boolean }) => { ); useEffect(() => { - const handlers = [handleNewTag, handleOpenShortcut, handleExplorerShortcut]; + const handlers = [handleNewTag, handleExplorerShortcut]; const handler = (event: KeyboardEvent) => { if (event.repeat || disabled) return; for (const handler of handlers) handler(event); }; document.body.addEventListener('keydown', handler); return () => document.body.removeEventListener('keydown', handler); - }, [disabled, handleNewTag, handleOpenShortcut, handleExplorerShortcut]); + }, [disabled, handleNewTag, handleExplorerShortcut]); }; diff --git a/interface/app/$libraryId/settings/client/keybindings.tsx b/interface/app/$libraryId/settings/client/keybindings.tsx index 325629673..6bf63f7e9 100644 --- a/interface/app/$libraryId/settings/client/keybindings.tsx +++ b/interface/app/$libraryId/settings/client/keybindings.tsx @@ -87,7 +87,7 @@ const shortcutCategories: Record = { action: 'Open selected item', keys: { all: { - value: [ModifierKeys.Control, 'ArrowUp'] + value: [ModifierKeys.Control, 'O'] } } },