From 9a23481c8a025e55a70943256d57bf40ba243e91 Mon Sep 17 00:00:00 2001 From: Nicolas Ramz Date: Sun, 5 May 2024 15:27:23 +0200 Subject: [PATCH] IconViewMode/TableViewMode: fix cursor navigation When no element is selected, next/prev cursor keys should select first/last element. Also fixed: changing sort method and using cursor navigation would use previous cursorIndex. --- src/components/FileView.tsx | 3 ++- .../viewmodes/IconViewMode/index.tsx | 27 +++++++++++++------ .../viewmodes/TableViewMode/index.tsx | 8 +++--- 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/components/FileView.tsx b/src/components/FileView.tsx index ecfb1c83..09d188cb 100644 --- a/src/components/FileView.tsx +++ b/src/components/FileView.tsx @@ -149,6 +149,7 @@ const FileView = observer(({ hide }: Props) => { event.preventDefault() const { getNextIndex } = getActions() const nextIndex = getNextIndex(cursorIndex, event.key as ArrowKey) + if (nextIndex === cursorIndex) return if (nextIndex > -1 && nextIndex <= rowCount - 1) { const file = cache.files[nextIndex] selectFile(file, false, event.shiftKey) @@ -180,7 +181,7 @@ const FileView = observer(({ hide }: Props) => { } } }, - [cursor, cache, rowCount], + [cursor, cache, rowCount, cursorIndex], ), ['ArrowDown', 'ArrowUp', 'ArrowLeft', 'ArrowRight', 'Enter', ' '], { alwaysCatchEvent: true }, diff --git a/src/components/viewmodes/IconViewMode/index.tsx b/src/components/viewmodes/IconViewMode/index.tsx index b937741e..124a0705 100644 --- a/src/components/viewmodes/IconViewMode/index.tsx +++ b/src/components/viewmodes/IconViewMode/index.tsx @@ -76,6 +76,8 @@ export const IconViewMode = forwardRef= itemCount ? itemCount - 1 : newIndex - case 'ArrowUp': - return index - itemsPerRow + case 'ArrowUp': { + const pos = index - itemsPerRow + if (index > -1) return pos >= 0 ? pos : index + else return lastIndex + } - case 'ArrowRight': - return index + 1 + case 'ArrowRight': { + const pos = index + 1 + if (index > -1) return pos <= lastIndex ? pos : index + else return 0 + } - case 'ArrowLeft': - return index - 1 + case 'ArrowLeft': { + const pos = index - 1 + if (index > -1) return pos > -1 ? pos : index + else return lastIndex + } default: console.warn(`getNextIndex: unknown direction ${direction}`) @@ -107,14 +118,14 @@ export const IconViewMode = forwardRef { // Position scrolling in these cases: // 1. new file has been selected: scroll to selected index to make sure it's visible // 2. new directory has been loaded (or same reloaded): scroll to top or selected index - if (status === 'ok' && itemsPerRow > 0) { + if (status === 'ok' && cursorIndex > -1 && itemsPerRow > 0) { const row = Math.floor(cursorIndex / itemsPerRow) scrollToIndex(cursorIndex === -1 ? 0 : row) } diff --git a/src/components/viewmodes/TableViewMode/index.tsx b/src/components/viewmodes/TableViewMode/index.tsx index 24e7f68a..6c0ffb02 100644 --- a/src/components/viewmodes/TableViewMode/index.tsx +++ b/src/components/viewmodes/TableViewMode/index.tsx @@ -54,10 +54,12 @@ export const TableViewMode = forwardRef -1) return index < itemCount - 1 ? index + 1 : index + else return 0 case 'ArrowUp': - return index - 1 + if (index > -1) return index > 0 ? index - 1 : index + else return itemCount - 1 default: console.warn(`getNextIndex: unknown direction ${direction}`) @@ -71,7 +73,7 @@ export const TableViewMode = forwardRef -1 && scrollToIndex(cursorIndex === -1 ? 0 : cursorIndex) }, [cursorIndex, status]) return (