Skip to content

Commit

Permalink
feat(player): Add media key control support
Browse files Browse the repository at this point in the history
  • Loading branch information
endrl committed May 4, 2023
1 parent c569143 commit 857ba59
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 16 deletions.
21 changes: 12 additions & 9 deletions frontend/locales/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@
},
"images": "Images",
"incorrectUsernameOrPassword": "Incorrect username or password",
"instantMixQueued": "Instant mix added to queue",
"instantMix": "Instant mix",
"instantMixQueued": "Instant mix added to queue",
"item": {
"artist": {
"albums": "Albums",
Expand Down Expand Up @@ -218,13 +218,6 @@
}
},
"menu": "Menu",
"refreshMetadata": "Refresh metadata",
"replaceAllMetadata": "Replace all metadata",
"refreshMetadataHint": "Metadata is refreshed based on settings and internet services that are enabled in the Dashboard",
"searchMissingMetadata": "Search for missing metadata",
"replaceExistingImages": "Replace existing images",
"scanForNewAndUpdatedFiles": "Scan for new and updated files",
"metadataRefreshQueued": "Metadata refresh enqueued",
"metadata": {
"source": "Source",
"sourceAll": "All",
Expand All @@ -233,6 +226,7 @@
},
"metadataEditor": "Metadata editor",
"metadataNoResultsMatching": "No results matching \"{search}\". Press enter to create a new one.",
"metadataRefreshQueued": "Metadata refresh enqueued",
"more": "More",
"moreLikeArtist": "More like {artist}",
"moreLikeThis": "More like this",
Expand Down Expand Up @@ -317,17 +311,23 @@
"rating": "Rating",
"refresh": "Refresh",
"refreshLibrary": "Refresh library",
"refreshMetadata": "Refresh metadata",
"refreshMetadataHint": "Metadata is refreshed based on settings and internet services that are enabled in the Dashboard",
"releaseDate": "Release date",
"remoteDevices": "Remote devices",
"replaceAllMetadata": "Replace all metadata",
"replaceExistingImages": "Replace existing images",
"resumable": "Resumable",
"resume": "Resume",
"role": "Role",
"save": "Save",
"saved": "Saved",
"scanForNewAndUpdatedFiles": "Scan for new and updated files",
"search": {
"name": "Search",
"topResults": "Top results"
},
"searchMissingMetadata": "Search for missing metadata",
"seasonEpisode": "Season {seasonNumber}, Episode {episodeNumber}",
"seasonEpisodeAbbrev": "S{seasonNumber} E{episodeNumber}",
"series": "Series",
Expand Down Expand Up @@ -379,6 +379,9 @@
"noActivityFound": "No activities found",
"noLogsFound": "No logs found"
},
"playback": {
"enableMediaKeys": "Enable keyboard media keys"
},
"settings": "Settings"
},
"settingsSections": {
Expand Down Expand Up @@ -572,4 +575,4 @@
"year": "Year",
"years": "Years",
"youMayAlsoLike": "You may also like"
}
}
4 changes: 2 additions & 2 deletions frontend/src/pages/settings/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ const userItems = computed(() => {
icon: IMdiPlayPause,
name: t('settingsSections.playback.name'),
description: t('settingsSections.playback.description'),
link: undefined
link: 'settings/playback'
},
{
icon: IMdiDiscPlayer,
Expand Down Expand Up @@ -213,7 +213,7 @@ const adminSections = computed(() => {
icon: IMdiKeyChain,
name: t('settings.apiKeys.apiKeys'),
description: t('settings.apiKeys.description'),
link: '/settings/apikeys'
link: 'settings/apikeys'
}
],
[
Expand Down
20 changes: 20 additions & 0 deletions frontend/src/pages/settings/playback.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<template>
<settings-page page-title="settingsSections.playback.name">
<template #content>
<v-checkbox
v-model="clientSettings.enableMediaKeys"
:label="$t('settings.playback.enableMediaKeys')" />
</template>
</settings-page>
</template>

<route lang="yaml">
meta:
admin: true
</route>

<script setup lang="ts">
import { clientSettingsStore } from '@/store';
const clientSettings = clientSettingsStore();
</script>
51 changes: 48 additions & 3 deletions frontend/src/store/clientSettings.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { computed, watch, nextTick } from 'vue';
import { computed, watch, nextTick, WatchStopHandle } from 'vue';
import {
RemovableRef,
useNavigatorLanguage,
usePreferredDark,
useStorage,
watchPausable
watchPausable,
useMagicKeys,
whenever
} from '@vueuse/core';
import { cloneDeep } from 'lodash-es';
import { playbackManagerStore } from '@/store';
import { fetchDefaultedCustomPrefs, syncCustomPrefs } from '@/utils/store-sync';
import { usei18n, useSnackbar, useRemote, useVuetify } from '@/composables';
import { mergeExcludingUnknown } from '@/utils/data-manipulation';
Expand All @@ -19,6 +22,7 @@ import { mergeExcludingUnknown } from '@/utils/data-manipulation';
interface ClientSettingsState {
darkMode: 'auto' | boolean;
locale: 'auto' | string;
enableMediaKeys: boolean;
}

/**
Expand All @@ -36,6 +40,7 @@ const BROWSER_LANGUAGE = computed<string>(() => {
});
const browserPrefersDark = usePreferredDark();
const storeKey = 'clientSettings';
const mediaKeysUnwatchers = new Array<WatchStopHandle>();

/**
* == CLASS CONSTRUCTOR ==
Expand All @@ -46,7 +51,8 @@ class ClientSettingsStore {
*/
private _defaultState: ClientSettingsState = {
darkMode: 'auto',
locale: 'auto'
locale: 'auto',
enableMediaKeys: true
};

private _state: RemovableRef<ClientSettingsState> = useStorage(
Expand Down Expand Up @@ -85,6 +91,14 @@ class ClientSettingsStore {
return this._state.value.darkMode;
}

public set enableMediaKeys(newVal: boolean) {
this._state.value.enableMediaKeys = newVal;
}

public get enableMediaKeys(): boolean {
return this._state.value.enableMediaKeys;
}

private _updateLocale = (): void => {
const i18n = usei18n();

Expand Down Expand Up @@ -177,6 +191,37 @@ class ClientSettingsStore {
{ immediate: true }
);

/**
* enableMediaKeys option change
*/
watch(
() => this._state.value.enableMediaKeys,
(newState) => {
if (newState && mediaKeysUnwatchers.length === 0) {
const playbackManager = playbackManagerStore();
const keys = useMagicKeys();

mediaKeysUnwatchers.push(
whenever(keys.mediapause, playbackManager.playPause),
whenever(keys.mediaplay, playbackManager.playPause),
whenever(keys.mediaplaypause, playbackManager.playPause),
whenever(keys.mediastop, playbackManager.stop),
whenever(keys.mediafastforward, playbackManager.skipForward),
whenever(keys.mediarewind, playbackManager.skipBackward),
whenever(keys.mediatracknext, playbackManager.setNextTrack),
whenever(keys.mediatrackprevious, playbackManager.setPreviousTrack),
whenever(keys.audiovolumemute, playbackManager.toggleMute)
);
} else {
for (const unwatch of mediaKeysUnwatchers) {
unwatch();
}

mediaKeysUnwatchers.splice(0);
}
}
);

watch(
() => remote.auth.currentUser,
() => {
Expand Down
5 changes: 3 additions & 2 deletions frontend/types/global/routes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import type {
// data fetching
_DataLoader,
_DefineLoaderOptions,
} from 'unplugin-vue-router/types'
} from 'unplugin-vue-router'

declare module 'vue-router/auto/routes' {
export interface RouteNamedMap {
Expand All @@ -54,6 +54,7 @@ declare module 'vue-router/auto/routes' {
'settings-apikeys': RouteRecordInfo<'settings-apikeys', '/settings/apikeys', Record<never, never>, Record<never, never>>,
'settings-devices': RouteRecordInfo<'settings-devices', '/settings/devices', Record<never, never>, Record<never, never>>,
'settings-logs-and-activity': RouteRecordInfo<'settings-logs-and-activity', '/settings/logs-and-activity', Record<never, never>, Record<never, never>>,
'settings-playback': RouteRecordInfo<'settings-playback', '/settings/playback', Record<never, never>, Record<never, never>>,
'wizard': RouteRecordInfo<'wizard', '/wizard', Record<never, never>, Record<never, never>>,
}
}
Expand Down Expand Up @@ -112,7 +113,7 @@ declare module 'vue-router/auto' {
export function onBeforeRouteUpdate(guard: NavigationGuard<RouteNamedMap>): void

export const RouterLink: RouterLinkTyped<RouteNamedMap>

// Experimental Data Fetching

export function defineLoader<
Expand Down

0 comments on commit 857ba59

Please sign in to comment.