selecting collections should change background

This commit is contained in:
Georges-Antoine Assi
2025-09-05 09:05:52 -04:00
parent 5d702c079e
commit 95585257a1
5 changed files with 91 additions and 83 deletions

View File

@@ -1,5 +1,12 @@
<script setup lang="ts">
import { computed, onMounted, ref, watchEffect, useTemplateRef } from "vue";
import {
computed,
onMounted,
ref,
watchEffect,
useTemplateRef,
watch,
} from "vue";
import {
collectionElementRegistry,
smartCollectionElementRegistry,
@@ -17,7 +24,7 @@ const props = defineProps<{
selected?: boolean;
loaded?: boolean;
}>();
const emit = defineEmits(["click", "mouseenter", "focus", "loaded"]);
const emit = defineEmits(["click", "mouseenter", "focus", "loaded", "select"]);
const collectionCardRef = useTemplateRef<HTMLButtonElement>(
"collection-card-ref",
);
@@ -87,6 +94,16 @@ watchEffect(() => {
const firstLargeCover = computed(() => memoizedCovers.value.large[0]);
const secondLargeCover = computed(() => memoizedCovers.value.large[1]);
watch(
() => props.selected,
(isSelected) => {
if (isSelected && collectionCoverImage.value) {
emit("select", firstLargeCover.value);
}
},
{ immediate: true },
);
onMounted(() => {
if (!collectionCardRef.value) return;
if (props.collection.is_smart) {

View File

@@ -0,0 +1,28 @@
import { ref } from "vue";
import { useConsoleTheme } from "@/stores/consoleTheme";
const selectedBackgroundArt = ref<string | null>(null);
export default function useBackgroundArt() {
const consoleTheme = useConsoleTheme();
const setSelectedBackgroundArt = (url: string) => {
selectedBackgroundArt.value = url;
if (url) {
document.documentElement.style.setProperty(
"--theme-background",
`url("${url}")`,
);
}
};
const clearSelectedBackgroundArt = () => {
selectedBackgroundArt.value = null;
consoleTheme.updateBackgroundCSS();
};
return {
setSelectedBackgroundArt,
clearSelectedBackgroundArt,
selectedBackgroundArt,
};
}

View File

@@ -1,31 +0,0 @@
import { ref } from "vue";
import { useConsoleTheme } from "@/stores/consoleTheme";
import type { SimpleRom } from "@/stores/roms";
const selectedGame = ref<SimpleRom | null>(null);
export function useSelectedGameBackground() {
const consoleTheme = useConsoleTheme();
const setSelectedGame = (rom: SimpleRom) => {
selectedGame.value = rom;
const coverSrc =
rom.path_cover_large || rom.path_cover_small || rom.url_cover || "";
if (coverSrc) {
document.documentElement.style.setProperty(
"--theme-background",
`url("${coverSrc}")`,
);
}
};
const clearSelectedGame = () => {
selectedGame.value = null;
consoleTheme.updateBackgroundCSS();
};
return {
setSelectedGame,
clearSelectedGame,
selectedGame,
};
}

View File

@@ -15,10 +15,10 @@ import useFavoriteToggle from "@/composables/useFavoriteToggle";
import BackButton from "@/console/components/BackButton.vue";
import GameCard from "@/console/components/GameCard.vue";
import NavigationHint from "@/console/components/NavigationHint.vue";
import useBackgroundArt from "@/console/composables/useBackgroundArt";
import { gamesListElementRegistry } from "@/console/composables/useElementRegistry";
import { useInputScope } from "@/console/composables/useInputScope";
import { useRovingDom } from "@/console/composables/useRovingDom";
import { useSelectedGameBackground } from "@/console/composables/useSelectedGameBackground";
import { useSpatialNav } from "@/console/composables/useSpatialNav";
import type { InputAction } from "@/console/input/actions";
import { ROUTES } from "@/plugins/router";
@@ -31,7 +31,7 @@ const route = useRoute();
const router = useRouter();
const storeConsole = consoleStore();
const { toggleFavorite: toggleFavoriteComposable } = useFavoriteToggle();
const { setSelectedGame } = useSelectedGameBackground();
const { setSelectedBackgroundArt } = useBackgroundArt();
const isCollectionRoute = route.name === ROUTES.CONSOLE_COLLECTION;
const isSmartCollectionRoute = route.name === ROUTES.CONSOLE_SMART_COLLECTION;
@@ -383,8 +383,10 @@ function markLoaded(id: number) {
loadedMap.value[id] = true;
}
function handleGameSelect(rom: SimpleRom) {
setSelectedGame(rom);
function handleGameSelected(rom: SimpleRom) {
setSelectedBackgroundArt(
rom.path_cover_large || rom.path_cover_small || rom.url_cover || "",
);
}
</script>
@@ -432,7 +434,7 @@ function handleGameSelect(rom: SimpleRom) {
@click="selectAndOpen(i, rom)"
@focus="mouseSelect(i)"
@loaded="markLoaded(rom.id)"
@select="handleGameSelect"
@select="handleGameSelected"
/>
</div>
</div>

View File

@@ -20,6 +20,7 @@ import GameCard from "@/console/components/GameCard.vue";
import NavigationHint from "@/console/components/NavigationHint.vue";
import SettingsModal from "@/console/components/SettingsModal.vue";
import SystemCard from "@/console/components/SystemCard.vue";
import useBackgroundArt from "@/console/composables/useBackgroundArt";
import {
systemElementRegistry,
recentElementRegistry,
@@ -29,7 +30,6 @@ import {
} from "@/console/composables/useElementRegistry";
import { useInputScope } from "@/console/composables/useInputScope";
import { useRovingDom } from "@/console/composables/useRovingDom";
import { useSelectedGameBackground } from "@/console/composables/useSelectedGameBackground";
import { useSpatialNav } from "@/console/composables/useSpatialNav";
import { isSupportedPlatform } from "@/console/constants/platforms";
import type { InputAction } from "@/console/input/actions";
@@ -46,7 +46,8 @@ const collectionsStore = storeCollections();
const storeConsole = consoleStore();
const { navigationMode } = storeToRefs(storeConsole);
const { toggleFavorite: toggleFavoriteComposable } = useFavoriteToggle();
const { setSelectedGame, clearSelectedGame } = useSelectedGameBackground();
const { setSelectedBackgroundArt, clearSelectedBackgroundArt } =
useBackgroundArt();
const { subscribe } = useInputScope();
const platforms = ref<PlatformSchema[]>([]);
@@ -59,7 +60,7 @@ const errorMessage = ref("");
const showSettings = ref(false);
// Navigation indices
const selectedIndex = ref(storeConsole.platformIndex);
const platformIndex = ref(storeConsole.platformIndex);
const recentIndex = ref(storeConsole.recentIndex);
const collectionsIndex = ref(storeConsole.collectionsIndex);
const smartCollectionsIndex = ref(storeConsole.smartCollectionsIndex);
@@ -99,7 +100,7 @@ const virtualCollectionElementAt = (i: number) =>
// Spatial navigation
const { moveLeft: moveSystemLeft, moveRight: moveSystemRight } = useSpatialNav(
selectedIndex,
platformIndex,
() => platforms.value.length || 1,
() => platforms.value.length,
);
@@ -131,7 +132,7 @@ const {
() => virtualCollections.value.length,
);
useRovingDom(selectedIndex, systemElementAt, {
useRovingDom(platformIndex, systemElementAt, {
inline: "center",
block: "nearest",
behavior: "smooth",
@@ -141,29 +142,29 @@ useRovingDom(recentIndex, recentElementAt, {
inline: "center",
block: "nearest",
behavior: "smooth",
scroll: false, // same as above
scroll: false,
});
useRovingDom(collectionsIndex, collectionElementAt, {
inline: "center",
block: "nearest",
behavior: "smooth",
scroll: false, // same as above
scroll: false,
});
useRovingDom(smartCollectionsIndex, smartCollectionElementAt, {
inline: "center",
block: "nearest",
behavior: "smooth",
scroll: false, // same as above
scroll: false,
});
useRovingDom(virtualCollectionsIndex, virtualCollectionElementAt, {
inline: "center",
block: "nearest",
behavior: "smooth",
scroll: false, // same as above
scroll: false,
});
// carousel scrolling that respects vertical scroll state
watch(selectedIndex, (newIdx) => {
watch(platformIndex, (newIdx) => {
if (!isVerticalScrolling) {
const el = systemElementAt(newIdx);
if (el && platformsRef.value) {
@@ -212,24 +213,24 @@ watch(virtualCollectionsIndex, (newIdx) => {
const navigationFunctions = {
systems: {
prev: () => {
const before = selectedIndex.value;
const before = platformIndex.value;
moveSystemLeft();
if (selectedIndex.value === before) {
selectedIndex.value = Math.max(0, platforms.value.length - 1);
if (platformIndex.value === before) {
platformIndex.value = Math.max(0, platforms.value.length - 1);
}
},
next: () => {
const before = selectedIndex.value;
const before = platformIndex.value;
moveSystemRight();
if (selectedIndex.value === before) {
selectedIndex.value = 0;
if (platformIndex.value === before) {
platformIndex.value = 0;
}
},
confirm: () => {
if (!platforms.value[selectedIndex.value]) return false;
if (!platforms.value[platformIndex.value]) return false;
router.push({
name: ROUTES.CONSOLE_PLATFORM,
params: { id: platforms.value[selectedIndex.value].id },
params: { id: platforms.value[platformIndex.value].id },
});
return true;
},
@@ -366,6 +367,9 @@ let isVerticalScrolling = false;
function scrollToCurrentRow() {
isVerticalScrolling = true;
// clear background art when switching sections
clearSelectedBackgroundArt();
// promise resolves when the scroll animation finishes
verticalScrollPromise = new Promise((resolve) => {
const behavior: ScrollBehavior = "smooth";
@@ -451,31 +455,16 @@ function goGame(game: SimpleRom) {
});
}
function handleGameSelect(rom: SimpleRom) {
setSelectedGame(rom);
function handleGameSelected(rom: SimpleRom) {
setSelectedBackgroundArt(
rom.path_cover_large || rom.path_cover_small || rom.url_cover || "",
);
}
function handleGameDeselect() {
clearSelectedGame();
function handleCollectionSelected(coverUrl: string) {
setSelectedBackgroundArt(coverUrl);
}
// Watch for changes in recent index to handle background clearing
watch(
[recentIndex, recentRoms, navigationMode],
([newIndex, newRoms, mode]) => {
// Clear background if not in recent mode, no games, or invalid selection
if (
mode !== "recent" ||
newRoms.length === 0 ||
newIndex < 0 ||
newIndex >= newRoms.length
) {
clearSelectedGame();
}
},
{ immediate: true },
);
function goCollection(collectionId: number) {
router.push({
name: ROUTES.CONSOLE_COLLECTION,
@@ -679,7 +668,7 @@ onMounted(async () => {
}
// Restore indices within bounds
if (selectedIndex.value >= platforms.value.length) selectedIndex.value = 0;
if (platformIndex.value >= platforms.value.length) platformIndex.value = 0;
if (recentIndex.value >= recentRoms.value.length) recentIndex.value = 0;
if (collectionsIndex.value >= collections.value.length)
collectionsIndex.value = 0;
@@ -692,7 +681,7 @@ onMounted(async () => {
scrollToCurrentRow();
// Center carousels
centerInCarousel(platformsRef.value, systemElementAt(selectedIndex.value));
centerInCarousel(platformsRef.value, systemElementAt(platformIndex.value));
centerInCarousel(recentRef.value, recentElementAt(recentIndex.value));
centerInCarousel(
collectionsRef.value,
@@ -714,7 +703,7 @@ let off: (() => void) | null = null;
onUnmounted(() => {
storeConsole.setHomeState({
platformIndex: selectedIndex.value,
platformIndex: platformIndex.value,
recentIndex: recentIndex.value,
collectionsIndex: collectionsIndex.value,
smartCollectionsIndex: smartCollectionsIndex.value,
@@ -801,10 +790,10 @@ onUnmounted(() => {
:platform="p"
:index="i"
:selected="
navigationMode === 'systems' && i === selectedIndex
navigationMode === 'systems' && i === platformIndex
"
@click="goPlatform(p.id)"
@focus="selectedIndex = i"
@focus="platformIndex = i"
/>
</div>
</div>
@@ -861,7 +850,7 @@ onUnmounted(() => {
:loaded="true"
@click="goGame(g)"
@focus="recentIndex = i"
@select="handleGameSelect"
@select="handleGameSelected"
/>
</div>
</div>
@@ -919,6 +908,7 @@ onUnmounted(() => {
:loaded="true"
@click="goCollection(c.id)"
@focus="collectionsIndex = i"
@select="handleCollectionSelected"
/>
</div>
</div>
@@ -977,6 +967,7 @@ onUnmounted(() => {
:loaded="true"
@click="goSmartCollection(c.id)"
@focus="smartCollectionsIndex = i"
@select="handleCollectionSelected"
/>
</div>
</div>
@@ -1035,6 +1026,7 @@ onUnmounted(() => {
:loaded="true"
@click="goVirtualCollection(c.id)"
@focus="virtualCollectionsIndex = i"
@select="handleCollectionSelected"
/>
</div>
</div>