Files
romm/frontend/src/console/composables/useSpatialNav.ts
Georges-Antoine Assi 4c34855c32 cleanup composables
2025-08-25 16:50:11 -04:00

30 lines
833 B
TypeScript

import type { Ref } from "vue";
export function useSpatialNav(
selectedIndex: Ref<number>,
getCols: () => number,
getCount: () => number,
) {
const moveLeft = () => {
selectedIndex.value = Math.max(0, selectedIndex.value - 1);
};
const moveRight = () => {
const cols = getCols();
const last = getCount() - 1;
const atRight = (selectedIndex.value + 1) % cols === 0;
if (!atRight) selectedIndex.value = Math.min(last, selectedIndex.value + 1);
};
const moveUp = () => {
const cols = getCols();
const ni = selectedIndex.value - cols;
if (ni >= 0) selectedIndex.value = ni;
};
const moveDown = () => {
const cols = getCols();
const ni = selectedIndex.value + cols;
if (ni < getCount()) selectedIndex.value = ni;
};
return { moveLeft, moveRight, moveUp, moveDown };
}