[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
This commit is contained in:
ameer2468 2023-10-03 19:35:45 +03:00 committed by GitHub
parent d6d5f1505c
commit 8af1019850
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 64 deletions

View file

@ -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 <Menu.Item label="Download" />;
else
@ -251,27 +250,7 @@ export const OpenOrDownload = new ConditionalItem({
<Menu.Item
label="Open"
keybind={keybind([ModifierKeys.Control], ['O'])}
onClick={async () => {
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()}
/>
<Conditional items={[OpenWith]} />
</>

View file

@ -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 (
<div className="flex h-full flex-col items-center justify-center text-ink-faint">
<div className="flex flex-col items-center justify-center h-full text-ink-faint">
{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]);
};

View file

@ -87,7 +87,7 @@ const shortcutCategories: Record<string, Shortcut[]> = {
action: 'Open selected item',
keys: {
all: {
value: [ModifierKeys.Control, 'ArrowUp']
value: [ModifierKeys.Control, 'O']
}
}
},