mirror of
https://github.com/rommapp/romm.git
synced 2026-12-02 18:18:47 +01:00
misc: Add PlayBtn component to unify button logic
The Play button logic was duplicated in three different places, including when to run either EmulatorJS or Ruffle. This change centralizes that logic in a new `PlayBtn` component, which internally decides which player to use, and binds to any arguments passed to it. Now all three places also consider platform versions when checking for emulation support.
This commit is contained in:
@@ -1,19 +1,13 @@
|
||||
<script setup lang="ts">
|
||||
import AdminMenu from "@/components/common/Game/AdminMenu.vue";
|
||||
import CopyRomDownloadLinkDialog from "@/components/common/Game/Dialog/CopyDownloadLink.vue";
|
||||
import PlayBtn from "@/components/common/Game/PlayBtn.vue";
|
||||
import romApi from "@/services/api/rom";
|
||||
import storeDownload from "@/stores/download";
|
||||
import storeHeartbeat from "@/stores/heartbeat";
|
||||
import storeConfig from "@/stores/config";
|
||||
import type { DetailedRom } from "@/stores/roms";
|
||||
import storeAuth from "@/stores/auth";
|
||||
import type { Events } from "@/types/emitter";
|
||||
import {
|
||||
getDownloadLink,
|
||||
is3DSCIARom,
|
||||
isEJSEmulationSupported,
|
||||
isRuffleEmulationSupported,
|
||||
} from "@/utils";
|
||||
import { getDownloadLink, is3DSCIARom } from "@/utils";
|
||||
import type { Emitter } from "mitt";
|
||||
import { computed, inject, ref } from "vue";
|
||||
import { storeToRefs } from "pinia";
|
||||
@@ -21,29 +15,11 @@ import { useI18n } from "vue-i18n";
|
||||
|
||||
const props = defineProps<{ rom: DetailedRom }>();
|
||||
const downloadStore = storeDownload();
|
||||
const heartbeatStore = storeHeartbeat();
|
||||
const emitter = inject<Emitter<Events>>("emitter");
|
||||
const playInfoIcon = ref("mdi-play");
|
||||
const qrCodeIcon = ref("mdi-qrcode");
|
||||
const configStore = storeConfig();
|
||||
const { config } = storeToRefs(configStore);
|
||||
const auth = storeAuth();
|
||||
const { t } = useI18n();
|
||||
|
||||
const platformSlug = computed(() =>
|
||||
props.rom.platform_slug in config.value.PLATFORMS_VERSIONS
|
||||
? config.value.PLATFORMS_VERSIONS[props.rom.platform_slug]
|
||||
: props.rom.platform_slug,
|
||||
);
|
||||
|
||||
const ejsEmulationSupported = computed(() => {
|
||||
return isEJSEmulationSupported(platformSlug.value, heartbeatStore.value);
|
||||
});
|
||||
|
||||
const ruffleEmulationSupported = computed(() => {
|
||||
return isRuffleEmulationSupported(platformSlug.value, heartbeatStore.value);
|
||||
});
|
||||
|
||||
const is3DSRom = computed(() => {
|
||||
return is3DSCIARom(props.rom);
|
||||
});
|
||||
@@ -105,34 +81,7 @@ async function copyDownloadLink(rom: DetailedRom) {
|
||||
>
|
||||
<v-icon icon="mdi-content-copy" />
|
||||
</v-btn>
|
||||
<v-btn
|
||||
v-if="ejsEmulationSupported"
|
||||
:disabled="rom.missing_from_fs"
|
||||
class="flex-grow-1"
|
||||
@click="
|
||||
$router.push({
|
||||
name: 'emulatorjs',
|
||||
params: { rom: rom?.id },
|
||||
})
|
||||
"
|
||||
:aria-label="`Play ${rom.name}`"
|
||||
>
|
||||
<v-icon :icon="playInfoIcon" />
|
||||
</v-btn>
|
||||
<v-btn
|
||||
v-if="ruffleEmulationSupported"
|
||||
:disabled="rom.missing_from_fs"
|
||||
class="flex-grow-1"
|
||||
@click="
|
||||
$router.push({
|
||||
name: 'ruffle',
|
||||
params: { rom: rom?.id },
|
||||
})
|
||||
"
|
||||
:aria-label="`Play ${rom.name}`"
|
||||
>
|
||||
<v-icon :icon="playInfoIcon" />
|
||||
</v-btn>
|
||||
<play-btn :rom="rom" class="flex-grow-1" />
|
||||
<v-btn
|
||||
v-if="is3DSRom"
|
||||
:disabled="rom.missing_from_fs"
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
<script setup lang="ts">
|
||||
import AdminMenu from "@/components/common/Game/AdminMenu.vue";
|
||||
import PlayBtn from "@/components/common/Game/PlayBtn.vue";
|
||||
import romApi from "@/services/api/rom";
|
||||
import storeDownload from "@/stores/download";
|
||||
import storeHeartbeat from "@/stores/heartbeat";
|
||||
@@ -7,12 +8,7 @@ import storeConfig from "@/stores/config";
|
||||
import type { SimpleRom } from "@/stores/roms";
|
||||
import storeAuth from "@/stores/auth";
|
||||
import type { Events } from "@/types/emitter";
|
||||
import {
|
||||
isEJSEmulationSupported,
|
||||
isRuffleEmulationSupported,
|
||||
is3DSCIARom,
|
||||
} from "@/utils";
|
||||
import { ROUTES } from "@/plugins/router";
|
||||
import { isAnyEmulationSupported, is3DSCIARom } from "@/utils";
|
||||
import type { Emitter } from "mitt";
|
||||
import { computed, inject, ref, watch } from "vue";
|
||||
import { storeToRefs } from "pinia";
|
||||
@@ -22,6 +18,7 @@ const props = defineProps<{ rom: SimpleRom; sizeActionBar: number }>();
|
||||
const emit = defineEmits(["menu-open", "menu-close"]);
|
||||
const downloadStore = storeDownload();
|
||||
const heartbeatStore = storeHeartbeat();
|
||||
const { value: heartbeat } = storeToRefs(heartbeatStore);
|
||||
const emitter = inject<Emitter<Events>>("emitter");
|
||||
const configStore = storeConfig();
|
||||
const { config } = storeToRefs(configStore);
|
||||
@@ -32,18 +29,12 @@ const computedSize = computed(() => {
|
||||
return props.sizeActionBar === 1 ? "small" : "x-small";
|
||||
});
|
||||
|
||||
const platformSlug = computed(() => {
|
||||
return props.rom.platform_slug in config.value.PLATFORMS_VERSIONS
|
||||
? config.value.PLATFORMS_VERSIONS[props.rom.platform_slug]
|
||||
: props.rom.platform_slug;
|
||||
});
|
||||
|
||||
const ejsEmulationSupported = computed(() => {
|
||||
return isEJSEmulationSupported(platformSlug.value, heartbeatStore.value);
|
||||
});
|
||||
|
||||
const ruffleEmulationSupported = computed(() => {
|
||||
return isRuffleEmulationSupported(platformSlug.value, heartbeatStore.value);
|
||||
const isEmulationSupported = computed(() => {
|
||||
return isAnyEmulationSupported(
|
||||
props.rom.platform_slug,
|
||||
heartbeat.value,
|
||||
config.value,
|
||||
);
|
||||
});
|
||||
|
||||
const is3DSRom = computed(() => {
|
||||
@@ -71,43 +62,15 @@ watch(menuOpen, (val) => {
|
||||
:aria-label="`${t('rom.download')} ${rom.name}`"
|
||||
/>
|
||||
</v-col>
|
||||
<v-col
|
||||
v-if="ejsEmulationSupported || ruffleEmulationSupported"
|
||||
class="d-flex"
|
||||
>
|
||||
<v-btn
|
||||
v-if="ejsEmulationSupported"
|
||||
:disabled="rom.missing_from_fs"
|
||||
<v-col v-if="isEmulationSupported" class="d-flex">
|
||||
<play-btn
|
||||
:rom="rom"
|
||||
:iconEmbedded="true"
|
||||
@click.prevent
|
||||
class="action-bar-btn-small flex-grow-1"
|
||||
:size="computedSize"
|
||||
@click="
|
||||
$router.push({
|
||||
name: ROUTES.EMULATORJS,
|
||||
params: { rom: rom?.id },
|
||||
})
|
||||
"
|
||||
icon="mdi-play"
|
||||
variant="text"
|
||||
rounded="0"
|
||||
:aria-label="`Play ${rom.name}`"
|
||||
/>
|
||||
<v-btn
|
||||
v-if="ruffleEmulationSupported"
|
||||
:disabled="rom.missing_from_fs"
|
||||
@click.prevent
|
||||
class="action-bar-btn-small flex-grow-1"
|
||||
:size="computedSize"
|
||||
@click="
|
||||
$router.push({
|
||||
name: ROUTES.RUFFLE,
|
||||
params: { rom: rom?.id },
|
||||
})
|
||||
"
|
||||
icon="mdi-play"
|
||||
variant="text"
|
||||
rounded="0"
|
||||
:aria-label="`Play ${rom.name}`"
|
||||
/>
|
||||
</v-col>
|
||||
<v-col v-if="is3DSRom" class="d-flex">
|
||||
|
||||
69
frontend/src/components/common/Game/PlayBtn.vue
Normal file
69
frontend/src/components/common/Game/PlayBtn.vue
Normal file
@@ -0,0 +1,69 @@
|
||||
<script setup lang="ts">
|
||||
import { storeToRefs } from "pinia";
|
||||
import { computed, useAttrs } from "vue";
|
||||
import { useRouter } from "vue-router";
|
||||
import storeConfig from "@/stores/config";
|
||||
import storeHeartbeat from "@/stores/heartbeat";
|
||||
import { type SimpleRom } from "@/stores/roms";
|
||||
import { ROUTES } from "@/plugins/router";
|
||||
import {
|
||||
isAnyEmulationSupported,
|
||||
isEJSEmulationSupported,
|
||||
isRuffleEmulationSupported,
|
||||
} from "@/utils";
|
||||
|
||||
const props = defineProps<{ rom: SimpleRom; iconEmbedded?: boolean }>();
|
||||
const attrs = useAttrs();
|
||||
const configStore = storeConfig();
|
||||
const heartbeatStore = storeHeartbeat();
|
||||
const router = useRouter();
|
||||
const { config } = storeToRefs(configStore);
|
||||
const { value: heartbeat } = storeToRefs(heartbeatStore);
|
||||
|
||||
const isEmulationSupported = computed(() => {
|
||||
return isAnyEmulationSupported(
|
||||
props.rom.platform_slug,
|
||||
heartbeat.value,
|
||||
config.value,
|
||||
);
|
||||
});
|
||||
|
||||
function goToPlayer(rom: SimpleRom) {
|
||||
if (
|
||||
isEJSEmulationSupported(rom.platform_slug, heartbeat.value, config.value)
|
||||
) {
|
||||
router.push({
|
||||
name: ROUTES.EMULATORJS,
|
||||
params: { rom: rom.id },
|
||||
});
|
||||
} else if (
|
||||
isRuffleEmulationSupported(rom.platform_slug, heartbeat.value, config.value)
|
||||
) {
|
||||
router.push({
|
||||
name: ROUTES.RUFFLE,
|
||||
params: { rom: rom.id },
|
||||
});
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<v-btn
|
||||
v-if="isEmulationSupported && iconEmbedded"
|
||||
v-bind="attrs"
|
||||
:disabled="rom.missing_from_fs"
|
||||
:aria-label="`Play ${rom.name}`"
|
||||
icon="mdi-play"
|
||||
@click="goToPlayer(rom)"
|
||||
/>
|
||||
|
||||
<v-btn
|
||||
v-if="isEmulationSupported && !iconEmbedded"
|
||||
v-bind="attrs"
|
||||
:disabled="rom.missing_from_fs"
|
||||
:aria-label="`Play ${rom.name}`"
|
||||
@click="goToPlayer(rom)"
|
||||
>
|
||||
<v-icon>mdi-play</v-icon>
|
||||
</v-btn>
|
||||
</template>
|
||||
@@ -2,22 +2,15 @@
|
||||
import PlatformIcon from "@/components/common/Platform/Icon.vue";
|
||||
import AdminMenu from "@/components/common/Game/AdminMenu.vue";
|
||||
import FavBtn from "@/components/common/Game/FavBtn.vue";
|
||||
import PlayBtn from "@/components/common/Game/PlayBtn.vue";
|
||||
import RAvatarRom from "@/components/common/Game/RAvatar.vue";
|
||||
import MissingFromFSIcon from "@/components/common/MissingFromFSIcon.vue";
|
||||
import romApi from "@/services/api/rom";
|
||||
import storeConfig from "@/stores/config";
|
||||
import storeDownload from "@/stores/download";
|
||||
import storeHeartbeat from "@/stores/heartbeat";
|
||||
import storeAuth from "@/stores/auth";
|
||||
import storeGalleryFilter from "@/stores/galleryFilter";
|
||||
import storeRoms, { type SimpleRom } from "@/stores/roms";
|
||||
import {
|
||||
formatBytes,
|
||||
isEJSEmulationSupported,
|
||||
isRuffleEmulationSupported,
|
||||
languageToEmoji,
|
||||
regionToEmoji,
|
||||
} from "@/utils";
|
||||
import { formatBytes, languageToEmoji, regionToEmoji } from "@/utils";
|
||||
import { ROUTES } from "@/plugins/router";
|
||||
import { isNull } from "lodash";
|
||||
import { storeToRefs } from "pinia";
|
||||
@@ -40,9 +33,6 @@ const downloadStore = storeDownload();
|
||||
const romsStore = storeRoms();
|
||||
const { filteredRoms, selectedRoms, fetchingRoms, fetchTotalRoms } =
|
||||
storeToRefs(romsStore);
|
||||
const heartbeatStore = storeHeartbeat();
|
||||
const configStore = storeConfig();
|
||||
const { config } = storeToRefs(configStore);
|
||||
const auth = storeAuth();
|
||||
const galleryFilterStore = storeGalleryFilter();
|
||||
|
||||
@@ -104,22 +94,6 @@ function rowClick(_: Event, row: { item: SimpleRom }) {
|
||||
romsStore.resetSelection();
|
||||
}
|
||||
|
||||
function getTruePlatformSlug(platformSlug: string) {
|
||||
return platformSlug in config.value.PLATFORMS_VERSIONS
|
||||
? config.value.PLATFORMS_VERSIONS[platformSlug]
|
||||
: platformSlug;
|
||||
}
|
||||
|
||||
function checkIfEJSEmulationSupported(platformSlug: string) {
|
||||
const slug = getTruePlatformSlug(platformSlug);
|
||||
return isEJSEmulationSupported(slug, heartbeatStore.value);
|
||||
}
|
||||
|
||||
function checkIfRuffleEmulationSupported(platformSlug: string) {
|
||||
const slug = getTruePlatformSlug(platformSlug);
|
||||
return isRuffleEmulationSupported(slug, heartbeatStore.value);
|
||||
}
|
||||
|
||||
function updateSelectAll() {
|
||||
if (selectedRoms.value.length === filteredRoms.value.length) {
|
||||
romsStore.resetSelection();
|
||||
@@ -319,34 +293,7 @@ function updateOptions({ sortBy }: { sortBy: SortBy }) {
|
||||
>
|
||||
<v-icon>mdi-download</v-icon>
|
||||
</v-btn>
|
||||
<v-btn
|
||||
v-if="checkIfEJSEmulationSupported(item.platform_slug)"
|
||||
:disabled="item.missing_from_fs"
|
||||
variant="text"
|
||||
size="small"
|
||||
@click.stop="
|
||||
$router.push({
|
||||
name: ROUTES.EMULATORJS,
|
||||
params: { rom: item?.id },
|
||||
})
|
||||
"
|
||||
>
|
||||
<v-icon>mdi-play</v-icon>
|
||||
</v-btn>
|
||||
<v-btn
|
||||
v-if="checkIfRuffleEmulationSupported(item.platform_slug)"
|
||||
:disabled="item.missing_from_fs"
|
||||
variant="text"
|
||||
size="small"
|
||||
@click.stop="
|
||||
$router.push({
|
||||
name: ROUTES.RUFFLE,
|
||||
params: { rom: item?.id },
|
||||
})
|
||||
"
|
||||
>
|
||||
<v-icon>mdi-play</v-icon>
|
||||
</v-btn>
|
||||
<play-btn :rom="item" @click.stop variant="text" size="small" />
|
||||
<v-menu
|
||||
v-if="
|
||||
auth.scopes.includes('roms.write') ||
|
||||
|
||||
@@ -3,6 +3,7 @@ import { defineStore } from "pinia";
|
||||
import type { ConfigResponse } from "@/__generated__";
|
||||
import api from "@/services/api";
|
||||
|
||||
export type Config = ConfigResponse;
|
||||
type ExclusionTypes =
|
||||
| "EXCLUDED_PLATFORMS"
|
||||
| "EXCLUDED_SINGLE_EXT"
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import cronstrue from "cronstrue";
|
||||
import type { Config } from "@/stores/config";
|
||||
import type { SimpleRom } from "@/stores/roms";
|
||||
import type { Heartbeat } from "@/stores/heartbeat";
|
||||
import type { RomFileSchema, RomUserStatus } from "@/__generated__";
|
||||
@@ -456,16 +457,19 @@ const gl =
|
||||
*
|
||||
* @param platformSlug The platform slug.
|
||||
* @param heartbeat The heartbeat object.
|
||||
* @param config Optional configuration object.
|
||||
* @returns True if supported, false otherwise.
|
||||
*/
|
||||
export function isEJSEmulationSupported(
|
||||
platformSlug: string,
|
||||
heartbeat: Heartbeat,
|
||||
config?: Config,
|
||||
) {
|
||||
if (heartbeat.EMULATION.DISABLE_EMULATOR_JS) return false;
|
||||
|
||||
const slug = config?.PLATFORMS_VERSIONS[platformSlug] || platformSlug;
|
||||
return (
|
||||
!heartbeat.EMULATION.DISABLE_EMULATOR_JS &&
|
||||
getSupportedEJSCores(platformSlug).length > 0 &&
|
||||
gl instanceof WebGLRenderingContext
|
||||
getSupportedEJSCores(slug).length > 0 && gl instanceof WebGLRenderingContext
|
||||
);
|
||||
}
|
||||
|
||||
@@ -521,15 +525,36 @@ export function getControlSchemeForPlatform(
|
||||
*
|
||||
* @param platformSlug The platform slug.
|
||||
* @param heartbeat The heartbeat object.
|
||||
* @param config Optional configuration object.
|
||||
* @returns True if supported, false otherwise.
|
||||
*/
|
||||
export function isRuffleEmulationSupported(
|
||||
platformSlug: string,
|
||||
heartbeat: Heartbeat,
|
||||
config?: Config,
|
||||
) {
|
||||
if (heartbeat.EMULATION.DISABLE_RUFFLE_RS) return false;
|
||||
|
||||
const slug = config?.PLATFORMS_VERSIONS[platformSlug] || platformSlug;
|
||||
return ["flash", "browser"].includes(slug.toLowerCase());
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether any kind of emulation is supported for a given platform.
|
||||
*
|
||||
* @param platformSlug The platform slug.
|
||||
* @param heartbeat The heartbeat object.
|
||||
* @param config Optional configuration object.
|
||||
* @returns True if any emulation is supported, false otherwise.
|
||||
*/
|
||||
export function isAnyEmulationSupported(
|
||||
platformSlug: string,
|
||||
heartbeat: Heartbeat,
|
||||
config?: Config,
|
||||
) {
|
||||
return (
|
||||
["flash", "browser"].includes(platformSlug.toLowerCase()) &&
|
||||
!heartbeat.EMULATION.DISABLE_RUFFLE_RS
|
||||
isEJSEmulationSupported(platformSlug, heartbeat, config) ||
|
||||
isRuffleEmulationSupported(platformSlug, heartbeat, config)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user